SQL: Fix result column names for arithmetic functions#33500
SQL: Fix result column names for arithmetic functions#33500matriv merged 8 commits intoelastic:masterfrom
Conversation
costin
left a comment
There was a problem hiding this comment.
Left a comment, otherwise LGTM
There was a problem hiding this comment.
No need for the appendFunctionArg - use Expressions.name instead which is more generic.
There was a problem hiding this comment.
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.
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 |
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
|
|
||
| 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.
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.
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.
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. |
nik9000
left a comment
There was a problem hiding this comment.
Still better than it was so I'm good with it.
| if (attr instanceof AggregateFunctionAttribute) { | ||
| return asScriptFrom((AggregateFunctionAttribute) attr); | ||
| } | ||
| if (attr instanceof LiteralAttribute) { |
There was a problem hiding this comment.
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.
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
SELECTclause 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