generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 180
Add MAP_APPEND internal function to Calcite PPL #4515
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 all commits
Commits
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
41 changes: 41 additions & 0 deletions
41
core/src/main/java/org/opensearch/sql/expression/function/CollectionUDF/MVAppendCore.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,41 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function.CollectionUDF; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** Core logic for `mvappend` command to collect elements from list of args */ | ||
| public class MVAppendCore { | ||
|
|
||
| /** | ||
| * Collect non-null elements from `args`. If an item is a list, it will collect non-null elements | ||
| * of the list. See {@ref MVAppendFunctionImplTest} for detailed behavior. | ||
| */ | ||
| public static List<Object> collectElements(Object... args) { | ||
| List<Object> elements = new ArrayList<>(); | ||
|
|
||
| for (Object arg : args) { | ||
| if (arg == null) { | ||
| continue; | ||
| } else if (arg instanceof List) { | ||
| addListElements((List<?>) arg, elements); | ||
| } else { | ||
| elements.add(arg); | ||
| } | ||
| } | ||
|
|
||
| return elements.isEmpty() ? null : elements; | ||
| } | ||
|
|
||
| private static void addListElements(List<?> list, List<Object> elements) { | ||
| for (Object item : list) { | ||
| if (item != null) { | ||
| elements.add(item); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
115 changes: 115 additions & 0 deletions
115
...main/java/org/opensearch/sql/expression/function/CollectionUDF/MapAppendFunctionImpl.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,115 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function.CollectionUDF; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| 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.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; | ||
|
|
||
| /** | ||
| * MapAppend function that merges two maps. All the values will be converted to list for type | ||
| * consistency. | ||
| */ | ||
| public class MapAppendFunctionImpl extends ImplementorUDF { | ||
|
|
||
| public MapAppendFunctionImpl() { | ||
| super(new MapAppendImplementor(), NullPolicy.ALL); | ||
| } | ||
|
|
||
| @Override | ||
| public SqlReturnTypeInference getReturnTypeInference() { | ||
| return sqlOperatorBinding -> { | ||
| RelDataTypeFactory typeFactory = sqlOperatorBinding.getTypeFactory(); | ||
| return typeFactory.createMapType( | ||
| typeFactory.createSqlType(SqlTypeName.VARCHAR), | ||
| typeFactory.createSqlType(SqlTypeName.ANY)); | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public UDFOperandMetadata getOperandMetadata() { | ||
| return null; | ||
| } | ||
|
|
||
| public static class MapAppendImplementor implements NotNullImplementor { | ||
| @Override | ||
| public Expression implement( | ||
| RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { | ||
| if (translatedOperands.size() != 2) { | ||
| throw new IllegalArgumentException("MAP_APPEND function requires exactly 2 arguments"); | ||
| } | ||
|
|
||
| return Expressions.call( | ||
| Types.lookupMethod(MapAppendFunctionImpl.class, "mapAppend", Object.class, Object.class), | ||
| translatedOperands.get(0), | ||
| translatedOperands.get(1)); | ||
| } | ||
| } | ||
|
|
||
| public static Object mapAppend(Object map1, Object map2) { | ||
| if (map1 == null && map2 == null) { | ||
| return null; | ||
| } | ||
| if (map1 == null) { | ||
| return mapAppendImpl(verifyMap(map2)); | ||
| } | ||
| if (map2 == null) { | ||
| return mapAppendImpl(verifyMap(map1)); | ||
| } | ||
|
|
||
| return mapAppendImpl(verifyMap(map1), verifyMap(map2)); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static Map<String, Object> verifyMap(Object map) { | ||
| if (!(map instanceof Map)) { | ||
| throw new IllegalArgumentException( | ||
| "MAP_APPEND function requires both arguments to be MAP type"); | ||
| } | ||
| return (Map<String, Object>) map; | ||
| } | ||
|
|
||
| static Map<String, Object> mapAppendImpl(Map<String, Object> map) { | ||
| Map<String, Object> result = new HashMap<>(); | ||
| for (String key : map.keySet()) { | ||
| result.put(key, MVAppendCore.collectElements(map.get(key))); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| static Map<String, Object> mapAppendImpl( | ||
| Map<String, Object> firstMap, Map<String, Object> secondMap) { | ||
| Map<String, Object> result = new HashMap<>(); | ||
|
|
||
| for (String key : mergeKeys(firstMap, secondMap)) { | ||
| result.put(key, MVAppendCore.collectElements(firstMap.get(key), secondMap.get(key))); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static Set<String> mergeKeys( | ||
| Map<String, Object> firstMap, Map<String, Object> secondMap) { | ||
| Set<String> keys = new HashSet<>(); | ||
| keys.addAll(firstMap.keySet()); | ||
| keys.addAll(secondMap.keySet()); | ||
| return keys; | ||
| } | ||
| } |
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
108 changes: 108 additions & 0 deletions
108
.../java/org/opensearch/sql/expression/function/CollectionUDF/MapAppendFunctionImplTest.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,108 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function.CollectionUDF; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class MapAppendFunctionImplTest { | ||
| @Test | ||
| void testMapAppendWithNonOverlappingKeys() { | ||
| Map<String, Object> map1 = getMap1(); | ||
| Map<String, Object> map2 = getMap2(); | ||
|
|
||
| Map<String, Object> result = MapAppendFunctionImpl.mapAppendImpl(map1, map2); | ||
|
|
||
| assertEquals(4, result.size()); | ||
| assertMapListValues(result, "a", "value1"); | ||
| assertMapListValues(result, "b", "value2"); | ||
| assertMapListValues(result, "c", "value3"); | ||
| assertMapListValues(result, "d", "value4"); | ||
| } | ||
|
|
||
| @Test | ||
| void testMapAppendWithOverlappingKeys() { | ||
| Map<String, Object> map1 = getMap1(); | ||
| Map<String, Object> map2 = Map.of("b", "value3", "c", "value4"); | ||
|
|
||
| Map<String, Object> result = MapAppendFunctionImpl.mapAppendImpl(map1, map2); | ||
|
|
||
| assertEquals(3, result.size()); | ||
| assertMapListValues(result, "a", "value1"); | ||
| assertMapListValues(result, "b", "value2", "value3"); | ||
| assertMapListValues(result, "c", "value4"); | ||
| } | ||
|
|
||
| @Test | ||
| void testMapAppendWithArrayValues() { | ||
| Map<String, Object> map1 = Map.of("a", List.of("item1", "item2"), "b", "single"); | ||
| Map<String, Object> map2 = Map.of("a", "item3", "c", List.of("item4", "item5")); | ||
|
|
||
| Map<String, Object> result = MapAppendFunctionImpl.mapAppendImpl(map1, map2); | ||
|
|
||
| assertEquals(3, result.size()); | ||
| assertMapListValues(result, "a", "item1", "item2", "item3"); | ||
| assertMapListValues(result, "b", "single"); | ||
| assertMapListValues(result, "c", "item4", "item5"); | ||
| } | ||
|
|
||
| @Test | ||
| void testMapAppendWithNullValues() { | ||
| Map<String, Object> map1 = getMap1(); | ||
| map1.put("b", null); | ||
| Map<String, Object> map2 = getMap2(); | ||
| map2.put("b", "value2"); | ||
| map2.put("a", null); | ||
|
|
||
| Map<String, Object> result = MapAppendFunctionImpl.mapAppendImpl(map1, map2); | ||
|
|
||
| assertEquals(4, result.size()); | ||
| assertMapListValues(result, "a", "value1"); | ||
| assertMapListValues(result, "b", "value2"); | ||
| assertMapListValues(result, "c", "value3"); | ||
| assertMapListValues(result, "d", "value4"); | ||
| } | ||
|
|
||
| @Test | ||
| void testMapAppendWithSingleParam() { | ||
| Map<String, Object> map1 = getMap1(); | ||
|
|
||
| Map<String, Object> result = MapAppendFunctionImpl.mapAppendImpl(map1); | ||
|
|
||
| assertEquals(2, result.size()); | ||
| assertMapListValues(result, "a", "value1"); | ||
| assertMapListValues(result, "b", "value2"); | ||
| } | ||
|
|
||
| private Map<String, Object> getMap1() { | ||
| Map<String, Object> map1 = new HashMap<>(); | ||
| map1.put("a", "value1"); | ||
| map1.put("b", "value2"); | ||
| return map1; | ||
| } | ||
|
|
||
| private Map<String, Object> getMap2() { | ||
| Map<String, Object> map2 = new HashMap<>(); | ||
| map2.put("c", "value3"); | ||
| map2.put("d", "value4"); | ||
| return map2; | ||
| } | ||
|
|
||
| private void assertMapListValues(Map<String, Object> map, String key, Object... expectedValues) { | ||
| Object val = map.get(key); | ||
| assertTrue(val instanceof List); | ||
| List<Object> result = (List<Object>) val; | ||
| assertEquals(expectedValues.length, result.size()); | ||
| for (int i = 0; i < expectedValues.length; i++) { | ||
| assertEquals(expectedValues[i], result.get(i)); | ||
| } | ||
| } | ||
| } |
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.