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
This commit is contained in:
Marios Trivyzas 2018-09-13 10:28:05 +02:00 committed by GitHub
parent 6ca36bba15
commit 7d3b99a9b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 1 deletions

View File

@ -111,4 +111,11 @@ public class Cast extends UnaryScalarFunction {
public String toString() {
return functionName() + "(" + field().toString() + " AS " + to().sqlName() + ")#" + id();
}
}
@Override
public String name() {
StringBuilder sb = new StringBuilder(super.name());
sb.insert(sb.length() - 1, " AS " + to().sqlName());
return sb.toString();
}
}

View File

@ -74,6 +74,30 @@ SELECT SUM(salary) FROM test_emp;
4824855
;
aggregateWithCastPruned
SELECT CAST(SUM(salary) AS INTEGER) FROM test_emp;
SUM(salary)
-------------
4824855
;
aggregateWithUpCast
SELECT CAST(SUM(salary) AS DOUBLE) FROM test_emp;
CAST(SUM(salary) AS DOUBLE)
-----------------------------
4824855.0
;
aggregateWithCastNumericToString
SELECT CAST(AVG(salary) AS VARCHAR) FROM test_emp;
CAST(AVG(salary) AS VARCHAR):s
--------------------------------
48248.55
;
kurtosisAndSkewnessNoGroup
SELECT KURTOSIS(emp_no) k, SKEWNESS(salary) s FROM test_emp;

View File

@ -90,6 +90,10 @@ aggCountImplicit
SELECT COUNT(*) AS count FROM test_emp;
aggCountImplicitWithCast
SELECT CAST(COUNT(*) AS INT) c FROM "test_emp";
aggCountImplicitWithUpCast
SELECT CAST(COUNT(*) AS DOUBLE) c FROM "test_emp";
aggCountImplicitWithPrunedCast
SELECT CAST(COUNT(*) AS BIGINT) c FROM "test_emp";
aggCountImplicitWithConstant
SELECT COUNT(1) FROM "test_emp";
aggCountImplicitWithConstantAndFilter
@ -184,6 +188,10 @@ SELECT MIN(emp_no) AS min FROM test_emp;
// end::min
aggMinImplicitWithCast
SELECT CAST(MIN(emp_no) AS SMALLINT) m FROM "test_emp";
aggMinImplicitWithUpCast
SELECT CAST(MIN(emp_no) AS DOUBLE) m FROM "test_emp";
aggMinImplicitWithPrunedCast
SELECT CAST(MIN(emp_no) AS INTEGER) m FROM "test_emp";
aggMin
SELECT gender g, MIN(emp_no) m FROM "test_emp" GROUP BY gender ORDER BY gender;
aggMinWithCast
@ -236,6 +244,10 @@ aggMaxImplicit
SELECT MAX(salary) AS max FROM test_emp;
aggMaxImplicitWithCast
SELECT CAST(MAX(emp_no) AS SMALLINT) c FROM "test_emp";
aggMaxImplicitWithUpCast
SELECT CAST(MAX(emp_no) AS DOUBLE) c FROM "test_emp";
aggMaxImplicitWithPrunedCast
SELECT CAST(MAX(emp_no) AS INTEGER) c FROM "test_emp";
aggMax
SELECT gender g, MAX(emp_no) m FROM "test_emp" GROUP BY gender ORDER BY gender;
aggMaxWithCast
@ -268,6 +280,10 @@ SELECT gender g, MAX(emp_no) m FROM "test_emp" GROUP BY g HAVING m > 10 AND MAX(
// SUM
aggSumImplicitWithCast
SELECT CAST(SUM(emp_no) AS BIGINT) s FROM "test_emp";
aggSumImplicitWithUpCast
SELECT CAST(SUM(emp_no) AS DOUBLE) s FROM "test_emp";
aggSumImplicitWithUpCast
SELECT CAST(SUM(emp_no) AS INTEGER) s FROM "test_emp";
aggSumWithCast
SELECT gender g, CAST(SUM(emp_no) AS BIGINT) s FROM "test_emp" GROUP BY gender ORDER BY gender;
aggSumWithCastAndCount
@ -298,6 +314,8 @@ SELECT gender g, CAST(SUM(emp_no) AS INT) s FROM "test_emp" GROUP BY g HAVING s
// AVG
aggAvgImplicitWithCast
SELECT CAST(AVG(emp_no) AS FLOAT) a FROM "test_emp";
aggAvgImplicitWithUpCast
SELECT CAST(AVG(emp_no) AS DOUBLE) a FROM "test_emp";
aggAvgWithCastToFloat
SELECT gender g, CAST(AVG(emp_no) AS FLOAT) a FROM "test_emp" GROUP BY gender ORDER BY gender;
// casting to an exact type - varchar, bigint, etc... will likely fail due to rounding error