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

The following examples show how to use org.apache.calcite.rex.RexBuilder. 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: flink   Source File: AuxiliaryConverter.java    License: Apache License 2.0 6 votes vote down vote up
public RexNode convert(RexBuilder rexBuilder, RexNode groupCall,
			RexNode e) {
			return rexBuilder.makeCall(this.f, e);
			// FLINK QUICK FIX
			// we do not use this logic right now
//      switch (f.getKind()) {
//      case TUMBLE_START:
//      case HOP_START:
//      case SESSION_START:
//      case SESSION_END: // TODO: ?
//        return e;
//      case TUMBLE_END:
//        return rexBuilder.makeCall(
//            SqlStdOperatorTable.PLUS, e,
//            ((RexCall) groupCall).operands.get(1));
//      case HOP_END:
//        return rexBuilder.makeCall(
//            SqlStdOperatorTable.PLUS, e,
//            ((RexCall) groupCall).operands.get(2));
//      default:
//        throw new AssertionError("unknown: " + f);
//      }
		}
 
Example 2
Source Project: calcite   Source File: RelMdSelectivity.java    License: Apache License 2.0 6 votes vote down vote up
public Double getSelectivity(Join rel, RelMetadataQuery mq, RexNode predicate) {
  if (!rel.isSemiJoin()) {
    return getSelectivity((RelNode) rel, mq, predicate);
  }
  // create a RexNode representing the selectivity of the
  // semijoin filter and pass it to getSelectivity
  RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  RexNode newPred = RelMdUtil.makeSemiJoinSelectivityRexNode(mq, rel);
  if (predicate != null) {
    newPred =
        rexBuilder.makeCall(
            SqlStdOperatorTable.AND,
            newPred,
            predicate);
  }

  return mq.getSelectivity(rel.getLeft(), newPred);
}
 
Example 3
Source Project: calcite   Source File: DateRangeRules.java    License: Apache License 2.0 6 votes vote down vote up
@Override public void onMatch(RelOptRuleCall call) {
  final Filter filter = call.rel(0);
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final String timeZone = filter.getCluster().getPlanner().getContext()
      .unwrap(CalciteConnectionConfig.class).timeZone();
  final RexNode condition =
      replaceTimeUnits(rexBuilder, filter.getCondition(), timeZone);
  if (condition.equals(filter.getCondition())) {
    return;
  }
  final RelBuilder relBuilder =
      relBuilderFactory.create(filter.getCluster(), null);
  relBuilder.push(filter.getInput())
      .filter(condition);
  call.transformTo(relBuilder.build());
}
 
Example 4
Source Project: Bats   Source File: AuxiliaryConverter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RexNode convert(RexBuilder rexBuilder, RexNode groupCall, RexNode e) {
    switch (f.getKind()) {
    case TUMBLE_START:
    case HOP_START:
    case SESSION_START:
    case SESSION_END: // TODO: ?
        return e;
    case TUMBLE_END:
        return rexBuilder.makeCall(SqlStdOperatorTable.PLUS, e, ((RexCall) groupCall).getOperands().get(1));
    case HOP_END:
        return rexBuilder.makeCall(SqlStdOperatorTable.PLUS, e, ((RexCall) groupCall).getOperands().get(2));
    default:
        throw new AssertionError("unknown: " + f);
    }
}
 
Example 5
Source Project: calcite   Source File: RelOptUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Ands two sets of join filters together, either of which can be null.
 *
 * @param rexBuilder rexBuilder to create AND expression
 * @param left       filter on the left that the right will be AND'd to
 * @param right      filter on the right
 * @return AND'd filter
 *
 * @see org.apache.calcite.rex.RexUtil#composeConjunction
 */
public static RexNode andJoinFilters(
    RexBuilder rexBuilder,
    RexNode left,
    RexNode right) {
  // don't bother AND'ing in expressions that always evaluate to
  // true
  if ((left != null) && !left.isAlwaysTrue()) {
    if ((right != null) && !right.isAlwaysTrue()) {
      left =
          rexBuilder.makeCall(
              SqlStdOperatorTable.AND,
              left,
              right);
    }
  } else {
    left = right;
  }

  // Joins must have some filter
  if (left == null) {
    left = rexBuilder.makeLiteral(true);
  }
  return left;
}
 
Example 6
Source Project: calcite   Source File: RexToLixTranslator.java    License: Apache License 2.0 6 votes vote down vote up
private RexToLixTranslator(RexProgram program,
    JavaTypeFactory typeFactory,
    Expression root,
    InputGetter inputGetter,
    BlockBuilder list,
    RexBuilder builder,
    SqlConformance conformance,
    Function1<String, InputGetter> correlates) {
  this.program = program; // may be null
  this.typeFactory = Objects.requireNonNull(typeFactory);
  this.conformance = Objects.requireNonNull(conformance);
  this.root = Objects.requireNonNull(root);
  this.inputGetter = inputGetter;
  this.list = Objects.requireNonNull(list);
  this.builder = Objects.requireNonNull(builder);
  this.correlates = correlates; // may be null
}
 
Example 7
Source Project: Bats   Source File: RelDecorrelator.java    License: Apache License 2.0 6 votes vote down vote up
private RexNode createCaseExpression(RexInputRef nullInputRef, RexLiteral lit, RexNode rexNode) {
    RexNode[] caseOperands = new RexNode[3];

    // Construct a CASE expression to handle the null indicator.
    //
    // This also covers the case where a left correlated sub-query
    // projects fields from outer relation. Since LOJ cannot produce
    // nulls on the LHS, the projection now need to make a nullable LHS
    // reference using a nullability indicator. If this this indicator
    // is null, it means the sub-query does not produce any value. As a
    // result, any RHS ref by this usbquery needs to produce null value.

    // WHEN indicator IS NULL
    caseOperands[0] = rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, RexBuilder.getRexFactory().makeInputRef(
            nullInputRef.getIndex(), typeFactory.createTypeWithNullability(nullInputRef.getType(), true)));

    // THEN CAST(NULL AS newInputTypeNullable)
    caseOperands[1] = rexBuilder.makeCast(typeFactory.createTypeWithNullability(rexNode.getType(), true), lit);

    // ELSE cast (newInput AS newInputTypeNullable) END
    caseOperands[2] = rexBuilder.makeCast(typeFactory.createTypeWithNullability(rexNode.getType(), true),
            rexNode);

    return rexBuilder.makeCall(SqlStdOperatorTable.CASE, caseOperands);
}
 
Example 8
Source Project: dremio-oss   Source File: FlattenConvertlet.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
  SqlFlattenOperator operator = (SqlFlattenOperator) call.getOperator();
  final List<RexNode> exprs = new LinkedList<>();

  for (SqlNode node : call.getOperandList()) {
    exprs.add(cx.convertExpression(node));
  }

  SqlFlattenOperator indexedOperator = operator.withIndex(((SqlValidatorImpl)cx.getValidator()).nextFlattenIndex());
  final RexBuilder rexBuilder = cx.getRexBuilder();
  // Since we don't have any way of knowing if the output of the flatten is nullable, we should always assume it is.
  // This is especially important when accelerating a count(column) query, because the normalizer will convert it to
  // a count(1) if it thinks this column is non-nullable, and then remove the flatten altogether. This is actually a
  // problem with the fact that flatten is not really a project operator (because it can output more than one row per input).
  RelDataType type = rexBuilder
    .getTypeFactory()
    .createTypeWithNullability(
      rexBuilder
        .getTypeFactory()
        .createSqlType(SqlTypeName.ANY),
      true
    );
  return rexBuilder.makeCall(type, indexedOperator, exprs);

}
 
Example 9
Source Project: Bats   Source File: MaterializedViewSubstitutionVisitor.java    License: Apache License 2.0 6 votes vote down vote up
private static List<RexNode> transformRex(List<RexNode> nodes, final List<RelDataTypeField> oldFields,
        final List<RelDataTypeField> newFields) {
    RexShuttle shuttle = new RexShuttle() {
        @Override
        public RexNode visitInputRef(RexInputRef ref) {
            RelDataTypeField f = oldFields.get(ref.getIndex());
            for (int index = 0; index < newFields.size(); index++) {
                RelDataTypeField newf = newFields.get(index);
                if (f.getKey().equals(newf.getKey()) && f.getValue() == newf.getValue()) {
                    return RexBuilder.getRexFactory().makeInputRef(index, f.getValue());
                }
            }
            throw MatchFailed.INSTANCE;
        }
    };
    return shuttle.apply(nodes);
}
 
Example 10
Source Project: dremio-oss   Source File: FilterMergeCrule.java    License: Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Filter topFilter = call.rel(0);
  final Filter bottomFilter = call.rel(1);

  // use RexPrograms to merge the two FilterRels into a single program
  // so we can convert the two LogicalFilter conditions to directly
  // reference the bottom LogicalFilter's child
  RexBuilder rexBuilder = topFilter.getCluster().getRexBuilder();
  RexProgram bottomProgram = createProgram(bottomFilter);
  RexProgram topProgram = createProgram(topFilter);

  RexProgram mergedProgram = RexProgramBuilder.mergePrograms(topProgram, bottomProgram, rexBuilder);

  RexNode newCondition = mergedProgram.expandLocalRef(mergedProgram.getCondition());

  final RelBuilder relBuilder = call.builder();
  relBuilder.push(bottomFilter.getInput()).filter(newCondition);

  call.transformTo(relBuilder.build());
}
 
Example 11
Source Project: calcite   Source File: RelOptPredicateList.java    License: Apache License 2.0 6 votes vote down vote up
/** Creates a RelOptPredicateList for a join.
 *
 * @param rexBuilder Rex builder
 * @param pulledUpPredicates Predicates that apply to the rows returned by the
 * relational expression
 * @param leftInferredPredicates Predicates that were inferred from the right
 *                               input
 * @param rightInferredPredicates Predicates that were inferred from the left
 *                                input
 */
public static RelOptPredicateList of(RexBuilder rexBuilder,
    Iterable<RexNode> pulledUpPredicates,
    Iterable<RexNode> leftInferredPredicates,
    Iterable<RexNode> rightInferredPredicates) {
  final ImmutableList<RexNode> pulledUpPredicatesList =
      ImmutableList.copyOf(pulledUpPredicates);
  final ImmutableList<RexNode> leftInferredPredicateList =
      ImmutableList.copyOf(leftInferredPredicates);
  final ImmutableList<RexNode> rightInferredPredicatesList =
      ImmutableList.copyOf(rightInferredPredicates);
  if (pulledUpPredicatesList.isEmpty()
      && leftInferredPredicateList.isEmpty()
      && rightInferredPredicatesList.isEmpty()) {
    return EMPTY;
  }
  final ImmutableMap<RexNode, RexNode> constantMap =
      RexUtil.predicateConstants(RexNode.class, rexBuilder,
          pulledUpPredicatesList);
  return new RelOptPredicateList(pulledUpPredicatesList,
      leftInferredPredicateList, rightInferredPredicatesList, constantMap);
}
 
Example 12
Source Project: Bats   Source File: SqlSplittableAggFunction.java    License: 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);
  if (predicate == null) {
    return rexBuilder.makeExactLiteral(BigDecimal.ONE);
  } else {
    return rexBuilder.makeCall(SqlStdOperatorTable.CASE, predicate,
        rexBuilder.makeExactLiteral(BigDecimal.ONE),
        rexBuilder.makeExactLiteral(BigDecimal.ZERO));
  }
}
 
Example 13
Source Project: Bats   Source File: SubstitutionVisitor.java    License: Apache License 2.0 6 votes vote down vote up
protected MutableRel invert(MutableRel model, MutableRel input, MutableProject project) {
    LOGGER.trace("SubstitutionVisitor: invert:\nmodel: {}\ninput: {}\nproject: {}\n", model, input, project);
    if (project.projects.size() < model.rowType.getFieldCount()) {
        throw MatchFailed.INSTANCE;
    }
    final List<RexNode> exprList = new ArrayList<>();
    final RexBuilder rexBuilder = model.cluster.getRexBuilder();
    for (RelDataTypeField field : model.rowType.getFieldList()) {
        exprList.add(rexBuilder.makeZeroLiteral(field.getType()));
    }
    for (Ord<RexNode> expr : Ord.zip(project.projects)) {
        if (expr.e instanceof RexInputRef) {
            final int target = ((RexInputRef) expr.e).getIndex();
            exprList.set(target,
                    rexBuilder.ensureType(expr.e.getType(), RexInputRef.of(expr.i, input.rowType), false));
        } else {
            throw MatchFailed.INSTANCE;
        }
    }
    return MutableProject.of(model.rowType, input, exprList);
}
 
Example 14
Source Project: Bats   Source File: DrillJoinRule.java    License: Apache License 2.0 6 votes vote down vote up
private RexNode buildJoinCondition(RelNode convertedLeft, RelNode convertedRight, List<Integer> leftKeys,
    List<Integer> rightKeys, List<Boolean> filterNulls, RexBuilder builder) {
  List<RexNode> equijoinList = Lists.newArrayList();
  final int numLeftFields = convertedLeft.getRowType().getFieldCount();
  List<RelDataTypeField> leftTypes = convertedLeft.getRowType().getFieldList();
  List<RelDataTypeField> rightTypes = convertedRight.getRowType().getFieldList();

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

    equijoinList.add(builder.makeCall(
         filterNulls.get(i) ? SqlStdOperatorTable.EQUALS : SqlStdOperatorTable.IS_NOT_DISTINCT_FROM,
         builder.makeInputRef(leftTypes.get(leftKeyOrdinal).getType(), leftKeyOrdinal),
         builder.makeInputRef(rightTypes.get(rightKeyOrdinal).getType(), rightKeyOrdinal + numLeftFields)
    ));
  }
  return RexUtil.composeConjunction(builder, equijoinList, false);
}
 
Example 15
Source Project: calcite   Source File: StandardConvertletTable.java    License: Apache License 2.0 6 votes vote down vote up
private static RexNode divide(RexBuilder rexBuilder, RexNode res,
    BigDecimal val) {
  if (val.equals(BigDecimal.ONE)) {
    return res;
  }
  // If val is between 0 and 1, rather than divide by val, multiply by its
  // reciprocal. For example, rather than divide by 0.001 multiply by 1000.
  if (val.compareTo(BigDecimal.ONE) < 0
      && val.signum() == 1) {
    try {
      final BigDecimal reciprocal =
          BigDecimal.ONE.divide(val, RoundingMode.UNNECESSARY);
      return multiply(rexBuilder, res,
          rexBuilder.makeExactLiteral(reciprocal));
    } catch (ArithmeticException e) {
      // ignore - reciprocal is not an integer
    }
  }
  return divideInt(rexBuilder, res, rexBuilder.makeExactLiteral(val));
}
 
Example 16
Source Project: Bats   Source File: DateRangeRules.java    License: Apache License 2.0 6 votes vote down vote up
private RexLiteral dateTimeLiteral(RexBuilder rexBuilder, Calendar calendar, RexNode operand) {
    final TimestampString ts;
    final int p;
    switch (operand.getType().getSqlTypeName()) {
    case TIMESTAMP:
        ts = TimestampString.fromCalendarFields(calendar);
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampLiteral(ts, p);
    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
        ts = TimestampString.fromCalendarFields(calendar);
        final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
        final TimestampString localTs = new TimestampWithTimeZoneString(ts, tz)
                .withTimeZone(DateTimeUtils.UTC_ZONE).getLocalTimestampString();
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampWithLocalTimeZoneLiteral(localTs, p);
    case DATE:
        final DateString d = DateString.fromCalendarFields(calendar);
        return rexBuilder.makeDateLiteral(d);
    default:
        throw Util.unexpected(operand.getType().getSqlTypeName());
    }
}
 
Example 17
Source Project: Bats   Source File: AggregateReduceFunctionsRule.java    License: Apache License 2.0 6 votes vote down vote up
private RexNode getSumAggregatedRexNode(Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping,
    RexBuilder rexBuilder,
    int argOrdinal,
    int filterArg) {
  final AggregateCall aggregateCall =
      AggregateCall.create(SqlStdOperatorTable.SUM,
          oldCall.isDistinct(),
          oldCall.isApproximate(),
          ImmutableIntList.of(argOrdinal),
          filterArg,
          oldCall.collation,
          oldAggRel.getGroupCount(),
          oldAggRel.getInput(),
          null,
          null);
  return rexBuilder.addAggCall(aggregateCall,
      oldAggRel.getGroupCount(),
      oldAggRel.indicator,
      newCalls,
      aggCallMapping,
      ImmutableList.of(aggregateCall.getType()));
}
 
Example 18
Source Project: calcite   Source File: RelOptUtil.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean containsGet(RexNode node) {
  try {
    node.accept(
        new RexVisitorImpl<Void>(true) {
          @Override public Void visitCall(RexCall call) {
            if (call.getOperator() == RexBuilder.GET_OPERATOR) {
              throw Util.FoundOne.NULL;
            }
            return super.visitCall(call);
          }
        });
    return false;
  } catch (Util.FoundOne e) {
    return true;
  }
}
 
Example 19
Source Project: calcite   Source File: StandardConvertletTable.java    License: Apache License 2.0 5 votes vote down vote up
protected RexNode convertFloorCeil(SqlRexContext cx, SqlCall call) {
  final boolean floor = call.getKind() == SqlKind.FLOOR;
  // Rewrite floor, ceil of interval
  if (call.operandCount() == 1
      && call.operand(0) instanceof SqlIntervalLiteral) {
    final SqlIntervalLiteral literal = call.operand(0);
    SqlIntervalLiteral.IntervalValue interval =
        (SqlIntervalLiteral.IntervalValue) literal.getValue();
    BigDecimal val =
        interval.getIntervalQualifier().getStartUnit().multiplier;
    RexNode rexInterval = cx.convertExpression(literal);

    final RexBuilder rexBuilder = cx.getRexBuilder();
    RexNode zero = rexBuilder.makeExactLiteral(BigDecimal.valueOf(0));
    RexNode cond = ge(rexBuilder, rexInterval, zero);

    RexNode pad =
        rexBuilder.makeExactLiteral(val.subtract(BigDecimal.ONE));
    RexNode cast = rexBuilder.makeReinterpretCast(
        rexInterval.getType(), pad, rexBuilder.makeLiteral(false));
    RexNode sum = floor
        ? minus(rexBuilder, rexInterval, cast)
        : plus(rexBuilder, rexInterval, cast);

    RexNode kase = floor
        ? case_(rexBuilder, rexInterval, cond, sum)
        : case_(rexBuilder, sum, cond, rexInterval);

    RexNode factor = rexBuilder.makeExactLiteral(val);
    RexNode div = divideInt(rexBuilder, kase, factor);
    return multiply(rexBuilder, div, factor);
  }

  // normal floor, ceil function
  return convertFunction(cx, (SqlFunction) call.getOperator(), call);
}
 
Example 20
Source Project: flink   Source File: PythonCorrelateSplitRule.java    License: Apache License 2.0 5 votes vote down vote up
private FlinkLogicalCalc createTopCalc(
	int primitiveLeftFieldCount,
	RexBuilder rexBuilder,
	ArrayBuffer<RexNode> extractedRexNodes,
	RelDataType calcRowType,
	FlinkLogicalCorrelate newCorrelate) {
	RexProgram rexProgram = new RexProgramBuilder(newCorrelate.getRowType(), rexBuilder).getProgram();
	int offset = extractedRexNodes.size() + primitiveLeftFieldCount;

	// extract correlate output RexNode.
	List<RexNode> newTopCalcProjects = rexProgram
		.getExprList()
		.stream()
		.filter(x -> x instanceof RexInputRef)
		.filter(x -> {
			int index = ((RexInputRef) x).getIndex();
			return index < primitiveLeftFieldCount || index >= offset;
		})
		.collect(Collectors.toList());

	return new FlinkLogicalCalc(
		newCorrelate.getCluster(),
		newCorrelate.getTraitSet(),
		newCorrelate,
		RexProgram.create(
			newCorrelate.getRowType(),
			newTopCalcProjects,
			null,
			calcRowType,
			rexBuilder));
}
 
Example 21
Source Project: calcite   Source File: RelMdDistinctRowCount.java    License: 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 22
Source Project: dremio-oss   Source File: HashPrelUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static ProjectPrel addHashProject(List<DistributionField> distFields, Prel input, Integer ringCount){

    // Insert Project SqlOperatorImpl with new column that will be a hash for HashToRandomExchange fields

    final List<String> outputFieldNames = Lists.newArrayList(input.getRowType().getFieldNames());
    final String fieldName = ringCount == null ? HashPrelUtil.HASH_EXPR_NAME : WriterPrel.BUCKET_NUMBER_FIELD;
    outputFieldNames.add(fieldName);

    final RexBuilder rexBuilder = input.getCluster().getRexBuilder();
    final List<RelDataTypeField> childRowTypeFields = input.getRowType().getFieldList();

    // create new hashed field.
    final HashExpressionCreatorHelper<RexNode> hashHelper = new RexNodeBasedHashExpressionCreatorHelper(rexBuilder);
    final List<RexNode> distFieldRefs = Lists.newArrayListWithExpectedSize(distFields.size());
    for(int i = 0; i < distFields.size(); i++) {
      final int fieldId = distFields.get(i).getFieldId();
      distFieldRefs.add(rexBuilder.makeInputRef(childRowTypeFields.get(fieldId).getType(), fieldId));
    }

    final List <RexNode> updatedExpr = Lists.newArrayListWithExpectedSize(childRowTypeFields.size() + 1);
    for ( RelDataTypeField field : childRowTypeFields) {
      RexNode rex = rexBuilder.makeInputRef(field.getType(), field.getIndex());
      updatedExpr.add(rex);
    }
    RexNode hashExpression = HashPrelUtil.createHashBasedPartitionExpression(distFieldRefs, hashHelper);

    if(ringCount != null){
      RelDataType intType = input.getCluster().getTypeFactory().createSqlType(SqlTypeName.INTEGER);
      hashExpression = rexBuilder.makeCall(SqlStdOperatorTable.MOD, ImmutableList.of(hashExpression, rexBuilder.makeExactLiteral(BigDecimal.valueOf(ringCount), intType)));
      hashExpression = rexBuilder.makeCall(SqlStdOperatorTable.ABS, Collections.singletonList(hashExpression));
    }
    updatedExpr.add(hashExpression);

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

    ProjectPrel addColumnprojectPrel = ProjectPrel.create(input.getCluster(), input.getTraitSet(), input, updatedExpr, rowType);
    return addColumnprojectPrel;
  }
 
Example 23
Source Project: quark   Source File: FilterAggStarRule.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Pushes a {@link org.apache.calcite.rel.core.Filter}
 * past a {@link org.apache.calcite.rel.core.Aggregate}.
 */
RelNode filterAggregateTranspose(RelOptRuleCall call,
                                 Filter filterRel,
                                 Aggregate aggRel) {
  final List<RexNode> conditions =
      RelOptUtil.conjunctions(filterRel.getCondition());
  final RexBuilder rexBuilder = filterRel.getCluster().getRexBuilder();
  final List<RelDataTypeField> origFields =
      aggRel.getRowType().getFieldList();
  final int[] adjustments = new int[origFields.size()];
  int i = 0;
  for (int key : aggRel.getGroupSet()) {
    adjustments[i] = key - i;
    i++;
  }
  final List<RexNode> pushedConditions = Lists.newArrayList();

  for (RexNode condition : conditions) {
    ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(condition);
    if (canPush(aggRel, rCols)) {
      pushedConditions.add(
          condition.accept(
              new RelOptUtil.RexInputConverter(rexBuilder, origFields,
                  aggRel.getInput(0).getRowType().getFieldList(),
                  adjustments)));
    } else {
      return null;
    }
  }

  final RelBuilder builder = call.builder();
  RelNode rel =
      builder.push(aggRel.getInput()).filter(pushedConditions).build();
  if (rel == aggRel.getInput(0)) {
    return null;
  }
  rel = aggRel.copy(aggRel.getTraitSet(), ImmutableList.of(rel));
  return rel;
}
 
Example 24
Source Project: Bats   Source File: DrillMergeFilterRule.java    License: Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
    Filter topFilter = call.rel(0);
    Filter bottomFilter = call.rel(1);

    // use RexPrograms to merge the two FilterRels into a single program
    // so we can convert the two FilterRel conditions to directly
    // reference the bottom FilterRel's child
    RexBuilder rexBuilder = topFilter.getCluster().getRexBuilder();
    RexProgram bottomProgram = createProgram(bottomFilter);
    RexProgram topProgram = createProgram(topFilter);

    RexProgram mergedProgram =
        RexProgramBuilder.mergePrograms(
            topProgram,
            bottomProgram,
            rexBuilder);

    RexNode newCondition =
        mergedProgram.expandLocalRef(
            mergedProgram.getCondition());

//    if(!RexUtil.isFlat(newCondition)){
//      RexCall newCall = (RexCall) newCondition;
//      newCondition = rexBuilder.makeFlatCall( newCall.getOperator(), newCall.getOperands());
//    }

    Filter newFilterRel =
        (Filter) filterFactory.createFilter(
            bottomFilter.getInput(),
            RexUtil.flatten(rexBuilder, newCondition));

    call.transformTo(newFilterRel);
  }
 
Example 25
Source Project: Bats   Source File: RelMdSelectivity.java    License: Apache License 2.0 5 votes vote down vote up
public Double getSelectivity(Union rel, RelMetadataQuery mq,
    RexNode predicate) {
  if ((rel.getInputs().size() == 0) || (predicate == null)) {
    return 1.0;
  }

  double sumRows = 0.0;
  double sumSelectedRows = 0.0;
  int[] adjustments = new int[rel.getRowType().getFieldCount()];
  RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
  for (RelNode input : rel.getInputs()) {
    Double nRows = mq.getRowCount(input);
    if (nRows == null) {
      return null;
    }

    // convert the predicate to reference the types of the union child
    RexNode modifiedPred =
        predicate.accept(
            new RelOptUtil.RexInputConverter(
                rexBuilder,
                null,
                input.getRowType().getFieldList(),
                adjustments));
    double sel = mq.getSelectivity(input, modifiedPred);

    sumRows += nRows;
    sumSelectedRows += nRows * sel;
  }

  if (sumRows < 1.0) {
    sumRows = 1.0;
  }
  return sumSelectedRows / sumRows;
}
 
Example 26
Source Project: flink   Source File: PythonCorrelateSplitRule.java    License: Apache License 2.0 5 votes vote down vote up
private FlinkLogicalCalc createTopCalc(
	int primitiveLeftFieldCount,
	RexBuilder rexBuilder,
	ArrayBuffer<RexNode> extractedRexNodes,
	RelDataType calcRowType,
	FlinkLogicalCorrelate newCorrelate) {
	RexProgram rexProgram = new RexProgramBuilder(newCorrelate.getRowType(), rexBuilder).getProgram();
	int offset = extractedRexNodes.size() + primitiveLeftFieldCount;

	// extract correlate output RexNode.
	List<RexNode> newTopCalcProjects = rexProgram
		.getExprList()
		.stream()
		.filter(x -> x instanceof RexInputRef)
		.filter(x -> {
			int index = ((RexInputRef) x).getIndex();
			return index < primitiveLeftFieldCount || index >= offset;
		})
		.collect(Collectors.toList());

	return new FlinkLogicalCalc(
		newCorrelate.getCluster(),
		newCorrelate.getTraitSet(),
		newCorrelate,
		RexProgram.create(
			newCorrelate.getRowType(),
			newTopCalcProjects,
			null,
			calcRowType,
			rexBuilder));
}
 
Example 27
Source Project: calcite   Source File: CountingFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Override public RexNode newColumnDefaultValue(RelOptTable table,
    int iColumn, InitializerContext context) {
  THREAD_CALL_COUNT.get().incrementAndGet();
  final RelDataTypeField field =
      table.getRowType().getFieldList().get(iColumn);
  if (defaultColumns.contains(field.getName())) {
    final RexBuilder rexBuilder = context.getRexBuilder();
    return rexBuilder.makeExactLiteral(BigDecimal.ONE);
  }
  return super.newColumnDefaultValue(table, iColumn, context);
}
 
Example 28
Source Project: calcite   Source File: SqlSplittableAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override public RexNode singleton(RexBuilder rexBuilder,
    RelDataType inputRowType, AggregateCall aggregateCall) {
  final int arg = aggregateCall.getArgList().get(0);
  final RelDataType type = inputRowType.getFieldList().get(arg).getType();
  final RelDataTypeFactory typeFactory = rexBuilder.getTypeFactory();
  final RelDataType type1 = typeFactory.getTypeSystem().deriveSumType(typeFactory, type);
  final RexNode inputRef = rexBuilder.makeInputRef(type1, arg);
  if (type.isNullable()) {
    return rexBuilder.makeCall(SqlStdOperatorTable.COALESCE, inputRef,
        rexBuilder.makeExactLiteral(BigDecimal.ZERO, type));
  } else {
    return inputRef;
  }
}
 
Example 29
Source Project: calcite   Source File: MaterializedViewRule.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * It swaps the table references and then the column references of the input
 * expressions using the table mapping and the equivalence classes.
 */
protected NodeLineage generateSwapTableColumnReferencesLineage(
    RexBuilder rexBuilder,
    RelMetadataQuery mq,
    RelNode node,
    BiMap<RelTableRef, RelTableRef> tableMapping,
    EquivalenceClasses ec,
    List<RexNode> nodeExprs) {
  final Map<RexNode, Integer> exprsLineage = new HashMap<>();
  final Map<RexNode, Integer> exprsLineageLosslessCasts = new HashMap<>();
  for (int i = 0; i < nodeExprs.size(); i++) {
    final Set<RexNode> s = mq.getExpressionLineage(node, nodeExprs.get(i));
    if (s == null) {
      // Next expression
      continue;
    }
    // We only support project - filter - join, thus it should map to
    // a single expression
    assert s.size() == 1;
    // Rewrite expr. First we swap the table references following the table
    // mapping, then we take first element from the corresponding equivalence class
    final RexNode e = RexUtil.swapTableColumnReferences(rexBuilder,
        s.iterator().next(), tableMapping, ec.getEquivalenceClassesMap());
    exprsLineage.put(e, i);
    if (RexUtil.isLosslessCast(e)) {
      exprsLineageLosslessCasts.put(((RexCall) e).getOperands().get(0), i);
    }
  }
  return new NodeLineage(exprsLineage, exprsLineageLosslessCasts);
}
 
Example 30
Source Project: Bats   Source File: IndexPlanUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static RexNode getTotalFilter(RexNode leadColsFilter, RexNode totRemColsFilter, RexBuilder rexBuilder) {
  RexNode condition = leadColsFilter;
  if (leadColsFilter != null && totRemColsFilter != null && !totRemColsFilter.isAlwaysTrue()) {
    List<RexNode> conditions = new ArrayList<RexNode>();
    conditions.add(leadColsFilter);
    conditions.add(totRemColsFilter);
    return RexUtil.composeConjunction(rexBuilder, conditions, true);
  }
  return condition;
}