Search in sources :

Example 1 with RelDataTypeSystemImpl

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();
}
Also used : AggregateProjectMergeRule(org.apache.calcite.rel.rules.AggregateProjectMergeRule) Programs(org.apache.calcite.tools.Programs) Arrays(java.util.Arrays) RelDistributions(org.apache.calcite.rel.RelDistributions) DateRangeRules(org.apache.calcite.rel.rules.DateRangeRules) Disabled(org.junit.jupiter.api.Disabled) Contexts(org.apache.calcite.plan.Contexts) Union(org.apache.calcite.rel.core.Union) CorrelationId(org.apache.calcite.rel.core.CorrelationId) EnumSet(java.util.EnumSet) RelTraitSet(org.apache.calcite.plan.RelTraitSet) ReduceExpressionsRule(org.apache.calcite.rel.rules.ReduceExpressionsRule) SqlOperatorBinding(org.apache.calcite.sql.SqlOperatorBinding) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) HepMatchOrder(org.apache.calcite.plan.hep.HepMatchOrder) EnumerableSort(org.apache.calcite.adapter.enumerable.EnumerableSort) SqlKind(org.apache.calcite.sql.SqlKind) Minus(org.apache.calcite.rel.core.Minus) RexExecutorImpl(org.apache.calcite.rex.RexExecutorImpl) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) DataContexts(org.apache.calcite.DataContexts) NL(org.apache.calcite.test.SqlToRelTestBase.NL) HintPredicates(org.apache.calcite.rel.hint.HintPredicates) AggregateProjectConstantToDummyJoinRule(org.apache.calcite.rel.rules.AggregateProjectConstantToDummyJoinRule) RelDataTypeSystemImpl(org.apache.calcite.rel.type.RelDataTypeSystemImpl) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelCollation(org.apache.calcite.rel.RelCollation) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) JoinCommuteRule(org.apache.calcite.rel.rules.JoinCommuteRule) SqlConformanceEnum(org.apache.calcite.sql.validate.SqlConformanceEnum) RuleSet(org.apache.calcite.tools.RuleSet) EnumerableLimitSort(org.apache.calcite.adapter.enumerable.EnumerableLimitSort) AggregateExtractProjectRule(org.apache.calcite.rel.rules.AggregateExtractProjectRule) ProjectToWindowRule(org.apache.calcite.rel.rules.ProjectToWindowRule) RexCall(org.apache.calcite.rex.RexCall) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ProjectFilterTransposeRule(org.apache.calcite.rel.rules.ProjectFilterTransposeRule) ProjectReduceExpressionsRule(org.apache.calcite.rel.rules.ReduceExpressionsRule.ProjectReduceExpressionsRule) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) AggregateProjectPullUpConstantsRule(org.apache.calcite.rel.rules.AggregateProjectPullUpConstantsRule) AggregateExpandWithinDistinctRule(org.apache.calcite.rel.rules.AggregateExpandWithinDistinctRule) ReturnTypes(org.apache.calcite.sql.type.ReturnTypes) Filter(org.apache.calcite.rel.core.Filter) Join(org.apache.calcite.rel.core.Join) RuleSets(org.apache.calcite.tools.RuleSets) RelDecorrelator(org.apache.calcite.sql2rel.RelDecorrelator) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) JoinAssociateRule(org.apache.calcite.rel.rules.JoinAssociateRule) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockCatalogReader(org.apache.calcite.test.catalog.MockCatalogReader) RelDataType(org.apache.calcite.rel.type.RelDataType) FilterProjectTransposeRule(org.apache.calcite.rel.rules.FilterProjectTransposeRule) SpatialRules(org.apache.calcite.rel.rules.SpatialRules) RelRule(org.apache.calcite.plan.RelRule) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Aggregate(org.apache.calcite.rel.core.Aggregate) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) SqlFunction(org.apache.calcite.sql.SqlFunction) EnumerableRules(org.apache.calcite.adapter.enumerable.EnumerableRules) JoinRelType(org.apache.calcite.rel.core.JoinRelType) ProjectMultiJoinMergeRule(org.apache.calcite.rel.rules.ProjectMultiJoinMergeRule) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) FilterMultiJoinMergeRule(org.apache.calcite.rel.rules.FilterMultiJoinMergeRule) UnionMergeRule(org.apache.calcite.rel.rules.UnionMergeRule) LogicalTableModify(org.apache.calcite.rel.logical.LogicalTableModify) RexUtil(org.apache.calcite.rex.RexUtil) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) FilterFlattenCorrelatedConditionRule(org.apache.calcite.rel.rules.FilterFlattenCorrelatedConditionRule) RexNode(org.apache.calcite.rex.RexNode) RelBuilder(org.apache.calcite.tools.RelBuilder) RelHint(org.apache.calcite.rel.hint.RelHint) Locale(java.util.Locale) Intersect(org.apache.calcite.rel.core.Intersect) HepProgram(org.apache.calcite.plan.hep.HepProgram) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) RelOptCluster(org.apache.calcite.plan.RelOptCluster) ValuesReduceRule(org.apache.calcite.rel.rules.ValuesReduceRule) Program(org.apache.calcite.tools.Program) Predicate(java.util.function.Predicate) RexLiteral(org.apache.calcite.rex.RexLiteral) DataContext(org.apache.calcite.DataContext) MultiJoin(org.apache.calcite.rel.rules.MultiJoin) CustomCorrelate(org.apache.calcite.test.SqlToRelTestBase.CustomCorrelate) RexInputRef(org.apache.calcite.rex.RexInputRef) Test(org.junit.jupiter.api.Test) List(java.util.List) FilterJoinRule(org.apache.calcite.rel.rules.FilterJoinRule) MockCatalogReaderExtended(org.apache.calcite.test.catalog.MockCatalogReaderExtended) OperandTypes(org.apache.calcite.sql.type.OperandTypes) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelTraitDef(org.apache.calcite.plan.RelTraitDef) SqlSpecialOperator(org.apache.calcite.sql.SqlSpecialOperator) Project(org.apache.calcite.rel.core.Project) RelDistributionTraitDef(org.apache.calcite.rel.RelDistributionTraitDef) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Function(java.util.function.Function) EnumerableConvention(org.apache.calcite.adapter.enumerable.EnumerableConvention) ImmutableList(com.google.common.collect.ImmutableList) Value(org.immutables.value.Value) HintStrategyTable(org.apache.calcite.rel.hint.HintStrategyTable) SqlLibrary(org.apache.calcite.sql.fun.SqlLibrary) SqlOperator(org.apache.calcite.sql.SqlOperator) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) RelCollations(org.apache.calcite.rel.RelCollations) SqlMonotonicity(org.apache.calcite.sql.validate.SqlMonotonicity) RelCollationTraitDef(org.apache.calcite.rel.RelCollationTraitDef) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexBuilder(org.apache.calcite.rex.RexBuilder) SqlFunctionCategory(org.apache.calcite.sql.SqlFunctionCategory) ProjectJoinTransposeRule(org.apache.calcite.rel.rules.ProjectJoinTransposeRule) AggregateReduceFunctionsRule(org.apache.calcite.rel.rules.AggregateReduceFunctionsRule) PushProjector(org.apache.calcite.rel.rules.PushProjector) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) RelNode(org.apache.calcite.rel.RelNode) SqlTestFactory(org.apache.calcite.sql.test.SqlTestFactory) EnumerableLimit(org.apache.calcite.adapter.enumerable.EnumerableLimit) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) CoerceInputsRule(org.apache.calcite.rel.rules.CoerceInputsRule) ProjectCorrelateTransposeRule(org.apache.calcite.rel.rules.ProjectCorrelateTransposeRule) RelOptRule(org.apache.calcite.plan.RelOptRule) CoreRules(org.apache.calcite.rel.rules.CoreRules) Collections(java.util.Collections) PruneEmptyRules(org.apache.calcite.rel.rules.PruneEmptyRules) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) SqlTestFactory(org.apache.calcite.sql.test.SqlTestFactory) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeSystemImpl(org.apache.calcite.rel.type.RelDataTypeSystemImpl) Test(org.junit.jupiter.api.Test)

Example 2 with RelDataTypeSystemImpl

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();
}
Also used : AggregateProjectMergeRule(org.apache.calcite.rel.rules.AggregateProjectMergeRule) Programs(org.apache.calcite.tools.Programs) Arrays(java.util.Arrays) RelDistributions(org.apache.calcite.rel.RelDistributions) DateRangeRules(org.apache.calcite.rel.rules.DateRangeRules) Disabled(org.junit.jupiter.api.Disabled) Contexts(org.apache.calcite.plan.Contexts) Union(org.apache.calcite.rel.core.Union) CorrelationId(org.apache.calcite.rel.core.CorrelationId) EnumSet(java.util.EnumSet) RelTraitSet(org.apache.calcite.plan.RelTraitSet) ReduceExpressionsRule(org.apache.calcite.rel.rules.ReduceExpressionsRule) SqlOperatorBinding(org.apache.calcite.sql.SqlOperatorBinding) ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) HepMatchOrder(org.apache.calcite.plan.hep.HepMatchOrder) EnumerableSort(org.apache.calcite.adapter.enumerable.EnumerableSort) SqlKind(org.apache.calcite.sql.SqlKind) Minus(org.apache.calcite.rel.core.Minus) RexExecutorImpl(org.apache.calcite.rex.RexExecutorImpl) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) DataContexts(org.apache.calcite.DataContexts) NL(org.apache.calcite.test.SqlToRelTestBase.NL) HintPredicates(org.apache.calcite.rel.hint.HintPredicates) AggregateProjectConstantToDummyJoinRule(org.apache.calcite.rel.rules.AggregateProjectConstantToDummyJoinRule) RelDataTypeSystemImpl(org.apache.calcite.rel.type.RelDataTypeSystemImpl) SqlStdOperatorTable(org.apache.calcite.sql.fun.SqlStdOperatorTable) RelCollation(org.apache.calcite.rel.RelCollation) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) JoinCommuteRule(org.apache.calcite.rel.rules.JoinCommuteRule) SqlConformanceEnum(org.apache.calcite.sql.validate.SqlConformanceEnum) RuleSet(org.apache.calcite.tools.RuleSet) EnumerableLimitSort(org.apache.calcite.adapter.enumerable.EnumerableLimitSort) AggregateExtractProjectRule(org.apache.calcite.rel.rules.AggregateExtractProjectRule) ProjectToWindowRule(org.apache.calcite.rel.rules.ProjectToWindowRule) RexCall(org.apache.calcite.rex.RexCall) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ProjectFilterTransposeRule(org.apache.calcite.rel.rules.ProjectFilterTransposeRule) ProjectReduceExpressionsRule(org.apache.calcite.rel.rules.ReduceExpressionsRule.ProjectReduceExpressionsRule) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) AggregateProjectPullUpConstantsRule(org.apache.calcite.rel.rules.AggregateProjectPullUpConstantsRule) AggregateExpandWithinDistinctRule(org.apache.calcite.rel.rules.AggregateExpandWithinDistinctRule) ReturnTypes(org.apache.calcite.sql.type.ReturnTypes) Filter(org.apache.calcite.rel.core.Filter) Join(org.apache.calcite.rel.core.Join) RuleSets(org.apache.calcite.tools.RuleSets) RelDecorrelator(org.apache.calcite.sql2rel.RelDecorrelator) LogicalCorrelate(org.apache.calcite.rel.logical.LogicalCorrelate) JoinAssociateRule(org.apache.calcite.rel.rules.JoinAssociateRule) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MockCatalogReader(org.apache.calcite.test.catalog.MockCatalogReader) RelDataType(org.apache.calcite.rel.type.RelDataType) FilterProjectTransposeRule(org.apache.calcite.rel.rules.FilterProjectTransposeRule) SpatialRules(org.apache.calcite.rel.rules.SpatialRules) RelRule(org.apache.calcite.plan.RelRule) HepProgramBuilder(org.apache.calcite.plan.hep.HepProgramBuilder) Aggregate(org.apache.calcite.rel.core.Aggregate) LogicalAggregate(org.apache.calcite.rel.logical.LogicalAggregate) SqlFunction(org.apache.calcite.sql.SqlFunction) EnumerableRules(org.apache.calcite.adapter.enumerable.EnumerableRules) JoinRelType(org.apache.calcite.rel.core.JoinRelType) ProjectMultiJoinMergeRule(org.apache.calcite.rel.rules.ProjectMultiJoinMergeRule) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) LogicalFilter(org.apache.calcite.rel.logical.LogicalFilter) FilterMultiJoinMergeRule(org.apache.calcite.rel.rules.FilterMultiJoinMergeRule) UnionMergeRule(org.apache.calcite.rel.rules.UnionMergeRule) LogicalTableModify(org.apache.calcite.rel.logical.LogicalTableModify) RexUtil(org.apache.calcite.rex.RexUtil) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) FilterFlattenCorrelatedConditionRule(org.apache.calcite.rel.rules.FilterFlattenCorrelatedConditionRule) RexNode(org.apache.calcite.rex.RexNode) RelBuilder(org.apache.calcite.tools.RelBuilder) RelHint(org.apache.calcite.rel.hint.RelHint) Locale(java.util.Locale) Intersect(org.apache.calcite.rel.core.Intersect) HepProgram(org.apache.calcite.plan.hep.HepProgram) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) RelOptCluster(org.apache.calcite.plan.RelOptCluster) ValuesReduceRule(org.apache.calcite.rel.rules.ValuesReduceRule) Program(org.apache.calcite.tools.Program) Predicate(java.util.function.Predicate) RexLiteral(org.apache.calcite.rex.RexLiteral) DataContext(org.apache.calcite.DataContext) MultiJoin(org.apache.calcite.rel.rules.MultiJoin) CustomCorrelate(org.apache.calcite.test.SqlToRelTestBase.CustomCorrelate) RexInputRef(org.apache.calcite.rex.RexInputRef) Test(org.junit.jupiter.api.Test) List(java.util.List) FilterJoinRule(org.apache.calcite.rel.rules.FilterJoinRule) MockCatalogReaderExtended(org.apache.calcite.test.catalog.MockCatalogReaderExtended) OperandTypes(org.apache.calcite.sql.type.OperandTypes) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) RelTraitDef(org.apache.calcite.plan.RelTraitDef) SqlSpecialOperator(org.apache.calcite.sql.SqlSpecialOperator) Project(org.apache.calcite.rel.core.Project) RelDistributionTraitDef(org.apache.calcite.rel.RelDistributionTraitDef) RelOptUtil(org.apache.calcite.plan.RelOptUtil) Function(java.util.function.Function) EnumerableConvention(org.apache.calcite.adapter.enumerable.EnumerableConvention) ImmutableList(com.google.common.collect.ImmutableList) Value(org.immutables.value.Value) HintStrategyTable(org.apache.calcite.rel.hint.HintStrategyTable) SqlLibrary(org.apache.calcite.sql.fun.SqlLibrary) SqlOperator(org.apache.calcite.sql.SqlOperator) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) RelCollations(org.apache.calcite.rel.RelCollations) SqlMonotonicity(org.apache.calcite.sql.validate.SqlMonotonicity) RelCollationTraitDef(org.apache.calcite.rel.RelCollationTraitDef) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexBuilder(org.apache.calcite.rex.RexBuilder) SqlFunctionCategory(org.apache.calcite.sql.SqlFunctionCategory) ProjectJoinTransposeRule(org.apache.calcite.rel.rules.ProjectJoinTransposeRule) AggregateReduceFunctionsRule(org.apache.calcite.rel.rules.AggregateReduceFunctionsRule) PushProjector(org.apache.calcite.rel.rules.PushProjector) HepPlanner(org.apache.calcite.plan.hep.HepPlanner) RelNode(org.apache.calcite.rel.RelNode) SqlTestFactory(org.apache.calcite.sql.test.SqlTestFactory) EnumerableLimit(org.apache.calcite.adapter.enumerable.EnumerableLimit) RelOptRuleCall(org.apache.calcite.plan.RelOptRuleCall) CoerceInputsRule(org.apache.calcite.rel.rules.CoerceInputsRule) ProjectCorrelateTransposeRule(org.apache.calcite.rel.rules.ProjectCorrelateTransposeRule) RelOptRule(org.apache.calcite.plan.RelOptRule) CoreRules(org.apache.calcite.rel.rules.CoreRules) Collections(java.util.Collections) PruneEmptyRules(org.apache.calcite.rel.rules.PruneEmptyRules) SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) SqlTestFactory(org.apache.calcite.sql.test.SqlTestFactory) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeSystemImpl(org.apache.calcite.rel.type.RelDataTypeSystemImpl) Test(org.junit.jupiter.api.Test)

Example 3 with RelDataTypeSystemImpl

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);
}
Also used : RelDataTypeSystemImpl(org.apache.calcite.rel.type.RelDataTypeSystemImpl) Test(org.junit.jupiter.api.Test) RelBuilderTest(org.apache.calcite.test.RelBuilderTest)

Example 4 with RelDataTypeSystemImpl

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");
}
Also used : SqlTypeFactoryImpl(org.apache.calcite.sql.type.SqlTypeFactoryImpl) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RelDataTypeFactory(org.apache.calcite.rel.type.RelDataTypeFactory) RelDataTypeSystemImpl(org.apache.calcite.rel.type.RelDataTypeSystemImpl) Test(org.junit.jupiter.api.Test)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 EnumSet (java.util.EnumSet)2 List (java.util.List)2 Locale (java.util.Locale)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2 DataContext (org.apache.calcite.DataContext)2 DataContexts (org.apache.calcite.DataContexts)2 EnumerableConvention (org.apache.calcite.adapter.enumerable.EnumerableConvention)2 EnumerableLimit (org.apache.calcite.adapter.enumerable.EnumerableLimit)2 EnumerableLimitSort (org.apache.calcite.adapter.enumerable.EnumerableLimitSort)2 EnumerableRules (org.apache.calcite.adapter.enumerable.EnumerableRules)2 EnumerableSort (org.apache.calcite.adapter.enumerable.EnumerableSort)2 CalciteConnectionConfig (org.apache.calcite.config.CalciteConnectionConfig)2 Contexts (org.apache.calcite.plan.Contexts)2 RelOptCluster (org.apache.calcite.plan.RelOptCluster)2 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)2 RelOptRule (org.apache.calcite.plan.RelOptRule)2