gustavodemorais commented on code in PR #26113: URL: https://github.com/apache/flink/pull/26113#discussion_r1957097592
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/table/UnnestRowsFunctionBase.java: ########## @@ -0,0 +1,211 @@ +/* + * 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.functions.table; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.data.ArrayData; +import org.apache.flink.table.data.MapData; +import org.apache.flink.table.functions.BuiltInFunctionDefinitions; +import org.apache.flink.table.functions.UserDefinedFunction; +import org.apache.flink.table.runtime.functions.BuiltInSpecializedFunction; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.logical.ArrayType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.MapType; +import org.apache.flink.table.types.logical.MultisetType; +import org.apache.flink.table.types.logical.RowType; + +/** Base class for flattening ARRAY, MAP, and MULTISET using a table function. */ +@Internal +public abstract class UnnestRowsFunctionBase extends BuiltInSpecializedFunction { + + public UnnestRowsFunctionBase() { + super(BuiltInFunctionDefinitions.INTERNAL_UNNEST_ROWS); + } + + @Override + public UserDefinedFunction specialize(SpecializedContext context) { + final LogicalType argType = + context.getCallContext().getArgumentDataTypes().get(0).getLogicalType(); + switch (argType.getTypeRoot()) { + case ARRAY: + final ArrayType arrayType = (ArrayType) argType; + return createCollectionUnnestFunction( + context, + arrayType.getElementType(), + ArrayData.createElementGetter(arrayType.getElementType())); + case MULTISET: + final MultisetType multisetType = (MultisetType) argType; + return createCollectionUnnestFunction( + context, + multisetType.getElementType(), + ArrayData.createElementGetter(multisetType.getElementType())); + case MAP: + final MapType mapType = (MapType) argType; + return createMapUnnestFunction( + context, + RowType.of(false, mapType.getKeyType(), mapType.getValueType()), + ArrayData.createElementGetter(mapType.getKeyType()), + ArrayData.createElementGetter(mapType.getValueType())); + default: + throw new UnsupportedOperationException("Unsupported type for UNNEST: " + argType); + } + } + + protected abstract UserDefinedFunction createCollectionUnnestFunction( + SpecializedContext context, + LogicalType elementType, + ArrayData.ElementGetter elementGetter); + + protected abstract UserDefinedFunction createMapUnnestFunction( + SpecializedContext context, + RowType keyValTypes, + ArrayData.ElementGetter keyGetter, + ArrayData.ElementGetter valueGetter); + + public static LogicalType getUnnestedType(LogicalType logicalType, boolean withOrdinality) { + LogicalType elementType; + switch (logicalType.getTypeRoot()) { + case ARRAY: + elementType = ((ArrayType) logicalType).getElementType(); + break; + case MULTISET: + elementType = ((MultisetType) logicalType).getElementType(); + break; + case MAP: + MapType mapType = (MapType) logicalType; + elementType = RowType.of(false, mapType.getKeyType(), mapType.getValueType()); + break; + default: + throw new UnsupportedOperationException("Unsupported UNNEST type: " + logicalType); + } + + if (withOrdinality) { + return wrapWithOrdinality(elementType); + } + return elementType; + } + + public static LogicalType wrapWithOrdinality(LogicalType baseType) { + // If baseType is already a ROW, extract its fields and add an ordinality field + if (baseType instanceof RowType) { + RowType rowType = (RowType) baseType; + int fieldCount = rowType.getFieldCount(); + LogicalType[] types = new LogicalType[fieldCount + 1]; + String[] names = new String[types.length]; + + for (int i = 0; i < fieldCount; i++) { + types[i] = rowType.getTypeAt(i); + names[i] = "f" + i; Review Comment: Oh yes, nice. That makes sense. I was blind to only using RowType.of and didn't consider using new RowType. Since we are have the fields array from `.getFields`, we could do both: 1. ``` return new RowType( false, Stream.concat( rowType.getFields().stream(), Stream.of(new RowType.RowField("ordinality", DataTypes.INT().notNull().getLogicalType()))) .collect(Collectors.toList())); ``` and 2. ``` return new RowType( false, new ArrayList<>() {{ addAll(rowType.getFields()); add(new RowType.RowField("ordinality", DataTypes.INT().notNull().getLogicalType())); }} ); ``` I find 2. a bit easier to read but pushed your suggestion for now (1.) - no strong preference, though. Let me know if you have a preference. According to my findings, the performance should be exactly the same for both O(n + 1). -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org