gustavodemorais commented on code in PR #26113: URL: https://github.com/apache/flink/pull/26113#discussion_r1961268518
########## flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/table/UnnestRowsWithOrdinalityFunction.java: ########## @@ -0,0 +1,149 @@ +/* + * 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.data.ArrayData; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.MapData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.functions.UserDefinedFunction; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.RowType; + +/** + * Flattens ARRAY, MAP, and MULTISET using a table function and adds one extra column with the + * position of the element. It does this by another level of specialization using a subclass of + * {@link UnnestTableFunctionBase}. + */ +@Internal +public class UnnestRowsWithOrdinalityFunction extends UnnestRowsFunctionBase { + + public UnnestRowsWithOrdinalityFunction() { + super(true); + } + + @Override + protected UserDefinedFunction createCollectionUnnestFunction( + SpecializedContext context, + LogicalType elementType, + ArrayData.ElementGetter elementGetter) { + return new CollectionUnnestWithOrdinalityFunction( + context, wrapWithOrdinality(elementType), elementGetter); + } + + @Override + protected UserDefinedFunction createMapUnnestFunction( + SpecializedContext context, + RowType keyValTypes, + ArrayData.ElementGetter keyGetter, + ArrayData.ElementGetter valueGetter) { + return new MapUnnestWithOrdinalityFunction( + context, wrapWithOrdinality(keyValTypes), keyGetter, valueGetter); + } + + /** + * Table function that unwraps the elements of a collection (array or multiset) with ordinality. + */ + public static final class CollectionUnnestWithOrdinalityFunction + extends UnnestTableFunctionBase { + private static final long serialVersionUID = 1L; + + private final ArrayData.ElementGetter elementGetter; + private RowData.FieldGetter[] fieldGetters = null; + + public CollectionUnnestWithOrdinalityFunction( + SpecializedContext context, + LogicalType elementType, + ArrayData.ElementGetter elementGetter) { + super(context, elementType, true); + this.elementGetter = elementGetter; + + if (elementType instanceof RowType) { + /* When unnesting a collection, according to Calcite's implementation, + row(a,b) unnests to a row(a, b, ordinality) and not to (row(a,b), ordinality). + That means, if we are unnesting a row, we need field getters + to be able to extract all field values */ + RowType rowType = (RowType) elementType; + this.fieldGetters = createFieldGetters(rowType); + } + } + + public void eval(ArrayData arrayData) { + evalArrayData(arrayData, elementGetter, this::collectWithOrdinality); + } + + public void eval(MapData mapData) { + evalMultisetData(mapData, elementGetter, this::collectWithOrdinality); + } + + private void collectWithOrdinality(Object element, int position) { + if (element instanceof RowData) { + RowData innerRow = (RowData) element; + int arity = innerRow.getArity(); + GenericRowData outRow = new GenericRowData(arity + 1); Review Comment: Great tip, thanks! See https://github.com/apache/flink/pull/26113/commits/5c72d20acdb392f8f82d1b0b9d813206064d6774 -- 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