generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
Add unified query compiler API #4974
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
dai-chen
merged 10 commits into
opensearch-project:main
from
dai-chen:add-unified-query-evaluate-api-with-context
Jan 7, 2026
+554
−42
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4dbfacf
Reuse context in test base class
dai-chen a86f0e4
Rename package from runtime to compiler
dai-chen f96801c
Refactor compiler to pass in context via constructor
dai-chen 310f168
Polish javadoc and test code
dai-chen 9fe3049
Add IT for OpenSearch integration with unified query API
dai-chen a549946
Simplify IT and reuse test fixtures from api module
dai-chen a8fadd0
Polish doc
dai-chen 78761d4
Fix connection close issue and close in context
dai-chen 0f69a61
Polish for PR review
dai-chen 58589e7
Address AI comment regarding double closing context
dai-chen 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
Some comments aren't visible on the classic Files Changed page.
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
71 changes: 71 additions & 0 deletions
71
api/src/main/java/org/opensearch/sql/api/compiler/UnifiedQueryCompiler.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.api.compiler; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import lombok.NonNull; | ||
| import org.apache.calcite.interpreter.Bindables; | ||
| import org.apache.calcite.plan.RelOptTable; | ||
| import org.apache.calcite.rel.RelHomogeneousShuttle; | ||
| import org.apache.calcite.rel.RelNode; | ||
| import org.apache.calcite.rel.core.TableScan; | ||
| import org.apache.calcite.rel.logical.LogicalTableScan; | ||
| import org.apache.calcite.tools.RelRunner; | ||
| import org.opensearch.sql.api.UnifiedQueryContext; | ||
|
|
||
| /** | ||
| * {@code UnifiedQueryCompiler} compiles Calcite logical plans ({@link RelNode}) into executable | ||
| * JDBC statements, separating query compilation from execution. | ||
| */ | ||
| public class UnifiedQueryCompiler { | ||
|
|
||
| /** Unified query context containing CalcitePlanContext with all configuration. */ | ||
| private final UnifiedQueryContext context; | ||
|
|
||
| /** | ||
| * Constructs a UnifiedQueryCompiler with a unified query context. | ||
| * | ||
| * @param context the unified query context containing CalcitePlanContext | ||
| */ | ||
| public UnifiedQueryCompiler(UnifiedQueryContext context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| /** | ||
| * Compiles a Calcite logical plan into an executable {@link PreparedStatement}. Similar to {@code | ||
| * CalciteToolsHelper.OpenSearchRelRunners.run()} but does not close the connection, leaving | ||
| * resource management to {@link UnifiedQueryContext}. | ||
| * | ||
| * @param plan the logical plan to compile (must not be null) | ||
| * @return a compiled PreparedStatement ready for execution | ||
| * @throws IllegalStateException if compilation fails | ||
| */ | ||
| public PreparedStatement compile(@NonNull RelNode plan) { | ||
| try { | ||
| // Apply shuttle to convert LogicalTableScan to BindableTableScan | ||
| final RelHomogeneousShuttle shuttle = | ||
| new RelHomogeneousShuttle() { | ||
| @Override | ||
| public RelNode visit(TableScan scan) { | ||
| final RelOptTable table = scan.getTable(); | ||
| if (scan instanceof LogicalTableScan | ||
| && Bindables.BindableTableScan.canHandle(table)) { | ||
| return Bindables.BindableTableScan.create(scan.getCluster(), table); | ||
| } | ||
| return super.visit(scan); | ||
| } | ||
| }; | ||
| RelNode transformedPlan = plan.accept(shuttle); | ||
|
|
||
| Connection connection = context.getPlanContext().connection; | ||
| final RelRunner runner = connection.unwrap(RelRunner.class); | ||
| return runner.prepareStatement(transformedPlan); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Failed to compile logical plan", e); | ||
| } | ||
| } | ||
| } |
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.
UnifiedQueryCompiler.compile() compiles a RelNode into an executable query plan by leveraging Calcite’s Enumerable physical operators?
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.
Yes, it's the same as current PPL Calcite logic in core module. Thanks!