Skip to content

SQL: Improve painless script generated from IN#35055

Merged
matriv merged 10 commits intoelastic:masterfrom
matriv:mt/polish-painless-in
Nov 1, 2018
Merged

SQL: Improve painless script generated from IN#35055
matriv merged 10 commits intoelastic:masterfrom
matriv:mt/polish-painless-in

Conversation

@matriv
Copy link
Copy Markdown
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 astefan and costin October 29, 2018 18:22
@matriv matriv added the :Analytics/SQL SQL querying label Oct 29, 2018
@elasticmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-search-aggs

@jdconrad
Copy link
Copy Markdown
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 matriv force-pushed the mt/polish-painless-in branch from 5a076d4 to a0cd723 Compare October 30, 2018 15:41
@matriv
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

matriv commented Oct 30, 2018

retest this please

Copy link
Copy Markdown
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!

}

public static Boolean in(Object value, List<Object> values) {
return In.doFold(value, values);
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.

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@matriv
Copy link
Copy Markdown
Contributor Author

matriv commented Oct 31, 2018

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

6 participants