Skip to content

Commit 7791c89

Browse files
Marios Trivyzasmatriv
Marios Trivyzas
authored andcommitted
SQL: Fix result column names for CAST (#33604)
Previously, when an non-pruned cast (casting as a different data type) got applied on a table column in the `SELECT` clause, the name of the result column didn't contain the target data type of the cast, e.g.: SELECT CAST(MAX(salary) AS DOUBLE) FROM "test_emp" returned as column name: CAST(MAX(salary)) instead of: CAST(MAX(salary) AS DOUBLE) Closes #33571 * Added more tests for trivial casts that are pruned
1 parent ccc0a35 commit 7791c89

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/Cast.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,11 @@ public boolean equals(Object obj) {
111111
public String toString() {
112112
return functionName() + "(" + field().toString() + " AS " + to().sqlName() + ")#" + id();
113113
}
114-
}
114+
115+
@Override
116+
public String name() {
117+
StringBuilder sb = new StringBuilder(super.name());
118+
sb.insert(sb.length() - 1, " AS " + to().sqlName());
119+
return sb.toString();
120+
}
121+
}

x-pack/qa/sql/src/main/resources/agg.csv-spec

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ SELECT SUM(salary) FROM test_emp;
7474
4824855
7575
;
7676

77+
aggregateWithCastPruned
78+
SELECT CAST(SUM(salary) AS INTEGER) FROM test_emp;
79+
80+
SUM(salary)
81+
-------------
82+
4824855
83+
;
84+
85+
aggregateWithUpCast
86+
SELECT CAST(SUM(salary) AS DOUBLE) FROM test_emp;
87+
88+
CAST(SUM(salary) AS DOUBLE)
89+
-----------------------------
90+
4824855.0
91+
;
92+
93+
aggregateWithCastNumericToString
94+
SELECT CAST(AVG(salary) AS VARCHAR) FROM test_emp;
95+
96+
CAST(AVG(salary) AS VARCHAR):s
97+
--------------------------------
98+
48248.55
99+
;
100+
77101
kurtosisAndSkewnessNoGroup
78102
SELECT KURTOSIS(emp_no) k, SKEWNESS(salary) s FROM test_emp;
79103

x-pack/qa/sql/src/main/resources/agg.sql-spec

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ aggCountImplicit
9090
SELECT COUNT(*) AS count FROM test_emp;
9191
aggCountImplicitWithCast
9292
SELECT CAST(COUNT(*) AS INT) c FROM "test_emp";
93+
aggCountImplicitWithUpCast
94+
SELECT CAST(COUNT(*) AS DOUBLE) c FROM "test_emp";
95+
aggCountImplicitWithPrunedCast
96+
SELECT CAST(COUNT(*) AS BIGINT) c FROM "test_emp";
9397
aggCountImplicitWithConstant
9498
SELECT COUNT(1) FROM "test_emp";
9599
aggCountImplicitWithConstantAndFilter
@@ -184,6 +188,10 @@ SELECT MIN(emp_no) AS min FROM test_emp;
184188
// end::min
185189
aggMinImplicitWithCast
186190
SELECT CAST(MIN(emp_no) AS SMALLINT) m FROM "test_emp";
191+
aggMinImplicitWithUpCast
192+
SELECT CAST(MIN(emp_no) AS DOUBLE) m FROM "test_emp";
193+
aggMinImplicitWithPrunedCast
194+
SELECT CAST(MIN(emp_no) AS INTEGER) m FROM "test_emp";
187195
aggMin
188196
SELECT gender g, MIN(emp_no) m FROM "test_emp" GROUP BY gender ORDER BY gender;
189197
aggMinWithCast
@@ -236,6 +244,10 @@ aggMaxImplicit
236244
SELECT MAX(salary) AS max FROM test_emp;
237245
aggMaxImplicitWithCast
238246
SELECT CAST(MAX(emp_no) AS SMALLINT) c FROM "test_emp";
247+
aggMaxImplicitWithUpCast
248+
SELECT CAST(MAX(emp_no) AS DOUBLE) c FROM "test_emp";
249+
aggMaxImplicitWithPrunedCast
250+
SELECT CAST(MAX(emp_no) AS INTEGER) c FROM "test_emp";
239251
aggMax
240252
SELECT gender g, MAX(emp_no) m FROM "test_emp" GROUP BY gender ORDER BY gender;
241253
aggMaxWithCast
@@ -268,6 +280,10 @@ SELECT gender g, MAX(emp_no) m FROM "test_emp" GROUP BY g HAVING m > 10 AND MAX(
268280
// SUM
269281
aggSumImplicitWithCast
270282
SELECT CAST(SUM(emp_no) AS BIGINT) s FROM "test_emp";
283+
aggSumImplicitWithUpCast
284+
SELECT CAST(SUM(emp_no) AS DOUBLE) s FROM "test_emp";
285+
aggSumImplicitWithUpCast
286+
SELECT CAST(SUM(emp_no) AS INTEGER) s FROM "test_emp";
271287
aggSumWithCast
272288
SELECT gender g, CAST(SUM(emp_no) AS BIGINT) s FROM "test_emp" GROUP BY gender ORDER BY gender;
273289
aggSumWithCastAndCount
@@ -298,6 +314,8 @@ SELECT gender g, CAST(SUM(emp_no) AS INT) s FROM "test_emp" GROUP BY g HAVING s
298314
// AVG
299315
aggAvgImplicitWithCast
300316
SELECT CAST(AVG(emp_no) AS FLOAT) a FROM "test_emp";
317+
aggAvgImplicitWithUpCast
318+
SELECT CAST(AVG(emp_no) AS DOUBLE) a FROM "test_emp";
301319
aggAvgWithCastToFloat
302320
SELECT gender g, CAST(AVG(emp_no) AS FLOAT) a FROM "test_emp" GROUP BY gender ORDER BY gender;
303321
// casting to an exact type - varchar, bigint, etc... will likely fail due to rounding error

0 commit comments

Comments
 (0)