be/src/exprs/function/cast/cast_to_array.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 "core/column/column_array.h" |
19 | | #include "core/column/column_nullable.h" |
20 | | #include "core/data_type/data_type_array.h" |
21 | | #include "exprs/function/cast/cast_base.h" |
22 | | |
23 | | namespace doris::CastWrapper { |
24 | | #include "common/compile_check_begin.h" |
25 | | WrapperType create_array_wrapper(FunctionContext* context, const DataTypePtr& from_type_untyped, |
26 | 1.30k | const DataTypeArray& to_type) { |
27 | | /// Conversion from String through parsing. |
28 | 1.30k | if (check_and_get_data_type<DataTypeString>(from_type_untyped.get())) { |
29 | 4 | if (context->enable_strict_mode()) { |
30 | 0 | return cast_from_string_to_complex_type_strict_mode; |
31 | 4 | } else { |
32 | 4 | return cast_from_string_to_complex_type; |
33 | 4 | } |
34 | 4 | } |
35 | | |
36 | 1.30k | const auto* from_type = check_and_get_data_type<DataTypeArray>(from_type_untyped.get()); |
37 | | |
38 | 1.30k | if (!from_type) { |
39 | 0 | return CastWrapper::create_unsupport_wrapper( |
40 | 0 | "CAST AS Array can only be performed between same-dimensional Array, String " |
41 | 0 | "types"); |
42 | 0 | } |
43 | | |
44 | 1.30k | DataTypePtr from_nested_type = from_type->get_nested_type(); |
45 | | |
46 | | /// In query SELECT CAST([] AS Array(Array(String))) from type is Array(Nothing) |
47 | 1.30k | bool from_empty_array = from_nested_type->get_primitive_type() == INVALID_TYPE; |
48 | | |
49 | 1.30k | if (from_type->get_number_of_dimensions() != to_type.get_number_of_dimensions() && |
50 | 1.30k | !from_empty_array) { |
51 | 0 | return CastWrapper::create_unsupport_wrapper( |
52 | 0 | "CAST AS Array can only be performed between same-dimensional array types"); |
53 | 0 | } |
54 | | |
55 | 1.30k | const DataTypePtr& to_nested_type = to_type.get_nested_type(); |
56 | | |
57 | | /// Prepare nested type conversion |
58 | 1.30k | const auto nested_function = |
59 | 1.30k | prepare_unpack_dictionaries(context, from_nested_type, to_nested_type); |
60 | | |
61 | 1.30k | return [nested_function, from_nested_type, to_nested_type]( |
62 | 1.30k | FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
63 | 1.30k | uint32_t result, size_t /*input_rows_count*/, |
64 | 1.30k | const NullMap::value_type* null_map = nullptr) -> Status { |
65 | 1.30k | ColumnPtr from_column = block.get_by_position(arguments.front()).column; |
66 | | |
67 | 1.30k | const auto* from_col_array = check_and_get_column<ColumnArray>(from_column.get()); |
68 | | |
69 | 1.30k | if (from_col_array) { |
70 | | /// create columns for converting nested column containing original and result columns |
71 | 1.30k | ColumnWithTypeAndName from_nested_column {from_col_array->get_data_ptr(), |
72 | 1.30k | from_nested_type, ""}; |
73 | | |
74 | | /// convert nested column |
75 | 1.30k | ColumnNumbers new_arguments {block.columns()}; |
76 | 1.30k | block.insert(from_nested_column); |
77 | | |
78 | 1.30k | uint32_t nested_result = block.columns(); |
79 | 1.30k | block.insert({to_nested_type, ""}); |
80 | 1.30k | RETURN_IF_ERROR(nested_function(context, block, new_arguments, nested_result, |
81 | 1.30k | from_col_array->get_data_ptr()->size(), null_map)); |
82 | 1.30k | auto nested_result_column = block.get_by_position(nested_result).column; |
83 | | |
84 | | /// set converted nested column to result |
85 | 1.30k | block.get_by_position(result).column = |
86 | 1.30k | ColumnArray::create(nested_result_column, from_col_array->get_offsets_ptr()); |
87 | 1.30k | } else { |
88 | 0 | return Status::RuntimeError("Illegal column {} for function CAST AS Array", |
89 | 0 | from_column->get_name()); |
90 | 0 | } |
91 | 1.30k | return Status::OK(); |
92 | 1.30k | }; |
93 | 1.30k | } |
94 | | #include "common/compile_check_end.h" |
95 | | } // namespace doris::CastWrapper |