Java Code Examples for org.apache.calcite.rex.RexBuilder#makeCall()

The following examples show how to use org.apache.calcite.rex.RexBuilder#makeCall() . 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: AuxiliaryConverter.java    From calcite with Apache License 2.0 6 votes vote down vote up
public RexNode convert(RexBuilder rexBuilder, RexNode groupCall,
    RexNode e) {
  switch (f.getKind()) {
  case TUMBLE_START:
  case HOP_START:
  case SESSION_START:
  case SESSION_END:
    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 File: RelMdSelectivity.java    From calcite with 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 File: RelOptUtil.java    From calcite with 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 4
Source File: AuxiliaryConverter.java    From Bats with 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 File: AuxiliaryConverter.java    From flink with 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 6
Source File: ClassRowTypeCache.java    From mat-calcite-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public RexNode apply(RexBuilderContext context) {
	RelOptCluster cluster = context.getCluster();
	RelDataTypeFactory typeFactory = cluster.getTypeFactory();
	final SqlFunction UDF =
			new SqlUserDefinedFunction(
					new SqlIdentifier("RESOLVE_SIMPLE", SqlParserPos.ZERO),
					ReturnTypes.explicit(typeFactory.createJavaType(Object.class)),
					null,
					OperandTypes.ANY_ANY,
					ImmutableList.of(typeFactory.createTypeWithNullability(typeFactory.createJavaType(IObject.class), false),
							typeFactory.createJavaType(int.class)),
					ScalarFunctionImpl.create(IObjectMethods.class, "resolveSimpleValue"));
	RexBuilder b = context.getBuilder();
	RexNode rexNode = b.makeCall(UDF, context.getIObject(), b.makeLiteral(name));
	return b.makeCast(dataType, rexNode);
}
 
Example 7
Source File: RelMdSelectivity.java    From Bats with Apache License 2.0 6 votes vote down vote up
public Double getSelectivity(SemiJoin rel, RelMetadataQuery mq,
    RexNode 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 8
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public static RelNode createNullFilter(RelNode rel, Integer[] fieldOrdinals) {
    RexNode condition = null;
    final RexBuilder rexBuilder = rel.getCluster().getRexBuilder();
    RelDataType rowType = rel.getRowType();
    int n;
    if (fieldOrdinals != null) {
        n = fieldOrdinals.length;
    } else {
        n = rowType.getFieldCount();
    }
    List<RelDataTypeField> fields = rowType.getFieldList();
    for (int i = 0; i < n; ++i) {
        int iField;
        if (fieldOrdinals != null) {
            iField = fieldOrdinals[i];
        } else {
            iField = i;
        }
        RelDataType type = fields.get(iField).getType();
        if (!type.isNullable()) {
            continue;
        }
        RexNode newCondition = rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL,
                rexBuilder.makeInputRef(type, iField));
        if (condition == null) {
            condition = newCondition;
        } else {
            condition = rexBuilder.makeCall(SqlStdOperatorTable.AND, condition, newCondition);
        }
    }
    if (condition == null) {
        // no filtering required
        return rel;
    }

    final RelFactories.FilterFactory factory = RelFactories.DEFAULT_FILTER_FACTORY;
    return factory.createFilter(rel, condition);
}
 
Example 9
Source File: CreateTableHandler.java    From Bats with Apache License 2.0 5 votes vote down vote up
private RexNode composeDisjunction(final RexBuilder rexBuilder, List<RexNode> compFuncs) {
  final DrillSqlOperator booleanOrFunc
           = new DrillSqlOperator("orNoShortCircuit", 2, true, false);
  RexNode node = compFuncs.remove(0);
  while (!compFuncs.isEmpty()) {
    node = rexBuilder.makeCall(booleanOrFunc, node, compFuncs.remove(0));
  }
  return node;
}
 
Example 10
Source File: SqlSplittableAggFunction.java    From calcite with 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 11
Source File: AggregateExpandDistinctAggregatesRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Given an {@link org.apache.calcite.rel.core.Aggregate}
 * and the ordinals of the arguments to a
 * particular call to an aggregate function, creates a 'select distinct'
 * relational expression which projects the group columns and those
 * arguments but nothing else.
 *
 * <p>For example, given
 *
 * <blockquote>
 * <pre>select f0, count(distinct f1), count(distinct f2)
 * from t group by f0</pre>
 * </blockquote>
 *
 * <p>and the argument list
 *
 * <blockquote>{2}</blockquote>
 *
 * <p>returns
 *
 * <blockquote>
 * <pre>select distinct f0, f2 from t</pre>
 * </blockquote>
 *
 * <p>The <code>sourceOf</code> map is populated with the source of each
 * column; in this case sourceOf.get(0) = 0, and sourceOf.get(1) = 2.
 *
 * @param relBuilder Relational expression builder
 * @param aggregate Aggregate relational expression
 * @param argList   Ordinals of columns to make distinct
 * @param filterArg Ordinal of column to filter on, or -1
 * @param sourceOf  Out parameter, is populated with a map of where each
 *                  output field came from
 * @return Aggregate relational expression which projects the required
 * columns
 */
private RelBuilder createSelectDistinct(RelBuilder relBuilder,
    Aggregate aggregate, List<Integer> argList, int filterArg,
    Map<Integer, Integer> sourceOf) {
  relBuilder.push(aggregate.getInput());
  final List<Pair<RexNode, String>> projects = new ArrayList<>();
  final List<RelDataTypeField> childFields =
      relBuilder.peek().getRowType().getFieldList();
  for (int i : aggregate.getGroupSet()) {
    sourceOf.put(i, projects.size());
    projects.add(RexInputRef.of2(i, childFields));
  }
  for (Integer arg : argList) {
    if (filterArg >= 0) {
      // Implement
      //   agg(DISTINCT arg) FILTER $f
      // by generating
      //   SELECT DISTINCT ... CASE WHEN $f THEN arg ELSE NULL END AS arg
      // and then applying
      //   agg(arg)
      // as usual.
      //
      // It works except for (rare) agg functions that need to see null
      // values.
      final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder();
      final RexInputRef filterRef = RexInputRef.of(filterArg, childFields);
      final Pair<RexNode, String> argRef = RexInputRef.of2(arg, childFields);
      RexNode condition =
          rexBuilder.makeCall(SqlStdOperatorTable.CASE, filterRef,
              argRef.left,
              rexBuilder.makeNullLiteral(argRef.left.getType()));
      sourceOf.put(arg, projects.size());
      projects.add(Pair.of(condition, "i$" + argRef.right));
      continue;
    }
    if (sourceOf.get(arg) != null) {
      continue;
    }
    sourceOf.put(arg, projects.size());
    projects.add(RexInputRef.of2(arg, childFields));
  }
  relBuilder.project(Pair.left(projects), Pair.right(projects));

  // Get the distinct values of the GROUP BY fields and the arguments
  // to the agg functions.
  relBuilder.push(
      aggregate.copy(aggregate.getTraitSet(), relBuilder.build(),
          ImmutableBitSet.range(projects.size()), null, ImmutableList.of()));
  return relBuilder;
}
 
Example 12
Source File: LoptOptimizeJoinRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Determines which join filters can be added to the current join tree. Note
 * that the join filter still reflects the original join ordering. It will
 * only be adjusted to reflect the new join ordering if the "adjust"
 * parameter is set to true.
 *
 * @param multiJoin join factors being optimized
 * @param leftTree left subtree of the join tree
 * @param leftIdx if &ge; 0, only consider filters that reference leftIdx in
 * leftTree; otherwise, consider all filters that reference any factor in
 * leftTree
 * @param rightTree right subtree of the join tree
 * @param filtersToAdd remaining join filters that need to be added; those
 * that are added are removed from the list
 * @param adjust if true, adjust filter to reflect new join ordering
 *
 * @return AND'd expression of the join filters that can be added to the
 * current join tree
 */
private RexNode addFilters(
    LoptMultiJoin multiJoin,
    LoptJoinTree leftTree,
    int leftIdx,
    LoptJoinTree rightTree,
    List<RexNode> filtersToAdd,
    boolean adjust) {
  // loop through the remaining filters to be added and pick out the
  // ones that reference only the factors in the new join tree
  final RexBuilder rexBuilder =
      multiJoin.getMultiJoinRel().getCluster().getRexBuilder();
  final ImmutableBitSet.Builder childFactorBuilder =
      ImmutableBitSet.builder();
  childFactorBuilder.addAll(rightTree.getTreeOrder());
  if (leftIdx >= 0) {
    childFactorBuilder.set(leftIdx);
  } else {
    childFactorBuilder.addAll(leftTree.getTreeOrder());
  }
  for (int child : rightTree.getTreeOrder()) {
    childFactorBuilder.set(child);
  }

  final ImmutableBitSet childFactor = childFactorBuilder.build();
  RexNode condition = null;
  final ListIterator<RexNode> filterIter = filtersToAdd.listIterator();
  while (filterIter.hasNext()) {
    RexNode joinFilter = filterIter.next();
    ImmutableBitSet filterBitmap =
        multiJoin.getFactorsRefByJoinFilter(joinFilter);

    // if all factors in the join filter are in the join tree,
    // AND the filter to the current join condition
    if (childFactor.contains(filterBitmap)) {
      if (condition == null) {
        condition = joinFilter;
      } else {
        condition =
            rexBuilder.makeCall(
                SqlStdOperatorTable.AND,
                condition,
                joinFilter);
      }
      filterIter.remove();
    }
  }

  if (adjust && (condition != null)) {
    int [] adjustments = new int[multiJoin.getNumTotalFields()];
    if (needsAdjustment(
        multiJoin,
        adjustments,
        leftTree,
        rightTree,
        false)) {
      condition =
          condition.accept(
              new RelOptUtil.RexInputConverter(
                  rexBuilder,
                  multiJoin.getMultiJoinFields(),
                  leftTree.getJoinTree().getRowType().getFieldList(),
                  rightTree.getJoinTree().getRowType().getFieldList(),
                  adjustments));
    }
  }

  if (condition == null) {
    condition = rexBuilder.makeLiteral(true);
  }

  return condition;
}
 
Example 13
Source File: FlinkAggregateExpandDistinctAggregatesRule.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Given an {@link org.apache.calcite.rel.core.Aggregate}
 * and the ordinals of the arguments to a
 * particular call to an aggregate function, creates a 'select distinct'
 * relational expression which projects the group columns and those
 * arguments but nothing else.
 *
 * <p>For example, given
 *
 * <blockquote>
 * <pre>select f0, count(distinct f1), count(distinct f2)
 * from t group by f0</pre>
 * </blockquote>
 *
 * <p>and the argument list
 *
 * <blockquote>{2}</blockquote>
 *
 * <p>returns
 *
 * <blockquote>
 * <pre>select distinct f0, f2 from t</pre>
 * </blockquote>
 *
 * <p>The <code>sourceOf</code> map is populated with the source of each
 * column; in this case sourceOf.get(0) = 0, and sourceOf.get(1) = 2.
 *
 * @param relBuilder Relational expression builder
 * @param aggregate Aggregate relational expression
 * @param argList   Ordinals of columns to make distinct
 * @param filterArg Ordinal of column to filter on, or -1
 * @param sourceOf  Out parameter, is populated with a map of where each
 *                  output field came from
 * @return Aggregate relational expression which projects the required
 * columns
 */
private RelBuilder createSelectDistinct(RelBuilder relBuilder,
		Aggregate aggregate, List<Integer> argList, int filterArg,
		Map<Integer, Integer> sourceOf) {
	relBuilder.push(aggregate.getInput());
	final List<Pair<RexNode, String>> projects = new ArrayList<>();
	final List<RelDataTypeField> childFields =
			relBuilder.peek().getRowType().getFieldList();
	for (int i : aggregate.getGroupSet()) {
		sourceOf.put(i, projects.size());
		projects.add(RexInputRef.of2(i, childFields));
	}
	if (filterArg >= 0) {
		sourceOf.put(filterArg, projects.size());
		projects.add(RexInputRef.of2(filterArg, childFields));
	}
	for (Integer arg : argList) {
		if (filterArg >= 0) {
			// Implement
			//   agg(DISTINCT arg) FILTER $f
			// by generating
			//   SELECT DISTINCT ... CASE WHEN $f THEN arg ELSE NULL END AS arg
			// and then applying
			//   agg(arg)
			// as usual.
			//
			// It works except for (rare) agg functions that need to see null
			// values.
			final RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder();
			final RexInputRef filterRef = RexInputRef.of(filterArg, childFields);
			final Pair<RexNode, String> argRef = RexInputRef.of2(arg, childFields);
			RexNode condition =
					rexBuilder.makeCall(SqlStdOperatorTable.CASE, filterRef,
							argRef.left,
							rexBuilder.makeNullLiteral(argRef.left.getType()));
			sourceOf.put(arg, projects.size());
			projects.add(Pair.of(condition, "i$" + argRef.right));
			continue;
		}
		if (sourceOf.get(arg) != null) {
			continue;
		}
		sourceOf.put(arg, projects.size());
		projects.add(RexInputRef.of2(arg, childFields));
	}
	relBuilder.project(Pair.left(projects), Pair.right(projects));

	// Get the distinct values of the GROUP BY fields and the arguments
	// to the agg functions.
	relBuilder.push(
			aggregate.copy(aggregate.getTraitSet(), relBuilder.build(),
					ImmutableBitSet.range(projects.size()),
					null, com.google.common.collect.ImmutableList.<AggregateCall>of()));
	return relBuilder;
}
 
Example 14
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 4 votes vote down vote up
private RexNode flattenComparison(
    RexBuilder rexBuilder,
    SqlOperator op,
    List<RexNode> exprs) {
  final List<Pair<RexNode, String>> flattenedExps = new ArrayList<>();
  flattenProjections(this, exprs, null, "", flattenedExps);
  int n = flattenedExps.size() / 2;
  boolean negate = false;
  if (op.getKind() == SqlKind.NOT_EQUALS) {
    negate = true;
    op = SqlStdOperatorTable.EQUALS;
  }
  if ((n > 1) && op.getKind() != SqlKind.EQUALS) {
    throw Util.needToImplement(
        "inequality comparison for row types");
  }
  RexNode conjunction = null;
  for (int i = 0; i < n; ++i) {
    RexNode comparison =
        rexBuilder.makeCall(
            op,
            flattenedExps.get(i).left,
            flattenedExps.get(i + n).left);
    if (conjunction == null) {
      conjunction = comparison;
    } else {
      conjunction =
          rexBuilder.makeCall(
              SqlStdOperatorTable.AND,
              conjunction,
              comparison);
    }
  }
  if (negate) {
    return rexBuilder.makeCall(
        SqlStdOperatorTable.NOT,
        conjunction);
  } else {
    return conjunction;
  }
}
 
Example 15
Source File: DrillReduceAggregatesRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RexNode reduceAvg(
    Aggregate oldAggRel,
    AggregateCall oldCall,
    List<AggregateCall> newCalls,
    Map<AggregateCall, RexNode> aggCallMapping) {
  final PlannerSettings plannerSettings = (PlannerSettings) oldAggRel.getCluster().getPlanner().getContext();
  final boolean isInferenceEnabled = plannerSettings.isTypeInferenceEnabled();
  final int nGroups = oldAggRel.getGroupCount();
  RelDataTypeFactory typeFactory =
      oldAggRel.getCluster().getTypeFactory();
  RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
  int iAvgInput = oldCall.getArgList().get(0);
  RelDataType avgInputType =
      getFieldType(
          oldAggRel.getInput(),
          iAvgInput);
  RelDataType sumType =
      TypeInferenceUtils.getDrillSqlReturnTypeInference(SqlKind.SUM.name(),
          ImmutableList.of())
        .inferReturnType(oldCall.createBinding(oldAggRel));
  sumType =
      typeFactory.createTypeWithNullability(
          sumType,
          sumType.isNullable() || nGroups == 0);
  SqlAggFunction sumAgg =
      new DrillCalciteSqlAggFunctionWrapper(new SqlSumEmptyIsZeroAggFunction(), sumType);
  AggregateCall sumCall = AggregateCall.create(sumAgg, oldCall.isDistinct(),
      oldCall.isApproximate(), oldCall.getArgList(), -1, sumType, null);
  final SqlCountAggFunction countAgg = (SqlCountAggFunction) SqlStdOperatorTable.COUNT;
  final RelDataType countType = countAgg.getReturnType(typeFactory);
  AggregateCall countCall = AggregateCall.create(countAgg, oldCall.isDistinct(),
      oldCall.isApproximate(), oldCall.getArgList(), -1, countType, null);

  RexNode tmpsumRef =
      rexBuilder.addAggCall(
          sumCall,
          nGroups,
          oldAggRel.indicator,
          newCalls,
          aggCallMapping,
          ImmutableList.of(avgInputType));

  RexNode tmpcountRef =
      rexBuilder.addAggCall(
          countCall,
          nGroups,
          oldAggRel.indicator,
          newCalls,
          aggCallMapping,
          ImmutableList.of(avgInputType));

  RexNode n = rexBuilder.makeCall(SqlStdOperatorTable.CASE,
      rexBuilder.makeCall(SqlStdOperatorTable.EQUALS,
          tmpcountRef, rexBuilder.makeExactLiteral(BigDecimal.ZERO)),
          rexBuilder.constantNull(),
          tmpsumRef);

  // NOTE:  these references are with respect to the output
  // of newAggRel
  /*
  RexNode numeratorRef =
      rexBuilder.makeCall(CastHighOp,
        rexBuilder.addAggCall(
            sumCall,
            nGroups,
            newCalls,
            aggCallMapping,
            ImmutableList.of(avgInputType))
      );
  */
  RexNode numeratorRef = rexBuilder.makeCall(CastHighOp,  n);

  RexNode denominatorRef =
      rexBuilder.addAggCall(
          countCall,
          nGroups,
          oldAggRel.indicator,
          newCalls,
          aggCallMapping,
          ImmutableList.of(avgInputType));
  if (isInferenceEnabled) {
    return rexBuilder.makeCall(
        new DrillSqlOperator(
            "divide",
            2,
            true,
            oldCall.getType(), false),
        numeratorRef,
        denominatorRef);
  } else {
    final RexNode divideRef =
        rexBuilder.makeCall(
            SqlStdOperatorTable.DIVIDE,
            numeratorRef,
            denominatorRef);
    return rexBuilder.makeCast(
        typeFactory.createSqlType(SqlTypeName.ANY), divideRef);
  }
}
 
Example 16
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RexNode and(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
  return rexBuilder.makeCall(SqlStdOperatorTable.AND, a0, a1);
}
 
Example 17
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
private static RexNode divideInt(RexBuilder rexBuilder, RexNode a0,
    RexNode a1) {
  return rexBuilder.makeCall(SqlStdOperatorTable.DIVIDE_INTEGER, a0, a1);
}
 
Example 18
Source File: StandardConvertletTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
private RexNode minus(RexBuilder rexBuilder, RexNode a0, RexNode a1) {
  return rexBuilder.makeCall(SqlStdOperatorTable.MINUS, a0, a1);
}
 
Example 19
Source File: RelOptUtil.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for
 * {@link #splitJoinCondition(RexBuilder, int, RexNode, List, List, List, List)} and
 * {@link #splitJoinCondition(List, List, RexNode, List, List, List, List)}.
 *
 * <p>If the given expr <code>call</code> is an expanded version of
 * IS NOT DISTINCT FROM function call, collapse it and return a
 * IS NOT DISTINCT FROM function call.
 *
 * <p>For example: {@code t1.key IS NOT DISTINCT FROM t2.key}
 * can rewritten in expanded form as
 * {@code t1.key = t2.key OR (t1.key IS NULL AND t2.key IS NULL)}.
 *
 * @param call       Function expression to try collapsing.
 * @param rexBuilder {@link RexBuilder} instance to create new {@link RexCall} instances.
 * @return If the given function is an expanded IS NOT DISTINCT FROM function call,
 *         return a IS NOT DISTINCT FROM function call. Otherwise return the input
 *         function call as it is.
 */
private static RexCall collapseExpandedIsNotDistinctFromExpr(final RexCall call, final RexBuilder rexBuilder) {
    if (call.getKind() != SqlKind.OR || call.getOperands().size() != 2) {
        return call;
    }

    final RexNode op0 = call.getOperands().get(0);
    final RexNode op1 = call.getOperands().get(1);

    if (!(op0 instanceof RexCall) || !(op1 instanceof RexCall)) {
        return call;
    }

    RexCall opEqCall = (RexCall) op0;
    RexCall opNullEqCall = (RexCall) op1;

    if (opEqCall.getKind() == SqlKind.AND && opNullEqCall.getKind() == SqlKind.EQUALS) {
        RexCall temp = opEqCall;
        opEqCall = opNullEqCall;
        opNullEqCall = temp;
    }

    if (opNullEqCall.getKind() != SqlKind.AND || opNullEqCall.getOperands().size() != 2
            || opEqCall.getKind() != SqlKind.EQUALS) {
        return call;
    }

    final RexNode op10 = opNullEqCall.getOperands().get(0);
    final RexNode op11 = opNullEqCall.getOperands().get(1);
    if (op10.getKind() != SqlKind.IS_NULL || op11.getKind() != SqlKind.IS_NULL) {
        return call;
    }
    final RexNode isNullInput0 = ((RexCall) op10).getOperands().get(0);
    final RexNode isNullInput1 = ((RexCall) op11).getOperands().get(0);

    final String isNullInput0Digest = isNullInput0.toString();
    final String isNullInput1Digest = isNullInput1.toString();
    final String equalsInput0Digest = opEqCall.getOperands().get(0).toString();
    final String equalsInput1Digest = opEqCall.getOperands().get(1).toString();

    if ((isNullInput0Digest.equals(equalsInput0Digest) && isNullInput1Digest.equals(equalsInput1Digest))
            || (isNullInput1Digest.equals(equalsInput0Digest) && isNullInput0Digest.equals(equalsInput1Digest))) {
        return (RexCall) rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_DISTINCT_FROM,
                ImmutableList.of(isNullInput0, isNullInput1));
    }

    return call;
}
 
Example 20
Source File: StandardConvertletTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RexNode convertCall(SqlRexContext cx, SqlCall call) {
  // TIMESTAMPDIFF(unit, t1, t2)
  //    => (t2 - t1) UNIT
  final RexBuilder rexBuilder = cx.getRexBuilder();
  final SqlLiteral unitLiteral = call.operand(0);
  TimeUnit unit = unitLiteral.symbolValue(TimeUnit.class);
  BigDecimal multiplier = BigDecimal.ONE;
  BigDecimal divider = BigDecimal.ONE;
  SqlTypeName sqlTypeName = unit == TimeUnit.NANOSECOND
      ? SqlTypeName.BIGINT
      : SqlTypeName.INTEGER;
  switch (unit) {
  case MICROSECOND:
  case MILLISECOND:
  case NANOSECOND:
  case WEEK:
    multiplier = BigDecimal.valueOf(DateTimeUtils.MILLIS_PER_SECOND);
    divider = unit.multiplier;
    unit = TimeUnit.SECOND;
    break;
  case QUARTER:
    divider = unit.multiplier;
    unit = TimeUnit.MONTH;
    break;
  }
  final SqlIntervalQualifier qualifier =
      new SqlIntervalQualifier(unit, null, SqlParserPos.ZERO);
  final RexNode op2 = cx.convertExpression(call.operand(2));
  final RexNode op1 = cx.convertExpression(call.operand(1));
  final RelDataType intervalType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlIntervalType(qualifier),
          op1.getType().isNullable() || op2.getType().isNullable());
  final RexCall rexCall = (RexCall) rexBuilder.makeCall(
      intervalType, SqlStdOperatorTable.MINUS_DATE,
      ImmutableList.of(op2, op1));
  final RelDataType intType =
      cx.getTypeFactory().createTypeWithNullability(
          cx.getTypeFactory().createSqlType(sqlTypeName),
          SqlTypeUtil.containsNullable(rexCall.getType()));
  RexNode e = rexBuilder.makeCast(intType, rexCall);
  return rexBuilder.multiplyDivide(e, multiplier, divider);
}