Java Code Examples for org.apache.calcite.rex.RexProgram#getCondition()

The following examples show how to use org.apache.calcite.rex.RexProgram#getCondition() . 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 Bats 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<>();
    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: RelToSqlConverter.java    From dremio-oss 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<>();
    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 3
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 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: RelMdUtil.java    From Bats 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 6
Source File: CalcRelSplitter.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether this tester's <code>RelType</code> can implement a
 * given program.
 *
 * @param program Program
 * @return Whether this tester's <code>RelType</code> can implement a
 * given program.
 */
public boolean canImplement(RexProgram program) {
    if ((program.getCondition() != null) && !canImplement(program.getCondition(), true)) {
        return false;
    }
    for (RexNode expr : program.getExprList()) {
        if (!canImplement(expr, false)) {
            return false;
        }
    }
    return true;
}
 
Example 7
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 8
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 9
Source File: RelMdSelectivity.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double getSelectivity(Calc rel, RelMetadataQuery mq, RexNode predicate) {
  final RexProgram rexProgram = rel.getProgram();
  final RexLocalRef programCondition = rexProgram.getCondition();
  if (programCondition == null) {
    return getSelectivity(rel.getInput(), mq, predicate);
  } else {
    return mq.getSelectivity(rel.getInput(),
        RelMdUtil.minusPreds(
            rel.getCluster().getRexBuilder(),
            predicate,
            rexProgram.expandLocalRef(programCondition)));
  }
}
 
Example 10
Source File: CalcRelSplitter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether this tester's <code>RelType</code> can implement a
 * given program.
 *
 * @param program Program
 * @return Whether this tester's <code>RelType</code> can implement a
 * given program.
 */
public boolean canImplement(RexProgram program) {
  if ((program.getCondition() != null)
      && !canImplement(program.getCondition(), true)) {
    return false;
  }
  for (RexNode expr : program.getExprList()) {
    if (!canImplement(expr, false)) {
      return false;
    }
  }
  return true;
}
 
Example 11
Source File: RexToLixTranslator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static Expression translateCondition(RexProgram program,
    JavaTypeFactory typeFactory, BlockBuilder list, InputGetter inputGetter,
    Function1<String, InputGetter> correlates, SqlConformance conformance) {
  if (program.getCondition() == null) {
    return RexImpTable.TRUE_EXPR;
  }
  final ParameterExpression root = DataContext.ROOT;
  RexToLixTranslator translator =
      new RexToLixTranslator(program, typeFactory, root, inputGetter, list,
          new RexBuilder(typeFactory), conformance, null);
  translator = translator.setCorrelates(correlates);
  return translator.translate(
      program.getCondition(),
      RexImpTable.NullAs.FALSE);
}
 
Example 12
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);
}