Skip to content

ESQL: Push down filter passed lookup join #118410

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

Merged
merged 12 commits into from
Dec 13, 2024

Conversation

costin
Copy link
Member

@costin costin commented Dec 11, 2024

Improve the planner to detect filters that can be pushed down 'through'
a LOOKUP JOIN by determining the conditions scoped to the left/main
side and moving them closer to the source.

Relates #118305

@elasticsearchmachine
Copy link
Collaborator

Hi @costin, I've created a changelog YAML for you.

costin and others added 2 commits December 10, 2024 22:02
Improve the planner to detect filters that can be pushed down 'through'
 a LOOKUP JOIN by determining the conditions scoped to the left/main
 side and moving them closer to the source.

Relates elastic#118305
@costin costin force-pushed the esql/push-filter-through-join branch from 9fc0020 to 5010855 Compare December 11, 2024 06:32
Copy link
Contributor

@alex-spies alex-spies left a comment

Choose a reason for hiding this comment

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

Yep, I believe this approach is simple, succinct and correct. Needs a couple tests and it's good to go IMO.

AttributeSet rightOutput = right.outputSet();

// first remove things that left scoped only
rest.removeIf(f -> f.references().subsetOf(leftOutput) && leftFilters.add(f));
Copy link
Contributor

Choose a reason for hiding this comment

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

Funky use of && :D

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

@bpintea bpintea left a comment

Choose a reason for hiding this comment

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

LGTM SoFar.

@costin costin marked this pull request as ready for review December 13, 2024 00:51
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Dec 13, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

3 | Error
;

lookupWithFieldOnJoinKey-Ignored
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 filter on right side removes all data, likely due to considering the field as missing.
We have a bug for that so I just disabled the test for now.

10093 | 3 | Spanish
;

lookupMessageWithFilterOnRightSideField-Ignored
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 filter on right side removes all data, likely due to considering the field as missing.
We have a bug for that so I just disabled the test for now.

Comment on lines +5848 to +5850
//
// Lookup JOIN
//
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 wanted to double check the pushdown happens inside the plan and since our CSV tests don't support (yet) lookup queries in non-Lucene mode, I've added added a bunch of testing her (such as the case with rename or breaking the filter up).

@costin costin added the v8.18.0 label Dec 13, 2024
Copy link
Contributor

@alex-spies alex-spies left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@bpintea bpintea left a comment

Choose a reason for hiding this comment

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

LGTM

assertThat(join.config().type(), equalTo(JoinTypes.LEFT));
project = as(join.left(), Project.class);
var filter = as(project.child(), Filter.class);
// assert that the rename has been undone and
Copy link
Contributor

@bpintea bpintea Dec 13, 2024

Choose a reason for hiding this comment

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

what a cliffhanger.

//

/**
* Filter on join keys should be pushed donw
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Filter on join keys should be pushed donw
* Filter on join keys should be pushed down

Copy link
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

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

I have also tried another, more interesting, query which passed, if you'd like to add it to the csv-spec file. Either way LGTM.

The query is

FROM employees
| WHERE CASE(languages <= 1, 1, languages <= 2, 2, 3) >= 3
| RENAME languages AS language_code
| LOOKUP JOIN languages_lookup ON language_code
| SORT emp_no
| KEEP emp_no, language_code, language_name
| WHERE emp_no >= 10091 AND emp_no < 10094 OR language_code > 3

Comment on lines +118 to +121
// split the filter condition in 3 parts:
// 1. filter scoped to the left
// 2. filter scoped to the right
// 3. filter that requires both sides to be evaluated
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I don't think this duplicated comment here is necessary, since the method itself has the same javadoc.

@costin costin added the auto-backport Automatically create backport pull requests when merged label Dec 13, 2024
@costin costin merged commit ac0ba24 into elastic:main Dec 13, 2024
16 checks passed
@costin costin deleted the esql/push-filter-through-join branch December 13, 2024 21:17
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.x

elasticsearchmachine pushed a commit that referenced this pull request Dec 13, 2024
Improve the planner to detect filters that can be pushed down 'through'
 a LOOKUP JOIN by determining the conditions scoped to the left/main
 side and moving them closer to the source.

Relates #118305
maxhniebergall pushed a commit to maxhniebergall/elasticsearch that referenced this pull request Dec 16, 2024
…18702)

Improve the planner to detect filters that can be pushed down 'through'
 a LOOKUP JOIN by determining the conditions scoped to the left/main
 side and moving them closer to the source.

Relates elastic#118305
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/ES|QL AKA ESQL auto-backport Automatically create backport pull requests when merged >enhancement Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants