lgbo-ustc commented on code in PR #12349:
URL: https://github.com/apache/gluten/pull/12349#discussion_r3479703577


##########
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(

Review Comment:
   `executeImpl` is getting quite long and currently mixes several concerns: 
unwrapping nullable input, handling nullable array entries, the 
`Array(Nothing)` special case, duplicate-key selection, and final map-column 
construction. Could we split the row-level null handling / duplicate-key 
selection into small helpers? That would make the Spark semantics easier to 
audit and also make the duplicate-key lookup optimization more localized.
   
   For example, the main loop could become closer to:
   
   ```cpp
   auto appendNullMap = [&]()
   {
       if (result_null_map_data)
           (*result_null_map_data)[row] = 1;
       result_offsets.push_back(result_offset);
   };
   
   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])
           || hasNullEntry(entry_null_map, previous_entry_offset, 
current_entry_offset))
       {
           appendNullMap();
           previous_entry_offset = current_entry_offset;
           continue;
       }
   
       auto selected_entries = selectEntriesForRow(
           key_column,
           previous_entry_offset,
           current_entry_offset);
   
       appendSelectedEntries(
           selected_entries,
           *key_insert_column,
           value_column,
           *result_key_column,
           *result_value_column,
           result_offset);
   
       result_offsets.push_back(result_offset);
       previous_entry_offset = current_entry_offset;
   }
   ```
   
   Then `selectEntriesForRow` could own the duplicate-key policy, including the 
possible hash-based implementation.



-- 
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]

Reply via email to