use of org.apache.calcite.rel.type.RelDataTypeSystemImpl in project calcite by apache.
the class RelOptRulesTest method testDistinctCountWithExpandSumType.
/**
* Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-4652">[CALCITE-4652]
* AggregateExpandDistinctAggregatesRule must cast top aggregates to original type</a>.
* <p>
* Checks AggregateExpandDistinctAggregatesRule when return type of the SUM aggregate
* is changed (expanded) by define custom type factory.
*/
@Test
void testDistinctCountWithExpandSumType() {
// Define new type system to expand SUM return type.
RelDataTypeSystemImpl typeSystem = new RelDataTypeSystemImpl() {
@Override
public RelDataType deriveSumType(RelDataTypeFactory typeFactory, RelDataType argumentType) {
switch(argumentType.getSqlTypeName()) {
case INTEGER:
case BIGINT:
return typeFactory.createSqlType(SqlTypeName.DECIMAL);
default:
return super.deriveSumType(typeFactory, argumentType);
}
}
};
SqlTestFactory.TypeFactoryFactory typeFactorySupplier = conformance -> new SqlTypeFactoryImpl(typeSystem);
// Expected plan:
// LogicalProject(EXPR$0=[CAST($0):BIGINT NOT NULL], EXPR$1=[$1])
// LogicalAggregate(group=[{}], EXPR$0=[$SUM0($1)], EXPR$1=[COUNT($0)])
// LogicalAggregate(group=[{0}], EXPR$0=[COUNT()])
// LogicalProject(COMM=[$6])
// LogicalTableScan(table=[[CATALOG, SALES, EMP]])
//
// The top 'LogicalProject' must be added in case SUM type is expanded
// because type of original expression 'COUNT(DISTINCT comm)' is BIGINT
// and type of SUM (of BIGINT) is DECIMAL.
sql("SELECT count(comm), COUNT(DISTINCT comm) FROM emp").withFactory(f -> f.withTypeFactoryFactory(typeFactorySupplier)).withRule(CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES_TO_JOIN).check();
}
use of org.apache.calcite.rel.type.RelDataTypeSystemImpl in project calcite by apache.
the class RelOptRulesTest method testSumAndDistinctSumWithExpandSumType.
/**
* Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-4818">[CALCITE-4818]
* AggregateExpandDistinctAggregatesRule must infer correct data type for top aggregate calls</a>.
* <p>
* Checks AggregateExpandDistinctAggregatesRule when return type of the SUM aggregate
* is changed (expanded) by define custom type factory.
*/
@Test
void testSumAndDistinctSumWithExpandSumType() {
// Define new type system to expand SUM return type.
RelDataTypeSystemImpl typeSystem = new RelDataTypeSystemImpl() {
@Override
public RelDataType deriveSumType(RelDataTypeFactory typeFactory, RelDataType argumentType) {
switch(argumentType.getSqlTypeName()) {
case INTEGER:
return typeFactory.createSqlType(SqlTypeName.BIGINT);
case BIGINT:
return typeFactory.createSqlType(SqlTypeName.DECIMAL);
default:
return super.deriveSumType(typeFactory, argumentType);
}
}
};
SqlTestFactory.TypeFactoryFactory typeFactoryFactory = conformance -> new SqlTypeFactoryImpl(typeSystem);
// Expected plan:
// LogicalProject(EXPR$0=[CAST($0):BIGINT], EXPR$1=[$1])
// LogicalAggregate(group=[{}], EXPR$0=[SUM($1)], EXPR$1=[SUM($0)]) // RowType[DECIMAL, BIGINT]
// LogicalAggregate(group=[{0}], EXPR$0=[SUM($0)]) // RowType[INTEGER, BIGINT]
// LogicalProject(COMM=[$6])
// LogicalTableScan(table=[[CATALOG, SALES, EMP]])
//
// The top 'LogicalProject' must be added in case SUM type is expanded
// because type of original expression 'COUNT(DISTINCT comm)' is BIGINT
// and type of SUM (of BIGINT) is DECIMAL.
sql("SELECT SUM(comm), SUM(DISTINCT comm) FROM emp").withFactory(f -> f.withTypeFactoryFactory(typeFactoryFactory)).withRule(CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES_TO_JOIN).check();
}
use of org.apache.calcite.rel.type.RelDataTypeSystemImpl in project calcite by apache.
the class RelToSqlConverterTest method testCastDecimalBigPrecision.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-4706">[CALCITE-4706]
* JDBC adapter generates casts exceeding Redshift's data types bounds</a>.
*/
@Test
void testCastDecimalBigPrecision() {
final String query = "select cast(\"product_id\" as decimal(60,2)) " + "from \"product\" ";
final String expectedRedshift = "SELECT CAST(\"product_id\" AS DECIMAL(38, 2))\n" + "FROM \"foodmart\".\"product\"";
sql(query).withTypeSystem(new RelDataTypeSystemImpl() {
@Override
public int getMaxNumericPrecision() {
// this change.
return 100;
}
}).withRedshift().ok(expectedRedshift);
}
use of org.apache.calcite.rel.type.RelDataTypeSystemImpl in project calcite by apache.
the class RexBuilderTest method testBigDecimalLiteral.
/**
* Tests {@link RexBuilder#makeExactLiteral(java.math.BigDecimal)}.
*/
@Test
void testBigDecimalLiteral() {
final RelDataTypeFactory typeFactory = new SqlTypeFactoryImpl(new RelDataTypeSystemImpl() {
@Override
public int getMaxPrecision(SqlTypeName typeName) {
return 38;
}
});
final RexBuilder builder = new RexBuilder(typeFactory);
checkBigDecimalLiteral(builder, "25");
checkBigDecimalLiteral(builder, "9.9");
checkBigDecimalLiteral(builder, "0");
checkBigDecimalLiteral(builder, "-75.5");
checkBigDecimalLiteral(builder, "10000000");
checkBigDecimalLiteral(builder, "100000.111111111111111111");
checkBigDecimalLiteral(builder, "-100000.111111111111111111");
// 2^66
checkBigDecimalLiteral(builder, "73786976294838206464");
checkBigDecimalLiteral(builder, "-73786976294838206464");
}
Aggregations