be/src/exprs/function/cast/cast_to_struct.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include <vector> |
19 | | |
20 | | #include "core/column/column_struct.h" |
21 | | #include "core/data_type/data_type_struct.h" |
22 | | #include "core/types.h" |
23 | | #include "exprs/function/cast/cast_base.h" |
24 | | |
25 | | namespace doris::CastWrapper { |
26 | | // check struct value type and get to_type value |
27 | | // TODO: need handle another type to cast struct |
28 | | WrapperType create_struct_wrapper(const CastOptions& options, const DataTypePtr& from_type, |
29 | 11 | const DataTypeStruct& to_type) { |
30 | | // support CAST AS Struct from string |
31 | 11 | if (is_string_type(from_type->get_primitive_type())) { |
32 | 4 | if (options.strict_mode) { |
33 | 1 | return cast_from_string_to_complex_type_strict_mode; |
34 | 3 | } else { |
35 | 3 | return cast_from_string_to_complex_type; |
36 | 3 | } |
37 | 4 | } |
38 | | |
39 | | // only support CAST AS Struct from struct or string types |
40 | 7 | const auto* from = check_and_get_data_type<DataTypeStruct>(from_type.get()); |
41 | 7 | if (!from) { |
42 | 0 | return CastWrapper::create_unsupport_wrapper( |
43 | 0 | fmt::format("CAST AS Struct can only be performed between struct types or from " |
44 | 0 | "String. Left type: {}, right type: {}", |
45 | 0 | from_type->get_name(), to_type.get_name())); |
46 | 0 | } |
47 | | |
48 | 7 | const auto& from_element_types = from->get_elements(); |
49 | 7 | const auto& to_element_types = to_type.get_elements(); |
50 | | // only support CAST AS Struct from struct type with same number of elements |
51 | 7 | if (from_element_types.size() != to_element_types.size()) { |
52 | 0 | return CastWrapper::create_unsupport_wrapper( |
53 | 0 | fmt::format("CAST AS Struct can only be performed between struct types with " |
54 | 0 | "the same number of elements. Left type: {}, right type: {}", |
55 | 0 | from_type->get_name(), to_type.get_name())); |
56 | 0 | } |
57 | | |
58 | 7 | auto element_wrappers = get_element_wrappers(options, from_element_types, to_element_types); |
59 | | /// A field whose type does not change is passed through, so it needs neither a child mask nor a |
60 | | /// call into its (identity) wrapper. |
61 | 7 | std::vector<bool> unchanged_fields; |
62 | 7 | unchanged_fields.reserve(from_element_types.size()); |
63 | 15 | for (size_t i = 0; i < from_element_types.size(); ++i) { |
64 | 8 | unchanged_fields.push_back(from_element_types[i]->equals(*to_element_types[i])); |
65 | 8 | } |
66 | 7 | return [element_wrappers, from_element_types, to_element_types, |
67 | 7 | unchanged_fields = std::move(unchanged_fields)]( |
68 | 7 | RuntimeState* state, const ColumnWithTypeAndName& source_column, |
69 | 7 | ColumnWithTypeAndName& target_column, size_t /*input_rows_count*/, |
70 | 14 | const NullMap::value_type* parent_null_map = nullptr) -> Status { |
71 | 14 | const auto* from_col_struct = |
72 | 14 | check_and_get_column<ColumnStruct>(source_column.column.get()); |
73 | 14 | if (!from_col_struct) { |
74 | 0 | return Status::RuntimeError("Illegal column {} for function CAST AS Struct", |
75 | 0 | source_column.column->get_name()); |
76 | 0 | } |
77 | | |
78 | 14 | size_t elements_num = to_element_types.size(); |
79 | 14 | Columns converted_columns(elements_num); |
80 | | // Fields share the rows of their parent, so the parent mask is scanned once for all of them. |
81 | 14 | const NullMap::value_type* effective_parent_null_map = |
82 | 14 | (parent_null_map != nullptr && |
83 | 14 | has_masked_row(parent_null_map, from_col_struct->size())) |
84 | 14 | ? parent_null_map |
85 | 14 | : nullptr; |
86 | 24 | for (size_t i = 0; i < elements_num; ++i) { |
87 | 15 | if (unchanged_fields[i]) { |
88 | | /// The field is not converted, so it cannot validate the hidden payload of a NULL |
89 | | /// row, and the NULL of the row hides the field anyway. |
90 | 1 | converted_columns[i] = from_col_struct->get_column_ptr(i); |
91 | 1 | continue; |
92 | 1 | } |
93 | | /// A field of a row that is NULL is a hidden payload as well. Fields share the rows of |
94 | | /// their parent, so the mask of the parent can be used as is. |
95 | 14 | auto child_mask = build_child_null_mask(effective_parent_null_map, nullptr, |
96 | 14 | from_col_struct->get_column_ptr(i)); |
97 | 14 | ColumnWithTypeAndName from_element_column {child_mask.column, from_element_types[i], |
98 | 14 | ""}; |
99 | 14 | ColumnWithTypeAndName element_result {nullptr, to_element_types[i], ""}; |
100 | 14 | RETURN_IF_ERROR(element_wrappers[i](state, from_element_column, element_result, |
101 | 14 | child_mask.column->size(), child_mask.null_map)); |
102 | 9 | converted_columns[i] = std::move(element_result.column); |
103 | 9 | } |
104 | | |
105 | 9 | target_column.column = ColumnStruct::create(converted_columns); |
106 | 9 | return Status::OK(); |
107 | 14 | }; |
108 | 7 | } |
109 | | } // namespace doris::CastWrapper |