org.apache.calcite.rex.RexUtil Java Examples

The following examples show how to use org.apache.calcite.rex.RexUtil. 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: RelBuilder.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected RelBuilder(Context context, RelOptCluster cluster,
    RelOptSchema relOptSchema) {
  this.cluster = cluster;
  this.relOptSchema = relOptSchema;
  if (context == null) {
    context = Contexts.EMPTY_CONTEXT;
  }
  this.config = getConfig(context);
  this.viewExpander = getViewExpander(cluster, context);
  this.struct =
      Objects.requireNonNull(RelFactories.Struct.fromContext(context));
  final RexExecutor executor =
      Util.first(context.unwrap(RexExecutor.class),
          Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR));
  final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
  this.simplifier =
      new RexSimplify(cluster.getRexBuilder(), predicates, executor);
}
 
Example #2
Source File: EnumerableTraitsUtils.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Determine whether there is mapping between project input and output fields.
 * Bail out if sort relies on non-trivial expressions.
 */
private static boolean isCollationOnTrivialExpr(
    List<RexNode> projects, RelDataTypeFactory typeFactory,
    Mappings.TargetMapping map, RelFieldCollation fc, boolean passDown) {
  final int index = fc.getFieldIndex();
  int target = map.getTargetOpt(index);
  if (target < 0) {
    return false;
  }

  final RexNode node = passDown ? projects.get(index) : projects.get(target);
  if (node.isA(SqlKind.CAST)) {
    // Check whether it is a monotonic preserving cast
    final RexCall cast = (RexCall) node;
    RelFieldCollation newFieldCollation = Objects.requireNonNull(RexUtil.apply(map, fc));
    final RexCallBinding binding =
        RexCallBinding.create(typeFactory, cast,
            ImmutableList.of(RelCollations.of(newFieldCollation)));
    if (cast.getOperator().getMonotonicity(binding)
        == SqlMonotonicity.NOT_MONOTONIC) {
      return false;
    }
  }

  return true;
}
 
Example #3
Source File: JoinFilterCanonicalizationRule.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Build a join condition based on the left/right keys
 *
 * @param leftRowType
 * @param rightRowType
 * @param leftKeys
 * @param rightKeys
 * @param filterNulls
 * @param builder
 * @return a conjunction of equi-join conditions
 */
static RexNode buildJoinCondition(RexBuilder builder, RelDataType leftRowType, RelDataType rightRowType, List<Integer> leftKeys, List<Integer> rightKeys, List<Boolean> filterNulls) {
  final List<RexNode> equijoinList = Lists.newArrayList();
  final int numLeftFields = leftRowType.getFieldCount();
  final List<RelDataTypeField> leftTypes = leftRowType.getFieldList();
  final List<RelDataTypeField> rightTypes = rightRowType.getFieldList();

  for (int i = 0; i < leftKeys.size(); i++) {
    int leftKeyOrdinal = leftKeys.get(i);
    int rightKeyOrdinal = rightKeys.get(i);

    SqlBinaryOperator operator = filterNulls.get(i) ? SqlStdOperatorTable.EQUALS : SqlStdOperatorTable.IS_NOT_DISTINCT_FROM;
    RexNode leftInput = builder.makeInputRef(leftTypes.get(leftKeyOrdinal).getType(), leftKeyOrdinal);
    RexNode rightInput = builder.makeInputRef(rightTypes.get(rightKeyOrdinal).getType(), rightKeyOrdinal + numLeftFields);
    equijoinList.add(builder.makeCall(operator, leftInput, rightInput));
  }

  return RexUtil.composeConjunction(builder, equijoinList, false);
}
 
Example #4
Source File: RelOptUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static RexNode splitCorrelatedFilterCondition(
    Filter filter,
    List<RexNode> joinKeys,
    List<RexNode> correlatedJoinKeys,
    boolean extractCorrelatedFieldAccess) {
  final List<RexNode> nonEquiList = new ArrayList<>();

  splitCorrelatedFilterCondition(
      filter,
      filter.getCondition(),
      joinKeys,
      correlatedJoinKeys,
      nonEquiList,
      extractCorrelatedFieldAccess);

  // Convert the remainders into a list that are AND'ed together.
  return RexUtil.composeConjunction(
      filter.getCluster().getRexBuilder(), nonEquiList, true);
}
 
Example #5
Source File: SqlSplittableAggFunction.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>{@code COUNT(*)}, and {@code COUNT} applied to all NOT NULL arguments,
 * become {@code 1}; otherwise
 * {@code CASE WHEN arg0 IS NOT NULL THEN 1 ELSE 0 END}.
 */
public RexNode singleton(RexBuilder rexBuilder, RelDataType inputRowType,
    AggregateCall aggregateCall) {
  final List<RexNode> predicates = new ArrayList<>();
  for (Integer arg : aggregateCall.getArgList()) {
    final RelDataType type = inputRowType.getFieldList().get(arg).getType();
    if (type.isNullable()) {
      predicates.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
              rexBuilder.makeInputRef(type, arg)));
    }
  }
  final RexNode predicate =
      RexUtil.composeConjunction(rexBuilder, predicates, true);
  final RexNode rexOne = rexBuilder.makeExactLiteral(
      BigDecimal.ONE, aggregateCall.getType());
  if (predicate == null) {
    return rexOne;
  } else {
    return rexBuilder.makeCall(SqlStdOperatorTable.CASE, predicate, rexOne,
        rexBuilder.makeExactLiteral(BigDecimal.ZERO, aggregateCall.getType()));
  }
}
 
Example #6
Source File: FilterRemoveIsNotDistinctFromRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void onMatch(RelOptRuleCall call) {
    Filter oldFilter = call.rel(0);
    RexNode oldFilterCond = oldFilter.getCondition();

    if (RexUtil.findOperatorCall(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM, oldFilterCond) == null) {
        // no longer contains isNotDistinctFromOperator
        return;
    }

    // Now replace all the "a isNotDistinctFrom b"
    // with the RexNode given by RelOptUtil.isDistinctFrom() method

    RemoveIsNotDistinctFromRexShuttle rewriteShuttle = new RemoveIsNotDistinctFromRexShuttle(
            oldFilter.getCluster().getRexBuilder());

    final RelBuilder relBuilder = call.builder();
    final RelNode newFilterRel = relBuilder.push(oldFilter.getInput()).filter(oldFilterCond.accept(rewriteShuttle))
            .build();

    call.transformTo(newFilterRel);
}
 
Example #7
Source File: JoinPrel.java    From Bats with Apache License 2.0 6 votes vote down vote up
private RelNode rename(RelNode input, List<RelDataTypeField> inputFields, List<String> outputFieldNames) {
  if (outputFieldNames.size() == 0) {
    return input;
  }
  List<RexNode> exprs = Lists.newArrayList();

  for (RelDataTypeField field : inputFields) {
    RexNode expr = input.getCluster().getRexBuilder().makeInputRef(field.getType(), field.getIndex());
    exprs.add(expr);
  }

  RelDataType rowType = RexUtil.createStructType(input.getCluster().getTypeFactory(),
      exprs, outputFieldNames, null);

  ProjectPrel proj = new ProjectPrel(input.getCluster(), input.getTraitSet(), input, exprs, rowType);

  return proj;
}
 
Example #8
Source File: RelOptUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static RexCall doCollapseExpandedIsNotDistinctFrom(final RexBuilder rexBuilder,
    final RexCall call, RexCall ifNull0Call, RexCall ifNull1Call, RexCall equalsCall) {
  final RexNode isNullInput0 = ifNull0Call.getOperands().get(0);
  final RexNode isNullInput1 = ifNull1Call.getOperands().get(0);

  final RexNode equalsInput0 = RexUtil
      .removeNullabilityCast(rexBuilder.getTypeFactory(), equalsCall.getOperands().get(0));
  final RexNode equalsInput1 = RexUtil
      .removeNullabilityCast(rexBuilder.getTypeFactory(), equalsCall.getOperands().get(1));

  if ((isNullInput0.equals(equalsInput0) && isNullInput1.equals(equalsInput1))
      || (isNullInput1.equals(equalsInput0) && isNullInput0.equals(equalsInput1))) {
    return (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM,
        ImmutableList.of(isNullInput0, isNullInput1));
  }

  return call;
}
 
Example #9
Source File: InsertHashProjectVisitor.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private Prel visit(ExchangePrel hashPrel, List<DistributionTrait.DistributionField> fields, Prel child) {
  final List<String> childFields = child.getRowType().getFieldNames();


  // Insert Project SqlOperatorImpl with new column that will be a hash for HashToRandomExchange fields
  final ProjectPrel addColumnprojectPrel = HashPrelUtil.addHashProject(fields, child, null);
  final Prel newPrel = (Prel) hashPrel.copy(addColumnprojectPrel.getTraitSet(), Collections.<RelNode>singletonList(addColumnprojectPrel));

  int validRows = newPrel.getRowType().getFieldCount() - 1;
  final List<RelDataTypeField> all = newPrel.getRowType().getFieldList();
  final List<RexNode> keptExprs = new ArrayList<>(validRows);

  final RexBuilder rexBuilder = newPrel.getCluster().getRexBuilder();
  for(int i = 0; i < validRows; i++){
    RexNode rex = rexBuilder.makeInputRef(all.get(i).getType(), i);
    keptExprs.add(rex);
  }

  // remove earlier inserted Project SqlOperatorImpl - since it creates issues down the road in HashJoin
  RelDataType removeRowType = RexUtil.createStructType(newPrel.getCluster().getTypeFactory(), keptExprs, childFields);
  return ProjectPrel.create(newPrel.getCluster(), newPrel.getTraitSet(), newPrel, keptExprs, removeRowType);
}
 
Example #10
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void rewriteRel(Sort rel) {
  RelCollation oldCollation = rel.getCollation();
  final RelNode oldChild = rel.getInput();
  final RelNode newChild = getNewForOldRel(oldChild);
  final Mappings.TargetMapping mapping =
      getNewForOldInputMapping(oldChild);

  // validate
  for (RelFieldCollation field : oldCollation.getFieldCollations()) {
    int oldInput = field.getFieldIndex();
    RelDataType sortFieldType =
        oldChild.getRowType().getFieldList().get(oldInput).getType();
    if (sortFieldType.isStruct()) {
      // TODO jvs 10-Feb-2005
      throw Util.needToImplement("sorting on structured types");
    }
  }
  RelCollation newCollation = RexUtil.apply(mapping, oldCollation);
  Sort newRel =
      LogicalSort.create(newChild, newCollation, rel.offset, rel.fetch);
  setNewForOldRel(rel, newRel);
}
 
Example #11
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static List<RexNode> convertExpressionList(SqlRexContext cx,
    List<SqlNode> nodes, SqlOperandTypeChecker.Consistency consistency) {
  final List<RexNode> exprs = new ArrayList<>();
  for (SqlNode node : nodes) {
    exprs.add(cx.convertExpression(node));
  }
  if (exprs.size() > 1) {
    final RelDataType type =
        consistentType(cx, consistency, RexUtil.types(exprs));
    if (type != null) {
      final List<RexNode> oldExprs = Lists.newArrayList(exprs);
      exprs.clear();
      for (RexNode expr : oldExprs) {
        exprs.add(cx.getRexBuilder().ensureType(type, expr, true));
      }
    }
  }
  return exprs;
}
 
Example #12
Source File: PythonCorrelateSplitRule.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
	FlinkLogicalCorrelate correlate = call.rel(0);
	RelNode right = ((HepRelVertex) correlate.getRight()).getCurrentRel();
	FlinkLogicalTableFunctionScan tableFuncScan;
	if (right instanceof FlinkLogicalTableFunctionScan) {
		tableFuncScan = (FlinkLogicalTableFunctionScan) right;
	} else if (right instanceof FlinkLogicalCalc) {
		Option<FlinkLogicalTableFunctionScan> scan = CorrelateUtil
			.getTableFunctionScan((FlinkLogicalCalc) right);
		if (scan.isEmpty()) {
			return false;
		}
		tableFuncScan = scan.get();
	} else {
		return false;
	}
	RexNode rexNode = tableFuncScan.getCall();
	if (rexNode instanceof RexCall) {
		return PythonUtil.isPythonCall(rexNode, null) && PythonUtil.containsNonPythonCall(rexNode)
			|| PythonUtil.isNonPythonCall(rexNode) && PythonUtil.containsPythonCall(rexNode, null)
			|| (PythonUtil.isPythonCall(rexNode, null) && RexUtil.containsFieldAccess(rexNode));
	}
	return false;
}
 
Example #13
Source File: ReduceDecimalsRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Lookup registered node
 */
private RexNode lookup(RexNode node) {
    Pair<RexNode, String> key = RexUtil.makeKey(node);
    if (irreducible.get(key) != null) {
        return node;
    }
    return results.get(key);
}
 
Example #14
Source File: SqlTypeUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether all types in a collection have the same family, as
 * determined by {@link #isSameFamily(RelDataType, RelDataType)}.
 *
 * @param types Types to check
 * @return true if all types are of the same family
 */
public static boolean areSameFamily(Iterable<RelDataType> types) {
  final List<RelDataType> typeList = ImmutableList.copyOf(types);
  if (Sets.newHashSet(RexUtil.families(typeList)).size() < 2) {
    return true;
  }
  for (Pair<RelDataType, RelDataType> adjacent : Pair.adjacents(typeList)) {
    if (!isSameFamily(adjacent.left, adjacent.right)) {
      return false;
    }
  }
  return true;
}
 
Example #15
Source File: MaterializedViewRule.java    From calcite with Apache License 2.0 5 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 expressions {@code exprsToRewrite}, replacing the
 * {@link RexTableInputRef} by references to the positions in {@code nodeExprs}.
 *
 * <p>The method will return the rewritten expressions. If any of the subexpressions in the input
 * expressions cannot be mapped, it will return null.
 */
protected List<RexNode> rewriteExpressions(
    RexBuilder rexBuilder,
    RelMetadataQuery mq,
    RelNode targetNode,
    RelNode node,
    List<RexNode> nodeExprs,
    BiMap<RelTableRef, RelTableRef> tableMapping,
    EquivalenceClasses ec,
    boolean swapTableColumn,
    List<RexNode> exprsToRewrite) {
  NodeLineage nodeLineage;
  if (swapTableColumn) {
    nodeLineage = generateSwapTableColumnReferencesLineage(rexBuilder, mq, node,
        tableMapping, ec, nodeExprs);
  } else {
    nodeLineage = generateSwapColumnTableReferencesLineage(rexBuilder, mq, node,
        tableMapping, ec, nodeExprs);
  }

  List<RexNode> rewrittenExprs = new ArrayList<>(exprsToRewrite.size());
  for (RexNode exprToRewrite : exprsToRewrite) {
    RexNode rewrittenExpr = replaceWithOriginalReferences(
        rexBuilder, targetNode, nodeLineage, exprToRewrite);
    if (RexUtil.containsTableInputRef(rewrittenExpr) != null) {
      // Some expressions were not present in view output
      return null;
    }
    rewrittenExprs.add(rewrittenExpr);
  }
  return rewrittenExprs;
}
 
Example #16
Source File: PrelFactories.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode createProject(RelNode child,
                             List<? extends RexNode> childExprs, List<String> fieldNames) {
  final RelOptCluster cluster = child.getCluster();
  final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), childExprs, fieldNames);
  final RelNode project = new ProjectPrel(cluster, child.getTraitSet().plus(Prel.DRILL_PHYSICAL),
      child, Lists.newArrayList(childExprs), rowType);

  return project;
}
 
Example #17
Source File: PreProcessLogicalRel.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitCall(final RexCall call) {
    final List<RexNode> clonedOperands = visitList(call.getOperands(), new boolean[] { true });
    final SqlOperator sqlOperator = DrillCalciteWrapperUtility
            .extractSqlOperatorFromWrapper(call.getOperator());
    return RexUtil.flatten(rexBuilder, rexBuilder.makeCall(call.getType(), sqlOperator, clonedOperands));
}
 
Example #18
Source File: SubstitutionVisitor.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SubstitutionVisitor(RelNode target_, RelNode query_,
    ImmutableList<UnifyRule> rules, RelBuilderFactory relBuilderFactory) {
  this.cluster = target_.getCluster();
  final RexExecutor executor =
      Util.first(cluster.getPlanner().getExecutor(), RexUtil.EXECUTOR);
  final RelOptPredicateList predicates = RelOptPredicateList.EMPTY;
  this.simplify =
      new RexSimplify(cluster.getRexBuilder(), predicates, executor);
  this.rules = rules;
  this.query = Holder.of(MutableRels.toMutable(query_));
  this.target = MutableRels.toMutable(target_);
  this.relBuilder = relBuilderFactory.create(cluster, null);
  final Set<MutableRel> parents = Sets.newIdentityHashSet();
  final List<MutableRel> allNodes = new ArrayList<>();
  final MutableRelVisitor visitor =
      new MutableRelVisitor() {
        public void visit(MutableRel node) {
          parents.add(node.getParent());
          allNodes.add(node);
          super.visit(node);
        }
      };
  visitor.go(target);

  // Populate the list of leaves in the tree under "target".
  // Leaves are all nodes that are not parents.
  // For determinism, it is important that the list is in scan order.
  allNodes.removeAll(parents);
  targetLeaves = ImmutableList.copyOf(allNodes);

  allNodes.clear();
  parents.clear();
  visitor.go(query);
  allNodes.removeAll(parents);
  queryLeaves = ImmutableList.copyOf(allNodes);
}
 
Example #19
Source File: RelToSqlConverter.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Transform a datetime plus operation to timestampadd if the dialect requires this.
 */
protected RexNode simplifyDatetimePlus(RexNode node, RexBuilder builder) {
  if (dialect.useTimestampAddInsteadOfDatetimePlus()) {
    final RexNode converted = RexUtil.convertDatetimePlusToTimestampAdd(builder, node);
    return converted != null
      ? converted
      : node;
  }
  return node;
}
 
Example #20
Source File: RelMdDistinctRowCount.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Double getDistinctRowCount(Aggregate rel, RelMetadataQuery mq,
    ImmutableBitSet groupKey, RexNode predicate) {
  if (predicate == null || predicate.isAlwaysTrue()) {
    if (groupKey.isEmpty()) {
      return 1D;
    }
  }
  // determine which predicates can be applied on the child of the
  // aggregate
  final List<RexNode> notPushable = new ArrayList<>();
  final List<RexNode> pushable = new ArrayList<>();
  RelOptUtil.splitFilters(
      rel.getGroupSet(),
      predicate,
      pushable,
      notPushable);
  final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RexNode childPreds =
      RexUtil.composeConjunction(rexBuilder, pushable, true);

  // set the bits as they correspond to the child input
  ImmutableBitSet.Builder childKey = ImmutableBitSet.builder();
  RelMdUtil.setAggChildKeys(groupKey, rel, childKey);

  Double distinctRowCount =
      mq.getDistinctRowCount(rel.getInput(), childKey.build(), childPreds);
  if (distinctRowCount == null) {
    return null;
  } else if (notPushable.isEmpty()) {
    return distinctRowCount;
  } else {
    RexNode preds =
        RexUtil.composeConjunction(rexBuilder, notPushable, true);
    return distinctRowCount * RelMdUtil.guessSelectivity(preds);
  }
}
 
Example #21
Source File: IndexPlanUtils.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static RexNode getTotalRemainderFilter(RexNode indexColsRemFilter, RexNode incColsRemFilter, RexBuilder rexBuilder) {
  if (indexColsRemFilter != null && incColsRemFilter != null) {
    List<RexNode> operands = Lists.newArrayList();
    operands.add(indexColsRemFilter);
    operands.add(incColsRemFilter);
    RexNode totalRemainder = RexUtil.composeConjunction(rexBuilder, operands, false);
    return totalRemainder;
  } else if (indexColsRemFilter != null) {
    return indexColsRemFilter;
  } else {
    return incColsRemFilter;
  }
}
 
Example #22
Source File: IndexConditionInfo.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Given a list of Index Expressions(usually indexed fields/functions from one or a set of indexes),
 * separate a filter condition into
 *     1), relevant subset of conditions (by relevant, it means at least one given index Expression was found) and,
 *     2), the rest in remainderCondition
 * @param relevantPaths
 * @param condition
 * @return
 */
public IndexConditionInfo indexConditionRelatedToFields(List<LogicalExpression> relevantPaths, RexNode condition) {
  // Use the same filter analyzer that is used for partitioning columns
  RewriteCombineBinaryOperators reverseVisitor =
      new RewriteCombineBinaryOperators(true, builder);

  condition = condition.accept(reverseVisitor);

  RexSeparator separator = new RexSeparator(relevantPaths, scan, builder);
  RexNode indexCondition = separator.getSeparatedCondition(condition);

  if (indexCondition == null) {
    return new IndexConditionInfo(null, null, false);
  }

  List<RexNode> conjuncts = RelOptUtil.conjunctions(condition);
  List<RexNode> indexConjuncts = RelOptUtil.conjunctions(indexCondition);
  for (RexNode indexConjunction: indexConjuncts) {
    RexUtil.removeAll(conjuncts, indexConjunction);
  }

  RexNode remainderCondition = RexUtil.composeConjunction(builder, conjuncts, false);

  indexCondition = indexCondition.accept(reverseVisitor);

  return new IndexConditionInfo(indexCondition, remainderCondition, true);
}
 
Example #23
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 #24
Source File: RelMdUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * AND's two predicates together, either of which may be null, removing
 * redundant filters.
 *
 * @param rexBuilder rexBuilder used to construct AND'd RexNode
 * @param pred1      first predicate
 * @param pred2      second predicate
 * @return AND'd predicate or individual predicates if one is null
 */
public static RexNode unionPreds(
    RexBuilder rexBuilder,
    RexNode pred1,
    RexNode pred2) {
  final Set<RexNode> unionList = new LinkedHashSet<>();
  unionList.addAll(RelOptUtil.conjunctions(pred1));
  unionList.addAll(RelOptUtil.conjunctions(pred2));
  return RexUtil.composeConjunction(rexBuilder, unionList, true);
}
 
Example #25
Source File: FilterProjectTransposeRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public FilterProjectTransposeRule(
    Class<? extends Filter> filterClass,
    RelFactories.FilterFactory filterFactory,
    Class<? extends Project> projectClass,
    RelFactories.ProjectFactory projectFactory) {
  this(filterClass, filter -> !RexUtil.containsCorrelation(filter.getCondition()),
      projectClass, project -> true,
      filterFactory == null,
      projectFactory == null,
      RelBuilder.proto(filterFactory, projectFactory));
}
 
Example #26
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitLiteral(RexLiteral literal) {
  // Use nullIndicator to decide whether to project null.
  // Do nothing if the literal is null.
  if (!RexUtil.isNull(literal)
      && projectPulledAboveLeftCorrelator
      && (nullIndicator != null)) {
    return createCaseExpression(nullIndicator, null, literal);
  }
  return literal;
}
 
Example #27
Source File: RelMdUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * AND's two predicates together, either of which may be null, removing
 * redundant filters.
 *
 * @param rexBuilder rexBuilder used to construct AND'd RexNode
 * @param pred1      first predicate
 * @param pred2      second predicate
 * @return AND'd predicate or individual predicates if one is null
 */
public static RexNode unionPreds(
    RexBuilder rexBuilder,
    RexNode pred1,
    RexNode pred2) {
  final Set<RexNode> unionList = new LinkedHashSet<>();
  unionList.addAll(RelOptUtil.conjunctions(pred1));
  unionList.addAll(RelOptUtil.conjunctions(pred2));
  return RexUtil.composeConjunction(rexBuilder, unionList, true);
}
 
Example #28
Source File: Strong.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns whether an expression is definitely null.
 *
 * <p>The answer is based on calls to {@link #isNull} for its constituent
 * expressions, and you may override methods to test hypotheses such as
 * "if {@code x} is null, is {@code x + y} null? */
public boolean isNull(RexNode node) {
  final Policy policy = policy(node.getKind());
  switch (policy) {
  case NOT_NULL:
    return false;
  case ANY:
    return anyNull(((RexCall) node).getOperands());
  default:
    break;
  }

  switch (node.getKind()) {
  case LITERAL:
    return ((RexLiteral) node).isNull();
  // We can only guarantee AND to return NULL if both inputs are NULL  (similar for OR)
  // AND(NULL, FALSE) = FALSE
  case AND:
  case OR:
  case COALESCE:
    return allNull(((RexCall) node).getOperands());
  case NULLIF:
    // NULLIF(null, X) where X can be NULL, returns NULL
    // NULLIF(X, Y) where X is not NULL, then this may return NULL if X = Y, otherwise X.
    return allNull(ImmutableList.of(((RexCall) node).getOperands().get(0)));
  case INPUT_REF:
    return isNull((RexInputRef) node);
  case CASE:
    final RexCall caseCall = (RexCall) node;
    final List<RexNode> caseValues = new ArrayList<>();
    for (int i = 0; i < caseCall.getOperands().size(); i++) {
      if (!RexUtil.isCasePredicate(caseCall, i)) {
        caseValues.add(caseCall.getOperands().get(i));
      }
    }
    return allNull(caseValues);
  default:
    return false;
  }
}
 
Example #29
Source File: FilterRelBase.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private double estimateCpuCost(RelMetadataQuery relMetadataQuery) {
  RelNode child = this.getInput();
  final double rows = relMetadataQuery.getRowCount(child);
  double compNum = rows;
  double rowCompNum = child.getRowType().getFieldCount() * rows ;


  for (int i = 0; i< numConjuncts; i++) {
    RexNode conjFilter = RexUtil.composeConjunction(this.getCluster().getRexBuilder(), conjunctions.subList(0, i + 1), false);
    compNum += RelMdUtil.estimateFilteredRows(child, conjFilter, relMetadataQuery);
  }

  return compNum * DremioCost.COMPARE_CPU_COST + rowCompNum * DremioCost.COPY_COST;
}
 
Example #30
Source File: StarColumnConverter.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Prel visitProject(ProjectPrel prel, Void value) throws RuntimeException {
  // Require prefix rename : there exists other expression, in addition to a star column.
  if (!prefixedForStar  // not set yet.
      && StarColumnHelper.containsStarColumnInProject(prel.getInput().getRowType(), prel.getProjects())
      && prel.getRowType().getFieldNames().size() > 1) {
    prefixedForStar = true;
  }

  // For project, we need make sure that the project's field name is same as the input,
  // when the project expression is RexInPutRef, since we may insert a PAS which will
  // rename the projected fields.

  RelNode child = ((Prel) prel.getInput(0)).accept(this, null);

  List<String> fieldNames = Lists.newArrayList();

  for (Pair<String, RexNode> pair : Pair.zip(prel.getRowType().getFieldNames(), prel.getProjects())) {
    if (pair.right instanceof RexInputRef) {
      String name = child.getRowType().getFieldNames().get(((RexInputRef) pair.right).getIndex());
      fieldNames.add(name);
    } else {
      fieldNames.add(pair.left);
    }
  }

  // Make sure the field names are unique : no allow of duplicate field names in a rowType.
  fieldNames = makeUniqueNames(fieldNames);

  RelDataType rowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(),
      prel.getProjects(), fieldNames, null);

  ProjectPrel newProj = (ProjectPrel) prel.copy(prel.getTraitSet(), child, prel.getProjects(), rowType);

  if (ProjectRemoveRule.isTrivial(newProj)) {
    return (Prel) child;
  } else {
    return newProj;
  }
}