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: Fix result column names for arithmetic functions #33500

Merged
merged 8 commits into from
Sep 10, 2018

Conversation

matriv
Copy link
Contributor

@matriv matriv commented Sep 7, 2018

Previously, When an arithmetic function is applied on a
table column in the SELECT clause the name of the result
column name contained weird characters used internally when
processing the SQL statement. E.g.:

SELECT CHAR(emp_no % 10000) FROM "test_emp"

returned:

CHAR((emp_no{f}#14) % 10000))

as the column name instead of:

CHAR((emp_no) % 10000))

Fixes: #31869

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 a comment, otherwise LGTM

@@ -65,7 +66,7 @@ protected ProcessorDefinition makeProcessorDefinition() {
public String name() {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append(left());
appendFunctionArg(left(), sb);
Copy link
Member

Choose a reason for hiding this comment

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

No need for the appendFunctionArg - use Expressions.name instead which is more generic.

Copy link
Contributor Author

@matriv matriv Sep 7, 2018

Choose a reason for hiding this comment

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

The instanceof check must be kept, because, currently, Literal->LeafExpression->Expression so doesn’t inherit from NamedExpression and the Expressions.name(<literal>) results to the String Literal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the code in Expressions.name() to include the case of Literal and replaced the appendFunctionArg() call with Expressions.name().

Thanks!

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search-aggs

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

Previously, When an arithmetic function is applied on a
table column in the `SELECT` clause the name of the result
column name contained weird characters used internally when
processing the SQL statement. E.g.:

  SELECT CHAR(emp_no % 10000) FROM "test_emp"

returned:

  CHAR((emp_no{f}elastic#14) % 10000))

as the column name instead of:

  CHAR((emp_no) % 10000))

Fixes: elastic#31869
@@ -82,7 +82,13 @@ public static AttributeSet references(List<? extends Expression> exps) {
}

public static String name(Expression e) {
return e instanceof NamedExpression ? ((NamedExpression) e).name() : e.nodeName();
if (e instanceof NamedExpression) {
Copy link
Member

Choose a reason for hiding this comment

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

I feel like we'd be better off being more OO here and adding a colunmName function to Expression or something like that. The fix is fine with me as it stands but I think something like that would make a good follow up change.

Copy link
Member

Choose a reason for hiding this comment

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

This is a good example of code exposing conceptual leaks. You are right that this needs OO-ing. There's already a class for that NamedExpression is just that Literals are not yet one. Which is the culprit - I'll raise an issue to chase that down and make sure it sits well in the overall structure.

Copy link
Member

Choose a reason for hiding this comment

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

Raised #33523

Add more integ tests for complex functions calls in `SELECT` clause,
to test both the correct column names returned but also the evaluation
itself.

Fix an issue that causes a ClassCastException to be thrown when using
functions where both arguments are literals.
@matriv
Copy link
Contributor Author

matriv commented Sep 7, 2018

Please take another look as a new commit is added that adds more tests and fixes a newly discovered issue.

Copy link
Member

@nik9000 nik9000 left a comment

Choose a reason for hiding this comment

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

Still better than it was so I'm good with it.

@@ -68,6 +69,9 @@ protected ScriptTemplate asScript(Expression exp) {
if (attr instanceof AggregateFunctionAttribute) {
return asScriptFrom((AggregateFunctionAttribute) attr);
}
if (attr instanceof LiteralAttribute) {
Copy link
Member

Choose a reason for hiding this comment

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

Good catch.
this is likely the cause of #33461.
I'm curious though why the resulting literal is not caught by the if (exp.foldable()) above, before getting the attribute from the expression?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because at that point the literal expression is an Alias which is not foldable and then becomes a LiteralAttribute from the Expressions.attribute(exp) call.

@matriv matriv merged commit f441bb8 into elastic:master Sep 10, 2018
matriv pushed a commit that referenced this pull request Sep 10, 2018
Previously, when an arithmetic function got applied on a
table column in the `SELECT` clause, the name of the result
column contained weird characters used internally when
processing the SQL statement e.g.:

  SELECT CHAR(emp_no % 10000) FROM "test_emp"

returned:

  CHAR((emp_no{f}#14) % 10000))

as the column name instead of:

  CHAR((emp_no) % 10000))

Also, fix an issue that causes a ClassCastException to be thrown
when using functions where both arguments are literals.

Closes #31869
Closes #33461
@matriv
Copy link
Contributor Author

matriv commented Sep 10, 2018

Backported to 6.x. -> d28c23a

jasontedor added a commit to jasontedor/elasticsearch that referenced this pull request Sep 11, 2018
* master:
  Add full cluster restart base class (elastic#33577)
  Validate list values for settings (elastic#33503)
  Copy and validatie soft-deletes setting on resize (elastic#33517)
  Test: Fix package name
  SQL: Fix result column names for arithmetic functions (elastic#33500)
  Upgrade to latest Lucene snapshot (elastic#33505)
  Enable not wiping cluster settings after REST test (elastic#33575)
  MINOR: Remove Dead Code in SearchScript (elastic#33569)
  [Test] Remove duplicate method in TestShardRouting (elastic#32815)
  mute test on windows
  Update beats template to include apm-server metrics (elastic#33286)
  Fix typos (elastic#33499)
  [CCR] Delay auto follow license check (elastic#33557)
  [CCR] Add create_follow_index privilege (elastic#33559)
  Strengthen FilterRoutingTests (elastic#33149)
  Correctly handle PKCS#11 tokens for system keystore (elastic#33460)
  Remove some duplicate request conversion methods. (elastic#33538)
jasontedor added a commit to martijnvg/elasticsearch that referenced this pull request Sep 11, 2018
* master: (91 commits)
  Preserve cluster settings on full restart tests (elastic#33590)
  Use IndexWriter.getFlushingBytes() rather than tracking it ourselves (elastic#33582)
  Fix upgrading of list settings (elastic#33589)
  Add read-only Engine (elastic#33563)
  HLRC: Add ML get categories API (elastic#33465)
  SQL: Adds MONTHNAME, DAYNAME and QUARTER functions (elastic#33411)
  Add predicate_token_filter (elastic#33431)
  Fix Replace function. Adds more tests to all string functions. (elastic#33478)
  [ML] Rename input_fields to column_names in file structure (elastic#33568)
  Add full cluster restart base class (elastic#33577)
  Validate list values for settings (elastic#33503)
  Copy and validatie soft-deletes setting on resize (elastic#33517)
  Test: Fix package name
  SQL: Fix result column names for arithmetic functions (elastic#33500)
  Upgrade to latest Lucene snapshot (elastic#33505)
  Enable not wiping cluster settings after REST test (elastic#33575)
  MINOR: Remove Dead Code in SearchScript (elastic#33569)
  [Test] Remove duplicate method in TestShardRouting (elastic#32815)
  mute test on windows
  Update beats template to include apm-server metrics (elastic#33286)
  ...
@matriv matriv deleted the mt/fix-31869 branch September 25, 2018 13:21
@jimczi jimczi removed the v7.0.0 label Feb 7, 2019
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