org.apache.calcite.rel.core.RelFactories Java Examples

The following examples show how to use org.apache.calcite.rel.core.RelFactories. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlanningConfigurationBuilder.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a configured {@link FlinkRelBuilder} for a planning session.
 *
 * @param currentCatalog the current default catalog to look for first during planning.
 * @param currentDatabase the current default database to look for first during planning.
 * @return configured rel builder
 */
public FlinkRelBuilder createRelBuilder(String currentCatalog, String currentDatabase) {
	RelOptCluster cluster = FlinkRelOptClusterFactory.create(
		planner,
		new RexBuilder(typeFactory));
	RelOptSchema relOptSchema = createCatalogReader(false, currentCatalog, currentDatabase);
	Context chain = Contexts.chain(
		context,
		// We need to overwrite the default scan factory, which does not
		// expand views. The expandingScanFactory uses the FlinkPlanner to translate a view
		// into a rel tree, before applying any subsequent rules.
		Contexts.of(RelFactories.expandingScanFactory(
			createFlinkPlanner(currentCatalog, currentDatabase),
			RelFactories.DEFAULT_TABLE_SCAN_FACTORY))
	);

	return new FlinkRelBuilder(chain, cluster, relOptSchema, expressionBridge);
}
 
Example #2
Source File: HepPlannerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testRuleClass() throws Exception {
  // Verify that an entire class of rules can be applied.

  HepProgramBuilder programBuilder = HepProgram.builder();
  programBuilder.addRuleClass(CoerceInputsRule.class);

  HepPlanner planner =
      new HepPlanner(
          programBuilder.build());

  planner.addRule(
      new CoerceInputsRule(LogicalUnion.class, false,
          RelFactories.LOGICAL_BUILDER));
  planner.addRule(
      new CoerceInputsRule(LogicalIntersect.class, false,
          RelFactories.LOGICAL_BUILDER));

  final String sql = "(select name from dept union select ename from emp)\n"
      + "intersect (select fname from customer.contact)";
  sql(sql).with(planner).check();
}
 
Example #3
Source File: RelOptUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RelNode createProjectJoinRel(List<Integer> outputProj, RelNode joinRel) {
    int newProjectOutputSize = outputProj.size();
    List<RelDataTypeField> joinOutputFields = joinRel.getRowType().getFieldList();

    // If no projection was passed in, or the number of desired projection
    // columns is the same as the number of columns returned from the
    // join, then no need to create a projection
    if ((newProjectOutputSize > 0) && (newProjectOutputSize < joinOutputFields.size())) {
        final List<Pair<RexNode, String>> newProjects = new ArrayList<>();
        final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(joinRel.getCluster(), null);
        final RexBuilder rexBuilder = relBuilder.getRexBuilder();
        for (int fieldIndex : outputProj) {
            final RelDataTypeField field = joinOutputFields.get(fieldIndex);
            newProjects.add(Pair.of(rexBuilder.makeInputRef(field.getType(), fieldIndex), field.getName()));
        }

        // Create a project rel on the output of the join.
        return relBuilder.push(joinRel).project(Pair.left(newProjects), Pair.right(newProjects), true).build();
    }

    return joinRel;
}
 
Example #4
Source File: SemiJoinFilterTransposeRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalJoin semiJoin = call.rel(0);
  LogicalFilter filter = call.rel(1);

  RelNode newSemiJoin =
      LogicalJoin.create(filter.getInput(),
          semiJoin.getRight(),
          // No need to copy the hints, the framework would try to do that.
          ImmutableList.of(),
          semiJoin.getCondition(),
          ImmutableSet.of(),
          JoinRelType.SEMI);

  final RelFactories.FilterFactory factory =
      RelFactories.DEFAULT_FILTER_FACTORY;
  RelNode newFilter =
      factory.createFilter(newSemiJoin, filter.getCondition(),
          ImmutableSet.of());

  call.transformTo(newFilter);
}
 
Example #5
Source File: AggregateJoinTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass,
    RelFactories.AggregateFactory aggregateFactory,
    Class<? extends Join> joinClass,
    RelFactories.JoinFactory joinFactory) {
  this(aggregateClass, joinClass,
      RelBuilder.proto(aggregateFactory, joinFactory), false);
}
 
Example #6
Source File: AggregateUnionTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateUnionTransposeRule(Class<? extends Aggregate> aggregateClass,
    RelFactories.AggregateFactory aggregateFactory,
    Class<? extends Union> unionClass,
    RelFactories.SetOpFactory setOpFactory) {
  this(aggregateClass, unionClass,
      RelBuilder.proto(aggregateFactory, setOpFactory));
}
 
Example #7
Source File: SortProjectTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public SortProjectTransposeRule(
    Class<? extends Sort> sortClass,
    Class<? extends Project> projectClass,
    String description) {
  this(sortClass, projectClass, RelFactories.LOGICAL_BUILDER, description);
}
 
Example #8
Source File: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a filter using the default factory,
 * or returns the original relational expression if the
 * condition is trivial. */
public static RelNode createFilter(RelNode child,
    Iterable<? extends RexNode> conditions,
    RelFactories.FilterFactory filterFactory) {
  final RelOptCluster cluster = child.getCluster();
  final RexNode condition =
      RexUtil.composeConjunction(cluster.getRexBuilder(), conditions, true);
  if (condition == null) {
    return child;
  } else {
    return filterFactory.createFilter(child, condition, ImmutableSet.of());
  }
}
 
Example #9
Source File: AggregateJoinTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass,
    RelFactories.AggregateFactory aggregateFactory,
    Class<? extends Join> joinClass,
    RelFactories.JoinFactory joinFactory,
    boolean allowFunctions) {
  this(aggregateClass, joinClass,
      RelBuilder.proto(aggregateFactory, joinFactory), allowFunctions);
}
 
Example #10
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FilterProjectTransposeRule with an explicit root operand and
 * factories.
 */
@Deprecated // to be removed before 2.0
protected FlinkFilterJoinRule(RelOptRuleOperand operand, String id,
		boolean smart, RelFactories.FilterFactory filterFactory,
		RelFactories.ProjectFactory projectFactory,
		Predicate predicate) {
	this(operand, id, smart, RelBuilder.proto(filterFactory, projectFactory),
			predicate);
}
 
Example #11
Source File: PlannerTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-648">[CALCITE-648]
 * Update ProjectMergeRule description for new naming convention</a>. */
@Test void testMergeProjectForceMode() throws Exception {
  RuleSet ruleSet =
      RuleSets.ofList(
          new ProjectMergeRule(true, ProjectMergeRule.DEFAULT_BLOAT,
              RelBuilder.proto(RelFactories.DEFAULT_PROJECT_FACTORY)));
  Planner planner = getPlanner(null, Programs.of(ruleSet));
  planner.close();
}
 
Example #12
Source File: FlinkAggregateJoinTransposeRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public FlinkAggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass,
		RelFactories.AggregateFactory aggregateFactory,
		Class<? extends Join> joinClass,
		RelFactories.JoinFactory joinFactory,
		boolean allowFunctions) {
	this(aggregateClass, joinClass,
			RelBuilder.proto(aggregateFactory, joinFactory), allowFunctions);
}
 
Example #13
Source File: FlinkAggregateJoinTransposeRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public FlinkAggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass,
		RelFactories.AggregateFactory aggregateFactory,
		Class<? extends Join> joinClass,
		RelFactories.JoinFactory joinFactory,
		RelFactories.ProjectFactory projectFactory,
		boolean allowFunctions) {
	this(aggregateClass, joinClass,
			RelBuilder.proto(aggregateFactory, joinFactory, projectFactory),
			allowFunctions);
}
 
Example #14
Source File: AggregateMergeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
private AggregateMergeRule() {
  this(
      operand(Aggregate.class,
          operandJ(Aggregate.class, null,
              agg -> Aggregate.isSimple(agg), any())),
      RelFactories.LOGICAL_BUILDER);
}
 
Example #15
Source File: FlinkAggregateJoinTransposeRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public FlinkAggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass,
		RelFactories.AggregateFactory aggregateFactory,
		Class<? extends Join> joinClass,
		RelFactories.JoinFactory joinFactory,
		boolean allowFunctions) {
	this(aggregateClass, joinClass,
			RelBuilder.proto(aggregateFactory, joinFactory), allowFunctions);
}
 
Example #16
Source File: AggregateUnionAggregateRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateUnionAggregateRule(Class<? extends Aggregate> aggregateClass,
    RelFactories.AggregateFactory aggregateFactory,
    Class<? extends Union> unionClass,
    RelFactories.SetOpFactory setOpFactory) {
  this(aggregateClass, unionClass, RelNode.class, RelNode.class,
      RelBuilder.proto(aggregateFactory, setOpFactory),
      "AggregateUnionAggregateRule");
}
 
Example #17
Source File: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RelNode createProjectJoinRel(
    List<Integer> outputProj,
    RelNode joinRel) {
  int newProjectOutputSize = outputProj.size();
  List<RelDataTypeField> joinOutputFields =
      joinRel.getRowType().getFieldList();

  // If no projection was passed in, or the number of desired projection
  // columns is the same as the number of columns returned from the
  // join, then no need to create a projection
  if ((newProjectOutputSize > 0)
      && (newProjectOutputSize < joinOutputFields.size())) {
    final List<Pair<RexNode, String>> newProjects = new ArrayList<>();
    final RelBuilder relBuilder =
        RelFactories.LOGICAL_BUILDER.create(joinRel.getCluster(), null);
    final RexBuilder rexBuilder = relBuilder.getRexBuilder();
    for (int fieldIndex : outputProj) {
      final RelDataTypeField field = joinOutputFields.get(fieldIndex);
      newProjects.add(
          Pair.of(
              rexBuilder.makeInputRef(field.getType(), fieldIndex),
              field.getName()));
    }

    // Create a project rel on the output of the join.
    return relBuilder.push(joinRel)
        .project(Pair.left(newProjects), Pair.right(newProjects), true)
        .build();
  }

  return joinRel;
}
 
Example #18
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public FlinkFilterIntoJoinRule(boolean smart,
		RelFactories.FilterFactory filterFactory,
		RelFactories.ProjectFactory projectFactory,
		Predicate predicate) {
	this(smart, RelBuilder.proto(filterFactory, projectFactory), predicate);
}
 
Example #19
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FlinkFilterJoinRule with an explicit root operand and
 * factories.
 */
@Deprecated // to be removed before 2.0
protected FlinkFilterJoinRule(RelOptRuleOperand operand, String id,
		boolean smart, RelFactories.FilterFactory filterFactory,
		RelFactories.ProjectFactory projectFactory) {
	this(operand, id, smart, RelBuilder.proto(filterFactory, projectFactory),
			TRUE_PREDICATE);
}
 
Example #20
Source File: AggregateUnionAggregateRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateUnionAggregateRule(Class<? extends Aggregate> aggregateClass,
    RelFactories.AggregateFactory aggregateFactory,
    Class<? extends Union> unionClass,
    RelFactories.SetOpFactory setOpFactory) {
  this(aggregateClass, unionClass, RelNode.class, RelNode.class,
      RelBuilder.proto(aggregateFactory, setOpFactory),
      "AggregateUnionAggregateRule");
}
 
Example #21
Source File: Programs.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RelNode run(RelOptPlanner planner, RelNode rel,
    RelTraitSet requiredOutputTraits,
    List<RelOptMaterialization> materializations,
    List<RelOptLattice> lattices) {
  final RelBuilder relBuilder =
      RelFactories.LOGICAL_BUILDER.create(rel.getCluster(), null);
  return new RelFieldTrimmer(null, relBuilder).trim(rel);
}
 
Example #22
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RelNode createNullFilter(RelNode rel, Integer[] fieldOrdinals) {
    RexNode condition = null;
    final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
    RelDataType rowType = rel.getRowType();
    int n;
    if (fieldOrdinals != null) {
        n = fieldOrdinals.length;
    } else {
        n = rowType.getFieldCount();
    }
    List<RelDataTypeField> fields = rowType.getFieldList();
    for (int i = 0; i < n; ++i) {
        int iField;
        if (fieldOrdinals != null) {
            iField = fieldOrdinals[i];
        } else {
            iField = i;
        }
        RelDataType type = fields.get(iField).getType();
        if (!type.isNullable()) {
            continue;
        }
        RexNode newCondition = rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
                rexBuilder.makeInputRef(type, iField));
        if (condition == null) {
            condition = newCondition;
        } else {
            condition = rexBuilder.makeCall(SqlStdOperatorTable.AND, condition, newCondition);
        }
    }
    if (condition == null) {
        // no filtering required
        return rel;
    }

    final RelFactories.FilterFactory factory = RelFactories.DEFAULT_FILTER_FACTORY;
    return factory.createFilter(rel, condition);
}
 
Example #23
Source File: FlinkAggregateJoinTransposeRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public FlinkAggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass,
		RelFactories.AggregateFactory aggregateFactory,
		Class<? extends Join> joinClass,
		RelFactories.JoinFactory joinFactory) {
	this(aggregateClass, joinClass,
			RelBuilder.proto(aggregateFactory, joinFactory), false);
}
 
Example #24
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FlinkFilterJoinRule with an explicit root operand and
 * factories.
 */
@Deprecated // to be removed before 2.0
protected FlinkFilterJoinRule(RelOptRuleOperand operand, String id,
		boolean smart, RelFactories.FilterFactory filterFactory,
		RelFactories.ProjectFactory projectFactory) {
	this(operand, id, smart, RelBuilder.proto(filterFactory, projectFactory),
			TRUE_PREDICATE);
}
 
Example #25
Source File: RelOptMaterializations.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Trim unused fields in relational expressions.
 */
private static RelNode trimUnusedfields(RelNode relNode) {
  final List<RelOptTable> relOptTables = RelOptUtil.findAllTables(relNode);
  RelOptSchema relOptSchema = null;
  if (relOptTables.size() != 0) {
    relOptSchema = relOptTables.get(0).getRelOptSchema();
  }
  final RelBuilder relBuilder = RelFactories.LOGICAL_BUILDER.create(
      relNode.getCluster(), relOptSchema);
  final RelFieldTrimmer relFieldTrimmer = new RelFieldTrimmer(null, relBuilder);
  final RelNode rel = relFieldTrimmer.trim(relNode);
  return rel;
}
 
Example #26
Source File: FilterJoinRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public FilterIntoJoinRule(boolean smart,
    RelFactories.FilterFactory filterFactory,
    RelFactories.ProjectFactory projectFactory,
    Predicate predicate) {
  this(smart, RelBuilder.proto(filterFactory, projectFactory), predicate);
}
 
Example #27
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FlinkFilterJoinRule with an explicit root operand and
 * factories.
 */
@Deprecated // to be removed before 2.0
protected FlinkFilterJoinRule(RelOptRuleOperand operand, String id,
		boolean smart, RelFactories.FilterFactory filterFactory,
		RelFactories.ProjectFactory projectFactory) {
	this(operand, id, smart, RelBuilder.proto(filterFactory, projectFactory),
			TRUE_PREDICATE);
}
 
Example #28
Source File: FlinkFilterJoinRule.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FilterProjectTransposeRule with an explicit root operand and
 * factories.
 */
@Deprecated // to be removed before 2.0
protected FlinkFilterJoinRule(RelOptRuleOperand operand, String id,
		boolean smart, RelFactories.FilterFactory filterFactory,
		RelFactories.ProjectFactory projectFactory,
		Predicate predicate) {
	this(operand, id, smart, RelBuilder.proto(filterFactory, projectFactory),
			predicate);
}
 
Example #29
Source File: RelFieldTrimmer.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public RelFieldTrimmer(SqlValidator validator,
    RelOptCluster cluster,
    RelFactories.ProjectFactory projectFactory,
    RelFactories.FilterFactory filterFactory,
    RelFactories.JoinFactory joinFactory,
    RelFactories.SortFactory sortFactory,
    RelFactories.AggregateFactory aggregateFactory,
    RelFactories.SetOpFactory setOpFactory) {
  this(validator,
      RelBuilder.proto(projectFactory, filterFactory, joinFactory,
          sortFactory, aggregateFactory, setOpFactory)
      .create(cluster, null));
}
 
Example #30
Source File: AggregateExpandDistinctAggregatesRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Deprecated // to be removed before 2.0
public AggregateExpandDistinctAggregatesRule(Class<? extends LogicalAggregate> clazz,
        RelFactories.JoinFactory joinFactory) {
    this(clazz, false, RelBuilder.proto(Contexts.of(joinFactory)));
}