Java Code Examples for org.apache.calcite.rex.RexNode

The following examples show how to use org.apache.calcite.rex.RexNode. These examples are extracted from open source projects. 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 Project: Bats   Source File: DateRangeRules.java    License: Apache License 2.0 6 votes vote down vote up
private RexNode compareFloorCeil(SqlKind comparison, RexNode operand, RexLiteral timeLiteral,
        TimeUnitRange timeUnit, boolean floor) {
    RangeSet<Calendar> rangeSet = operandRanges.get(operand);
    if (rangeSet == null) {
        rangeSet = ImmutableRangeSet.<Calendar> of().complement();
    }
    final RangeSet<Calendar> s2 = TreeRangeSet.create();
    final Calendar c = timestampValue(timeLiteral);
    final Range<Calendar> range = floor ? floorRange(timeUnit, comparison, c)
            : ceilRange(timeUnit, comparison, c);
    s2.add(range);
    // Intersect old range set with new.
    s2.removeAll(rangeSet.complement());
    operandRanges.put(operand, ImmutableRangeSet.copyOf(s2));
    if (range.isEmpty()) {
        return rexBuilder.makeLiteral(false);
    }
    return toRex(operand, range);
}
 
Example 2
Source Project: Bats   Source File: RelJson.java    License: Apache License 2.0 6 votes vote down vote up
private List<RexFieldCollation> toRexFieldCollationList(
    RelInput relInput, List<Map<String, Object>> order) {
  if (order == null) {
    return null;
  }

  List<RexFieldCollation> list = new ArrayList<>();
  for (Map<String, Object> o : order) {
    RexNode expr = toRex(relInput, o.get("expr"));
    Set<SqlKind> directions = new HashSet<>();
    if (Direction.valueOf((String) o.get("direction")) == Direction.DESCENDING) {
      directions.add(SqlKind.DESCENDING);
    }
    if (NullDirection.valueOf((String) o.get("null-direction")) == NullDirection.FIRST) {
      directions.add(SqlKind.NULLS_FIRST);
    } else {
      directions.add(SqlKind.NULLS_LAST);
    }
    list.add(new RexFieldCollation(expr, directions));
  }
  return list;
}
 
Example 3
Source Project: calcite   Source File: DruidQueryFilterTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testBetweenFilterStringCase() throws IOException {
  final Fixture f = new Fixture();
  final List<RexNode> listRexNodes =
      ImmutableList.of(f.rexBuilder.makeLiteral(false),
          f.rexBuilder.makeInputRef(f.varcharRowType, 0),
          f.rexBuilder.makeLiteral("lower-bound"),
          f.rexBuilder.makeLiteral("upper-bound"));
  RelDataType relDataType = f.typeFactory.createSqlType(SqlTypeName.BOOLEAN);
  RexNode betweenRexNode = f.rexBuilder.makeCall(relDataType,
      SqlStdOperatorTable.BETWEEN, listRexNodes);

  DruidJsonFilter returnValue = DruidJsonFilter
      .toDruidFilters(betweenRexNode, f.varcharRowType, druidQuery);
  assertThat("Filter is null", returnValue, notNullValue());
  JsonFactory jsonFactory = new JsonFactory();
  final StringWriter sw = new StringWriter();
  JsonGenerator jsonGenerator = jsonFactory.createGenerator(sw);
  returnValue.write(jsonGenerator);
  jsonGenerator.close();
  assertThat(sw.toString(),
      is("{\"type\":\"bound\",\"dimension\":\"dimensionName\",\"lower\":\"lower-bound\","
          + "\"lowerStrict\":false,\"upper\":\"upper-bound\",\"upperStrict\":false,"
          + "\"ordering\":\"lexicographic\"}"));
}
 
Example 4
Source Project: calcite   Source File: MaterializedViewRule.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Replaces all the input references by the position in the
 * input column set. If a reference index cannot be found in
 * the input set, then we return null.
 */
protected RexNode shuttleReferences(final RexBuilder rexBuilder,
    final RexNode node, final Mapping mapping) {
  try {
    RexShuttle visitor =
        new RexShuttle() {
          @Override public RexNode visitInputRef(RexInputRef inputRef) {
            int pos = mapping.getTargetOpt(inputRef.getIndex());
            if (pos != -1) {
              // Found it
              return rexBuilder.makeInputRef(inputRef.getType(), pos);
            }
            throw Util.FoundOne.NULL;
          }
        };
    return visitor.apply(node);
  } catch (Util.FoundOne ex) {
    Util.swallow(ex, null);
    return null;
  }
}
 
Example 5
Source Project: dk-fitting   Source File: ElasticsearchFilter.java    License: Apache License 2.0 6 votes vote down vote up
private Void translateMatch(RexNode node) {
    switch (node.getKind()) {
        case EQUALS:
        case NOT_EQUALS:
        case LIKE:
        case IS_NULL:
        case IS_NOT_NULL:
            return translateBinary(node.getKind(), node.getKind(), (RexCall) node);
        case LESS_THAN:
            return translateBinary(SqlKind.LESS_THAN, SqlKind.GREATER_THAN, (RexCall) node);
        case LESS_THAN_OR_EQUAL:
            return translateBinary(SqlKind.LESS_THAN_OR_EQUAL, SqlKind.GREATER_THAN_OR_EQUAL, (RexCall) node);
        case GREATER_THAN:
            return translateBinary(SqlKind.GREATER_THAN, SqlKind.LESS_THAN, (RexCall) node);
        case GREATER_THAN_OR_EQUAL:
            return translateBinary(SqlKind.GREATER_THAN_OR_EQUAL, SqlKind.LESS_THAN_OR_EQUAL, (RexCall) node);
        default:
            throw new UnsupportedOperationException("cannot translate " + node);
    }
}
 
Example 6
Source Project: calcite   Source File: MaterializedViewRule.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * First, the method takes the node expressions {@code nodeExprs} and swaps the table
 * and column references using the table mapping and the equivalence classes.
 * If {@code swapTableColumn} is true, it swaps the table reference and then the column reference,
 * otherwise it swaps the column reference and then the table reference.
 *
 * <p>Then, the method will rewrite the input expression {@code exprToRewrite}, replacing the
 * {@link RexTableInputRef} by references to the positions in {@code nodeExprs}.
 *
 * <p>The method will return the rewritten expression. If any of the expressions in the input
 * expression cannot be mapped, it will return null.
 */
protected RexNode rewriteExpression(
    RexBuilder rexBuilder,
    RelMetadataQuery mq,
    RelNode targetNode,
    RelNode node,
    List<RexNode> nodeExprs,
    BiMap<RelTableRef, RelTableRef> tableMapping,
    EquivalenceClasses ec,
    boolean swapTableColumn,
    RexNode exprToRewrite) {
  List<RexNode> rewrittenExprs = rewriteExpressions(rexBuilder, mq, targetNode, node, nodeExprs,
      tableMapping, ec, swapTableColumn, ImmutableList.of(exprToRewrite));
  if (rewrittenExprs == null) {
    return null;
  }
  assert rewrittenExprs.size() == 1;
  return rewrittenExprs.get(0);
}
 
Example 7
Source Project: Bats   Source File: CalcRelSplitter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Traces the given array of level expression lists at the finer level.
 *
 * @param exprs             Array expressions
 * @param exprLevels        For each expression, the ordinal of its level
 * @param levelTypeOrdinals For each level, the ordinal of its type in
 *                          the {@link #relTypes} array
 * @param levelCount        The number of levels
 */
private void traceLevelExpressions(RexNode[] exprs, int[] exprLevels, int[] levelTypeOrdinals, int levelCount) {
    StringWriter traceMsg = new StringWriter();
    PrintWriter traceWriter = new PrintWriter(traceMsg);
    traceWriter.println("FarragoAutoCalcRule result expressions for: ");
    traceWriter.println(program.toString());

    for (int level = 0; level < levelCount; level++) {
        traceWriter.println("Rel Level " + level + ", type " + relTypes[levelTypeOrdinals[level]]);

        for (int i = 0; i < exprs.length; i++) {
            RexNode expr = exprs[i];
            assert (exprLevels[i] >= -1) && (exprLevels[i] < levelCount) : "expression's level is out of range";
            if (exprLevels[i] == level) {
                traceWriter.println("\t" + i + ": " + expr);
            }
        }
        traceWriter.println();
    }
    String msg = traceMsg.toString();
    RULE_LOGGER.trace(msg);
}
 
Example 8
Source Project: dremio-oss   Source File: TestFilterFinder.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void equalityWithCast(){

  final RexNode node = builder.makeCall(SqlStdOperatorTable.EQUALS,
      builder.makeCast(
          factory.createSqlType(SqlTypeName.ANY),
          builder.makeBigintLiteral(BigDecimal.ONE)
      ),
      builder.makeInputRef(factory.createSqlType(SqlTypeName.BIGINT), 0)
  );

  FindSimpleFilters finder = new FindSimpleFilters(builder);
  StateHolder holder = node.accept(finder);
  ImmutableList<RexCall> conditions = holder.getConditions();

  assertEquals(1, conditions.size());
  assertEquals(SqlKind.EQUALS, conditions.get(0).getKind());
  // Make sure CAST was removed
  assertEquals(SqlKind.LITERAL, conditions.get(0).getOperands().get(0).getKind());
  assertFalse(holder.hasRemainingExpression());

}
 
Example 9
Source Project: Bats   Source File: RelBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/** Infers the alias of an expression.
 *
 * <p>If the expression was created by {@link #alias}, replaces the expression
 * in the project list.
 */
private String inferAlias(List<RexNode> exprList, RexNode expr, int i) {
    switch (expr.getKind()) {
    case INPUT_REF:
        final RexInputRef ref = (RexInputRef) expr;
        return stack.peek().fields.get(ref.getIndex()).getValue().getName();
    case CAST:
        return inferAlias(exprList, ((RexCall) expr).getOperands().get(0), -1);
    case AS:
        final RexCall call = (RexCall) expr;
        if (i >= 0) {
            exprList.set(i, call.getOperands().get(0));
        }
        return ((NlsString) ((RexLiteral) call.getOperands().get(1)).getValue()).getValue();
    default:
        return null;
    }
}
 
Example 10
Source Project: calcite   Source File: DruidJsonFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
private static DruidJsonFilter toSimpleDruidFilter(RexNode e, RelDataType rowType,
    DruidQuery druidQuery) {
  switch (e.getKind()) {
  case EQUALS:
  case NOT_EQUALS:
    return toEqualityKindDruidFilter(e, rowType, druidQuery);
  case GREATER_THAN:
  case GREATER_THAN_OR_EQUAL:
  case LESS_THAN:
  case LESS_THAN_OR_EQUAL:
    return toBoundDruidFilter(e, rowType, druidQuery);
  case BETWEEN:
    return toBetweenDruidFilter(e, rowType, druidQuery);
  case IN:
  case NOT_IN:
    return toInKindDruidFilter(e, rowType, druidQuery);
  case IS_NULL:
  case IS_NOT_NULL:
    return toIsNullKindDruidFilter(e, rowType, druidQuery);
  default:
    return null;
  }
}
 
Example 11
Source Project: Bats   Source File: RelMdPredicates.java    License: Apache License 2.0 6 votes vote down vote up
private void infer(RexNode predicates, Set<RexNode> allExprs,
    List<RexNode> inferredPredicates, boolean includeEqualityInference,
    ImmutableBitSet inferringFields) {
  for (RexNode r : RelOptUtil.conjunctions(predicates)) {
    if (!includeEqualityInference
        && equalityPredicates.contains(r)) {
      continue;
    }
    for (Mapping m : mappings(r)) {
      RexNode tr = r.accept(
          new RexPermuteInputsShuttle(m, joinRel.getInput(0),
              joinRel.getInput(1)));
      // Filter predicates can be already simplified, so we should work with
      // simplified RexNode versions as well. It also allows prevent of having
      // some duplicates in in result pulledUpPredicates
      RexNode simplifiedTarget =
          simplify.simplifyFilterPredicates(RelOptUtil.conjunctions(tr));
      if (checkTarget(inferringFields, allExprs, tr)
          && checkTarget(inferringFields, allExprs, simplifiedTarget)) {
        inferredPredicates.add(simplifiedTarget);
        allExprs.add(simplifiedTarget);
      }
    }
  }
}
 
Example 12
Source Project: Bats   Source File: IndexSelector.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This method analyzes an index's columns and starting from the first column, checks
 * which part of the filter condition matches that column.  This process continues with
 * subsequent columns.  The goal is to identify the portion of the filter condition that
 * match the prefix columns.  If there are additional conditions that don't match prefix
 * columns, that condition is set as a remainder condition.
 * @param indexProps
 */
public void analyzePrefixMatches(IndexProperties indexProps) {
  RexNode initCondition = indexCondition.isAlwaysTrue() ? null : indexCondition;
  Map<LogicalExpression, RexNode> leadingPrefixMap = Maps.newHashMap();
  List<LogicalExpression> indexCols = indexProps.getIndexDesc().getIndexColumns();
  boolean satisfiesCollation = false;

  if (indexCols.size() > 0) {
    if (initCondition != null) { // check filter condition
      initCondition = IndexPlanUtils.getLeadingPrefixMap(leadingPrefixMap, indexCols, builder, indexCondition);
    }
    if (requiredCollation()) {
      satisfiesCollation = buildAndCheckCollation(indexProps);
    }
  }

  indexProps.setProperties(leadingPrefixMap, satisfiesCollation,
      initCondition /* the remainder condition for indexed columns */, stats);
}
 
Example 13
Source Project: dremio-oss   Source File: RelToSqlConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @see #dispatch
 */
public Result visit(Project e) {
  Result x = visitChild(0, e.getInput());
  parseCorrelTable(e, x);
  if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
    return x;
  }
  final Builder builder =
    x.builder(e, Clause.SELECT);
  final List<SqlNode> selectList = new ArrayList<>();
  for (RexNode ref : e.getChildExps()) {
    SqlNode sqlExpr = builder.context.toSql(null,
      simplifyDatetimePlus(ref, e.getCluster().getRexBuilder()));
    addSelect(selectList, sqlExpr, e.getRowType());
  }

  builder.setSelect(new SqlNodeList(selectList, POS));
  return builder.result();
}
 
Example 14
Source Project: dremio-oss   Source File: TestORCFindRelevantFilters.java    License: Apache License 2.0 6 votes vote down vote up
private void singleOpTest(SqlOperator op) {
  RexNode eqInt = builder.makeCall(op, asList(input(0), intLit(0, 23)));
  assertEqualsDigest(eqInt, eqInt.accept(finder));

  RexNode eqBigInt = builder.makeCall(op, asList(input(1), bigIntLit(1, 23234234L)));
  assertEqualsDigest(eqBigInt, eqBigInt.accept(finder));

  RexNode eqFloat = builder.makeCall(op, asList(input(2), floatLit(2, 23234.233f)));
  assertEqualsDigest(eqFloat, eqFloat.accept(finder));

  RexNode eqDouble = builder.makeCall(op, asList(input(3), doubleLit(3, 235234234.234324d)));
  assertEqualsDigest(eqDouble, eqDouble.accept(finder));

  RexNode eqDate = builder.makeCall(op, asList(input(4), dateLit(4, 1231293712)));
  assertEqualsDigest(eqDate, eqDate.accept(finder));

  RexNode eqTs = builder.makeCall(op, asList(input(5), tsLit(5, 232349893L)));
  assertEqualsDigest(eqTs, eqTs.accept(finder));

  RexNode eqVarchar = builder.makeCall(op, asList(input(6), varcharLit(6, "str")));
  assertEqualsDigest(eqVarchar, eqVarchar.accept(finder));

  RexNode eqBool = builder.makeCall(op, asList(input(7), boolLit(7, false)));
  assertEqualsDigest(eqBool, eqBool.accept(finder));
}
 
Example 15
Source Project: calcite   Source File: RelMdDistinctRowCount.java    License: Apache License 2.0 6 votes vote down vote up
public Double getDistinctRowCount(RelSubset rel, RelMetadataQuery mq,
    ImmutableBitSet groupKey, RexNode predicate) {
  final RelNode best = rel.getBest();
  if (best != null) {
    return mq.getDistinctRowCount(best, groupKey, predicate);
  }
  if (!Bug.CALCITE_1048_FIXED) {
    return getDistinctRowCount((RelNode) rel, mq, groupKey, predicate);
  }
  Double d = null;
  for (RelNode r2 : rel.getRels()) {
    try {
      Double d2 = mq.getDistinctRowCount(r2, groupKey, predicate);
      d = NumberUtil.min(d, d2);
    } catch (CyclicMetadataException e) {
      // Ignore this relational expression; there will be non-cyclic ones
      // in this set.
    }
  }
  return d;
}
 
Example 16
Source Project: Bats   Source File: RelMdUtil.java    License: Apache License 2.0 6 votes vote down vote up
/** Returns an estimate of the number of rows returned by a {@link Join}. */
public static Double getJoinRowCount(RelMetadataQuery mq, Join join,
    RexNode condition) {
  // Row count estimates of 0 will be rounded up to 1.
  // So, use maxRowCount where the product is very small.
  final Double left = mq.getRowCount(join.getLeft());
  final Double right = mq.getRowCount(join.getRight());
  if (left == null || right == null) {
    return null;
  }
  if (left <= 1D || right <= 1D) {
    Double max = mq.getMaxRowCount(join);
    if (max != null && max <= 1D) {
      return max;
    }
  }
  double product = left * right;

  // TODO:  correlation factor
  return product * mq.getSelectivity(join, condition);
}
 
Example 17
Source Project: Bats   Source File: ReduceDecimalsRule.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Rewrites a call in place, from bottom up, as follows:
 *
 * <ol>
 * <li>visit operands
 * <li>visit call node
 *
 * <ol>
 * <li>rewrite call
 * <li>visit the rewritten call
 * </ol>
 * </ol>
 */
@Override
public RexNode visitCall(RexCall call) {
    RexNode savedResult = lookup(call);
    if (savedResult != null) {
        return savedResult;
    }

    // permanently updates a call in place
    List<RexNode> newOperands = apply(call.getOperands());
    if (true) {
        // FIXME: Operands are now immutable. Create a new call with
        // new operands?
        throw new AssertionError();
    }

    RexNode newCall = call;
    RexNode rewrite = rewriteCall(call);
    if (rewrite != call) {
        newCall = rewrite.accept(this);
    }

    register(call, newCall);
    return newCall;
}
 
Example 18
Source Project: Bats   Source File: JoinToMultiJoinRule.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Shifts a filter originating from the right child of the LogicalJoin to the
 * right, to reflect the filter now being applied on the resulting
 * MultiJoin.
 *
 * @param joinRel     the original LogicalJoin
 * @param left        the left child of the LogicalJoin
 * @param right       the right child of the LogicalJoin
 * @param rightFilter the filter originating from the right child
 * @return the adjusted right filter
 */
private RexNode shiftRightFilter(
    Join joinRel,
    RelNode left,
    MultiJoin right,
    RexNode rightFilter) {
  if (rightFilter == null) {
    return null;
  }

  int nFieldsOnLeft = left.getRowType().getFieldList().size();
  int nFieldsOnRight = right.getRowType().getFieldList().size();
  int[] adjustments = new int[nFieldsOnRight];
  for (int i = 0; i < nFieldsOnRight; i++) {
    adjustments[i] = nFieldsOnLeft;
  }
  rightFilter =
      rightFilter.accept(
          new RelOptUtil.RexInputConverter(
              joinRel.getCluster().getRexBuilder(),
              right.getRowType().getFieldList(),
              joinRel.getRowType().getFieldList(),
              adjustments));
  return rightFilter;
}
 
Example 19
Source Project: dk-fitting   Source File: ElasticsearchFilter.java    License: Apache License 2.0 6 votes vote down vote up
private Void translateMatch(RexNode node) {
    switch (node.getKind()) {
        case EQUALS:
        case NOT_EQUALS:
        case LIKE:
        case IS_NULL:
        case IS_NOT_NULL:
            return translateBinary(node.getKind(), node.getKind(), (RexCall) node);
        case LESS_THAN:
            return translateBinary(SqlKind.LESS_THAN, SqlKind.GREATER_THAN, (RexCall) node);
        case LESS_THAN_OR_EQUAL:
            return translateBinary(SqlKind.LESS_THAN_OR_EQUAL, SqlKind.GREATER_THAN_OR_EQUAL, (RexCall) node);
        case GREATER_THAN:
            return translateBinary(SqlKind.GREATER_THAN, SqlKind.LESS_THAN, (RexCall) node);
        case GREATER_THAN_OR_EQUAL:
            return translateBinary(SqlKind.GREATER_THAN_OR_EQUAL, SqlKind.LESS_THAN_OR_EQUAL, (RexCall) node);
        default:
            throw new UnsupportedOperationException("cannot translate " + node);
    }
}
 
Example 20
Source Project: calcite   Source File: RelRoot.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns the root relational expression as a {@link LogicalProject}.
 *
 * @param force Create a Project even if all fields are used */
public RelNode project(boolean force) {
  if (isRefTrivial()
      && (SqlKind.DML.contains(kind)
          || !force
          || rel instanceof LogicalProject)) {
    return rel;
  }
  final List<RexNode> projects = new ArrayList<>(fields.size());
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  for (Pair<Integer, String> field : fields) {
    projects.add(rexBuilder.makeInputRef(rel, field.left));
  }
  return LogicalProject.create(rel, hints, projects, Pair.right(fields));
}
 
Example 21
Source Project: calcite   Source File: Bindables.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a BindableTableScan.
 *
 * <p>Use {@link #create} unless you know what you are doing. */
BindableTableScan(RelOptCluster cluster, RelTraitSet traitSet,
    RelOptTable table, ImmutableList<RexNode> filters,
    ImmutableIntList projects) {
  super(cluster, traitSet, ImmutableList.of(), table);
  this.filters = Objects.requireNonNull(filters);
  this.projects = Objects.requireNonNull(projects);
  Preconditions.checkArgument(canHandle(table));
}
 
Example 22
Source Project: dremio-oss   Source File: PreProcessRel.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalProject project) {
  final RenameConvertToConvertFromVisitor renameVisitor = new RenameConvertToConvertFromVisitor(project.getCluster().getRexBuilder(), table);
  final List<RexNode> projExpr = Lists.newArrayList();
  for(RexNode rexNode : project.getChildExps()) {
    projExpr.add(rexNode.accept(unwrappingExpressionVisitor));
  }

  project =  project.copy(project.getTraitSet(),
      project.getInput(),
      projExpr,
      project.getRowType());

  List<RexNode> exprList = new ArrayList<>();
  boolean rewrite = false;

  for (RexNode rex : project.getChildExps()) {
    RexNode newExpr = rex.accept(renameVisitor);
    if (newExpr != rex) {
      rewrite = true;
    }
    exprList.add(newExpr);
  }

  if (rewrite == true) {
    LogicalProject newProject = project.copy(project.getTraitSet(), project.getInput(0), exprList, project.getRowType());
    return visitChild(newProject, 0, project.getInput());
  }

  return visitChild(project, 0, project.getInput());
}
 
Example 23
Source Project: calcite   Source File: RelOptUtil.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a filter using the default factory,
 * or returns the original relational expression if the
 * condition is trivial. */
public static RelNode createFilter(RelNode child,
    Iterable<? extends RexNode> conditions,
    RelFactories.FilterFactory filterFactory) {
  final RelOptCluster cluster = child.getCluster();
  final RexNode condition =
      RexUtil.composeConjunction(cluster.getRexBuilder(), conditions, true);
  if (condition == null) {
    return child;
  } else {
    return filterFactory.createFilter(child, condition, ImmutableSet.of());
  }
}
 
Example 24
Source Project: calcite   Source File: CalcRelSplitter.java    License: Apache License 2.0 5 votes vote down vote up
private String deriveFieldName(RexNode expr, int ordinal) {
  if (expr instanceof RexInputRef) {
    int inputIndex = ((RexInputRef) expr).getIndex();
    String fieldName =
        child.getRowType().getFieldList().get(inputIndex).getName();
    // Don't inherit field names like '$3' from child: that's
    // confusing.
    if (!fieldName.startsWith("$") || fieldName.startsWith("$EXPR")) {
      return fieldName;
    }
  }
  return "$" + ordinal;
}
 
Example 25
Source Project: calcite   Source File: RelMetadataTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test void testExpressionLineageStar() {
  // All columns in output
  final RelNode tableRel = convertSql("select * from emp");
  final RelMetadataQuery mq = tableRel.getCluster().getMetadataQuery();

  final RexNode ref = RexInputRef.of(4, tableRel.getRowType().getFieldList());
  final Set<RexNode> r = mq.getExpressionLineage(tableRel, ref);
  final String inputRef = RexInputRef.of(4, tableRel.getRowType().getFieldList()).toString();
  assertThat(r.size(), is(1));
  final String resultString = r.iterator().next().toString();
  assertThat(resultString, startsWith(EMP_QNAME.toString()));
  assertThat(resultString, endsWith(inputRef));
}
 
Example 26
Source Project: flink   Source File: RelDecorrelator.java    License: Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
  int newInputOutputOffset = 0;
  for (RelNode input : currentRel.getInputs()) {
    final Frame frame = map.get(input);

    if (frame != null) {
      // try to find in this input rel the position of corVar
      final CorRef corRef = cm.mapFieldAccessToCorRef.get(fieldAccess);

      if (corRef != null) {
        Integer newInputPos = frame.corDefOutputs.get(corRef.def());
        if (newInputPos != null) {
          // This input does produce the corVar referenced.
          return new RexInputRef(newInputPos + newInputOutputOffset,
              frame.r.getRowType().getFieldList().get(newInputPos)
                  .getType());
        }
      }

      // this input does not produce the corVar needed
      newInputOutputOffset += frame.r.getRowType().getFieldCount();
    } else {
      // this input is not rewritten
      newInputOutputOffset += input.getRowType().getFieldCount();
    }
  }
  return fieldAccess;
}
 
Example 27
Source Project: calcite   Source File: JdbcTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override public TableModify toModificationRel(RelOptCluster cluster,
    RelOptTable table, CatalogReader catalogReader, RelNode input,
    Operation operation, List<String> updateColumnList,
    List<RexNode> sourceExpressionList, boolean flattened) {
  jdbcSchema.convention.register(cluster.getPlanner());

  return new LogicalTableModify(cluster, cluster.traitSetOf(Convention.NONE),
      table, catalogReader, input, operation, updateColumnList,
      sourceExpressionList, flattened);
}
 
Example 28
Source Project: calcite   Source File: RelMdSelectivity.java    License: Apache License 2.0 5 votes vote down vote up
public Double getSelectivity(Project rel, RelMetadataQuery mq,
    RexNode predicate) {
  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      ImmutableBitSet.range(rel.getRowType().getFieldCount()),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RexNode childPred =
      RexUtil.composeConjunction(rexBuilder, pushable, true);

  RexNode modifiedPred;
  if (childPred == null) {
    modifiedPred = null;
  } else {
    modifiedPred = RelOptUtil.pushPastProject(childPred, rel);
  }
  Double selectivity = mq.getSelectivity(rel.getInput(), modifiedPred);
  if (selectivity == null) {
    return null;
  } else {
    RexNode pred =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    return selectivity * RelMdUtil.guessSelectivity(pred);
  }
}
 
Example 29
Source Project: Bats   Source File: RelOptUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates projection expressions reflecting the swapping of a join's input.
 *
 * @param newJoin   the RelNode corresponding to the join with its inputs
 *                  swapped
 * @param origJoin  original LogicalJoin
 * @param origOrder if true, create the projection expressions to reflect
 *                  the original (pre-swapped) join projection; otherwise,
 *                  create the projection to reflect the order of the swapped
 *                  projection
 * @return array of expression representing the swapped join inputs
 */
public static List<RexNode> createSwappedJoinExprs(RelNode newJoin, Join origJoin, boolean origOrder) {
    final List<RelDataTypeField> newJoinFields = newJoin.getRowType().getFieldList();
    final RexBuilder rexBuilder = newJoin.getCluster().getRexBuilder();
    final List<RexNode> exps = new ArrayList<>();
    final int nFields = origOrder ? origJoin.getRight().getRowType().getFieldCount()
            : origJoin.getLeft().getRowType().getFieldCount();
    for (int i = 0; i < newJoinFields.size(); i++) {
        final int source = (i + nFields) % newJoinFields.size();
        RelDataTypeField field = origOrder ? newJoinFields.get(source) : newJoinFields.get(i);
        exps.add(rexBuilder.makeInputRef(field.getType(), source));
    }
    return exps;
}
 
Example 30
Source Project: kareldb   Source File: EnumerableTableModifyExtension.java    License: Apache License 2.0 5 votes vote down vote up
public EnumerableTableModifyExtension(RelOptCluster cluster, RelTraitSet traits,
                                      RelOptTable table, Prepare.CatalogReader catalogReader, RelNode child,
                                      Operation operation, List<String> updateColumnList,
                                      List<RexNode> sourceExpressionList, boolean flattened) {
    super(cluster, traits, table, catalogReader, child, operation,
        updateColumnList, sourceExpressionList, flattened);
}