use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.
the class RexBuilderTest method testDateLiteral.
/**
* Tests {@link RexBuilder#makeDateLiteral(DateString)}.
*/
@Test
public void testDateLiteral() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
RelDataType dateType = typeFactory.createSqlType(SqlTypeName.DATE);
final RexBuilder builder = new RexBuilder(typeFactory);
// Old way: provide a Calendar
final Calendar calendar = Util.calendar();
// one small step
calendar.set(1969, Calendar.JULY, 21);
calendar.set(Calendar.MILLISECOND, 0);
checkDate(builder.makeLiteral(calendar, dateType, false));
// Old way #2: Provide in Integer
checkDate(builder.makeLiteral(MOON_DAY, dateType, false));
// The new way
final DateString d = new DateString(1969, 7, 21);
checkDate(builder.makeLiteral(d, dateType, false));
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.
the class RexBuilderTest method testTimestampWithLocalTimeZoneLiteral.
/**
* Tests
* {@link RexBuilder#makeTimestampWithLocalTimeZoneLiteral(TimestampString, int)}.
*/
@Test
public void testTimestampWithLocalTimeZoneLiteral() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
final RelDataType timestampType = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE);
final RelDataType timestampType3 = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE, 3);
final RelDataType timestampType9 = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE, 9);
final RelDataType timestampType18 = typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE, 18);
final RexBuilder builder = new RexBuilder(typeFactory);
// The new way
final TimestampWithTimeZoneString ts = new TimestampWithTimeZoneString(1969, 7, 21, 2, 56, 15, TimeZone.getTimeZone("PST").getID());
checkTimestampWithLocalTimeZone(builder.makeLiteral(ts.getLocalTimestampString(), timestampType, false));
// Now with milliseconds
final TimestampWithTimeZoneString ts2 = ts.withMillis(56);
assertThat(ts2.toString(), is("1969-07-21 02:56:15.056 PST"));
final RexNode literal2 = builder.makeLiteral(ts2.getLocalTimestampString(), timestampType3, false);
assertThat(((RexLiteral) literal2).getValue().toString(), is("1969-07-21 02:56:15.056"));
// Now with nanoseconds
final TimestampWithTimeZoneString ts3 = ts.withNanos(56);
final RexNode literal3 = builder.makeLiteral(ts3.getLocalTimestampString(), timestampType9, false);
assertThat(((RexLiteral) literal3).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15"));
final TimestampWithTimeZoneString ts3b = ts.withNanos(2345678);
final RexNode literal3b = builder.makeLiteral(ts3b.getLocalTimestampString(), timestampType9, false);
assertThat(((RexLiteral) literal3b).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15.002"));
// Now with a very long fraction
final TimestampWithTimeZoneString ts4 = ts.withFraction("102030405060708090102");
final RexNode literal4 = builder.makeLiteral(ts4.getLocalTimestampString(), timestampType18, false);
assertThat(((RexLiteral) literal4).getValueAs(TimestampString.class).toString(), is("1969-07-21 02:56:15.102"));
// toString
assertThat(ts2.round(1).toString(), is("1969-07-21 02:56:15 PST"));
assertThat(ts2.round(2).toString(), is("1969-07-21 02:56:15.05 PST"));
assertThat(ts2.round(3).toString(), is("1969-07-21 02:56:15.056 PST"));
assertThat(ts2.round(4).toString(), is("1969-07-21 02:56:15.056 PST"));
assertThat(ts2.toString(6), is("1969-07-21 02:56:15.056000 PST"));
assertThat(ts2.toString(1), is("1969-07-21 02:56:15.0 PST"));
assertThat(ts2.toString(0), is("1969-07-21 02:56:15 PST"));
assertThat(ts2.round(0).toString(), is("1969-07-21 02:56:15 PST"));
assertThat(ts2.round(0).toString(0), is("1969-07-21 02:56:15 PST"));
assertThat(ts2.round(0).toString(1), is("1969-07-21 02:56:15.0 PST"));
assertThat(ts2.round(0).toString(2), is("1969-07-21 02:56:15.00 PST"));
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.
the class RexBuilderTest method testEnsureTypeWithDifference.
/**
* Test RexBuilder.ensureType()
*/
@Test
public void testEnsureTypeWithDifference() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
RexBuilder builder = new RexBuilder(typeFactory);
RexNode node = new RexLiteral(Boolean.TRUE, typeFactory.createSqlType(SqlTypeName.BOOLEAN), SqlTypeName.BOOLEAN);
RexNode ensuredNode = builder.ensureType(typeFactory.createSqlType(SqlTypeName.INTEGER), node, true);
assertNotEquals(node, ensuredNode);
assertEquals(ensuredNode.getType(), typeFactory.createSqlType(SqlTypeName.INTEGER));
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project calcite by apache.
the class RexBuilderTest method testEnsureTypeWithAny.
/**
* Test RexBuilder.ensureType()
*/
@Test
public void testEnsureTypeWithAny() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
RexBuilder builder = new RexBuilder(typeFactory);
RexNode node = new RexLiteral(Boolean.TRUE, typeFactory.createSqlType(SqlTypeName.BOOLEAN), SqlTypeName.BOOLEAN);
RexNode ensuredNode = builder.ensureType(typeFactory.createSqlType(SqlTypeName.ANY), node, true);
assertEquals(node, ensuredNode);
}
use of org.apache.calcite.rel.type.RelDataTypeFactory in project drill by axbaretto.
the class DrillReduceAggregatesRule method reduceStddev.
private RexNode reduceStddev(Aggregate oldAggRel, AggregateCall oldCall, boolean biased, boolean sqrt, List<AggregateCall> newCalls, Map<AggregateCall, RexNode> aggCallMapping, List<RexNode> inputExprs) {
// stddev_pop(x) ==>
// power(
// (sum(x * x) - sum(x) * sum(x) / count(x))
// / count(x),
// .5)
//
// stddev_samp(x) ==>
// power(
// (sum(x * x) - sum(x) * sum(x) / count(x))
// / nullif(count(x) - 1, 0),
// .5)
final PlannerSettings plannerSettings = (PlannerSettings) oldAggRel.getCluster().getPlanner().getContext();
final boolean isInferenceEnabled = plannerSettings.isTypeInferenceEnabled();
final int nGroups = oldAggRel.getGroupCount();
RelDataTypeFactory typeFactory = oldAggRel.getCluster().getTypeFactory();
final RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
assert oldCall.getArgList().size() == 1 : oldCall.getArgList();
final int argOrdinal = oldCall.getArgList().get(0);
final RelDataType argType = getFieldType(oldAggRel.getInput(), argOrdinal);
// final RexNode argRef = inputExprs.get(argOrdinal);
RexNode argRef = rexBuilder.makeCall(CastHighOp, inputExprs.get(argOrdinal));
inputExprs.set(argOrdinal, argRef);
final RexNode argSquared = rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, argRef, argRef);
final int argSquaredOrdinal = lookupOrAdd(inputExprs, argSquared);
RelDataType sumType = TypeInferenceUtils.getDrillSqlReturnTypeInference(SqlKind.SUM.name(), ImmutableList.<DrillFuncHolder>of()).inferReturnType(oldCall.createBinding(oldAggRel));
sumType = typeFactory.createTypeWithNullability(sumType, true);
final AggregateCall sumArgSquaredAggCall = AggregateCall.create(new DrillCalciteSqlAggFunctionWrapper(new SqlSumAggFunction(sumType), sumType), oldCall.isDistinct(), oldCall.isApproximate(), ImmutableIntList.of(argSquaredOrdinal), -1, sumType, null);
final RexNode sumArgSquared = rexBuilder.addAggCall(sumArgSquaredAggCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(argType));
final AggregateCall sumArgAggCall = AggregateCall.create(new DrillCalciteSqlAggFunctionWrapper(new SqlSumAggFunction(sumType), sumType), oldCall.isDistinct(), oldCall.isApproximate(), ImmutableIntList.of(argOrdinal), -1, sumType, null);
final RexNode sumArg = rexBuilder.addAggCall(sumArgAggCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(argType));
final RexNode sumSquaredArg = rexBuilder.makeCall(SqlStdOperatorTable.MULTIPLY, sumArg, sumArg);
final SqlCountAggFunction countAgg = (SqlCountAggFunction) SqlStdOperatorTable.COUNT;
final RelDataType countType = countAgg.getReturnType(typeFactory);
final AggregateCall countArgAggCall = AggregateCall.create(countAgg, oldCall.isDistinct(), oldCall.isApproximate(), oldCall.getArgList(), -1, countType, null);
final RexNode countArg = rexBuilder.addAggCall(countArgAggCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, ImmutableList.of(argType));
final RexNode avgSumSquaredArg = rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE, sumSquaredArg, countArg);
final RexNode diff = rexBuilder.makeCall(SqlStdOperatorTable.MINUS, sumArgSquared, avgSumSquaredArg);
final RexNode denominator;
if (biased) {
denominator = countArg;
} else {
final RexLiteral one = rexBuilder.makeExactLiteral(BigDecimal.ONE);
final RexNode nul = rexBuilder.makeNullLiteral(countArg.getType());
final RexNode countMinusOne = rexBuilder.makeCall(SqlStdOperatorTable.MINUS, countArg, one);
final RexNode countEqOne = rexBuilder.makeCall(SqlStdOperatorTable.EQUALS, countArg, one);
denominator = rexBuilder.makeCall(SqlStdOperatorTable.CASE, countEqOne, nul, countMinusOne);
}
final SqlOperator divide;
if (isInferenceEnabled) {
divide = new DrillSqlOperator("divide", 2, true, oldCall.getType(), false);
} else {
divide = SqlStdOperatorTable.DIVIDE;
}
final RexNode div = rexBuilder.makeCall(divide, diff, denominator);
RexNode result = div;
if (sqrt) {
final RexNode half = rexBuilder.makeExactLiteral(new BigDecimal("0.5"));
result = rexBuilder.makeCall(SqlStdOperatorTable.POWER, div, half);
}
if (isInferenceEnabled) {
return result;
} else {
/*
* Currently calcite's strategy to infer the return type of aggregate functions
* is wrong because it uses the first known argument to determine output type. For
* instance if we are performing stddev on an integer column then it interprets the
* output type to be integer which is incorrect as it should be double. So based on
* this if we add cast after rewriting the aggregate we add an additional cast which
* would cause wrong results. So we simply add a cast to ANY.
*/
return rexBuilder.makeCast(typeFactory.createSqlType(SqlTypeName.ANY), result);
}
}
Aggregations