-
Notifications
You must be signed in to change notification settings - Fork 14k
[FLINK-40357][table] Add ARRAY_FLATTEN function to Flink SQL #28945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cytq-123
wants to merge
13
commits into
apache:master
Choose a base branch
from
cytq-123:add-array-flatten-FLINK-40357
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1b6965d
[FLINK-40357][table] Add ARRAY_FLATTEN function to Flink SQL
cytq-123 34cd432
[FLINK-40357] Fix type error in ArrayFlattenFunction
cytq-123 e4700f5
[FLINK-40357] Fix code formatting
cytq-123 6ffffd7
[FLINK-40357] Fix type inference and code formatting
cytq-123 8296fe4
[FLINK-40357] Simplify ARRAY_FLATTEN test cases to basic scenarios
cytq-123 3e4d631
[FLINK-40357] Add NULL handling tests for ARRAY_FLATTEN
cytq-123 3b9f17b
[FLINK-40357] Add edge case tests for ARRAY_FLATTEN (all NULL inner a…
cytq-123 61dd151
[FLINK-40357] Remove all-NULL inner arrays test due to test framework…
cytq-123 efffb0e
[FLINK-40357] Add documentation and error test cases for ARRAY_FLATTEN
cytq-123 69b07f1
[FLINK-40357] Add empty array edge case test for ARRAY_FLATTEN
cytq-123 2c0ee64
Fix error message format for ARRAY_FLATTEN validation
cytq-123 9de53c9
[FLINK-40357][table] Fix ARRAY_FLATTEN validation error message and a…
cytq-123 45240e7
[FLINK-40357][table] Remove empty-array input test case due to test f…
cytq-123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...e/src/main/java/org/apache/flink/table/runtime/functions/scalar/ArrayFlattenFunction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * 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.scalar; | ||
|
|
||
| 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.functions.BuiltInFunctionDefinitions; | ||
| import org.apache.flink.table.functions.SpecializedFunction; | ||
| import org.apache.flink.table.types.CollectionDataType; | ||
| import org.apache.flink.table.types.DataType; | ||
| import org.apache.flink.util.FlinkRuntimeException; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Implementation of {@link BuiltInFunctionDefinitions#ARRAY_FLATTEN}. | ||
| * | ||
| * <p>Flattens a nested array by one level. | ||
| * | ||
| * <p>NULL handling: | ||
| * | ||
| * <ul> | ||
| * <li>If the input array is NULL, returns NULL | ||
| * <li>NULL inner arrays are skipped | ||
| * <li>NULL elements within arrays are preserved | ||
| * </ul> | ||
| */ | ||
| @Internal | ||
| public class ArrayFlattenFunction extends BuiltInScalarFunction { | ||
| private final ArrayData.ElementGetter outerElementGetter; | ||
| private final ArrayData.ElementGetter innerElementGetter; | ||
|
|
||
| public ArrayFlattenFunction(SpecializedFunction.SpecializedContext context) { | ||
| super(BuiltInFunctionDefinitions.ARRAY_FLATTEN, context); | ||
|
|
||
| // Get the input data type (ARRAY<ARRAY<T>>) | ||
| final DataType inputDataType = context.getCallContext().getArgumentDataTypes().get(0); | ||
| // Get the inner array type (ARRAY<T>) | ||
| final DataType innerArrayDataType = | ||
| ((CollectionDataType) inputDataType).getElementDataType(); | ||
| // Get the element type (T) | ||
| final DataType elementDataType = | ||
| ((CollectionDataType) innerArrayDataType).getElementDataType(); | ||
|
|
||
| // Create element getters | ||
| // Outer getter retrieves inner arrays from the outer array | ||
| outerElementGetter = ArrayData.createElementGetter(innerArrayDataType.getLogicalType()); | ||
| // Inner getter retrieves elements from inner arrays | ||
| innerElementGetter = ArrayData.createElementGetter(elementDataType.getLogicalType()); | ||
| } | ||
|
|
||
| /** | ||
| * Flattens a nested array by one level. | ||
| * | ||
| * @param array the input array of arrays | ||
| * @return the flattened array, or NULL if input is NULL | ||
| */ | ||
| public @Nullable ArrayData eval(ArrayData array) { | ||
| if (array == null) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| List<Object> result = new ArrayList<>(); | ||
|
|
||
| // Iterate through outer array | ||
| for (int i = 0; i < array.size(); i++) { | ||
| ArrayData innerArray = (ArrayData) outerElementGetter.getElementOrNull(array, i); | ||
|
|
||
| if (innerArray == null) { | ||
| // Skip NULL inner arrays | ||
| continue; | ||
| } | ||
|
|
||
| // Iterate through inner array and add all elements (including NULL) | ||
| for (int j = 0; j < innerArray.size(); j++) { | ||
| Object element = innerElementGetter.getElementOrNull(innerArray, j); | ||
| result.add(element); // Preserve NULL elements | ||
| } | ||
| } | ||
|
|
||
| return new GenericArrayData(result.toArray()); | ||
| } catch (Throwable t) { | ||
| throw new FlinkRuntimeException(t); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.