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

SQL: Improve painless script generated from IN #35055

Merged
merged 10 commits into from Nov 1, 2018

Conversation

matriv
Copy link
Contributor

@matriv matriv commented Oct 29, 2018

Replace standard || and == painless operators with
or and eq null safe alternatives from InternalSqlScriptUtils.

Follow up to #34750

@matriv matriv requested review from costin and astefan October 29, 2018 18:22
@matriv matriv added the :Analytics/SQL SQL querying label Oct 29, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search-aggs

@jdconrad
Copy link
Contributor

jdconrad commented Oct 29, 2018

Just a note that == in Painless may already cover this use case. Internally, during a comparison, if either the lhs or rhs is a non-primitive type, Objects.equals is called.

Replace standard `||` and `==` painless operators with
`or` and `eq` null safe alternatives from `InternalSqlScriptUtils`.

Follow up to elastic#34750
@matriv
Copy link
Contributor Author

matriv commented Oct 30, 2018

@jdconrad thx for info! With @costin's guidance we follow now a different approach with implementing our in script function so that the logic is still in once place in Java code.

@matriv
Copy link
Contributor Author

matriv commented Oct 30, 2018

retest this please

Copy link
Member

@costin costin left a comment

Choose a reason for hiding this comment

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

Left some minor comments. Looks good!

@@ -113,6 +115,10 @@ public static Boolean notNull(Object expression) {
return IsNotNullProcessor.apply(expression);
}

public static Boolean in(Object value, List<Object> values) {
return In.doFold(value, values);
Copy link
Member

Choose a reason for hiding this comment

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

The naming convention is to call the method on the processor (typically InProcessor.apply)

Boolean result = false;
for (Expression rightValue : list) {
Boolean compResult = Comparisons.eq(foldedLeftValue, rightValue.fold());
for (Object v : values) {
Copy link
Member

Choose a reason for hiding this comment

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

Move this method to the processor please.

for (Expression rightValue : list) {
Boolean compResult = Comparisons.eq(foldedLeftValue, rightValue.fold());
for (Object v : values) {
Boolean compResult = Comparisons.eq(value, v);
Copy link
Member

Choose a reason for hiding this comment

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

Let's be explicit about boxing. Boolean result = Boolean.FALSE. if (compResult == Boolean.TRUE) or compResult.boolean()` ...


return new ScriptTemplate(format(Locale.ROOT, "%s", sj.toString()), paramsBuilder.build(), dataType());
// remove duplicates
List<Object> values = new ArrayList<>(
Copy link
Member

Choose a reason for hiding this comment

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

new LinkedHashSet(Foldables.valuesOf(list, dataType())
This has the side effect of triggering a conversion if needed (which should expose potential bugs).

assertEquals("InternalSqlScriptUtils.nullSafeFilter(InternalSqlScriptUtils.in(" +
"InternalSqlScriptUtils.power(InternalSqlScriptUtils.docValue(doc,params.v0),params.v1), params.v2))",
sc.script().toString());
assertEquals("[{v=int}, {v=2}, {v=[10, null, 20]}]", sc.script().params().toString());
Copy link
Member

Choose a reason for hiding this comment

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

👍

@matriv
Copy link
Contributor Author

matriv commented Oct 31, 2018

@costin Thanks a lot for the review and all your help on this PR!

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.

LGTM

@matriv matriv merged commit 4f78e2b into elastic:master Nov 1, 2018
@matriv matriv changed the title SQL: "Polish" painless script generated from IN SQL: Improve painless script generated from IN Nov 1, 2018
@matriv matriv deleted the mt/polish-painless-in branch November 1, 2018 09:24
matriv pushed a commit that referenced this pull request Nov 1, 2018
Replace standard `||` and `==` painless operators with
new `in` method introduced in `InternalSqlScriptUtils`.
This allows the list of values to become a script variable
which is replaced each time with the list of  values provided
by the user.

Move In to the same package as InPipe & InProcessor

Follow up to #34750

Co-authored-by: Costin Leau <costin.leau@gmail.com>
@matriv
Copy link
Contributor Author

matriv commented Nov 1, 2018

Backported to 6.x with dab6665

matriv pushed a commit that referenced this pull request Nov 1, 2018
Replace standard `||` and `==` painless operators with
new `in` method introduced in `InternalSqlScriptUtils`.
This allows the list of values to become a script variable
which is replaced each time with the list of  values provided
by the user.

Move In to the same package as InPipe & InProcessor

Follow up to #34750

Co-authored-by: Costin Leau <costin.leau@gmail.com>
@matriv
Copy link
Contributor Author

matriv commented Nov 1, 2018

Backported to 6.5 with c94aeef

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.

None yet

6 participants