Skip to content

Rewrite exotic LAST_VALUE/FIRST_VALUE to self-reference.#16063

Merged
soumyava merged 7 commits intoapache:masterfrom
kgyrtkirk:window_last_value_orderby
Mar 25, 2024
Merged

Rewrite exotic LAST_VALUE/FIRST_VALUE to self-reference.#16063
soumyava merged 7 commits intoapache:masterfrom
kgyrtkirk:window_last_value_orderby

Conversation

@kgyrtkirk
Copy link
Member

@kgyrtkirk kgyrtkirk commented Mar 6, 2024

  • rewrite LAST_VALUE(x) OVER (ORDER BY y) to LAG(x,0) OVER (ORDER BY y)
    • the current LAST_VALUE logic has no idea about the frame itself; and doesn't care about the order by
  • not directly to x because some queries get unplannable that way
  • restrict NTILE from framing - as its not supported
  • add test to ensure that all of the KNOWN_WINDOW_FNS's framing is accounted for

Fixes #XXXX.

Description

Fixed the bug ...

Renamed the class ...

Added a forbidden-apis entry ...

Release note


Key changed/added classes in this PR
  • MyFoo
  • OurBar
  • TheirBaz

This PR has:

  • been self-reviewed.
  • added documentation for new or modified features or behaviors.
  • a release note entry in the PR description.
  • added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
  • added or updated version, license, or notice information in licenses.yaml
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
  • added integration tests.
  • been tested in a test Druid cluster.

* rewrite `LAST_VALUE(x) OVER (ORDER BY y)` to `LAG(x,0) OVER (ORDER BY y)`
* not directly to `x` because some queries get unplannable that way
* restrict `NTILE` from framing - as its not supported
* add test to ensure that all of the `KNOWN_WINDOW_FNS`'s framing is accounted for
{
public RewriteFirstValueLastValueRule()
{
super(operand(RelNode.class, any()));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation

Invoking [RelOptRule.operand](1) should be avoided because it has been deprecated.
{
public RewriteFirstValueLastValueRule()
{
super(operand(RelNode.class, any()));

Check notice

Code scanning / CodeQL

Deprecated method or constructor invocation

Invoking [RelOptRule.any](1) should be avoided because it has been deprecated.
@kgyrtkirk kgyrtkirk marked this pull request as ready for review March 7, 2024 08:40
Copy link
Contributor

@soumyava soumyava left a comment

Choose a reason for hiding this comment

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

Made one pass

{
// could remove `last_value( x ) over ( .... order by y )`
// best would be to: return over.getOperands().get(0);
// however that make some queries too good
Copy link
Contributor

@soumyava soumyava Mar 11, 2024

Choose a reason for hiding this comment

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

What does this comment mean by making queries too good ?

Copy link
Member Author

Choose a reason for hiding this comment

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

the best would be to just:

return over.getOperands().get(0);

however that will lead to plans which become unplannable as the query changes to be a window query directly on a dataset - and right now that's not allowed.

to not alter that for now: I've choosen to retain the Window function in this case but rewrite it to something which just references the current_row's value via LAG(x,0) instead.

static {
NullHandling.initializeForTests();
}
) || developerIDEdetected();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this change needed ? Is there a special handling only for eclipse ?

Copy link
Member Author

Choose a reason for hiding this comment

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

not closely related to this patch - I just had to enable this option every time I was updating results ....rather than doing that I would prefer to have it enabled for all IDE runs without having to realize that it needs to set; set it and re-run the test.

@kgyrtkirk kgyrtkirk closed this Mar 17, 2024
@kgyrtkirk kgyrtkirk reopened this Mar 17, 2024
@kgyrtkirk kgyrtkirk requested a review from soumyava March 21, 2024 07:55
Copy link
Contributor

@soumyava soumyava left a comment

Choose a reason for hiding this comment

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

LGTM ! Thanks for putting in the explanation as the javadocs. If not done recently, please merge with master once and then we can merge this in

@soumyava soumyava merged commit a16092b into apache:master Mar 25, 2024
@adarshsanjeev adarshsanjeev added this to the 30.0.0 milestone May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants