Skip to content
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

WIP: Add support for external engines #84

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

WIP: Add support for external engines #84

wants to merge 3 commits into from

Conversation

ryannedolan
Copy link
Collaborator

@ryannedolan ryannedolan commented Jan 15, 2025

Summary

This adds support for installing remote query engines, e.g. Trino, DuckDB, or Flink SQL Gateway.

  • Added Engine CRD.
  • Added k8s.engines metadata table.
  • Added RemoteTableScan, RemoteJoin, associated optimizer rules.

Details

The Hoptimator JDBC Driver is able to talk to remote Databases, but it previously relied on Calcite's Enumerable engine to process queries locally. For example, joining tables in two different Databases would involve first fetching the rows from each table and then joining locally in the driver itself.

With Engines, we can outsource these operations to fast, distributed query engines like Trino. Queries are sent off to the remote engine, and the Driver simply collects the results.

Testing

Without an Engine installed, a query must be processed locally via the Enumerable convention:

0: Hoptimator> explain plan for select * from ads.ad_clicks, profile.members;
PLAN  EnumerableNestedLoopJoin(condition=[true], joinType=[inner])
  JdbcToEnumerableConverter
    JdbcTableScan(table=[[ADS, AD_CLICKS]])
  JdbcToEnumerableConverter
    JdbcTableScan(table=[[PROFILE, MEMBERS]])


1 row selected (0.062 seconds)

The EnumerableNestedLoopJoin would be very slow for large datasets.

After installing an engine, we see that the query plan now involves a RemoteJoin instead:

0: Hoptimator> explain plan for select * from ads.ad_clicks, profile.members;
Registering rules for flink-engine-ads-database
Registering rules for flink-engine-profile-database
PLAN  JdbcToEnumerableConverter
  RemoteJoin(condition=[true], joinType=[inner])
    JdbcTableScan(table=[[ADS, AD_CLICKS]])
    JdbcTableScan(table=[[PROFILE, MEMBERS]])


1 row selected (0.041 seconds)

The RemoteJoin is able to leverage Trino or similar distributed query engines.

Copy link
Collaborator

@jogrogan jogrogan left a comment

Choose a reason for hiding this comment

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

Is the planner able to distinguish when it should and shouldn't use these remote rules?


@Override
public SqlDialect dialect() {
return SqlDialect.FLINK; // TODO fix hardcoded dialect
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't this just return this.dialect?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yup whoops

Comment on lines +27 to +30
public String NAME;
public String URL;
public String DIALECT;
public String DRIVER;
public String[] DATABASES;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: should prob be lowercase so we don't need to disable checkstyle

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a quirk of Calcite. These fields are referenced in the generated code, and things go sideways if they aren't all caps.

String name = engine.engineName() + "-" + inTrait.database();
JdbcConvention outTrait = JdbcConvention.of(dialect, inTrait.expression, name);

System.out.println("Registering rules for " + name + " using dialect " + dialect.toString());
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: use logger or remove (applicable to other lines in this file as well)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will do

@ryannedolan
Copy link
Collaborator Author

Is the planner able to distinguish when it should and shouldn't use these remote rules?

Yes, via four mechanisms:

  1. Whether an engine is installed or not. If there are no engines in the current namespace, the planner will just run queries locally as before.
  2. The database(s) involved. Engines can target specific databases or all databases. If a query involves a database that isn't supported by any engine, that part of the query will fall back to local execution.
  3. The rules only match certain types of query/sub-query. Some queries won't match and fall back.
  4. Cost models. Right now I've hardcoded zero cost for remote queries, but we should be able to be smarter here.

Eventually we may want to add more details to the Engine CRD, e.g. to specify the engine's capabilities. That metadata could theoretically inform the planner better.

Internally, we can install different engines that target different databases, e.g. Trino can target offline while Flink targets nearline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants