gustavodemorais commented on code in PR #28948:
URL: https://github.com/apache/flink/pull/28948#discussion_r3842482044
##########
docs/data/sql_functions.yml:
##########
@@ -928,6 +928,27 @@ collection:
- sql: MAP_FROM_ARRAYS(array_of_keys, array_of_values)
table: mapFromArrays(array_of_keys, array_of_values)
description: Returns a map created from an arrays of keys and values. Note
that the lengths of two arrays should be the same.
+ - sql: MAP_FROM_ENTRIES(array_of_entries)
+ table: array.mapFromEntries()
+ description: |
+ Returns a map created from the given array of entries. Each entry must
be a ROW with exactly
+ two fields, where the first field becomes the key and the second one the
value. If there are
+ duplicate keys, the value of the last entry with that key wins; NULL
keys are treated as
+ equal and collapse into a single entry. If the array itself or any of
its entries is null,
+ null is returned.
+ eg.
+ -- {1=one, 2=two}
+ MAP_FROM_ENTRIES(ARRAY[ROW(1, 'one'), ROW(2, 'two')])
+
+ -- {1=uno, 2=two}
+ MAP_FROM_ENTRIES(ARRAY[ROW(1, 'one'), ROW(2, 'two'), ROW(1, 'uno')])
+
+ -- {null=b}
+ MAP_FROM_ENTRIES(ARRAY[ROW(CAST(NULL AS INT), 'a'), ROW(CAST(NULL AS
INT), 'b')])
+
+ -- NULL
+ MAP_FROM_ENTRIES(ARRAY[ROW(1, 'one'), CAST(NULL AS ROW(k INT, v
STRING))])
+
Review Comment:
Let's improve docs format. Adjust everywhere where relevant.
```suggestion
Returns a map created from the given array of entries. Each entry must
be a ROW with exactly
two fields, where the first field becomes the key and the second one
the value.
If there are duplicate keys, the value of the last entry with that key
wins; NULL keys are
treated as equal and collapse into a single entry. If the array itself
or any of its entries
is null, null is returned.
```sql
-- Returns {1=one, 2=two}
MAP_FROM_ENTRIES(ARRAY[ROW(1, 'one'), ROW(2, 'two')])
-- Returns {1=uno, 2=two}
MAP_FROM_ENTRIES(ARRAY[ROW(1, 'one'), ROW(2, 'two'), ROW(1, 'uno')])
-- Returns {null=b}
MAP_FROM_ENTRIES(ARRAY[ROW(CAST(NULL AS INT), 'a'), ROW(CAST(NULL AS
INT), 'b')])
-- Returns NULL
MAP_FROM_ENTRIES(ARRAY[ROW(1, 'one'), CAST(NULL AS ROW(k INT, v
STRING))])
```
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/SpecificTypeStrategies.java:
##########
@@ -171,6 +171,27 @@ public final class SpecificTypeStrategies {
((CollectionDataType)
callContext.getArgumentDataTypes().get(1))
.getElementDataType()));
+ /**
+ * Type strategy specific for {@link
BuiltInFunctionDefinitions#MAP_FROM_ENTRIES}.
+ *
+ * <p>Derives {@code MAP<key, value>} from the {@code ROW} element of the
{@code ARRAY}
+ * argument. The result is nullable if the array itself is nullable or if
its elements are,
+ * since a {@code NULL} entry makes the whole map {@code NULL}.
+ */
+ public static final TypeStrategy MAP_FROM_ENTRIES =
+ callContext -> {
+ final DataType arrayDataType =
callContext.getArgumentDataTypes().get(0);
+ final DataType entryDataType =
+ ((CollectionDataType)
arrayDataType).getElementDataType();
+ final List<DataType> fieldDataTypes =
entryDataType.getChildren();
Review Comment:
getChildren is generic. Try getChildren() → getFieldDataTypes() where
feasible
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/util/MapDataContainer.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ *
+ */
+
+package org.apache.flink.table.runtime.util;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.data.MapData;
+
+/** A {@link MapData} backed directly by a key array and a value array. */
+@Internal
+public class MapDataContainer implements MapData {
Review Comment:
+1 both MapFromArraysFunction and MapDataForMapFromArrays have the same
logic.
MapFromArraysFunction needs a small adjustment. MapUnionFunction could use
MapContainer as-is. I think all three could share this one util
--
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]