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

QL: move query AST from SQL to QL #52069

Merged
merged 2 commits into from
Feb 8, 2020
Merged

Conversation

costin
Copy link
Member

@costin costin commented Feb 7, 2020

No description provided.

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/SQL)

@costin costin requested review from matriv and bpintea February 7, 2020 18:00
Copy link
Contributor

@matriv matriv left a comment

Choose a reason for hiding this comment

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

LGTM, Left a comment.

if (e.foldable()) {
set.add(DataTypeConverter.convert(e.fold(), dt));
} else {
throw new QlIllegalArgumentException("Cannot determine value for {}", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this would be the case of something like:

SELECT * FROM test WHERE test.a = test.b
  • Isn't this caught earlier?
    • Should we add a test for this exception?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the intermediate query AST - in this case terms will be more like A in (1, 2).
The check there is a translation of the existing code - which is defensive - if one of the values cannot be folded and it hasn't been caught, bail out.
An alternative would be to simply add the folded values directly.

q = new TermsQuery(in.source(), fa.exactAttribute().name(), in.list());
FieldAttribute fa = (FieldAttribute) in.value();
List<Expression> list = in.list();
// TODO: this needs to be handled inside the optimizer
Copy link
Member Author

Choose a reason for hiding this comment

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

@matriv This case of eliminating nulls need to be moved from the query to the optimizer (maybe in a dedicated rule).
It seems to be inconsistent with the foldable method - currently In has a fairly scattered behavior when it comes to list that would be good to centralize.

Copy link
Contributor

@matriv matriv Feb 7, 2020

Choose a reason for hiding this comment

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

What was the issue? which tests were failing?

The In uses a HashSet to eliminate duplicates but if there are nulls in the list of values it keeps at list one, in order to implement the following behavior (adopted from Postgres):

postgres=# select 1 IN (2, 3, null);
 ?column?
----------

(1 row)

postgres=# select 1 IN (1, 2, 3, null);
 ?column?
----------
 t
(1 row)

postgres=# select 1 IN (2, 3);
 ?column?
----------
 f
(1 row)

So if there is no match in the list, and there a null in the list, we should return null instead of false.

Copy link
Member Author

Choose a reason for hiding this comment

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

Eliminating the null from the list of values before doing the translation. This logic should be externalized from the query translation which should only worry about that not do any clean-up.

@costin costin merged commit 5936896 into elastic:master Feb 8, 2020
@costin costin deleted the ql/query-ast branch February 8, 2020 21:10
costin added a commit that referenced this pull request Feb 8, 2020
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.

5 participants