-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Conversation
There was a problem hiding this 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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!
Pinging @elastic/es-search-aggs |
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 Literal
s 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.
There was a problem hiding this comment.
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.
Please take another look as a new commit is added that adds more tests and fixes a newly discovered issue. |
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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
Backported to |
* 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)
* 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) ...
Previously, When an arithmetic function is applied on a
table column in the
SELECT
clause the name of the resultcolumn 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