| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you 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. |
| |
| #include "simple_function_factory.h" |
| #include "vec/columns/column_array.h" |
| #include "vec/columns/column_map.h" |
| #include "vec/data_types/data_type_array.h" |
| #include "vec/data_types/data_type_number.h" |
| #include "vec/functions/array/function_array_utils.h" |
| #include "vec/functions/function.h" |
| #include "vec/functions/function_helpers.h" |
| |
| namespace doris::vectorized { |
| |
| // size function for size with map and array |
| class FunctionSize : public IFunction { |
| public: |
| static constexpr auto name = "size"; |
| static FunctionPtr create() { return std::make_shared<FunctionSize>(); } |
| String get_name() const override { return name; } |
| bool is_variadic() const override { return true; } |
| size_t get_number_of_arguments() const override { return 0; } |
| bool use_default_implementation_for_constants() const override { return false; } |
| |
| DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
| DataTypePtr datatype = arguments[0]; |
| DCHECK(datatype->get_primitive_type() == TYPE_MAP || |
| datatype->get_primitive_type() == TYPE_ARRAY) |
| << "first argument for function: " << name |
| << " should be DataTypeMap or DataTypeArray"; |
| return std::make_shared<DataTypeInt64>(); |
| } |
| |
| Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
| uint32_t result, size_t input_rows_count) const override { |
| const auto& [left_column, left_const] = |
| unpack_if_const(block.get_by_position(arguments[0]).column); |
| const auto type = block.get_by_position(arguments[0]).type; |
| const ColumnArray* array_column = nullptr; |
| const ColumnMap* map_column = nullptr; |
| if (type->get_primitive_type() == TYPE_ARRAY) { |
| array_column = check_and_get_column<ColumnArray>(*left_column.get()); |
| } else if (type->get_primitive_type() == TYPE_MAP) { |
| map_column = check_and_get_column<ColumnMap>(*left_column.get()); |
| } |
| |
| auto dst_column = ColumnInt64::create(input_rows_count); |
| auto& dst_data = dst_column->get_data(); |
| |
| if (left_const && map_column) { |
| for (size_t i = 0; i < input_rows_count; i++) { |
| dst_data[i] = map_column->size_at(0); |
| } |
| } else if (left_const && array_column) { |
| for (size_t i = 0; i < input_rows_count; i++) { |
| dst_data[i] = array_column->size_at(0); |
| } |
| } else if (map_column) { |
| for (size_t i = 0; i < map_column->size(); i++) { |
| dst_data[i] = map_column->size_at(i); |
| } |
| } else if (array_column) { |
| for (size_t i = 0; i < array_column->size(); i++) { |
| dst_data[i] = array_column->size_at(i); |
| } |
| } else { |
| return Status::RuntimeError("unsupported types for function {}({})", get_name(), |
| block.get_by_position(arguments[0]).type->get_name()); |
| } |
| |
| block.replace_by_position(result, std::move(dst_column)); |
| return Status::OK(); |
| } |
| }; |
| |
| void register_function_size(SimpleFunctionFactory& factory) { |
| factory.register_function<FunctionSize>(); |
| factory.register_alias(FunctionSize::name, "map_size"); |
| factory.register_alias(FunctionSize::name, "cardinality"); |
| factory.register_alias(FunctionSize::name, "array_size"); |
| } |
| } // namespace doris::vectorized |