risingwave_storage/row_serde/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;

use risingwave_common::catalog::{ColumnDesc, ColumnId};
use risingwave_common::row::{OwnedRow, Project, RowExt};

pub mod row_serde_util;

pub mod value_serde;

/// Find out the [`ColumnDesc`] selected with a list of [`ColumnId`].
///
/// # Returns
///
/// A pair of columns and their indexes in input columns
pub fn find_columns_by_ids(
    table_columns: &[ColumnDesc],
    column_ids: &[ColumnId],
) -> (Vec<ColumnDesc>, Vec<usize>) {
    if column_ids.is_empty() {
        // shortcut
        return (vec![], vec![]);
    }
    let id_to_columns = table_columns
        .iter()
        .enumerate()
        .map(|(index, c)| (c.column_id, (c.clone(), index)))
        .collect::<HashMap<_, _>>();
    column_ids
        .iter()
        .map(|id| id_to_columns.get(id).expect("column id not found").clone())
        .unzip()
}

#[derive(Clone)]
pub struct ColumnMapping {
    output_indices: Vec<usize>,
}

#[allow(clippy::len_without_is_empty)]
impl ColumnMapping {
    /// Create a mapping with given `table_columns` projected on the `column_ids`.
    pub fn new(output_indices: Vec<usize>) -> Self {
        Self { output_indices }
    }

    /// Project a row with this mapping
    pub fn project(&self, origin_row: OwnedRow) -> Project<'_, OwnedRow> {
        origin_row.project(&self.output_indices)
    }
}

#[cfg(test)]
mod test {
    use std::fmt::Debug;

    use expect_test::{expect, Expect};
    use risingwave_common::types::DataType;

    use super::*;

    fn check(actual: impl Debug, expect: Expect) {
        let actual = format!("{:#?}", actual);
        expect.assert_eq(&actual);
    }

    #[test]
    fn test_find_columns_by_ids() {
        let table_columns = vec![
            ColumnDesc::unnamed(1.into(), DataType::Varchar),
            ColumnDesc::unnamed(2.into(), DataType::Int64),
            ColumnDesc::unnamed(3.into(), DataType::Int16),
        ];
        let column_ids = vec![2.into(), 3.into()];
        let result = find_columns_by_ids(&table_columns, &column_ids);
        check(
            result,
            expect![[r#"
                (
                    [
                        ColumnDesc {
                            data_type: Int64,
                            column_id: #2,
                            name: "",
                            field_descs: [],
                            type_name: "",
                            generated_or_default_column: None,
                            description: None,
                            additional_column: AdditionalColumn {
                                column_type: None,
                            },
                            version: Pr13707,
                            system_column: None,
                        },
                        ColumnDesc {
                            data_type: Int16,
                            column_id: #3,
                            name: "",
                            field_descs: [],
                            type_name: "",
                            generated_or_default_column: None,
                            description: None,
                            additional_column: AdditionalColumn {
                                column_type: None,
                            },
                            version: Pr13707,
                            system_column: None,
                        },
                    ],
                    [
                        1,
                        2,
                    ],
                )"#]],
        );

        let table_columns = vec![
            ColumnDesc::unnamed(2.into(), DataType::Int64),
            ColumnDesc::unnamed(1.into(), DataType::Varchar),
            ColumnDesc::unnamed(3.into(), DataType::Int16),
        ];
        let column_ids = vec![2.into(), 1.into()];
        let result = find_columns_by_ids(&table_columns, &column_ids);
        check(
            result,
            expect![[r#"
                (
                    [
                        ColumnDesc {
                            data_type: Int64,
                            column_id: #2,
                            name: "",
                            field_descs: [],
                            type_name: "",
                            generated_or_default_column: None,
                            description: None,
                            additional_column: AdditionalColumn {
                                column_type: None,
                            },
                            version: Pr13707,
                            system_column: None,
                        },
                        ColumnDesc {
                            data_type: Varchar,
                            column_id: #1,
                            name: "",
                            field_descs: [],
                            type_name: "",
                            generated_or_default_column: None,
                            description: None,
                            additional_column: AdditionalColumn {
                                column_type: None,
                            },
                            version: Pr13707,
                            system_column: None,
                        },
                    ],
                    [
                        0,
                        1,
                    ],
                )"#]],
        );
    }
}