Skip to content

Push down COALESCE in PostgreSQL connector#11535

Closed
ebyhr wants to merge 6 commits intotrinodb:masterfrom
ebyhr:ebi/connector-expression-coalesce
Closed

Push down COALESCE in PostgreSQL connector#11535
ebyhr wants to merge 6 commits intotrinodb:masterfrom
ebyhr:ebi/connector-expression-coalesce

Conversation

@ebyhr
Copy link
Copy Markdown
Member

@ebyhr ebyhr commented Mar 17, 2022

Description

Push down COALESCE in PostgreSQL connector

Documentation

(x) No documentation is needed.
( ) Sufficient documentation is included in this PR.
( ) Documentation PR is available with #prnumber.
( ) Documentation issue #issuenumber is filed, and can be handled later.

Release notes

( ) No release notes entries required.
(x) Release notes entries required with the following suggested text:

# PostgreSQL
* Improve performance of queries involving `COALESCE` by pushing expression
  computation to the underlying database. ({issue}`issuenumber`)

@findepi
Copy link
Copy Markdown
Member

findepi commented Mar 21, 2022

@ebyhr can you please rebase?

@ebyhr ebyhr force-pushed the ebi/connector-expression-coalesce branch from 4dff92a to 5bb372b Compare March 21, 2022 14:08
@ebyhr
Copy link
Copy Markdown
Member Author

ebyhr commented Mar 21, 2022

@findepi Rebased on upstream.

Copy link
Copy Markdown
Member

@findepi findepi left a comment

Choose a reason for hiding this comment

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

"Translate COALESCE to connector expressions"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@wendigo 's PR adds some shorthand function for this, am i right, @wendigo ?

no change requested now, but we should unify later

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes,I will note it down to unify that after those PRs are merged.

@ebyhr ebyhr force-pushed the ebi/connector-expression-coalesce branch from 5bb372b to 22099ed Compare March 21, 2022 23:17
@ebyhr ebyhr force-pushed the ebi/connector-expression-coalesce branch from 22099ed to c77604c Compare March 22, 2022 13:40
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

TryExpression is desugared to try function and it's marked as non-deterministic.
We don't push non-deterministic predicates down into the connector, however, they are handled separately from non-translatable ones.

Thus, it's a wrong change, as it changes the semantics of the test case.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Changed to SearchedCaseExpression though I have a local patch to support it as connector expressions. Do you have other suggested expressions?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See #11511 (comment) and #11544 (comment)

I don't have a plan for testing things. @martint do you have one?
cc @wendigo @hashhar

@ebyhr ebyhr force-pushed the ebi/connector-expression-coalesce branch from c77604c to 6810da6 Compare March 23, 2022 01:25
@assaf2 assaf2 self-requested a review March 27, 2022 10:24
@ebyhr ebyhr force-pushed the ebi/connector-expression-coalesce branch from 6810da6 to 1cd9346 Compare March 28, 2022 00:08
@ebyhr ebyhr force-pushed the ebi/connector-expression-coalesce branch from 1cd9346 to d4722a4 Compare April 1, 2022 08:05
@ebyhr ebyhr requested a review from findepi April 5, 2022 09:13
@findepi
Copy link
Copy Markdown
Member

findepi commented Apr 5, 2022

@martint by any chance, does coalesce have lazy eval semantics?

cc @kasiafi

@martint
Copy link
Copy Markdown
Member

martint commented Apr 5, 2022

@martint by any chance, does coalesce have lazy eval semantics?

Yes, coalesce is described by the SQL spec as syntactic sugar for a CASE expression.

<case expression> ::=
    <case abbreviation>
  | <case specification>

<case abbreviation> ::=
    NULLIF <left paren> <value expression> <comma> <value expression> <right paren>
  | COALESCE <left paren> <value expression>
      { <comma> <value expression> }... <right paren>
d) COALESCE (V1, V2) is equivalent to the following <case specification>:

  CASE 
    WHEN V1 IS NOT NULL THEN 
      V1 
    ELSE
      V2 
  END

e) COALESCE (V1, V2, ..., Vn), for n ≥ 3, is equivalent to the following <case specification>:

  CASE 
    WHEN V1 IS NOT NULL THEN 
      V1 
    ELSE 
      COALESCE (V2, ..., Vn) 
  END

@findepi
Copy link
Copy Markdown
Member

findepi commented Apr 6, 2022

If i remember correctly, the CASE's conditions are semantically evaluated eagerly, but the values are conditional.
Since the COALESCE is defined as equivalent to recursive CASE (not flattened), it means the components are semantically evaluated lazily,

E.g. this should not fail

coalesce(CASE WHEN x = 0 THEN 1 ELSE NULL END, 10 / x)

similar to #11699 (comment), this means this should not be modeled as a function call.

OTOH, modeling COALESCE as a recursive CASE in ConnectorExpressions will be harder to consume within connectors.

@martint what about we model COALESCE as a new Coalesce class?

@ebyhr ebyhr force-pushed the ebi/connector-expression-coalesce branch from d4722a4 to 0d80cff Compare April 14, 2022 09:33
@ebyhr
Copy link
Copy Markdown
Member Author

ebyhr commented Apr 14, 2022

(Rebased on upstream to resolve conflicts)

@findepi
Copy link
Copy Markdown
Member

findepi commented Apr 14, 2022

OTOH, modeling COALESCE as a recursive CASE in ConnectorExpressions will be harder to consume within connectors.

@martint what about we model COALESCE as a new Coalesce class?

@martint and I talked about this and because coalesce features lazy eval, the current consensus is that should be modeled as a function that takes lazy value suppliers, i.e. lambdas. The alternative of implementing this as a dedicated form of a ConnectorExpression would work, but we would deviate from "have connector expressions as simple as possible" principle and was currently rejected.

However, lambdas support is not a trivial thing, and we don't think we're currently ready to work on this yet.

@ebyhr
Copy link
Copy Markdown
Member Author

ebyhr commented Apr 15, 2022

Thanks for your explanation. Let me close this PR.

@ebyhr ebyhr closed this Apr 15, 2022
@ebyhr ebyhr deleted the ebi/connector-expression-coalesce branch April 15, 2022 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants