generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
Implement spath command with field resolution #5028
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
14
commits into
opensearch-project:main
Choose a base branch
from
ykmr1224:spath/field-resolution
base: main
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.
+2,957
−168
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
38e6115
Implement spath command with field resolution
ykmr1224 af26eb9
Fix for test failure
ykmr1224 8a96e7d
Another test fix
ykmr1224 9a02525
Fix test failure
ykmr1224 85d1bce
Address comments
ykmr1224 864490a
Address comment
ykmr1224 b23c4b3
Address comments
ykmr1224 540ebf8
Address comment
ykmr1224 8932a82
Add test case, etc.
ykmr1224 162fc0b
Quick fix
ykmr1224 7ac1ea0
Fix javadoc and test
ykmr1224 74aef92
Fix DebugUtils
ykmr1224 10a6fa7
Minor fix
ykmr1224 f3e597a
Merge branch 'main' into spath/field-resolution
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
89 changes: 89 additions & 0 deletions
89
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,89 @@ | ||
| /* | ||
| * 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; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| /** | ||
| * Utility class for debugging operations. This class is only for debugging purpose, and not | ||
| * intended to be used in production code. | ||
| */ | ||
| public class DebugUtils { | ||
| // Update this to true while you are debugging. (Safe guard to avoid usage in production code. ) | ||
| private static final boolean IS_DEBUG = false; | ||
| private static final Logger logger = LogManager.getLogger(DebugUtils.class); | ||
|
|
||
| public static <T> T debug(T obj, String message) { | ||
| verifyDebug(); | ||
| print("### %s: %s (at %s)", message, stringify(obj), getCalledFrom(1)); | ||
| return obj; | ||
| } | ||
|
|
||
| public static <T> T debug(T obj) { | ||
| verifyDebug(); | ||
| print("### %s (at %s)", stringify(obj), getCalledFrom(1)); | ||
| return obj; | ||
| } | ||
|
|
||
| private static void verifyDebug() { | ||
| if (!IS_DEBUG) { | ||
| throw new RuntimeException("DebugUtils can be used only for local debugging."); | ||
| } | ||
| } | ||
|
|
||
| private static void print(String format, Object... args) { | ||
| logger.info(String.format(format, args)); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
common/src/test/java/org/opensearch/sql/common/utils/DebugUtilsTest.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,23 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.common.utils; | ||
|
|
||
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class DebugUtilsTest { | ||
|
|
||
| @Test | ||
| public void testDebugThrowsRuntimeException() { | ||
| assertThrows(RuntimeException.class, () -> DebugUtils.debug("test", "test message")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDebugWithoutMessageThrowsRuntimeException() { | ||
| assertThrows(RuntimeException.class, () -> DebugUtils.debug("test")); | ||
| } | ||
| } |
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
39 changes: 39 additions & 0 deletions
39
core/src/main/java/org/opensearch/sql/ast/AstNodeUtils.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,39 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast; | ||
|
|
||
| import lombok.experimental.UtilityClass; | ||
| import org.opensearch.sql.ast.expression.Let; | ||
| import org.opensearch.sql.ast.expression.subquery.SubqueryExpression; | ||
|
|
||
| /** Utility class for AST node operations shared among visitor classes. */ | ||
| @UtilityClass | ||
| public class AstNodeUtils { | ||
|
|
||
| /** | ||
| * Checks if an AST node contains a subquery expression. | ||
| * | ||
| * @param expr The AST node to check | ||
| * @return true if the node or any of its children contains a subquery expression | ||
| */ | ||
| public static boolean containsSubqueryExpression(Node expr) { | ||
| if (expr == null) { | ||
| return false; | ||
| } | ||
| if (expr instanceof SubqueryExpression) { | ||
| return true; | ||
| } | ||
| if (expr instanceof Let l) { | ||
| return containsSubqueryExpression(l.getExpression()); | ||
| } | ||
| for (Node child : expr.getChild()) { | ||
| if (containsSubqueryExpression(child)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
ykmr1224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
71 changes: 71 additions & 0 deletions
71
core/src/main/java/org/opensearch/sql/ast/analysis/FieldResolutionContext.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,71 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.ast.analysis; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import lombok.Getter; | ||
| import org.opensearch.sql.ast.tree.Relation; | ||
| import org.opensearch.sql.ast.tree.UnresolvedPlan; | ||
|
|
||
| /** Context for field resolution using stack-based traversal. */ | ||
| public class FieldResolutionContext { | ||
|
|
||
| @Getter private final Map<UnresolvedPlan, FieldResolutionResult> results; | ||
| private final Deque<FieldResolutionResult> requirementsStack; | ||
|
|
||
| public FieldResolutionContext() { | ||
| this.results = new IdentityHashMap<>(); | ||
| this.requirementsStack = new ArrayDeque<>(); | ||
| this.requirementsStack.push(new FieldResolutionResult(Set.of(), "*")); | ||
| } | ||
|
|
||
| public void pushRequirements(FieldResolutionResult result) { | ||
| requirementsStack.push(result); | ||
| } | ||
|
|
||
| public FieldResolutionResult popRequirements() { | ||
| return requirementsStack.pop(); | ||
| } | ||
|
|
||
| public FieldResolutionResult getCurrentRequirements() { | ||
| if (requirementsStack.isEmpty()) { | ||
| throw new RuntimeException("empty stack"); | ||
| } else { | ||
| return requirementsStack.peek(); | ||
| } | ||
| } | ||
|
|
||
| public void setResult(UnresolvedPlan relation, FieldResolutionResult result) { | ||
| results.put(relation, result); | ||
| } | ||
|
|
||
| public Set<Relation> getRelations() { | ||
| return results.keySet().stream() | ||
| .filter(k -> k instanceof Relation) | ||
| .map(k -> (Relation) k) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| public static String mergeWildcardPatterns(Set<String> patterns) { | ||
| if (patterns == null || patterns.isEmpty()) { | ||
| return null; | ||
| } | ||
| if (patterns.size() == 1) { | ||
| return patterns.iterator().next(); | ||
| } | ||
| return String.join(" | ", patterns.stream().sorted().toList()); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "FieldResolutionContext{relationResults=" + results + "}"; | ||
| } | ||
| } |
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.