Skip to content

BETWEEN connector expression translation#11684

Merged
findepi merged 1 commit intotrinodb:masterfrom
assaf2:between-connector-expression-pushdown
Mar 31, 2022
Merged

BETWEEN connector expression translation#11684
findepi merged 1 commit intotrinodb:masterfrom
assaf2:between-connector-expression-pushdown

Conversation

@assaf2
Copy link
Member

@assaf2 assaf2 commented Mar 27, 2022

Translate BETWEEN predicate to connector expression and rewrite it in PostgreSQL connector

@cla-bot cla-bot bot added the cla-signed label Mar 27, 2022
@assaf2 assaf2 force-pushed the between-connector-expression-pushdown branch 2 times, most recently from 965d1b6 to 31a437e Compare March 27, 2022 16:06
Copy link
Member

Choose a reason for hiding this comment

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

Do we need a new function?
I would prefer to model this as $and(x >= y, x <= z).

BETWEEN has important property that it evaluates the LHS value once, but that doesn't matter for deterministic predicates, and we don't want to handle non-deterministic predicates in connector expressions.

(That's also why we could model boolean AND as an ordinary function. Lazy-evaluation (or lack thereof) isn't defined for connector expressions.)

Copy link
Member Author

Choose a reason for hiding this comment

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

After writing this PR, I've noticed that BETWEEN is already modeled the way you said at DomainTranslator, so the implementation will only be reflected in the projection pushdown. Do you think I should implement the conversion to an $and right now or just wait with this PR?

Copy link
Member

Choose a reason for hiding this comment

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

What for would you want to wait for here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought I might need to wait for a connector that will actually use this, but seems like we're ok with just adding this (with tests) so I'll continue

Copy link
Member

Choose a reason for hiding this comment

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

PostgreSQL would use this already, so you can try add tests there.
see io.trino.plugin.postgresql.TestPostgreSqlConnectorTest#test*PredicatePushdown

Copy link
Member Author

Choose a reason for hiding this comment

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

GREATER_THAN_OR_EQUAL and LESS_THAN_OR_EQUAL are currently not supported in PostgreSQL:

// TODO allow all comparison operators for numeric types
.add(new RewriteComparison(RewriteComparison.ComparisonOperator.EQUAL, RewriteComparison.ComparisonOperator.NOT_EQUAL))

Copy link
Member

Choose a reason for hiding this comment

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

i guess we should just add them there?

Copy link
Member Author

Choose a reason for hiding this comment

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

Since:

  1. connectorExpressionRewriter is created at the constructor.
  2. RewriteComparison is part of io.trino.plugin.jdbc.expression and not of io.trino.plugin.postgresql.
  3. My understanding of this:
    // PostgreSQL is case sensitive by default, but orders varchars differently
    JoinCondition.Operator operator = joinCondition.getOperator();
    switch (operator) {
    case LESS_THAN:
    case LESS_THAN_OR_EQUAL:
    case GREATER_THAN:
    case GREATER_THAN_OR_EQUAL:
    return isEnableStringPushdownWithCollate(session);

I think that my best option is to add a subclass to RewriteComparison at io.trino.plugin.postgresql and return Optional.empty() on rewrite if the operator is one of these and io.trino.plugin.postgresql#isEnableStringPushdownWithCollate(context.getSession()) == false. Maybe I can also check if the operands are of String type (how do you think I should do that?).
Another option is to create two connectorExpressionRewriters (with these operators and without) and dynamically choose between the two according to the result of isEnableStringPushdownWithCollate.
WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

cc @hashhar on the above

in any case, let's split the engine part (handling of BETWEEN)
and PostgreSQL part (handling of inequalities) into separate PRs.

thanks

@assaf2 assaf2 force-pushed the between-connector-expression-pushdown branch from 31a437e to d39d4b3 Compare March 28, 2022 14:19
@assaf2 assaf2 requested review from findepi and wendigo March 28, 2022 14:22
@assaf2 assaf2 changed the title Between connector expression pushdown Between connector expression translation Mar 28, 2022
@assaf2 assaf2 marked this pull request as ready for review March 28, 2022 14:24
@assaf2 assaf2 changed the title Between connector expression translation BETWEEN connector expression translation Mar 28, 2022
@assaf2 assaf2 requested a review from ebyhr March 28, 2022 16:21
@assaf2 assaf2 force-pushed the between-connector-expression-pushdown branch 5 times, most recently from ca8a655 to d39d4b3 Compare March 30, 2022 10:41
@findepi
Copy link
Member

findepi commented Mar 31, 2022

CI #11016

@findepi findepi merged commit 788b522 into trinodb:master Mar 31, 2022
@findepi findepi added the no-release-notes This pull request does not require release notes entry label Mar 31, 2022
@github-actions github-actions bot added this to the 376 milestone Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed no-release-notes This pull request does not require release notes entry

Development

Successfully merging this pull request may close these issues.

2 participants