lgbo-ustc commented on code in PR #12349: URL: https://github.com/apache/gluten/pull/12349#discussion_r3479557175
########## cpp-ch/local-engine/Functions/SparkFunctionMapFromEntries.cpp: ########## @@ -0,0 +1,307 @@ +/* + * 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 <Columns/ColumnArray.h> +#include <Columns/ColumnLowCardinality.h> +#include <Columns/ColumnMap.h> +#include <Columns/ColumnNullable.h> +#include <Columns/ColumnTuple.h> +#include <Columns/ColumnsNumber.h> +#include <DataTypes/DataTypeArray.h> +#include <DataTypes/DataTypeMap.h> +#include <DataTypes/DataTypeNothing.h> +#include <DataTypes/DataTypeNullable.h> +#include <DataTypes/DataTypeTuple.h> +#include <Functions/FunctionFactory.h> +#include <Functions/FunctionHelpers.h> +#include <Functions/IFunction.h> +#include <Common/assert_cast.h> +#include <utility> +#include <vector> + +namespace DB +{ +namespace ErrorCodes +{ + extern const int BAD_ARGUMENTS; + extern const int ILLEGAL_COLUMN; + extern const int ILLEGAL_TYPE_OF_ARGUMENT; +} + +template <bool last_win> +class SparkFunctionMapFromEntries : public IFunction +{ +public: + static constexpr auto name = last_win ? "sparkMapFromEntriesLastWin" : "sparkMapFromEntries"; + + static FunctionPtr create(ContextPtr) { return std::make_shared<SparkFunctionMapFromEntries>(); } + + String getName() const override { return name; } + + size_t getNumberOfArguments() const override { return 1; } + + bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo &) const override { return true; } + bool useDefaultImplementationForConstants() const override { return true; } + bool useDefaultImplementationForNulls() const override { return false; } + bool useDefaultImplementationForLowCardinalityColumns() const override { return false; } + + DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override + { + const auto * array_type = checkAndGetDataType<DataTypeArray>(removeNullable(arguments[0]).get()); + if (!array_type) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "Argument for function {} must be Array, but it has type {}", + getName(), + arguments[0]->getName()); + + const auto & entry_type = array_type->getNestedType(); + const auto entry_type_without_nullable = removeNullable(entry_type); + if (isNothing(entry_type_without_nullable)) + { + auto map_type = std::make_shared<DataTypeMap>( + std::make_shared<DataTypeNothing>(), + std::make_shared<DataTypeNothing>()); + if (arguments[0]->isNullable() || entry_type->isNullable()) + return makeNullable(map_type); + return map_type; + } + + const auto * tuple_type = checkAndGetDataType<DataTypeTuple>(entry_type_without_nullable.get()); + if (!tuple_type || tuple_type->getElements().size() != 2) + throw Exception( + ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, + "Argument for function {} must be Array of pair Tuple, but it has nested type {}", + getName(), + entry_type->getName()); + + const auto & elements = tuple_type->getElements(); + auto map_type = std::make_shared<DataTypeMap>(removeNullableOrLowCardinalityNullable(elements[0]), elements[1]); + if (arguments[0]->isNullable() || entry_type->isNullable()) + return makeNullable(map_type); + return map_type; + } + + ColumnPtr executeImpl( + const ColumnsWithTypeAndName & arguments, + const DataTypePtr & result_type, + size_t input_rows_count) const override + { + ColumnPtr holder = arguments[0].column->convertToFullColumnIfConst(); + + const PaddedPODArray<UInt8> * input_null_map = nullptr; + if (const auto * nullable = checkAndGetColumn<ColumnNullable>(holder.get())) + { + input_null_map = &nullable->getNullMapData(); + holder = nullable->getNestedColumnPtr(); + } + + const auto * entries_array = checkAndGetColumn<ColumnArray>(holder.get()); + if (!entries_array) + throw Exception( + ErrorCodes::ILLEGAL_COLUMN, + "Argument column for function {} must be Array, but it is {}", + getName(), + holder->getName()); + + const auto & entries_offsets = entries_array->getOffsets(); + const IColumn * entries_data = &entries_array->getData(); + const PaddedPODArray<UInt8> * entry_null_map = nullptr; + if (const auto * nullable_entries = checkAndGetColumn<ColumnNullable>(entries_data)) + { + entry_null_map = &nullable_entries->getNullMapData(); + entries_data = &nullable_entries->getNestedColumn(); + } + + const auto & result_map_type = assert_cast<const DataTypeMap &>(*removeNullable(result_type)); + if (isNothing(entries_data->getDataType())) + { + auto result_key_column = result_map_type.getKeyType()->createColumn(); + auto result_value_column = result_map_type.getValueType()->createColumn(); + auto result_offsets_column = ColumnArray::ColumnOffsets::create(input_rows_count, 0); + + ColumnUInt8::MutablePtr result_null_map; + PaddedPODArray<UInt8> * result_null_map_data = nullptr; + if (result_type->isNullable()) + { + result_null_map = ColumnUInt8::create(input_rows_count, 0); + result_null_map_data = &result_null_map->getData(); + } + + size_t previous_entry_offset = 0; + for (size_t row = 0; row < input_rows_count; ++row) + { + const auto current_entry_offset = entries_offsets[row]; + if (input_null_map && (*input_null_map)[row]) + { + if (result_null_map_data) + (*result_null_map_data)[row] = 1; + } + else if (entry_null_map) + { + for (size_t entry = previous_entry_offset; entry < current_entry_offset; ++entry) + { + if ((*entry_null_map)[entry]) + { + if (result_null_map_data) + (*result_null_map_data)[row] = 1; + break; + } + } Review Comment: ```cpp bool entry_map_null = false if (entry_null_map) { for (size_t entry = previous_entry_offset; entry < current_entry_offset; ++entry) { if ((*entry_null_map)[entry]) { entry_map_null = true; break; } } } if (result_null_map_data) (*result_null_map_data)[row] = entry_map_null || (input_null_map && (*input_null_map)[row]); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
