Java Code Examples for org.apache.calcite.rex.RexInputRef#getIndex()

The following examples show how to use org.apache.calcite.rex.RexInputRef#getIndex() . 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: RelMdColumnUniqueness.java    From Bats with Apache License 2.0 6 votes vote down vote up
private boolean simplyProjects(RelNode rel, ImmutableBitSet columns) {
  if (!(rel instanceof Project)) {
    return false;
  }
  Project project = (Project) rel;
  final List<RexNode> projects = project.getProjects();
  for (int column : columns) {
    if (column >= projects.size()) {
      return false;
    }
    if (!(projects.get(column) instanceof RexInputRef)) {
      return false;
    }
    final RexInputRef ref = (RexInputRef) projects.get(column);
    if (ref.getIndex() != column) {
      return false;
    }
  }
  return true;
}
 
Example 2
Source File: JoinCommuteRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RexNode visitInputRef(RexInputRef inputRef) {
  int index = inputRef.getIndex();
  if (index < leftFields.size()) {
    // Field came from left side of join. Move it to the right.
    return rexBuilder.makeInputRef(
        leftFields.get(index).getType(),
        rightFields.size() + index);
  }
  index -= leftFields.size();
  if (index < rightFields.size()) {
    // Field came from right side of join. Move it to the left.
    return rexBuilder.makeInputRef(
        rightFields.get(index).getType(),
        index);
  }
  throw new AssertionError("Bad field offset: index=" + inputRef.getIndex()
      + ", leftFieldCount=" + leftFields.size()
      + ", rightFieldCount=" + rightFields.size());
}
 
Example 3
Source File: RelOptUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RexNode visitInputRef(RexInputRef var) {
  int srcIndex = var.getIndex();
  int destIndex = srcIndex + adjustments[srcIndex];

  RelDataType type;
  if (destFields != null) {
    type = destFields.get(destIndex).getType();
  } else if (leftDestFields != null) {
    if (destIndex < nLeftDestFields) {
      type = leftDestFields.get(destIndex).getType();
    } else {
      type =
          rightDestFields.get(destIndex - nLeftDestFields).getType();
    }
  } else {
    type = srcFields.get(srcIndex).getType();
  }
  if ((adjustments[srcIndex] != 0)
      || (srcFields == null)
      || (type != srcFields.get(srcIndex).getType())) {
    return rexBuilder.makeInputRef(type, destIndex);
  } else {
    return var;
  }
}
 
Example 4
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RexNode visitInputRef(RexInputRef inputRef) {
    if (currentRel instanceof LogicalCorrelate) {
        // if this rel references corVar
        // and now it needs to be rewritten
        // it must have been pulled above the Correlate
        // replace the input ref to account for the LHS of the
        // Correlate
        final int leftInputFieldCount = ((LogicalCorrelate) currentRel).getLeft().getRowType().getFieldCount();
        RelDataType newType = inputRef.getType();

        if (projectPulledAboveLeftCorrelator) {
            newType = typeFactory.createTypeWithNullability(newType, true);
        }

        int pos = inputRef.getIndex();
        RexInputRef newInputRef = RexBuilder.getRexFactory().makeInputRef(leftInputFieldCount + pos, newType);

        if ((isCount != null) && isCount.contains(pos)) {
            return createCaseExpression(newInputRef, rexBuilder.makeExactLiteral(BigDecimal.ZERO), newInputRef);
        } else {
            return newInputRef;
        }
    }
    return inputRef;
}
 
Example 5
Source File: RelMdColumnUniqueness.java    From calcite with Apache License 2.0 6 votes vote down vote up
private boolean simplyProjects(RelNode rel, ImmutableBitSet columns) {
  if (!(rel instanceof Project)) {
    return false;
  }
  Project project = (Project) rel;
  final List<RexNode> projects = project.getProjects();
  for (int column : columns) {
    if (column >= projects.size()) {
      return false;
    }
    if (!(projects.get(column) instanceof RexInputRef)) {
      return false;
    }
    final RexInputRef ref = (RexInputRef) projects.get(column);
    if (ref.getIndex() != column) {
      return false;
    }
  }
  return true;
}
 
Example 6
Source File: RelOptUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RexNode visitInputRef(RexInputRef var) {
    int srcIndex = var.getIndex();
    int destIndex = srcIndex + adjustments[srcIndex];

    RelDataType type;
    if (destFields != null) {
        type = destFields.get(destIndex).getType();
    } else if (leftDestFields != null) {
        if (destIndex < nLeftDestFields) {
            type = leftDestFields.get(destIndex).getType();
        } else {
            type = rightDestFields.get(destIndex - nLeftDestFields).getType();
        }
    } else {
        type = srcFields.get(srcIndex).getType();
    }
    if ((adjustments[srcIndex] != 0) || (srcFields == null) || (type != srcFields.get(srcIndex).getType())) {
        return rexBuilder.makeInputRef(type, destIndex);
    } else {
        return var;
    }
}
 
Example 7
Source File: RelMdUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Double visitInputRef(RexInputRef var) {
  int index = var.getIndex();
  ImmutableBitSet col = ImmutableBitSet.of(index);
  Double distinctRowCount =
      mq.getDistinctRowCount(rel.getInput(), col, null);
  if (distinctRowCount == null) {
    return null;
  } else {
    return numDistinctVals(distinctRowCount, mq.getRowCount(rel));
  }
}
 
Example 8
Source File: DrillRelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public PathSegment visitInputRef(RexInputRef inputRef) {
    int index = inputRef.getIndex();
    String name = fieldNames.get(index);
    RelDataTypeField field = fields.get(index);
    addDesiredField(name, field.getType(), inputRef);
    return new PathSegment.NameSegment(name);
}
 
Example 9
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitInputRef(RexInputRef inputRef) {
  final RexInputRef ref = getNewForOldInputRef(currentRel, map, inputRef);
  if (ref.getIndex() == inputRef.getIndex()
      && ref.getType() == inputRef.getType()) {
    return inputRef; // re-use old object, to prevent needless expr cloning
  }
  return ref;
}
 
Example 10
Source File: TupleExpressionVisitor.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public TupleExpression visitInputRef(RexInputRef inputRef) {
    int index = inputRef.getIndex();
    // check it for rewrite count
    if (index < inputRowType.size()) {
        TblColRef column = inputRowType.getColumnByIndex(index);
        TupleExpression tuple = new ColumnTupleExpression(column);
        tuple.setDigest(inputRef.toString());
        return tuple;
    } else {
        throw new IllegalStateException("Can't find " + inputRef + " from child columnrowtype");
    }
}
 
Example 11
Source File: MoreRelOptUtil.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the leading edge of a given array of expressions is
 * wholly {@link RexInputRef} objects with types and names corresponding
 * to the underlying row type. */
public static boolean containIdentity(List<? extends RexNode> exps,
                                      RelDataType rowType, RelDataType childRowType) {
  List<RelDataTypeField> fields = rowType.getFieldList();
  List<RelDataTypeField> childFields = childRowType.getFieldList();
  int fieldCount = childFields.size();
  if (exps.size() != fieldCount) {
    return false;
  }
  for (int i = 0; i < exps.size(); i++) {
    RexNode exp = exps.get(i);
    if (!(exp instanceof RexInputRef)) {
      return false;
    }
    RexInputRef var = (RexInputRef) exp;
    if (var.getIndex() != i) {
      return false;
    }
    if (!fields.get(i).getName().equals(childFields.get(i).getName())) {
      return false;
    }
    if (!fields.get(i).getType().equals(childFields.get(i).getType())) {
      return false;
    }
  }
  return true;
}
 
Example 12
Source File: SchemaField.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public SchemaPath visitInputRef(RexInputRef inputRef) {
  if(inputRef instanceof SchemaField){
    return ((SchemaField) inputRef).getPath();
  }

  final int index = inputRef.getIndex();
  final RelDataTypeField field = inputRowType.getFieldList().get(index);
  return FieldReference.getWithQuotedRef(field.getName());
}
 
Example 13
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 boolean analyzeSimpleEquiJoin(LogicalJoin join, int[] joinFieldOrdinals) {
    RexNode joinExp = join.getCondition();
    if (joinExp.getKind() != SqlKind.EQUALS) {
        return false;
    }
    RexCall binaryExpression = (RexCall) joinExp;
    RexNode leftComparand = binaryExpression.getOperands().get(0);
    RexNode rightComparand = binaryExpression.getOperands().get(1);
    if (!(leftComparand instanceof RexInputRef)) {
        return false;
    }
    if (!(rightComparand instanceof RexInputRef)) {
        return false;
    }

    final int leftFieldCount = join.getLeft().getRowType().getFieldCount();
    RexInputRef leftFieldAccess = (RexInputRef) leftComparand;
    if (!(leftFieldAccess.getIndex() < leftFieldCount)) {
        // left field must access left side of join
        return false;
    }

    RexInputRef rightFieldAccess = (RexInputRef) rightComparand;
    if (!(rightFieldAccess.getIndex() >= leftFieldCount)) {
        // right field must access right side of join
        return false;
    }

    joinFieldOrdinals[0] = leftFieldAccess.getIndex();
    joinFieldOrdinals[1] = rightFieldAccess.getIndex() - leftFieldCount;
    return true;
}
 
Example 14
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitInputRef(RexInputRef inputRef) {
  if (currentRel instanceof LogicalCorrelate) {
    // if this rel references corVar
    // and now it needs to be rewritten
    // it must have been pulled above the Correlate
    // replace the input ref to account for the LHS of the
    // Correlate
    final int leftInputFieldCount =
        ((LogicalCorrelate) currentRel).getLeft().getRowType()
            .getFieldCount();
    RelDataType newType = inputRef.getType();

    if (projectPulledAboveLeftCorrelator) {
      newType =
          typeFactory.createTypeWithNullability(newType, true);
    }

    int pos = inputRef.getIndex();
    RexInputRef newInputRef =
        new RexInputRef(leftInputFieldCount + pos, newType);

    if ((isCount != null) && isCount.contains(pos)) {
      return createCaseExpression(
          newInputRef,
          rexBuilder.makeExactLiteral(BigDecimal.ZERO),
          newInputRef);
    } else {
      return newInputRef;
    }
  }
  return inputRef;
}
 
Example 15
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitInputRef(RexInputRef inputRef) {
    final RexInputRef ref = getNewForOldInputRef(currentRel, map, inputRef);
    if (ref.getIndex() == inputRef.getIndex() && ref.getType() == inputRef.getType()) {
        return inputRef; // re-use old object, to prevent needless expr cloning
    }
    return ref;
}
 
Example 16
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitInputRef(RexInputRef inputRef) {
  final RexInputRef ref = getNewForOldInputRef(currentRel, map, inputRef);
  if (ref.getIndex() == inputRef.getIndex()
      && ref.getType() == inputRef.getType()) {
    return inputRef; // re-use old object, to prevent needless expr cloning
  }
  return ref;
}
 
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 boolean checkProjAndChildInputs(
    Project project,
    boolean checkNames) {
  int n = project.getProjects().size();
  RelDataType inputType = project.getInput().getRowType();
  if (inputType.getFieldList().size() != n) {
    return false;
  }
  List<RelDataTypeField> projFields = project.getRowType().getFieldList();
  List<RelDataTypeField> inputFields = inputType.getFieldList();
  boolean namesDifferent = false;
  for (int i = 0; i < n; ++i) {
    RexNode exp = project.getProjects().get(i);
    if (!(exp instanceof RexInputRef)) {
      return false;
    }
    RexInputRef fieldAccess = (RexInputRef) exp;
    if (i != fieldAccess.getIndex()) {
      // can't support reorder yet
      return false;
    }
    if (checkNames) {
      String inputFieldName = inputFields.get(i).getName();
      String projFieldName = projFields.get(i).getName();
      if (!projFieldName.equals(inputFieldName)) {
        namesDifferent = true;
      }
    }
  }

  // inputs are the same; return value depends on the checkNames
  // parameter
  return !checkNames || namesDifferent;
}
 
Example 18
Source File: PrelUtil.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public PathSegment visitInputRef(RexInputRef inputRef) {
  int index = inputRef.getIndex();
  String name = fieldNames.get(index);
  RelDataTypeField field = fields.get(index);
  DesiredField f = new DesiredField(index, name, field);
  desiredFields.add(f);
  inputRefs.add(inputRef);
  return new NameSegment(name);
}
 
Example 19
Source File: IndexPlanUtils.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Build collation property for the 'upper' project, the one above the filter
 * @param projectRexs
 * @param inputCollation
 * @param indexInfo
 * @param collationFilterMap
 * @return the output RelCollation
 */
public static RelCollation buildCollationUpperProject(List<RexNode> projectRexs,
                                                      RelCollation inputCollation, FunctionalIndexInfo indexInfo,
                                                      Map<Integer, List<RexNode>> collationFilterMap) {
  List<RelFieldCollation> outputFieldCollations = Lists.newArrayList();

  if (inputCollation != null) {
    List<RelFieldCollation> inputFieldCollations = inputCollation.getFieldCollations();
    if (!indexInfo.hasFunctional()) {
      for (int projectExprIdx = 0; projectExprIdx < projectRexs.size(); projectExprIdx++) {
        RexNode n = projectRexs.get(projectExprIdx);
        if (n instanceof RexInputRef) {
          RexInputRef ref = (RexInputRef)n;
          boolean eligibleForCollation = true;
          int maxIndex = getIndexFromCollation(ref.getIndex(), inputFieldCollations);
          if (maxIndex < 0) {
            eligibleForCollation = false;
            continue;
          }
          // check if the prefix has equality conditions
          for (int i = 0; i < maxIndex; i++) {
            int fieldIdx = inputFieldCollations.get(i).getFieldIndex();
            List<RexNode> conditions = collationFilterMap != null ? collationFilterMap.get(fieldIdx) : null;
            if ((conditions == null || conditions.size() == 0) &&
                i < maxIndex-1) {
              // if an intermediate column has no filter condition, it would select all values
              // of that column, so a subsequent column cannot be eligible for collation
              eligibleForCollation = false;
              break;
            } else {
              for (RexNode r : conditions) {
                if (!(r.getKind() == SqlKind.EQUALS)) {
                  eligibleForCollation = false;
                  break;
                }
              }
            }
          }
          // for every projected expr, if it is eligible for collation, get the
          // corresponding field collation from the input
          if (eligibleForCollation) {
            for (RelFieldCollation c : inputFieldCollations) {
              if (ref.getIndex() == c.getFieldIndex()) {
                RelFieldCollation outFieldCollation = new RelFieldCollation(projectExprIdx, c.getDirection(), c.nullDirection);
                outputFieldCollations.add(outFieldCollation);
              }
            }
          }
        }
      }
    } else {
      // TODO: handle functional index
    }
  }
  return RelCollations.of(outputFieldCollations);
}
 
Example 20
Source File: ElasticSourceNameFinder.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public List<List<String>> visitInputRef(RexInputRef inputRef) {
  final int index = inputRef.getIndex();
  final RelDataTypeField field = rowType.getFieldList().get(index);
  return Lists.<List<String>>newArrayList(Lists.newArrayList(field.getName()));
}