SQL: Support for coercing to DECIMAL. (#4028)

Useful for running queries that involve math of ints and floats, which
Calcite types as decimal.
This commit is contained in:
Gian Merlino 2017-03-13 16:29:23 -07:00 committed by Jonathan Wei
parent dfe4bda7fd
commit bad250fe6d
2 changed files with 10 additions and 6 deletions

View File

@ -366,7 +366,7 @@ public class QueryMaker
return ColumnMetaData.Rep.of(Integer.class);
} else if (sqlType == SqlTypeName.BIGINT) {
return ColumnMetaData.Rep.of(Long.class);
} else if (sqlType == SqlTypeName.FLOAT || sqlType == SqlTypeName.DOUBLE) {
} else if (sqlType == SqlTypeName.FLOAT || sqlType == SqlTypeName.DOUBLE || sqlType == SqlTypeName.DECIMAL) {
return ColumnMetaData.Rep.of(Double.class);
} else if (sqlType == SqlTypeName.OTHER) {
return ColumnMetaData.Rep.of(Object.class);
@ -435,7 +435,7 @@ public class QueryMaker
} else {
throw new ISE("Cannot coerce[%s] to %s", value.getClass().getName(), sqlType);
}
} else if (sqlType == SqlTypeName.FLOAT || sqlType == SqlTypeName.DOUBLE) {
} else if (sqlType == SqlTypeName.FLOAT || sqlType == SqlTypeName.DOUBLE || sqlType == SqlTypeName.DECIMAL) {
if (value instanceof String) {
coercedValue = Doubles.tryParse((String) value);
} else if (value instanceof Number) {

View File

@ -1350,7 +1350,7 @@ public class CalciteQueryTest
public void testExpressionAggregations() throws Exception
{
testQuery(
"SELECT SUM(cnt * 3), LN(SUM(cnt) + SUM(m1)) FROM druid.foo",
"SELECT SUM(cnt * 3), LN(SUM(cnt) + SUM(m1)), SUM(cnt) / 0.25 FROM druid.foo",
ImmutableList.<Query>of(
Druids.newTimeseriesQueryBuilder()
.dataSource(CalciteTests.DATASOURCE1)
@ -1361,14 +1361,18 @@ public class CalciteQueryTest
new LongSumAggregatorFactory("a1", "cnt", null),
new DoubleSumAggregatorFactory("a2", "m1", null)
))
.postAggregators(ImmutableList.<PostAggregator>of(
new ExpressionPostAggregator("a3", "log((\"a1\" + \"a2\"))")
.postAggregators(ImmutableList.of(
new ExpressionPostAggregator("a3", "log((\"a1\" + \"a2\"))"),
new ArithmeticPostAggregator("a4", "quotient", ImmutableList.of(
new FieldAccessPostAggregator(null, "a1"),
new ConstantPostAggregator(null, 0.25)
))
))
.context(TIMESERIES_CONTEXT_DEFAULT)
.build()
),
ImmutableList.of(
new Object[]{18L, 3.295836866004329}
new Object[]{18L, 3.295836866004329, 24.0}
)
);
}