org.apache.calcite.rel.logical.LogicalCalc Java Examples

The following examples show how to use org.apache.calcite.rel.logical.LogicalCalc. 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: ReduceDecimalsRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
    LogicalCalc calc = call.rel(0);

    // Expand decimals in every expression in this program. If no
    // expression changes, don't apply the rule.
    final RexProgram program = calc.getProgram();
    if (!RexUtil.requiresDecimalExpansion(program, true)) {
        return;
    }

    final RexBuilder rexBuilder = calc.getCluster().getRexBuilder();
    final RexShuttle shuttle = new DecimalShuttle(rexBuilder);
    RexProgramBuilder programBuilder = RexProgramBuilder.create(rexBuilder, calc.getInput().getRowType(),
            program.getExprList(), program.getProjectList(), program.getCondition(), program.getOutputRowType(),
            shuttle, true);

    final RexProgram newProgram = programBuilder.getProgram();
    LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), newProgram);
    call.transformTo(newCalc);
}
 
Example #2
Source File: SparkRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalCalc calc = (LogicalCalc) rel;

  // If there's a multiset, let FarragoMultisetSplitter work on it
  // first.
  final RexProgram program = calc.getProgram();
  if (RexMultisetUtil.containsMultiset(program)
      || program.containsAggs()) {
    return null;
  }

  return new SparkCalc(
      rel.getCluster(),
      rel.getTraitSet().replace(SparkRel.CONVENTION),
      convert(calc.getInput(),
          calc.getInput().getTraitSet().replace(SparkRel.CONVENTION)),
      program);
}
 
Example #3
Source File: ElasticsearchCalcRule.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
        final LogicalCalc calc = (LogicalCalc) rel;
        final RelTraitSet traitSet = calc.getTraitSet().replace(getOutTrait());
//        return new ElasticsearchFilter(rel.getCluster(), traitSet,
//                convert(calc.getInput(), getOutTrait()),
//                calc.getCondition());
        return null;
    }
 
Example #4
Source File: ReduceDecimalsRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalCalc calc = call.rel(0);

  // Expand decimals in every expression in this program. If no
  // expression changes, don't apply the rule.
  final RexProgram program = calc.getProgram();
  if (!RexUtil.requiresDecimalExpansion(program, true)) {
    return;
  }

  final RexBuilder rexBuilder = calc.getCluster().getRexBuilder();
  final RexShuttle shuttle = new DecimalShuttle(rexBuilder);
  RexProgramBuilder programBuilder =
      RexProgramBuilder.create(
          rexBuilder,
          calc.getInput().getRowType(),
          program.getExprList(),
          program.getProjectList(),
          program.getCondition(),
          program.getOutputRowType(),
          shuttle,
          true);

  final RexProgram newProgram = programBuilder.getProgram();
  LogicalCalc newCalc = LogicalCalc.create(calc.getInput(), newProgram);
  call.transformTo(newCalc);
}
 
Example #5
Source File: FilterCalcMergeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final LogicalFilter filter = call.rel(0);
  final LogicalCalc calc = call.rel(1);

  // Don't merge a filter onto a calc which contains windowed aggregates.
  // That would effectively be pushing a multiset down through a filter.
  // We'll have chance to merge later, when the over is expanded.
  if (calc.getProgram().containsAggs()) {
    return;
  }

  // Create a program containing the filter.
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final RexProgramBuilder progBuilder =
      new RexProgramBuilder(
          calc.getRowType(),
          rexBuilder);
  progBuilder.addIdentity();
  progBuilder.addCondition(filter.getCondition());
  RexProgram topProgram = progBuilder.getProgram();
  RexProgram bottomProgram = calc.getProgram();

  // Merge the programs together.
  RexProgram mergedProgram =
      RexProgramBuilder.mergePrograms(
          topProgram,
          bottomProgram,
          rexBuilder);
  final LogicalCalc newCalc =
      LogicalCalc.create(calc.getInput(), mergedProgram);
  call.transformTo(newCalc);
}
 
Example #6
Source File: FilterCalcMergeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FilterCalcMergeRule.
 *
 * @param relBuilderFactory Builder for relational expressions
 */
public FilterCalcMergeRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(
          Filter.class,
          operand(LogicalCalc.class, any())),
      relBuilderFactory, null);
}
 
Example #7
Source File: CalcRemoveRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalCalc calc = call.rel(0);
  RexProgram program = calc.getProgram();
  if (!program.isTrivial()) {
    return;
  }
  RelNode input = calc.getInput();
  input = call.getPlanner().register(input, calc);
  call.transformTo(
      convert(
          input,
          calc.getTraitSet()));
}
 
Example #8
Source File: CalcRelSplitter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether a relational expression can be implemented solely in a
 * given {@link RelType}.
 *
 * @param rel         Calculation relational expression
 * @param relTypeName Name of a {@link RelType}
 * @return Whether relational expression can be implemented
 */
protected boolean canImplement(LogicalCalc rel, String relTypeName) {
  for (RelType relType : relTypes) {
    if (relType.name.equals(relTypeName)) {
      return relType.canImplement(rel.getProgram());
    }
  }
  throw new AssertionError("unknown type " + relTypeName);
}
 
Example #9
Source File: ProjectToCalcRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final LogicalProject project = call.rel(0);
  final RelNode input = project.getInput();
  final RexProgram program =
      RexProgram.create(
          input.getRowType(),
          project.getProjects(),
          null,
          project.getRowType(),
          project.getCluster().getRexBuilder());
  final LogicalCalc calc = LogicalCalc.create(input, program);
  call.transformTo(calc);
}
 
Example #10
Source File: ProjectCalcMergeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a ProjectCalcMergeRule.
 *
 * @param relBuilderFactory Builder for relational expressions
 */
public ProjectCalcMergeRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(
          LogicalProject.class,
          operand(LogicalCalc.class, any())),
      relBuilderFactory, null);
}
 
Example #11
Source File: SparkRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
private SparkCalcRule() {
  super(
      LogicalCalc.class,
      Convention.NONE,
      SparkRel.CONVENTION,
      "SparkCalcRule");
}
 
Example #12
Source File: ElasticsearchCalcRule.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
        final LogicalCalc calc = (LogicalCalc) rel;
        final RelTraitSet traitSet = calc.getTraitSet().replace(getOutTrait());
//        return new ElasticsearchFilter(rel.getCluster(), traitSet,
//                convert(calc.getInput(), getOutTrait()),
//                calc.getCondition());
        return null;
    }
 
Example #13
Source File: EnumerableCalcRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
EnumerableCalcRule() {
  // The predicate ensures that if there's a multiset, FarragoMultisetSplitter
  // will work on it first.
  super(LogicalCalc.class,
      (Predicate<Calc>) RelOptUtil::notContainsWindowedAgg,
      Convention.NONE, EnumerableConvention.INSTANCE,
      RelFactories.LOGICAL_BUILDER, "EnumerableCalcRule");
}
 
Example #14
Source File: EnumerableCalcRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalCalc calc = (LogicalCalc) rel;
  final RelNode input = calc.getInput();
  return EnumerableCalc.create(
      convert(input,
          input.getTraitSet().replace(EnumerableConvention.INSTANCE)),
      calc.getProgram());
}
 
Example #15
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void rewriteRel(LogicalCalc rel) {
    // Translate the child.
    final RelNode newInput = getNewForOldRel(rel.getInput());

    final RelOptCluster cluster = rel.getCluster();
    RexProgramBuilder programBuilder = new RexProgramBuilder(newInput.getRowType(), cluster.getRexBuilder());

    // Convert the common expressions.
    final RexProgram program = rel.getProgram();
    final RewriteRexShuttle shuttle = new RewriteRexShuttle();
    for (RexNode expr : program.getExprList()) {
        programBuilder.registerInput(expr.accept(shuttle));
    }

    // Convert the projections.
    final List<Pair<RexNode, String>> flattenedExpList = new ArrayList<>();
    List<String> fieldNames = rel.getRowType().getFieldNames();
    flattenProjections(new RewriteRexShuttle(), program.getProjectList(), fieldNames, "", flattenedExpList);

    // Register each of the new projections.
    for (Pair<RexNode, String> flattenedExp : flattenedExpList) {
        programBuilder.addProject(flattenedExp.left, flattenedExp.right);
    }

    // Translate the condition.
    final RexLocalRef conditionRef = program.getCondition();
    if (conditionRef != null) {
        final Ord<RelDataType> newField = getNewFieldForOldInput(conditionRef.getIndex());
        programBuilder.addCondition(RexBuilder.getRexFactory().makeInputRef(newField.i, newField.e));
    }

    RexProgram newProgram = programBuilder.getProgram();

    // Create a new calc relational expression.
    LogicalCalc newRel = LogicalCalc.create(newInput, newProgram);
    setNewForOldRel(rel, newRel);
}
 
Example #16
Source File: CalcRemoveRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalCalc calc = call.rel(0);
  RexProgram program = calc.getProgram();
  if (!program.isTrivial()) {
    return;
  }
  RelNode input = calc.getInput();
  input = call.getPlanner().register(input, calc);
  call.transformTo(
      convert(
          input,
          calc.getTraitSet()));
}
 
Example #17
Source File: RelMetadataTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Nonnull
private Function<String, RelNode> convertProjectAsCalc() {
  return s -> {
    Project project = (Project) convertSql(s);
    RexProgram program = RexProgram.create(
        project.getInput().getRowType(),
        project.getProjects(),
        null,
        project.getRowType(),
        project.getCluster().getRexBuilder());
    return LogicalCalc.create(project.getInput(), program);
  };
}
 
Example #18
Source File: FilterCalcMergeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final LogicalFilter filter = call.rel(0);
  final LogicalCalc calc = call.rel(1);

  // Don't merge a filter onto a calc which contains windowed aggregates.
  // That would effectively be pushing a multiset down through a filter.
  // We'll have chance to merge later, when the over is expanded.
  if (calc.getProgram().containsAggs()) {
    return;
  }

  // Create a program containing the filter.
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final RexProgramBuilder progBuilder =
      new RexProgramBuilder(
          calc.getRowType(),
          rexBuilder);
  progBuilder.addIdentity();
  progBuilder.addCondition(filter.getCondition());
  RexProgram topProgram = progBuilder.getProgram();
  RexProgram bottomProgram = calc.getProgram();

  // Merge the programs together.
  RexProgram mergedProgram =
      RexProgramBuilder.mergePrograms(
          topProgram,
          bottomProgram,
          rexBuilder);
  final LogicalCalc newCalc =
      LogicalCalc.create(calc.getInput(), mergedProgram);
  call.transformTo(newCalc);
}
 
Example #19
Source File: FilterCalcMergeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a FilterCalcMergeRule.
 *
 * @param relBuilderFactory Builder for relational expressions
 */
public FilterCalcMergeRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(
          Filter.class,
          operand(LogicalCalc.class, any())),
      relBuilderFactory, null);
}
 
Example #20
Source File: RelWriterTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCalc() {
  final FrameworkConfig config = RelBuilderTest.config().build();
  final RelBuilder builder = RelBuilder.create(config);
  final RexBuilder rexBuilder = builder.getRexBuilder();
  final LogicalTableScan scan = (LogicalTableScan) builder.scan("EMP").build();
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(scan.getRowType(), rexBuilder);
  final RelDataTypeField field = scan.getRowType().getField("SAL", false, false);
  programBuilder.addIdentity();
  programBuilder.addCondition(
      rexBuilder.makeCall(SqlStdOperatorTable.GREATER_THAN,
          new RexInputRef(field.getIndex(), field.getType()),
          builder.literal(10)));
  final LogicalCalc calc = LogicalCalc.create(scan, programBuilder.getProgram());
  String relJson = RelOptUtil.dumpPlan("", calc,
      SqlExplainFormat.JSON, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
  String s =
      Frameworks.withPlanner((cluster, relOptSchema, rootSchema) -> {
        final RelJsonReader reader = new RelJsonReader(
            cluster, getSchema(calc), rootSchema);
        RelNode node;
        try {
          node = reader.read(relJson);
        } catch (IOException e) {
          throw TestUtil.rethrow(e);
        }
        return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT,
            SqlExplainLevel.EXPPLAN_ATTRIBUTES);
      });
  final String expected =
      "LogicalCalc(expr#0..7=[{inputs}], expr#8=[10], expr#9=[>($t5, $t8)],"
          + " proj#0..7=[{exprs}], $condition=[$t9])\n"
          + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(s, isLinux(expected));
}
 
Example #21
Source File: CalcRelSplitter.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether a relational expression can be implemented solely in a
 * given {@link RelType}.
 *
 * @param rel         Calculation relational expression
 * @param relTypeName Name of a {@link RelType}
 * @return Whether relational expression can be implemented
 */
protected boolean canImplement(LogicalCalc rel, String relTypeName) {
    for (RelType relType : relTypes) {
        if (relType.name.equals(relTypeName)) {
            return relType.canImplement(rel.getProgram());
        }
    }
    throw new AssertionError("unknown type " + relTypeName);
}
 
Example #22
Source File: ProjectToCalcRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final LogicalProject project = call.rel(0);
  final RelNode input = project.getInput();
  final RexProgram program =
      RexProgram.create(
          input.getRowType(),
          project.getProjects(),
          null,
          project.getRowType(),
          project.getCluster().getRexBuilder());
  final LogicalCalc calc = LogicalCalc.create(input, program);
  call.transformTo(calc);
}
 
Example #23
Source File: ProjectCalcMergeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a ProjectCalcMergeRule.
 *
 * @param relBuilderFactory Builder for relational expressions
 */
public ProjectCalcMergeRule(RelBuilderFactory relBuilderFactory) {
  super(
      operand(
          LogicalProject.class,
          operand(LogicalCalc.class, any())),
      relBuilderFactory, null);
}
 
Example #24
Source File: ProjectCalcMergeRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final LogicalProject project = call.rel(0);
  final LogicalCalc calc = call.rel(1);

  // Don't merge a project which contains windowed aggregates onto a
  // calc. That would effectively be pushing a windowed aggregate down
  // through a filter. Transform the project into an identical calc,
  // which we'll have chance to merge later, after the over is
  // expanded.
  final RelOptCluster cluster = project.getCluster();
  RexProgram program =
      RexProgram.create(
          calc.getRowType(),
          project.getProjects(),
          null,
          project.getRowType(),
          cluster.getRexBuilder());
  if (RexOver.containsOver(program)) {
    LogicalCalc projectAsCalc = LogicalCalc.create(calc, program);
    call.transformTo(projectAsCalc);
    return;
  }

  // Create a program containing the project node's expressions.
  final RexBuilder rexBuilder = cluster.getRexBuilder();
  final RexProgramBuilder progBuilder =
      new RexProgramBuilder(
          calc.getRowType(),
          rexBuilder);
  for (Pair<RexNode, String> field : project.getNamedProjects()) {
    progBuilder.addProject(field.left, field.right);
  }
  RexProgram topProgram = progBuilder.getProgram();
  RexProgram bottomProgram = calc.getProgram();

  // Merge the programs together.
  RexProgram mergedProgram =
      RexProgramBuilder.mergePrograms(
          topProgram,
          bottomProgram,
          rexBuilder);
  final LogicalCalc newCalc =
      LogicalCalc.create(calc.getInput(), mergedProgram);
  call.transformTo(newCalc);
}
 
Example #25
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void rewriteRel(LogicalCalc rel) {
  // Translate the child.
  final RelNode newInput = getNewForOldRel(rel.getInput());

  final RelOptCluster cluster = rel.getCluster();
  RexProgramBuilder programBuilder =
      new RexProgramBuilder(
          newInput.getRowType(),
          cluster.getRexBuilder());

  // Convert the common expressions.
  final RexProgram program = rel.getProgram();
  final RewriteRexShuttle shuttle = new RewriteRexShuttle();
  for (RexNode expr : program.getExprList()) {
    programBuilder.registerInput(expr.accept(shuttle));
  }

  // Convert the projections.
  final List<Pair<RexNode, String>> flattenedExpList = new ArrayList<>();
  List<String> fieldNames = rel.getRowType().getFieldNames();
  flattenProjections(new RewriteRexShuttle(),
      program.getProjectList(),
      fieldNames,
      "",
      flattenedExpList);

  // Register each of the new projections.
  for (Pair<RexNode, String> flattenedExp : flattenedExpList) {
    programBuilder.addProject(flattenedExp.left, flattenedExp.right);
  }

  // Translate the condition.
  final RexLocalRef conditionRef = program.getCondition();
  if (conditionRef != null) {
    final Ord<RelDataType> newField =
        getNewFieldForOldInput(conditionRef.getIndex());
    programBuilder.addCondition(new RexLocalRef(newField.i, newField.e));
  }

  RexProgram newProgram = programBuilder.getProgram();

  // Create a new calc relational expression.
  LogicalCalc newRel = LogicalCalc.create(newInput, newProgram);
  setNewForOldRel(rel, newRel);
}
 
Example #26
Source File: CalciteMaterializer.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode visit(LogicalCalc calc) {
  return calc;
}
 
Example #27
Source File: RelShuttleImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode visit(LogicalCalc calc) {
  return visitChildren(calc);
}
 
Example #28
Source File: ReduceDecimalsRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a ReduceDecimalsRule.
 */
public ReduceDecimalsRule(RelBuilderFactory relBuilderFactory) {
  super(operand(LogicalCalc.class, any()), relBuilderFactory, null);
}
 
Example #29
Source File: CalcRelSplitter.java    From calcite with Apache License 2.0 4 votes vote down vote up
protected RelNode makeRel(RelOptCluster cluster,
    RelTraitSet traitSet, RelBuilder relBuilder, RelNode input,
    RexProgram program) {
  return LogicalCalc.create(input, program);
}
 
Example #30
Source File: ProjectCalcMergeRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final LogicalProject project = call.rel(0);
  final LogicalCalc calc = call.rel(1);

  // Don't merge a project which contains windowed aggregates onto a
  // calc. That would effectively be pushing a windowed aggregate down
  // through a filter. Transform the project into an identical calc,
  // which we'll have chance to merge later, after the over is
  // expanded.
  final RelOptCluster cluster = project.getCluster();
  RexProgram program =
      RexProgram.create(
          calc.getRowType(),
          project.getProjects(),
          null,
          project.getRowType(),
          cluster.getRexBuilder());
  if (RexOver.containsOver(program)) {
    LogicalCalc projectAsCalc = LogicalCalc.create(calc, program);
    call.transformTo(projectAsCalc);
    return;
  }

  // Create a program containing the project node's expressions.
  final RexBuilder rexBuilder = cluster.getRexBuilder();
  final RexProgramBuilder progBuilder =
      new RexProgramBuilder(
          calc.getRowType(),
          rexBuilder);
  for (Pair<RexNode, String> field : project.getNamedProjects()) {
    progBuilder.addProject(field.left, field.right);
  }
  RexProgram topProgram = progBuilder.getProgram();
  RexProgram bottomProgram = calc.getProgram();

  // Merge the programs together.
  RexProgram mergedProgram =
      RexProgramBuilder.mergePrograms(
          topProgram,
          bottomProgram,
          rexBuilder);
  final LogicalCalc newCalc =
      LogicalCalc.create(calc.getInput(), mergedProgram);
  call.transformTo(newCalc);
}