generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 178
Support aggregation/window commands with dynamic fields #4743
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
ykmr1224
wants to merge
9
commits into
opensearch-project:feature/permissive
Choose a base branch
from
ykmr1224:dynamic-aggregation
base: feature/permissive
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
9 commits
Select commit
Hold shift + click to select a range
ed37afb
Fix aggregation for dynamic fields
ykmr1224 f49b645
Address comments
ykmr1224 bb5e543
Utilize coercion
ykmr1224 910a1b4
Fix timechart and trendline
ykmr1224 07d1cf8
minor fix
ykmr1224 5e4f6bc
Minor refactoring
ykmr1224 89c1b86
Add tests for spark sql verification
ykmr1224 0e191dd
Add comment
ykmr1224 d552010
Add explain verification
ykmr1224 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
73 changes: 73 additions & 0 deletions
73
common/src/main/java/org/opensearch/sql/common/utils/DebugUtils.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,73 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.common.utils; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** Utility class for debugging operations. */ | ||
| public class DebugUtils { | ||
|
|
||
| private static void print(String format, Object... args) { | ||
| System.out.println(String.format(format, args)); | ||
| } | ||
|
|
||
| public static <T> T debug(T obj, String message) { | ||
| print("### %s: %s (at %s)", message, stringify(obj), getCalledFrom(1)); | ||
| return obj; | ||
| } | ||
|
|
||
| public static <T> T debug(T obj) { | ||
| print("### %s (at %s)", stringify(obj), getCalledFrom(1)); | ||
| return obj; | ||
| } | ||
|
|
||
| private static String getCalledFrom(int pos) { | ||
| RuntimeException e = new RuntimeException(); | ||
| StackTraceElement item = e.getStackTrace()[pos + 1]; | ||
| return item.getClassName() + "." + item.getMethodName() + ":" + item.getLineNumber(); | ||
| } | ||
|
|
||
| private static String stringify(Collection<?> items) { | ||
| if (items == null) { | ||
| return "null"; | ||
| } | ||
|
|
||
| if (items.isEmpty()) { | ||
| return "()"; | ||
| } | ||
|
|
||
| String result = items.stream().map(i -> stringify(i)).collect(Collectors.joining(",")); | ||
|
|
||
| return "(" + result + ")"; | ||
| } | ||
|
|
||
| private static String stringify(Map<?, ?> map) { | ||
| if (map == null) { | ||
| return "[[null]]"; | ||
| } | ||
|
|
||
| if (map.isEmpty()) { | ||
| return "[[EMPTY]]"; | ||
| } | ||
|
|
||
| String result = | ||
| map.entrySet().stream() | ||
| .map(entry -> entry.getKey() + ": " + stringify(entry.getValue())) | ||
| .collect(Collectors.joining(",")); | ||
| return "{" + result + "}"; | ||
| } | ||
|
|
||
| private static String stringify(Object obj) { | ||
| if (obj instanceof Collection) { | ||
| return stringify((Collection) obj); | ||
| } else if (obj instanceof Map) { | ||
| return stringify((Map) obj); | ||
| } | ||
| return String.valueOf(obj); | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
common/src/main/java/org/opensearch/sql/common/utils/JsonUtils.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,48 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.common.utils; | ||
|
|
||
| import lombok.experimental.UtilityClass; | ||
|
|
||
| @UtilityClass | ||
| public class JsonUtils { | ||
| /** | ||
| * Utility method to build JSON string from multiple strings with single-quotes. This is just for | ||
| * ease of read and maintain in tests. sjson("{", "'key': 'name'", "}") -> "{\n \"key\": | ||
| * \"name\"\n}" | ||
| * | ||
| * @param lines lines using single-quote instead for double-quote | ||
| * @return sting joined inputs and replaces single-quotes with double-quotes | ||
| */ | ||
| public static String sjson(String... lines) { | ||
| StringBuilder builder = new StringBuilder(); | ||
| for (String line : lines) { | ||
| builder.append(replaceQuote(line)); | ||
| builder.append("\n"); | ||
| } | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| private static String replaceQuote(String line) { | ||
| return line.replace("'", "\""); | ||
| } | ||
|
|
||
| /** | ||
| * Utility method to build multiline string from list of strings. Last line will also have new | ||
| * line at the end. | ||
| * | ||
| * @param lines input lines | ||
| * @return string contains lines | ||
| */ | ||
| public static String lines(String... lines) { | ||
| StringBuilder builder = new StringBuilder(); | ||
| for (String line : lines) { | ||
| builder.append(line); | ||
| builder.append("\n"); | ||
| } | ||
| return builder.toString(); | ||
| } | ||
| } |
124 changes: 124 additions & 0 deletions
124
common/src/test/java/org/opensearch/sql/common/utils/JsonUtilsTest.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,124 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.common.utils; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class JsonUtilsTest { | ||
|
|
||
| @Test | ||
| public void testSjsonWithSingleLine() { | ||
| String result = JsonUtils.sjson("{'key': 'value'}"); | ||
| assertEquals("{\"key\": \"value\"}\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithMultipleLines() { | ||
| String result = JsonUtils.sjson("{", " 'name': 'John',", " 'age': 30", "}"); | ||
| assertEquals("{\n \"name\": \"John\",\n \"age\": 30\n}\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithEmptyString() { | ||
| String result = JsonUtils.sjson(""); | ||
| assertEquals("\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithNoQuotes() { | ||
| String result = JsonUtils.sjson("no quotes here"); | ||
| assertEquals("no quotes here\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithMixedQuotes() { | ||
| String result = JsonUtils.sjson("'single' and \"double\" quotes"); | ||
| assertEquals("\"single\" and \"double\" quotes\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithMultipleSingleQuotes() { | ||
| String result = JsonUtils.sjson("'key1': 'value1', 'key2': 'value2'"); | ||
| assertEquals("\"key1\": \"value1\", \"key2\": \"value2\"\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithNestedJson() { | ||
| String result = JsonUtils.sjson("{", " 'outer': {", " 'inner': 'value'", " }", "}"); | ||
| assertEquals("{\n \"outer\": {\n \"inner\": \"value\"\n }\n}\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithArrays() { | ||
| String result = JsonUtils.sjson("{", " 'items': ['item1', 'item2', 'item3']", "}"); | ||
| assertEquals("{\n \"items\": [\"item1\", \"item2\", \"item3\"]\n}\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithSpecialCharacters() { | ||
| String result = JsonUtils.sjson("{'key': 'value with \\'escaped\\' quotes'}"); | ||
| assertEquals("{\"key\": \"value with \\\"escaped\\\" quotes\"}\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithEmptyArray() { | ||
| String result = JsonUtils.sjson(); | ||
| assertEquals("", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithNullValues() { | ||
| String result = JsonUtils.sjson("{'key': null}"); | ||
| assertEquals("{\"key\": null}\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithNumbers() { | ||
| String result = | ||
| JsonUtils.sjson("{", " 'integer': 42,", " 'float': 3.14,", " 'negative': -10", "}"); | ||
| assertEquals("{\n \"integer\": 42,\n \"float\": 3.14,\n \"negative\": -10\n}\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithBooleans() { | ||
| String result = JsonUtils.sjson("{'active': true, 'deleted': false}"); | ||
| assertEquals("{\"active\": true, \"deleted\": false}\n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonPreservesWhitespace() { | ||
| String result = JsonUtils.sjson(" {'key': 'value'} "); | ||
| assertEquals(" {\"key\": \"value\"} \n", result); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSjsonWithComplexJson() { | ||
| String result = | ||
| JsonUtils.sjson( | ||
| "{", | ||
| " 'user': {", | ||
| " 'name': 'Alice',", | ||
| " 'email': '[email protected]',", | ||
| " 'roles': ['admin', 'user'],", | ||
| " 'active': true,", | ||
| " 'loginCount': 42", | ||
| " }", | ||
| "}"); | ||
| String expected = | ||
| "{\n" | ||
| + " \"user\": {\n" | ||
| + " \"name\": \"Alice\",\n" | ||
| + " \"email\": \"[email protected]\",\n" | ||
| + " \"roles\": [\"admin\", \"user\"],\n" | ||
| + " \"active\": true,\n" | ||
| + " \"loginCount\": 42\n" | ||
| + " }\n" | ||
| + "}\n"; | ||
| assertEquals(expected, result); | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it required for all visitor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could u add a test in CalciteDynamicFieldsTimechartIT to help understand what is correspond logical plan / sql
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added CalcitePPLDynamicFieldsTest.java for spark SQL. Added explains in IT.