org.apache.calcite.rex.RexNode Java Examples

The following examples show how to use org.apache.calcite.rex.RexNode. 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: IndexSelector.java    From Bats with 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 #2
Source File: RelJson.java    From Bats with 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 File: MaterializedViewRule.java    From calcite with 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 #4
Source File: ReduceDecimalsRule.java    From Bats with 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 #5
Source File: DateRangeRules.java    From Bats with 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 #6
Source File: JoinToMultiJoinRule.java    From Bats with 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 #7
Source File: DruidQueryFilterTest.java    From calcite with 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 #8
Source File: ElasticsearchFilter.java    From dk-fitting with 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 #9
Source File: MaterializedViewRule.java    From calcite with 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 #10
Source File: CalcRelSplitter.java    From Bats with 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 #11
Source File: RelMdUtil.java    From Bats with 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 #12
Source File: RelMdDistinctRowCount.java    From calcite with 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 #13
Source File: TestFilterFinder.java    From dremio-oss with 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 #14
Source File: TestORCFindRelevantFilters.java    From dremio-oss with 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 File: RelBuilder.java    From Bats with 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 #16
Source File: RelToSqlConverter.java    From dremio-oss with 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 #17
Source File: ElasticsearchFilter.java    From dk-fitting with 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 #18
Source File: DruidJsonFilter.java    From calcite with 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 #19
Source File: RelMdPredicates.java    From Bats with 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 #20
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Decomposes a predicate into a list of expressions that are AND'ed
 * together.
 *
 * @param rexPredicate predicate to be analyzed
 * @param rexList      list of decomposed RexNodes
 */
public static void decomposeConjunction(RexNode rexPredicate, List<RexNode> rexList) {
    if (rexPredicate == null || rexPredicate.isAlwaysTrue()) {
        return;
    }
    if (rexPredicate.isA(SqlKind.AND)) {
        for (RexNode operand : ((RexCall) rexPredicate).getOperands()) {
            decomposeConjunction(operand, rexList);
        }
    } else {
        rexList.add(rexPredicate);
    }
}
 
Example #21
Source File: TestFilterFinder.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void multipleConditions() {
  final RexNode node = builder.makeCall(
    SqlStdOperatorTable.AND,
    builder.makeCall(
      SqlStdOperatorTable.GREATER_THAN_OR_EQUAL,
      builder.makeInputRef(factory.createSqlType(SqlTypeName.BIGINT), 0),
      builder.makeBigintLiteral(new BigDecimal("1"))
    ),
    builder.makeCall(
      SqlStdOperatorTable.AND,
      builder.makeCall(
        SqlStdOperatorTable.LESS_THAN_OR_EQUAL,
        builder.makeInputRef(factory.createSqlType(SqlTypeName.BIGINT), 0),
        builder.makeBigintLiteral(new BigDecimal("1"))
      ),
      builder.makeCall(
        SqlStdOperatorTable.LIKE,
        builder.makeInputRef(factory.createSqlType(SqlTypeName.BIGINT), 1),
        builder.makeLiteral("%1mg")
      )
    )
  );
  FindSimpleFilters finder = new FindSimpleFilters((builder));
  StateHolder holder = node.accept(finder);
  assertEquals(holder.getConditions().size(), 2);
  assertTrue(holder.hasRemainingExpression());
}
 
Example #22
Source File: LogicalJoin.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalJoin, flagged with whether it has been translated to a
 * semi-join. */
public static LogicalJoin create(RelNode left, RelNode right,
    RexNode condition, Set<CorrelationId> variablesSet, JoinRelType joinType,
    boolean semiJoinDone, ImmutableList<RelDataTypeField> systemFieldList) {
  final RelOptCluster cluster = left.getCluster();
  final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE);
  return new LogicalJoin(cluster, traitSet, left, right, condition,
      variablesSet, joinType, semiJoinDone, systemFieldList);
}
 
Example #23
Source File: PushProjector.java    From calcite with Apache License 2.0 5 votes vote down vote up
RefAndExprConverter(
    RexBuilder rexBuilder,
    List<RelDataTypeField> srcFields,
    List<RelDataTypeField> destFields,
    int[] adjustments,
    List<RexNode> preserveLeft,
    int firstLeftRef,
    List<RexNode> preserveRight,
    int firstRightRef) {
  super(rexBuilder, srcFields, destFields, adjustments);
  this.preserveLeft = preserveLeft;
  this.firstLeftRef = firstLeftRef;
  this.preserveRight = preserveRight;
  this.firstRightRef = firstRightRef;
}
 
Example #24
Source File: SubstitutionVisitor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Explain filtering condition and projections from MutableCalc. */
private static Pair<RexNode, List<RexNode>> explainCalc(MutableCalc calc) {
  final RexShuttle shuttle = getExpandShuttle(calc.program);
  final RexNode condition = shuttle.apply(calc.program.getCondition());
  final List<RexNode> projects = new ArrayList<>();
  for (RexNode rex: shuttle.apply(calc.program.getProjectList())) {
    projects.add(rex);
  }
  if (condition == null) {
    return Pair.of(calc.cluster.getRexBuilder().makeLiteral(true), projects);
  } else {
    return Pair.of(condition, projects);
  }
}
 
Example #25
Source File: SolrProject.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void implement(Implementor implementor) {
  implementor.visitChild(0, getInput());
  final SolrRules.RexToSolrTranslator translator = new SolrRules.RexToSolrTranslator(
      (JavaTypeFactory) getCluster().getTypeFactory(), SolrRules.solrFieldNames(getInput().getRowType()));
  for (Pair<RexNode, String> pair : getNamedProjects()) {
    final String name = pair.right;
    final String expr = pair.left.accept(translator);
    implementor.addFieldMapping(name, expr, false);
  }
}
 
Example #26
Source File: JdbcTable.java    From calcite with 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 #27
Source File: TestFilterFinder.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Test
public void typeMismatchFailure(){
  final RexNode node = builder.makeCall(SqlStdOperatorTable.EQUALS,
      builder.makeInputRef(factory.createSqlType(SqlTypeName.INTEGER), 0),
      builder.makeBigintLiteral(BigDecimal.ONE)
      );

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

  assertEquals(0, conditions.size());
  assertTrue(holder.hasRemainingExpression());
}
 
Example #28
Source File: RelFieldTrimmer.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
 * {@link org.apache.calcite.rel.logical.LogicalFilter}.
 */
public TrimResult trimFields(Filter filter, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    final RelDataType rowType = filter.getRowType();
    final int fieldCount = rowType.getFieldCount();
    final RexNode conditionExpr = filter.getCondition();
    final RelNode input = filter.getInput();

    // We use the fields used by the consumer, plus any fields used in the
    // filter.
    final Set<RelDataTypeField> inputExtraFields = new LinkedHashSet<>(extraFields);
    RelOptUtil.InputFinder inputFinder = new RelOptUtil.InputFinder(inputExtraFields);
    inputFinder.inputBitSet.addAll(fieldsUsed);
    conditionExpr.accept(inputFinder);
    final ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();

    // Create input with trimmed columns.
    TrimResult trimResult = trimChild(filter, input, inputFieldsUsed, inputExtraFields);
    RelNode newInput = trimResult.left;
    final Mapping inputMapping = trimResult.right;

    // If the input is unchanged, and we need to project all columns,
    // there's nothing we can do.
    if (newInput == input && fieldsUsed.cardinality() == fieldCount) {
        return result(filter, Mappings.createIdentity(fieldCount));
    }

    // Build new project expressions, and populate the mapping.
    final RexVisitor<RexNode> shuttle = new RexPermuteInputsShuttle(inputMapping, newInput);
    RexNode newConditionExpr = conditionExpr.accept(shuttle);

    // Use copy rather than relBuilder so that correlating variables get set.
    relBuilder.push(filter.copy(filter.getTraitSet(), newInput, newConditionExpr));

    // The result has the same mapping as the input gave us. Sometimes we
    // return fields that the consumer didn't ask for, because the filter
    // needs them for its condition.
    return result(relBuilder.build(), inputMapping);
}
 
Example #29
Source File: RelMdSelectivity.java    From calcite with 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 #30
Source File: EnumerableTableModifyExtension.java    From kareldb with 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);
}