Search in sources :

Example 1 with ReduceExpressionsRule

use of org.apache.calcite.rel.rules.ReduceExpressionsRule in project calcite by apache.

the class RelOptRulesTest method testReduceConstantsRequiresExecutor.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-566">[CALCITE-566]
 * ReduceExpressionsRule requires planner to have an Executor</a>.
 */
@Test
void testReduceConstantsRequiresExecutor() {
    // Rule should not fire, but there should be no NPE
    // Create a new planner instance, so we can remove its executor without
    // breaking other tests.
    final RelOptPlanner planner = new MockRelOptPlanner(Contexts.empty());
    final String sql = "select * from (values (1,2)) where 1 + 2 > 3 + CAST(NULL AS INTEGER)";
    sql(sql).withFactory(t -> t.withPlannerFactory(context -> planner)).withBefore((fixture, r) -> {
        // Remove the executor
        assertThat(r.getCluster().getPlanner(), sameInstance(planner));
        r.getCluster().getPlanner().setExecutor(null);
        return r;
    }).withRule(CoreRules.FILTER_REDUCE_EXPRESSIONS).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) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Test(org.junit.jupiter.api.Test)

Example 2 with ReduceExpressionsRule

use of org.apache.calcite.rel.rules.ReduceExpressionsRule in project calcite by apache.

the class RelOptRulesTest method checkDynamicFunctions.

RelOptFixture checkDynamicFunctions(boolean treatDynamicCallsAsConstant) {
    // Create a customized executor with given context operator that reduces
    // "USER" to "happyCalciteUser"
    final RexExecutorImpl executor = new RexExecutorImpl(DataContexts.of(name -> name.equals(DataContext.Variable.USER.camelName) ? "happyCalciteUser" : fail("unknown: " + name)));
    RelOptPlanner planner = new MockRelOptPlanner(Contexts.empty());
    planner.setExecutor(executor);
    final ReduceExpressionsRule<?> rule = CoreRules.PROJECT_REDUCE_EXPRESSIONS.config.withOperandFor(LogicalProject.class).withTreatDynamicCallsAsConstant(treatDynamicCallsAsConstant).as(ProjectReduceExpressionsRule.Config.class).toRule();
    final String sql = "select USER from emp";
    return sql(sql).withFactory(t -> t.withPlannerFactory(context -> planner)).withRule(rule);
}
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) RexExecutorImpl(org.apache.calcite.rex.RexExecutorImpl) CalciteConnectionConfig(org.apache.calcite.config.CalciteConnectionConfig) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner)

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