org.apache.calcite.rex.RexLocalRef Java Examples

The following examples show how to use org.apache.calcite.rex.RexLocalRef. 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: LogicalWindow.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static void addWindows(Multimap<WindowKey, RexOver> windowMap, RexOver over, final int inputFieldCount) {
    final RexWindow aggWindow = over.getWindow();

    // Look up or create a window.
    RelCollation orderKeys = getCollation(Lists.newArrayList(Util.filter(aggWindow.orderKeys, rexFieldCollation ->
    // If ORDER BY references constant (i.e. RexInputRef),
    // then we can ignore such ORDER BY key.
    rexFieldCollation.left instanceof RexLocalRef)));
    ImmutableBitSet groupSet = ImmutableBitSet.of(getProjectOrdinals(aggWindow.partitionKeys));
    final int groupLength = groupSet.length();
    if (inputFieldCount < groupLength) {
        // If PARTITION BY references constant, we can ignore such partition key.
        // All the inputs after inputFieldCount are literals, thus we can clear.
        groupSet = groupSet.except(ImmutableBitSet.range(inputFieldCount, groupLength));
    }

    WindowKey windowKey = new WindowKey(groupSet, orderKeys, aggWindow.isRows(), aggWindow.getLowerBound(),
            aggWindow.getUpperBound());
    windowMap.put(windowKey, over);
}
 
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: RelMdColumnOrigins.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Set<RelColumnOrigin> getColumnOrigins(Calc rel,
    final RelMetadataQuery mq, int iOutputColumn) {
  final RelNode input = rel.getInput();
  final RexShuttle rexShuttle = new RexShuttle() {
    @Override public RexNode visitLocalRef(RexLocalRef localRef) {
      return rel.getProgram().expandLocalRef(localRef);
    }
  };
  final List<RexNode> projects = new ArrayList<>();
  for (RexNode rex: rexShuttle.apply(rel.getProgram().getProjectList())) {
    projects.add(rex);
  }
  final RexNode rexNode = projects.get(iOutputColumn);
  if (rexNode instanceof RexInputRef) {
    // Direct reference:  no derivation added.
    RexInputRef inputRef = (RexInputRef) rexNode;
    return mq.getColumnOrigins(input, inputRef.getIndex());
  }
  // Anything else is a derivation, possibly from multiple columns.
  final Set<RelColumnOrigin> set = getMultipleColumns(rexNode, input, mq);
  return createDerivedColumnOrigins(set);
}
 
Example #5
Source File: CalcRelSplitter.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RexNode visitLocalRef(RexLocalRef local) {
  // A reference to a local variable becomes a reference to an input
  // if the local was computed at a previous level.
  final int localIndex = local.getIndex();
  final int exprLevel = exprLevels[localIndex];
  if (exprLevel < level) {
    if (allExprs[localIndex] instanceof RexLiteral) {
      // Expression is to be inlined. Use the original expression.
      return allExprs[localIndex];
    }
    int inputIndex = indexOf(localIndex, inputExprOrdinals);
    assert inputIndex >= 0;
    return new RexLocalRef(
        inputIndex,
        local.getType());
  } else {
    // It's a reference to what was a local expression at the
    // previous level, and was then projected.
    final int exprIndex = exprInverseOrdinals[localIndex];
    return new RexLocalRef(
        exprIndex,
        local.getType());
  }
}
 
Example #6
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 #7
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 #8
Source File: Window.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static RelCollation getCollation(
    final List<RexFieldCollation> collations) {
  return RelCollations.of(
      new AbstractList<RelFieldCollation>() {
        public RelFieldCollation get(int index) {
          final RexFieldCollation collation = collations.get(index);
          return new RelFieldCollation(
              ((RexLocalRef) collation.left).getIndex(),
              collation.getDirection(),
              collation.getNullDirection());
        }

        public int size() {
          return collations.size();
        }
      });
}
 
Example #9
Source File: CalcRelSplitter.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RexNode visitLocalRef(RexLocalRef local) {
    // A reference to a local variable becomes a reference to an input
    // if the local was computed at a previous level.
    final int localIndex = local.getIndex();
    final int exprLevel = exprLevels[localIndex];
    if (exprLevel < level) {
        if (allExprs[localIndex] instanceof RexLiteral) {
            // Expression is to be inlined. Use the original expression.
            return allExprs[localIndex];
        }
        int inputIndex = indexOf(localIndex, inputExprOrdinals);
        assert inputIndex >= 0;
        return RexBuilder.getRexFactory().makeLocalRef(inputIndex, local.getType());
    } else {
        // It's a reference to what was a local expression at the
        // previous level, and was then projected.
        final int exprIndex = exprInverseOrdinals[localIndex];
        return RexBuilder.getRexFactory().makeLocalRef(exprIndex, local.getType());
    }
}
 
Example #10
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 #11
Source File: SqlImplementor.java    From Bats 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 #12
Source File: Window.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static RelCollation getCollation(final List<RexFieldCollation> collations) {
    return RelCollations.of(new AbstractList<RelFieldCollation>() {
        @Override
        public RelFieldCollation get(int index) {
            final RexFieldCollation collation = collations.get(index);
            return new RelFieldCollation(((RexLocalRef) collation.left).getIndex(), collation.getDirection(),
                    collation.getNullDirection());
        }

        @Override
        public int size() {
            return collations.size();
        }
    });
}
 
Example #13
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 #14
Source File: CalcRelSplitter.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the order in which to visit expressions, so that we decide the
 * level of an expression only after the levels of lower expressions have
 * been decided.
 *
 * <p>First, we need to ensure that an expression is visited after all of
 * its inputs.
 *
 * <p>Further, if the expression is a member of a cohort, we need to visit
 * it after the inputs of all other expressions in that cohort. With this
 * condition, expressions in the same cohort will very likely end up in the
 * same level.
 *
 * <p>Note that if there are no cohorts, the expressions from the
 * {@link RexProgram} are already in a suitable order. We perform the
 * topological sort just to ensure that the code path is well-trodden.
 *
 * @param exprs   Expressions
 * @param cohorts List of cohorts, each of which is a set of expr ordinals
 * @return Expression ordinals in topological order
 */
private List<Integer> computeTopologicalOrdering(RexNode[] exprs, List<Set<Integer>> cohorts) {
    final DirectedGraph<Integer, DefaultEdge> graph = DefaultDirectedGraph.create();
    for (int i = 0; i < exprs.length; i++) {
        graph.addVertex(i);
    }
    for (int i = 0; i < exprs.length; i++) {
        final RexNode expr = exprs[i];
        final Set<Integer> cohort = findCohort(cohorts, i);
        final Set<Integer> targets;
        if (cohort == null) {
            targets = Collections.singleton(i);
        } else {
            targets = cohort;
        }
        expr.accept(new RexVisitorImpl<Void>(true) {
            @Override
            public Void visitLocalRef(RexLocalRef localRef) {
                for (Integer target : targets) {
                    graph.addEdge(localRef.getIndex(), target);
                }
                return null;
            }
        });
    }
    TopologicalOrderIterator<Integer, DefaultEdge> iter = new TopologicalOrderIterator<>(graph);
    final List<Integer> permutation = new ArrayList<>();
    while (iter.hasNext()) {
        permutation.add(iter.next());
    }
    return permutation;
}
 
Example #15
Source File: CopyWithCluster.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public RexLocalRef apply(@Nullable RexLocalRef input) {
  // Note: RexCopier throws an UnsupportedException if we try to copy a RexLocalRef, so the assumption for now
  // is that we'll never hit this in practice
  return input == null ? null : (RexLocalRef) copyOf(input);
}
 
Example #16
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 #17
Source File: SqlImplementor.java    From calcite 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 #18
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 #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: 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 #21
Source File: RelToSqlConverter.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 #22
Source File: LogicalWindow.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static void addWindows(
    Multimap<WindowKey, RexOver> windowMap,
    RexOver over, final int inputFieldCount) {
  final RexWindow aggWindow = over.getWindow();

  // Look up or create a window.
  RelCollation orderKeys = getCollation(
      Lists.newArrayList(
          Util.filter(aggWindow.orderKeys,
              rexFieldCollation ->
                  // If ORDER BY references constant (i.e. RexInputRef),
                  // then we can ignore such ORDER BY key.
                  rexFieldCollation.left instanceof RexLocalRef)));
  ImmutableBitSet groupSet =
      ImmutableBitSet.of(getProjectOrdinals(aggWindow.partitionKeys));
  final int groupLength = groupSet.length();
  if (inputFieldCount < groupLength) {
    // If PARTITION BY references constant, we can ignore such partition key.
    // All the inputs after inputFieldCount are literals, thus we can clear.
    groupSet =
        groupSet.except(ImmutableBitSet.range(inputFieldCount, groupLength));
  }

  WindowKey windowKey =
      new WindowKey(
          groupSet, orderKeys, aggWindow.isRows(),
          aggWindow.getLowerBound(), aggWindow.getUpperBound());
  windowMap.put(windowKey, over);
}
 
Example #23
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 #24
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 #25
Source File: CalcRelSplitter.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the order in which to visit expressions, so that we decide the
 * level of an expression only after the levels of lower expressions have
 * been decided.
 *
 * <p>First, we need to ensure that an expression is visited after all of
 * its inputs.
 *
 * <p>Further, if the expression is a member of a cohort, we need to visit
 * it after the inputs of all other expressions in that cohort. With this
 * condition, expressions in the same cohort will very likely end up in the
 * same level.
 *
 * <p>Note that if there are no cohorts, the expressions from the
 * {@link RexProgram} are already in a suitable order. We perform the
 * topological sort just to ensure that the code path is well-trodden.
 *
 * @param exprs   Expressions
 * @param cohorts List of cohorts, each of which is a set of expr ordinals
 * @return Expression ordinals in topological order
 */
private List<Integer> computeTopologicalOrdering(
    RexNode[] exprs,
    List<Set<Integer>> cohorts) {
  final DirectedGraph<Integer, DefaultEdge> graph =
      DefaultDirectedGraph.create();
  for (int i = 0; i < exprs.length; i++) {
    graph.addVertex(i);
  }
  for (int i = 0; i < exprs.length; i++) {
    final RexNode expr = exprs[i];
    final Set<Integer> cohort = findCohort(cohorts, i);
    final Set<Integer> targets;
    if (cohort == null) {
      targets = Collections.singleton(i);
    } else {
      targets = cohort;
    }
    expr.accept(
        new RexVisitorImpl<Void>(true) {
          public Void visitLocalRef(RexLocalRef localRef) {
            for (Integer target : targets) {
              graph.addEdge(localRef.getIndex(), target);
            }
            return null;
          }
        });
  }
  TopologicalOrderIterator<Integer, DefaultEdge> iter =
      new TopologicalOrderIterator<>(graph);
  final List<Integer> permutation = new ArrayList<>();
  while (iter.hasNext()) {
    permutation.add(iter.next());
  }
  return permutation;
}
 
Example #26
Source File: TupleFilterVisitor.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public TupleFilter visitLocalRef(RexLocalRef localRef) {
    throw new UnsupportedOperationException("local ref:" + localRef);
}
 
Example #27
Source File: ElasticSourceNameFinder.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public List<List<String>> visitLocalRef(RexLocalRef localRef) {
  return visitUnknown(localRef);
}
 
Example #28
Source File: ProjectAnalyzer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public FunctionRender visitLocalRef(RexLocalRef localRef) {
  return visitUnknown(localRef);
}
 
Example #29
Source File: TupleExpressionVisitor.java    From kylin with Apache License 2.0 4 votes vote down vote up
@Override
public TupleExpression visitLocalRef(RexLocalRef localRef) {
    throw new UnsupportedOperationException("local ref:" + localRef);
}
 
Example #30
Source File: IndexableExprMarker.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public Boolean visitLocalRef(RexLocalRef localRef) {
    return false;
}