-
Notifications
You must be signed in to change notification settings - Fork 214
Support mvdedup eval function
#4828
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
50 changes: 50 additions & 0 deletions
50
core/src/main/java/org/opensearch/sql/expression/function/CollectionUDF/MVDedupCore.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,50 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function.CollectionUDF; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| /** Core logic for `mvdedup` command to remove duplicate values from a multivalue field */ | ||
| public class MVDedupCore { | ||
|
|
||
| /** | ||
| * Remove duplicate elements from the input array while preserving order. Null input returns null. | ||
| * Empty array returns empty array. Null elements within the array are filtered out. | ||
| * | ||
| * @param array input array (can be null) | ||
| * @return array with duplicates removed and null elements filtered out, or null if input is null | ||
| */ | ||
| public static List<Object> removeDuplicates(Object array) { | ||
|
ahkcs marked this conversation as resolved.
Outdated
|
||
| if (array == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!(array instanceof List)) { | ||
| // If not a list, wrap it in a list | ||
| List<Object> result = new ArrayList<>(); | ||
| result.add(array); | ||
| return result; | ||
| } | ||
|
|
||
| List<?> inputList = (List<?>) array; | ||
| if (inputList.isEmpty()) { | ||
| return new ArrayList<>(); | ||
| } | ||
|
|
||
| // Use LinkedHashSet to preserve insertion order while removing duplicates | ||
| Set<Object> seen = new LinkedHashSet<>(); | ||
| for (Object item : inputList) { | ||
| if (item != null) { | ||
| seen.add(item); | ||
| } | ||
| } | ||
|
|
||
| return new ArrayList<>(seen); | ||
| } | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
...c/main/java/org/opensearch/sql/expression/function/CollectionUDF/MVDedupFunctionImpl.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,78 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function.CollectionUDF; | ||
|
|
||
| import static org.apache.calcite.sql.type.SqlTypeUtil.createArrayType; | ||
|
|
||
| import java.util.List; | ||
| import org.apache.calcite.adapter.enumerable.NotNullImplementor; | ||
| import org.apache.calcite.adapter.enumerable.NullPolicy; | ||
| import org.apache.calcite.adapter.enumerable.RexToLixTranslator; | ||
| import org.apache.calcite.linq4j.tree.Expression; | ||
| import org.apache.calcite.linq4j.tree.Expressions; | ||
| import org.apache.calcite.linq4j.tree.Types; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.sql.type.SqlReturnTypeInference; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
| import org.opensearch.sql.expression.function.ImplementorUDF; | ||
| import org.opensearch.sql.expression.function.UDFOperandMetadata; | ||
|
|
||
| /** | ||
| * MVDedup function that removes duplicate values from a multivalue array while preserving order. | ||
| * Returns an array with duplicates removed or null for consistent type behavior. | ||
| */ | ||
| public class MVDedupFunctionImpl extends ImplementorUDF { | ||
|
|
||
| public MVDedupFunctionImpl() { | ||
| super(new MVDedupImplementor(), NullPolicy.ARG0); | ||
| } | ||
|
|
||
| @Override | ||
| public SqlReturnTypeInference getReturnTypeInference() { | ||
| return sqlOperatorBinding -> { | ||
| RelDataTypeFactory typeFactory = sqlOperatorBinding.getTypeFactory(); | ||
|
|
||
| if (sqlOperatorBinding.getOperandCount() == 0) { | ||
| return typeFactory.createSqlType(SqlTypeName.NULL); | ||
| } | ||
|
|
||
| RelDataType operandType = sqlOperatorBinding.getOperandType(0); | ||
|
|
||
| // If operand is already an array, return the same array type | ||
| if (!operandType.isStruct() && operandType.getComponentType() != null) { | ||
| return createArrayType( | ||
| typeFactory, | ||
| typeFactory.createTypeWithNullability(operandType.getComponentType(), true), | ||
| true); | ||
| } | ||
|
|
||
| // If operand is not an array, wrap it in an array type | ||
| return createArrayType( | ||
| typeFactory, typeFactory.createTypeWithNullability(operandType, true), true); | ||
|
ahkcs marked this conversation as resolved.
Outdated
|
||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public UDFOperandMetadata getOperandMetadata() { | ||
| return null; | ||
|
ahkcs marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public static class MVDedupImplementor implements NotNullImplementor { | ||
| @Override | ||
| public Expression implement( | ||
| RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { | ||
| return Expressions.call( | ||
| Types.lookupMethod(MVDedupFunctionImpl.class, "mvdedup", Object.class), | ||
| translatedOperands.get(0)); | ||
| } | ||
| } | ||
|
|
||
| public static Object mvdedup(Object array) { | ||
| return MVDedupCore.removeDuplicates(array); | ||
| } | ||
| } | ||
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
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 |
|---|---|---|
|
|
@@ -1095,6 +1095,7 @@ collectionFunctionName | |
| | MVAPPEND | ||
| | MVJOIN | ||
| | MVINDEX | ||
| | MVDEDUP | ||
| | FORALL | ||
| | EXISTS | ||
| | FILTER | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.