generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 180
[Calcite] Build integration test framework #3342
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
LantaoJin
merged 5 commits into
opensearch-project:feature/calcite-engine
from
LantaoJin:pr/issues/3330
Feb 26, 2025
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de45f08
Build integration test framework
LantaoJin 64177ae
make local work
LantaoJin 2469c26
Fix the timestamp issue
LantaoJin 4883b30
address comments
LantaoJin c3c2f3b
fix java style and rename CalcitePPLTestCase back to CalcitePPLIntegT…
LantaoJin 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
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
185 changes: 185 additions & 0 deletions
185
core/src/main/java/org/opensearch/sql/calcite/utils/CalciteToolsHelper.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,185 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.utils; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.SQLException; | ||
| import java.util.Properties; | ||
| import org.apache.calcite.adapter.java.JavaTypeFactory; | ||
| import org.apache.calcite.avatica.AvaticaConnection; | ||
| import org.apache.calcite.avatica.AvaticaFactory; | ||
| import org.apache.calcite.avatica.UnregisteredDriver; | ||
| import org.apache.calcite.config.CalciteConnectionProperty; | ||
| import org.apache.calcite.interpreter.Bindables; | ||
| import org.apache.calcite.jdbc.CalciteFactory; | ||
| import org.apache.calcite.jdbc.CalciteJdbc41Factory; | ||
| import org.apache.calcite.jdbc.CalcitePrepare; | ||
| import org.apache.calcite.jdbc.CalciteSchema; | ||
| import org.apache.calcite.jdbc.Driver; | ||
| import org.apache.calcite.plan.Context; | ||
| import org.apache.calcite.plan.RelOptCluster; | ||
| import org.apache.calcite.plan.RelOptPlanner; | ||
| import org.apache.calcite.plan.RelOptSchema; | ||
| import org.apache.calcite.plan.RelOptTable; | ||
| import org.apache.calcite.prepare.CalciteCatalogReader; | ||
| import org.apache.calcite.prepare.CalcitePrepareImpl; | ||
| import org.apache.calcite.rel.RelHomogeneousShuttle; | ||
| import org.apache.calcite.rel.RelNode; | ||
| import org.apache.calcite.rel.RelShuttle; | ||
| import org.apache.calcite.rel.core.TableScan; | ||
| import org.apache.calcite.rel.logical.LogicalTableScan; | ||
| import org.apache.calcite.rel.type.RelDataTypeSystem; | ||
| import org.apache.calcite.rex.RexBuilder; | ||
| import org.apache.calcite.schema.SchemaPlus; | ||
| import org.apache.calcite.server.CalciteServerStatement; | ||
| import org.apache.calcite.tools.FrameworkConfig; | ||
| import org.apache.calcite.tools.Frameworks; | ||
| import org.apache.calcite.tools.RelBuilder; | ||
| import org.apache.calcite.tools.RelRunner; | ||
| import org.apache.calcite.util.Util; | ||
| import org.opensearch.sql.calcite.CalcitePlanContext; | ||
|
|
||
| /** | ||
| * Calcite Tools Helper. This class is used to create customized: 1. Connection 2. JavaTypeFactory | ||
| * 3. RelBuilder 4. RelRunner TODO delete it in future if possible. | ||
| */ | ||
| public class CalciteToolsHelper { | ||
|
|
||
| /** Create a RelBuilder with testing */ | ||
| public static RelBuilder create(FrameworkConfig config) { | ||
| return RelBuilder.create(config); | ||
| } | ||
|
|
||
| /** Create a RelBuilder with typeFactory */ | ||
| public static RelBuilder create( | ||
| FrameworkConfig config, JavaTypeFactory typeFactory, Connection connection) { | ||
| return withPrepare( | ||
| config, | ||
| typeFactory, | ||
| connection, | ||
| (cluster, relOptSchema, rootSchema, statement) -> | ||
| new OpenSearchRelBuilder(config.getContext(), cluster, relOptSchema)); | ||
| } | ||
|
|
||
| public static Connection connect(FrameworkConfig config, JavaTypeFactory typeFactory) { | ||
| final Properties info = new Properties(); | ||
| if (config.getTypeSystem() != RelDataTypeSystem.DEFAULT) { | ||
| info.setProperty( | ||
| CalciteConnectionProperty.TYPE_SYSTEM.camelName(), | ||
| config.getTypeSystem().getClass().getName()); | ||
| } | ||
| try { | ||
| return new OpenSearchDriver().connect("jdbc:calcite:", info, null, typeFactory); | ||
| } catch (SQLException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This method copied from {@link Frameworks#withPrepare(FrameworkConfig, | ||
| * Frameworks.BasePrepareAction)}. The purpose is the method {@link | ||
| * CalciteFactory#newConnection(UnregisteredDriver, AvaticaFactory, String, Properties)} create | ||
| * connection with null instance of JavaTypeFactory. So we add a parameter JavaTypeFactory. | ||
| */ | ||
| private static <R> R withPrepare( | ||
| FrameworkConfig config, | ||
| JavaTypeFactory typeFactory, | ||
| Connection connection, | ||
| Frameworks.BasePrepareAction<R> action) { | ||
| try { | ||
| final Properties info = new Properties(); | ||
| if (config.getTypeSystem() != RelDataTypeSystem.DEFAULT) { | ||
| info.setProperty( | ||
| CalciteConnectionProperty.TYPE_SYSTEM.camelName(), | ||
| config.getTypeSystem().getClass().getName()); | ||
| } | ||
| final CalciteServerStatement statement = | ||
| connection.createStatement().unwrap(CalciteServerStatement.class); | ||
| return new OpenSearchPrepareImpl().perform(statement, config, typeFactory, action); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public static class OpenSearchDriver extends Driver { | ||
|
|
||
| public Connection connect( | ||
| String url, Properties info, CalciteSchema rootSchema, JavaTypeFactory typeFactory) | ||
| throws SQLException { | ||
| CalciteJdbc41Factory factory = new CalciteJdbc41Factory(); | ||
| AvaticaConnection connection = | ||
| factory.newConnection((Driver) this, factory, url, info, rootSchema, typeFactory); | ||
| this.handler.onConnectionInit(connection); | ||
| return connection; | ||
| } | ||
| } | ||
|
|
||
| /** do nothing, just extend for a public construct for new */ | ||
| public static class OpenSearchRelBuilder extends RelBuilder { | ||
| public OpenSearchRelBuilder(Context context, RelOptCluster cluster, RelOptSchema relOptSchema) { | ||
| super(context, cluster, relOptSchema); | ||
| } | ||
| } | ||
|
|
||
| public static class OpenSearchPrepareImpl extends CalcitePrepareImpl { | ||
| /** | ||
| * Similar to {@link CalcitePrepareImpl#perform(CalciteServerStatement, FrameworkConfig, | ||
| * Frameworks.BasePrepareAction)}, but with a custom typeFactory. | ||
| */ | ||
| public <R> R perform( | ||
| CalciteServerStatement statement, | ||
| FrameworkConfig config, | ||
| JavaTypeFactory typeFactory, | ||
| Frameworks.BasePrepareAction<R> action) { | ||
| final CalcitePrepare.Context prepareContext = statement.createPrepareContext(); | ||
| SchemaPlus defaultSchema = config.getDefaultSchema(); | ||
| final CalciteSchema schema = | ||
| defaultSchema != null | ||
| ? CalciteSchema.from(defaultSchema) | ||
| : prepareContext.getRootSchema(); | ||
| CalciteCatalogReader catalogReader = | ||
| new CalciteCatalogReader( | ||
| schema.root(), schema.path(null), typeFactory, prepareContext.config()); | ||
| final RexBuilder rexBuilder = new RexBuilder(typeFactory); | ||
| final RelOptPlanner planner = | ||
| createPlanner(prepareContext, config.getContext(), config.getCostFactory()); | ||
| final RelOptCluster cluster = createCluster(planner, rexBuilder); | ||
| return action.apply(cluster, catalogReader, prepareContext.getRootSchema().plus(), statement); | ||
| } | ||
| } | ||
|
|
||
| public static class OpenSearchRelRunners { | ||
| /** | ||
| * Runs a relational expression by existing connection. This class copied from {@link | ||
| * org.apache.calcite.tools.RelRunners#run(RelNode)} | ||
| */ | ||
| public static PreparedStatement run(CalcitePlanContext context, RelNode rel) { | ||
| final RelShuttle shuttle = | ||
| new RelHomogeneousShuttle() { | ||
| @Override | ||
| public RelNode visit(TableScan scan) { | ||
| final RelOptTable table = scan.getTable(); | ||
| if (scan instanceof LogicalTableScan | ||
| && Bindables.BindableTableScan.canHandle(table)) { | ||
| // Always replace the LogicalTableScan with BindableTableScan | ||
| // because it's implementation does not require a "schema" as context. | ||
| return Bindables.BindableTableScan.create(scan.getCluster(), table); | ||
| } | ||
| return super.visit(scan); | ||
| } | ||
| }; | ||
| rel = rel.accept(shuttle); | ||
| // the line we changed here | ||
| try (Connection connection = context.connection) { | ||
| final RelRunner runner = connection.unwrap(RelRunner.class); | ||
| return runner.prepareStatement(rel); | ||
| } catch (SQLException e) { | ||
| throw Util.throwAsRuntime(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
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.
I think we should add calcite's license header after ours since we copy code from that repo.
There is similar example in
sql/async-query-core/src/main/antlr/SparkSqlBase.g4
Line 6 in 28275b8