org.apache.calcite.rex.RexProgram Java Examples

The following examples show how to use org.apache.calcite.rex.RexProgram. 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: RelToSqlConverter.java    From quark with Apache License 2.0 6 votes vote down vote up
public Result visitCalc(Calc e) {
  Result x = visitChild(0, e.getInput());
  final RexProgram program = e.getProgram();
  Builder builder =
      program.getCondition() != null
          ? x.builder(e, Clause.WHERE)
          : x.builder(e);
  if (!isStar(program)) {
    final List<SqlNode> selectList = new ArrayList<>();
    for (RexLocalRef ref : program.getProjectList()) {
      SqlNode sqlExpr = builder.context.toSql(program, ref);
      addSelect(selectList, sqlExpr, e.getRowType());
    }
    builder.setSelect(new SqlNodeList(selectList, POS));
  }

  if (program.getCondition() != null) {
    builder.setWhere(
        builder.context.toSql(program, program.getCondition()));
  }
  return builder.result();
}
 
Example #2
Source File: FilterMergeCrule.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Filter topFilter = call.rel(0);
  final Filter bottomFilter = call.rel(1);

  // use RexPrograms to merge the two FilterRels into a single program
  // so we can convert the two LogicalFilter conditions to directly
  // reference the bottom LogicalFilter's child
  RexBuilder rexBuilder = topFilter.getCluster().getRexBuilder();
  RexProgram bottomProgram = createProgram(bottomFilter);
  RexProgram topProgram = createProgram(topFilter);

  RexProgram mergedProgram = RexProgramBuilder.mergePrograms(topProgram, bottomProgram, rexBuilder);

  RexNode newCondition = mergedProgram.expandLocalRef(mergedProgram.getCondition());

  final RelBuilder relBuilder = call.builder();
  relBuilder.push(bottomFilter.getInput()).filter(newCondition);

  call.transformTo(relBuilder.build());
}
 
Example #3
Source File: RexToLixTranslator.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Translates a {@link RexProgram} to a sequence of expressions and
 * declarations.
 *
 * @param program Program to be translated
 * @param typeFactory Type factory
 * @param conformance SQL conformance
 * @param list List of statements, populated with declarations
 * @param outputPhysType Output type, or null
 * @param root Root expression
 * @param inputGetter Generates expressions for inputs
 * @param correlates Provider of references to the values of correlated
 *                   variables
 * @return Sequence of expressions, optional condition
 */
public static List<Expression> translateProjects(RexProgram program,
    JavaTypeFactory typeFactory, SqlConformance conformance,
    BlockBuilder list, PhysType outputPhysType, Expression root,
    InputGetter inputGetter, Function1<String, InputGetter> correlates) {
  List<Type> storageTypes = null;
  if (outputPhysType != null) {
    final RelDataType rowType = outputPhysType.getRowType();
    storageTypes = new ArrayList<>(rowType.getFieldCount());
    for (int i = 0; i < rowType.getFieldCount(); i++) {
      storageTypes.add(outputPhysType.getJavaFieldType(i));
    }
  }
  return new RexToLixTranslator(program, typeFactory, root, inputGetter,
      list, new RexBuilder(typeFactory), conformance, null)
      .setCorrelates(correlates)
      .translateList(program.getProjectList(), storageTypes);
}
 
Example #4
Source File: RelToSqlConverter.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** @see #dispatch */
public Result visit(Calc e) {
  Result x = visitChild(0, e.getInput());
  parseCorrelTable(e, x);
  final RexProgram program = e.getProgram();
  Builder builder =
      program.getCondition() != null
          ? x.builder(e, Clause.WHERE)
          : x.builder(e);
  if (!isStar(program)) {
    final List<SqlNode> selectList = new ArrayList<>(program.getProjectList().size());
    for (RexLocalRef ref : program.getProjectList()) {
      SqlNode sqlExpr = builder.context.toSql(program, ref);
      addSelect(selectList, sqlExpr, e.getRowType());
    }
    builder.setSelect(new SqlNodeList(selectList, POS));
  }

  if (program.getCondition() != null) {
    builder.setWhere(
        builder.context.toSql(program, program.getCondition()));
  }
  return builder.result();
}
 
Example #5
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
protected SqlNode toSql(RexProgram program, RexFieldCollation rfc) {
  SqlNode node = toSql(program, rfc.left);
  switch (rfc.getDirection()) {
    case DESCENDING:
    case STRICTLY_DESCENDING:
      node = SqlStdOperatorTable.DESC.createCall(POS, node);
  }
  if (rfc.getNullDirection()
    != dialect.defaultNullDirection(rfc.getDirection())) {
    switch (rfc.getNullDirection()) {
      case FIRST:
        node = SqlStdOperatorTable.NULLS_FIRST.createCall(POS, node);
        break;
      case LAST:
        node = SqlStdOperatorTable.NULLS_LAST.createCall(POS, node);
        break;
    }
  }
  return node;
}
 
Example #6
Source File: LogicalCalc.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalCalc. */
public LogicalCalc(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    List<RelHint> hints,
    RelNode child,
    RexProgram program) {
  super(cluster, traitSet, hints, child, program);
}
 
Example #7
Source File: Calc.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
protected Calc(
    RelOptCluster cluster,
    RelTraitSet traits,
    RelNode child,
    RexProgram program) {
  this(cluster, traits, ImmutableList.of(), child, program);
}
 
Example #8
Source File: PythonCorrelateSplitRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private FlinkLogicalCalc createTopCalc(
	int primitiveLeftFieldCount,
	RexBuilder rexBuilder,
	ArrayBuffer<RexNode> extractedRexNodes,
	RelDataType calcRowType,
	FlinkLogicalCorrelate newCorrelate) {
	RexProgram rexProgram = new RexProgramBuilder(newCorrelate.getRowType(), rexBuilder).getProgram();
	int offset = extractedRexNodes.size() + primitiveLeftFieldCount;

	// extract correlate output RexNode.
	List<RexNode> newTopCalcProjects = rexProgram
		.getExprList()
		.stream()
		.filter(x -> x instanceof RexInputRef)
		.filter(x -> {
			int index = ((RexInputRef) x).getIndex();
			return index < primitiveLeftFieldCount || index >= offset;
		})
		.collect(Collectors.toList());

	return new FlinkLogicalCalc(
		newCorrelate.getCluster(),
		newCorrelate.getTraitSet(),
		newCorrelate,
		RexProgram.create(
			newCorrelate.getRowType(),
			newTopCalcProjects,
			null,
			calcRowType,
			rexBuilder));
}
 
Example #9
Source File: LogicalCalc.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static LogicalCalc create(final RelNode input,
    final RexProgram program) {
  final RelOptCluster cluster = input.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet = cluster.traitSet()
      .replace(Convention.NONE)
      .replaceIfs(RelCollationTraitDef.INSTANCE,
          () -> RelMdCollation.calc(mq, input, program))
      .replaceIf(RelDistributionTraitDef.INSTANCE,
          () -> RelMdDistribution.calc(mq, input, program));
  return new LogicalCalc(cluster, traitSet, ImmutableList.of(), input, program);
}
 
Example #10
Source File: SqlImplementor.java    From calcite with Apache License 2.0 5 votes vote down vote up
private List<SqlNode> toSql(RexProgram program, List<RexNode> operandList) {
  final List<SqlNode> list = new ArrayList<>();
  for (RexNode rex : operandList) {
    list.add(toSql(program, rex));
  }
  return list;
}
 
Example #11
Source File: LogicalCalc.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a LogicalCalc by parsing serialized output.
 */
public LogicalCalc(RelInput input) {
  this(input.getCluster(),
      input.getTraitSet(),
      ImmutableList.of(),
      input.getInput(),
      RexProgram.create(input));
}
 
Example #12
Source File: LogicalCalc.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public LogicalCalc(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    RelNode child,
    RexProgram program) {
  this(cluster, traitSet, ImmutableList.of(), child, program);
}
 
Example #13
Source File: EnumerableProjectToCalcRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final EnumerableProject 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 EnumerableCalc calc = EnumerableCalc.create(input, program);
  call.transformTo(calc);
}
 
Example #14
Source File: Calc.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a Calc.
 *
 * @param cluster Cluster
 * @param traits Traits
 * @param hints Hints of this relational expression
 * @param child Input relation
 * @param program Calc program
 */
protected Calc(
    RelOptCluster cluster,
    RelTraitSet traits,
    List<RelHint> hints,
    RelNode child,
    RexProgram program) {
  super(cluster, traits, child);
  this.rowType = program.getOutputRowType();
  this.program = program;
  this.hints = ImmutableList.copyOf(hints);
  assert isValid(Litmus.THROW, null);
}
 
Example #15
Source File: SparkRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SparkCalc(RelOptCluster cluster,
    RelTraitSet traitSet,
    RelNode input,
    RexProgram program) {
  super(cluster, traitSet, input);
  assert getConvention() == SparkRel.CONVENTION;
  assert !program.containsAggs();
  this.program = program;
  this.rowType = program.getOutputRowType();
}
 
Example #16
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 #17
Source File: OLAPFilterRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    // keep it for having clause
    RexBuilder rexBuilder = getCluster().getRexBuilder();
    RelDataType inputRowType = getInput().getRowType();
    RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
    programBuilder.addIdentity();
    programBuilder.addCondition(this.condition);
    RexProgram program = programBuilder.getProgram();

    return new EnumerableCalc(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
            sole(inputs), program);
}
 
Example #18
Source File: RelMdUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static double estimateFilteredRows(RelNode child, RexProgram program,
    RelMetadataQuery mq) {
  // convert the program's RexLocalRef condition to an expanded RexNode
  RexLocalRef programCondition = program.getCondition();
  RexNode condition;
  if (programCondition == null) {
    condition = null;
  } else {
    condition = program.expandLocalRef(programCondition);
  }
  return estimateFilteredRows(child, condition, mq);
}
 
Example #19
Source File: JoinCalcTransposeRule.java    From quark with Apache License 2.0 5 votes vote down vote up
private static boolean isStar(RexProgram program) {
  int i = 0;

  for (RexLocalRef ref : program.getProjectList()) {
    if (ref.getIndex() != i++) {
      return false;
    }
  }
  return i == program.getInputRowType().getFieldCount();
}
 
Example #20
Source File: EnumerableCalc.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates an EnumerableCalc. */
public static EnumerableCalc create(final RelNode input,
    final RexProgram program) {
  final RelOptCluster cluster = input.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet = cluster.traitSet()
      .replace(EnumerableConvention.INSTANCE)
      .replaceIfs(RelCollationTraitDef.INSTANCE,
          () -> RelMdCollation.calc(mq, input, program))
      .replaceIf(RelDistributionTraitDef.INSTANCE,
          () -> RelMdDistribution.calc(mq, input, program));
  return new EnumerableCalc(cluster, traitSet, input, program);
}
 
Example #21
Source File: ExpressionCompiler.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
/**
 * Create quasi-Java expression from given {@link RexNode}
 *
 * @param node Expression in the form of {@link RexNode}
 * @param inputRowType Input Data type to expression in the form of {@link RelDataType}
 * @param outputRowType Output data type of expression in the form of {@link RelDataType}
 *
 * @return Returns quasi-Java expression
 */
public String getExpression(RexNode node, RelDataType inputRowType, RelDataType outputRowType)
{
  final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
  programBuilder.addProject(node, null);
  final RexProgram program = programBuilder.getProgram();

  final BlockBuilder builder = new BlockBuilder();
  final JavaTypeFactory javaTypeFactory = (JavaTypeFactory)rexBuilder.getTypeFactory();

  final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList
      .of(Pair.<Expression, PhysType>of(Expressions.variable(Object[].class, "inputValues"),
      PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
  final Function1<String, RexToLixTranslator.InputGetter> correlates =
      new Function1<String, RexToLixTranslator.InputGetter>()
    {
      public RexToLixTranslator.InputGetter apply(String a0)
      {
        throw new UnsupportedOperationException();
      }
    };

  final List<Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, builder,
      PhysTypeImpl.of(javaTypeFactory, outputRowType, JavaRowFormat.ARRAY, false), null, inputGetter, correlates);

  for (int i = 0; i < list.size(); i++) {
    Statement statement = Expressions.statement(list.get(i));
    builder.add(statement);
  }

  return finalizeExpression(builder.toBlock(), inputRowType);
}
 
Example #22
Source File: EnumerableCalc.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public EnumerableCalc(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    RelNode input,
    RexProgram program,
    List<RelCollation> collationList) {
  this(cluster, traitSet, input, program);
  Util.discard(collationList);
}
 
Example #23
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 #24
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public SqlNode toSql(RexProgram program, RexNode rex) {
  if (rex.getKind() == SqlKind.LITERAL) {
    final RexLiteral literal = (RexLiteral) rex;
    if (literal.getTypeName().getFamily() == SqlTypeFamily.CHARACTER) {
      return new SqlIdentifier(RexLiteral.stringValue(literal), POS);
    }
  }
  return super.toSql(program, rex);
}
 
Example #25
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
protected List<SqlNode> toSql(RexProgram program, List<RexNode> operandList) {
  final List<SqlNode> list = new ArrayList<>();
  for (RexNode rex : operandList) {
    list.add(toSql(program, rex));
  }
  return list;
}
 
Example #26
Source File: RelMdCollation.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Helper method to determine a
 * {@link org.apache.calcite.rel.core.Calc}'s collation. */
public static List<RelCollation> calc(RelMetadataQuery mq, RelNode input,
    RexProgram program) {
  final List<RexNode> projects =
      program
          .getProjectList()
          .stream()
          .map(program::expandLocalRef)
          .collect(Collectors.toList());
  return project(mq, input, projects);
}
 
Example #27
Source File: SqlImplementor.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static boolean isStar(RexProgram program) {
  int i = 0;
  for (RexLocalRef ref : program.getProjectList()) {
    if (ref.getIndex() != i++) {
      return false;
    }
  }
  return i == program.getInputRowType().getFieldCount();
}
 
Example #28
Source File: SubstitutionVisitor.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RexShuttle getExpandShuttle(RexProgram rexProgram) {
  return new RexShuttle() {
    @Override public RexNode visitLocalRef(RexLocalRef localRef) {
      return rexProgram.expandLocalRef(localRef);
    }
  };
}
 
Example #29
Source File: CopyWithCluster.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public RexProgram copyOf(RexProgram program) {
  return new RexProgram(
    copyOf(program.getInputRowType()),
    copyRexNodes(program.getExprList()),
    Lists.transform(program.getProjectList(), COPY_REX_LOCAL_REF),
    (RexLocalRef) copyOf(program.getCondition()),
    copyOf(program.getOutputRowType())
  );
}
 
Example #30
Source File: OLAPFilterRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public EnumerableRel implementEnumerable(List<EnumerableRel> inputs) {
    // keep it for having clause
    RexBuilder rexBuilder = getCluster().getRexBuilder();
    RelDataType inputRowType = getInput().getRowType();
    RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
    programBuilder.addIdentity();
    programBuilder.addCondition(this.condition);
    RexProgram program = programBuilder.getProgram();

    return new EnumerableCalc(getCluster(), getCluster().traitSetOf(EnumerableConvention.INSTANCE), //
            sole(inputs), program);
}