Skip to content

Conversation

LantaoJin
Copy link
Member

@LantaoJin LantaoJin commented Jul 7, 2025

Description

Currently, when the Calcite engine is enabled, ​​all query plans​​ -- regardless of their structure -- are converted to EnumerableRel. This means the plan is transformed into a Linq4j Expression, generates Java code, and undergoes just-in-time (JIT) compilation before execution.

For ​​simple or fully pushdown-compatible queries​​ (e.g., source=t, source=t | where a=1, source=t | sort a, or source=t | where a=1 | sort b | head 10), the optimized plans only contain node EnumerableTableScan. Despite their simplicity, these plans ​​unnecessarily undergo code generation and dynamic compilation​​, introducing overhead.

To ​​reduce codegen and compilation time​​, we propose converting such plans to BindableRel instead of EnumerableRel. This bypasses code generation and compilation entirely, ​​improving plan execution time by ~30% (By benchmarking the simple queries by #3822)

Related Issues

Resolves #3852

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Signed-off-by: Lantao Jin <ltjin@amazon.com>
@LantaoJin LantaoJin added enhancement New feature or request calcite calcite migration releated labels Jul 7, 2025
Signed-off-by: Lantao Jin <ltjin@amazon.com>
LantaoJin added 2 commits July 7, 2025 22:40
Signed-off-by: Lantao Jin <ltjin@amazon.com>
Signed-off-by: Lantao Jin <ltjin@amazon.com>
@LantaoJin LantaoJin added the performance Make it fast! label Jul 8, 2025
import org.apache.calcite.linq4j.Enumerable;
import org.checkerframework.checker.nullness.qual.Nullable;

public interface Scannable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add javadoc to explain

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

@Override
protected Function0<CalcitePrepare> createPrepareFactory() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Looks like this method is marked as @Deprecated. Use withPrepareFactory() in OpenSearchDriver instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we cannot use withPrepareFactory since withPrepareFactory will return a Driver instead of OpenSearchDriver. We need the added connect() method of OpenSearchDriver.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Em, maybe we could with casting.

    try {
      OpenSearchDriver driver = (OpenSearchDriver) new OpenSearchDriver().withPrepareFactory(OpenSearchPrepareImpl::new);
      return driver.connect("jdbc:calcite:", info, null, typeFactory);
    } catch (SQLException e) {
      throw new RuntimeException(e);
    }

Anyway, let' keep current createPrepareFactory() unless it is removed in future.

Signed-off-by: Lantao Jin <ltjin@amazon.com>
Signed-off-by: Lantao Jin <ltjin@amazon.com>
@LantaoJin LantaoJin marked this pull request as ready for review July 8, 2025 05:08
Comment on lines +248 to +256
final JavaTypeFactory typeFactory = context.getTypeFactory();
final EnumerableRel.Prefer prefer;
if (elementType == Object[].class) {
prefer = EnumerableRel.Prefer.ARRAY;
} else {
prefer = EnumerableRel.Prefer.CUSTOM;
}
final Convention resultConvention =
enableBindable ? BindableConvention.INSTANCE : EnumerableConvention.INSTANCE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comments if copy from calcite? e.g.

// START FROM CALCITE

// END FROM CALCITE

}

@Override
protected PreparedResult implement(RelRoot root) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leverage BindableConvention.INSTANCE and regsiter a rule for convertion?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, Bindable and Enumerable conventions cannot work together. The resultConvention of physical plan can only be one of them.

@penghuo penghuo merged commit a3cd42e into opensearch-project:main Jul 10, 2025
25 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 10, 2025
* Skipping codegen and compile for Scan only plan

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* fix IT

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* fix IT

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* Fix explain IT

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* Fix explain IT without pushdown

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* add javadoc

Signed-off-by: Lantao Jin <ltjin@amazon.com>

---------

Signed-off-by: Lantao Jin <ltjin@amazon.com>
(cherry picked from commit a3cd42e)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
qianheng-aws pushed a commit that referenced this pull request Jul 11, 2025
…3866)

* Skipping codegen and compile for Scan only plan (#3853)

* Skipping codegen and compile for Scan only plan

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* fix IT

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* fix IT

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* Fix explain IT

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* Fix explain IT without pushdown

Signed-off-by: Lantao Jin <ltjin@amazon.com>

* add javadoc

Signed-off-by: Lantao Jin <ltjin@amazon.com>

---------

Signed-off-by: Lantao Jin <ltjin@amazon.com>
(cherry picked from commit a3cd42e)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Fix compile error in JDK11

Signed-off-by: Lantao Jin <ltjin@amazon.com>

---------

Signed-off-by: Lantao Jin <ltjin@amazon.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Lantao Jin <ltjin@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 2.19-dev calcite calcite migration releated enhancement New feature or request performance Make it fast!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Skipping codegen and compile for Scan only plan

4 participants