Java Code Examples for org.apache.calcite.sql.fun.SqlStdOperatorTable#IS_NOT_DISTINCT_FROM

The following examples show how to use org.apache.calcite.sql.fun.SqlStdOperatorTable#IS_NOT_DISTINCT_FROM . 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: RelOptUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
public static SqlOperator op(SqlKind kind, SqlOperator operator) {
    switch (kind) {
    case EQUALS:
        return SqlStdOperatorTable.EQUALS;
    case NOT_EQUALS:
        return SqlStdOperatorTable.NOT_EQUALS;
    case GREATER_THAN:
        return SqlStdOperatorTable.GREATER_THAN;
    case GREATER_THAN_OR_EQUAL:
        return SqlStdOperatorTable.GREATER_THAN_OR_EQUAL;
    case LESS_THAN:
        return SqlStdOperatorTable.LESS_THAN;
    case LESS_THAN_OR_EQUAL:
        return SqlStdOperatorTable.LESS_THAN_OR_EQUAL;
    case IS_DISTINCT_FROM:
        return SqlStdOperatorTable.IS_DISTINCT_FROM;
    case IS_NOT_DISTINCT_FROM:
        return SqlStdOperatorTable.IS_NOT_DISTINCT_FROM;
    default:
        return operator;
    }
}
 
Example 2
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 3
Source File: RelOptUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static SqlOperator op(SqlKind kind, SqlOperator operator) {
  switch (kind) {
  case EQUALS:
    return SqlStdOperatorTable.EQUALS;
  case NOT_EQUALS:
    return SqlStdOperatorTable.NOT_EQUALS;
  case GREATER_THAN:
    return SqlStdOperatorTable.GREATER_THAN;
  case GREATER_THAN_OR_EQUAL:
    return SqlStdOperatorTable.GREATER_THAN_OR_EQUAL;
  case LESS_THAN:
    return SqlStdOperatorTable.LESS_THAN;
  case LESS_THAN_OR_EQUAL:
    return SqlStdOperatorTable.LESS_THAN_OR_EQUAL;
  case IS_DISTINCT_FROM:
    return SqlStdOperatorTable.IS_DISTINCT_FROM;
  case IS_NOT_DISTINCT_FROM:
    return SqlStdOperatorTable.IS_NOT_DISTINCT_FROM;
  default:
    return operator;
  }
}
 
Example 4
Source File: FilterRemoveIsNotDistinctFromRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitCall(RexCall call) {
    RexNode newCall = super.visitCall(call);

    if (call.getOperator() == SqlStdOperatorTable.IS_NOT_DISTINCT_FROM) {
        RexCall tmpCall = (RexCall) newCall;
        newCall = RelOptUtil.isDistinctFrom(rexBuilder, tmpCall.getOperands().get(0),
                tmpCall.getOperands().get(1), true);
    }
    return newCall;
}
 
Example 5
Source File: FilterRemoveIsNotDistinctFromRule.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RexNode visitCall(RexCall call) {
  RexNode newCall = super.visitCall(call);

  if (call.getOperator()
      == SqlStdOperatorTable.IS_NOT_DISTINCT_FROM) {
    RexCall tmpCall = (RexCall) newCall;
    newCall =
        RelOptUtil.isDistinctFrom(
            rexBuilder,
            tmpCall.operands.get(0),
            tmpCall.operands.get(1),
            true);
  }
  return newCall;
}
 
Example 6
Source File: RelMdPredicates.java    From Bats with Apache License 2.0 4 votes vote down vote up
/**
 * Infers predicates for a project.
 *
 * <ol>
 * <li>create a mapping from input to projection. Map only positions that
 * directly reference an input column.
 * <li>Expressions that only contain above columns are retained in the
 * Project's pullExpressions list.
 * <li>For e.g. expression 'a + e = 9' below will not be pulled up because 'e'
 * is not in the projection list.
 *
 * <blockquote><pre>
 * inputPullUpExprs:      {a &gt; 7, b + c &lt; 10, a + e = 9}
 * projectionExprs:       {a, b, c, e / 2}
 * projectionPullupExprs: {a &gt; 7, b + c &lt; 10}
 * </pre></blockquote>
 *
 * </ol>
 */
public RelOptPredicateList getPredicates(Project project,
    RelMetadataQuery mq) {
  final RelNode input = project.getInput();
  final RexBuilder rexBuilder = project.getCluster().getRexBuilder();
  final RelOptPredicateList inputInfo = mq.getPulledUpPredicates(input);
  final List<RexNode> projectPullUpPredicates = new ArrayList<>();

  ImmutableBitSet.Builder columnsMappedBuilder = ImmutableBitSet.builder();
  Mapping m = Mappings.create(MappingType.PARTIAL_FUNCTION,
      input.getRowType().getFieldCount(),
      project.getRowType().getFieldCount());

  for (Ord<RexNode> expr : Ord.zip(project.getProjects())) {
    if (expr.e instanceof RexInputRef) {
      int sIdx = ((RexInputRef) expr.e).getIndex();
      m.set(sIdx, expr.i);
      columnsMappedBuilder.set(sIdx);
    // Project can also generate constants. We need to include them.
    } else if (RexLiteral.isNullLiteral(expr.e)) {
      projectPullUpPredicates.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL,
              rexBuilder.makeInputRef(project, expr.i)));
    } else if (RexUtil.isConstant(expr.e)) {
      final List<RexNode> args =
          ImmutableList.of(rexBuilder.makeInputRef(project, expr.i), expr.e);
      final SqlOperator op = args.get(0).getType().isNullable()
          || args.get(1).getType().isNullable()
          ? SqlStdOperatorTable.IS_NOT_DISTINCT_FROM
          : SqlStdOperatorTable.EQUALS;
      projectPullUpPredicates.add(rexBuilder.makeCall(op, args));
    }
  }

  // Go over childPullUpPredicates. If a predicate only contains columns in
  // 'columnsMapped' construct a new predicate based on mapping.
  final ImmutableBitSet columnsMapped = columnsMappedBuilder.build();
  for (RexNode r : inputInfo.pulledUpPredicates) {
    RexNode r2 = projectPredicate(rexBuilder, input, r, columnsMapped);
    if (!r2.isAlwaysTrue()) {
      r2 = r2.accept(new RexPermuteInputsShuttle(m, input));
      projectPullUpPredicates.add(r2);
    }
  }
  return RelOptPredicateList.of(rexBuilder, projectPullUpPredicates);
}
 
Example 7
Source File: RexUtil.java    From Bats with Apache License 2.0 4 votes vote down vote up
static SqlOperator op(SqlKind kind) {
    switch (kind) {
    case IS_FALSE:
        return SqlStdOperatorTable.IS_FALSE;
    case IS_TRUE:
        return SqlStdOperatorTable.IS_TRUE;
    case IS_UNKNOWN:
        return SqlStdOperatorTable.IS_UNKNOWN;
    case IS_NULL:
        return SqlStdOperatorTable.IS_NULL;
    case IS_NOT_FALSE:
        return SqlStdOperatorTable.IS_NOT_FALSE;
    case IS_NOT_TRUE:
        return SqlStdOperatorTable.IS_NOT_TRUE;
    case IS_NOT_NULL:
        return SqlStdOperatorTable.IS_NOT_NULL;
    case IS_DISTINCT_FROM:
        return SqlStdOperatorTable.IS_DISTINCT_FROM;
    case IS_NOT_DISTINCT_FROM:
        return SqlStdOperatorTable.IS_NOT_DISTINCT_FROM;
    case EQUALS:
        return SqlStdOperatorTable.EQUALS;
    case NOT_EQUALS:
        return SqlStdOperatorTable.NOT_EQUALS;
    case LESS_THAN:
        return SqlStdOperatorTable.LESS_THAN;
    case GREATER_THAN:
        return SqlStdOperatorTable.GREATER_THAN;
    case LESS_THAN_OR_EQUAL:
        return SqlStdOperatorTable.LESS_THAN_OR_EQUAL;
    case GREATER_THAN_OR_EQUAL:
        return SqlStdOperatorTable.GREATER_THAN_OR_EQUAL;
    case AND:
        return SqlStdOperatorTable.AND;
    case OR:
        return SqlStdOperatorTable.OR;
    case COALESCE:
        return SqlStdOperatorTable.COALESCE;
    default:
        throw new AssertionError(kind);
    }
}
 
Example 8
Source File: EqualityConvertlet.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static RexNode convertEquality(SqlRexContext cx, SqlCall call) {

    final List<SqlNode> operands = call.getOperandList();
    final RexBuilder rexBuilder = cx.getRexBuilder();
    final List<RexNode> exprs =
      convertExpressionList(cx, operands);
    SqlOperator op = call.getOperator();
    RelDataType type = rexBuilder.deriveReturnType(op, exprs);
    RexCall convertedCall = (RexCall) rexBuilder.makeCall(type, op, RexUtil.flatten(exprs, op));

    // The following is copied from Calcite's StdConvertletTable.convertEqualsOrNotEquals()
    final RexNode op0 = convertedCall.getOperands().get(0);
    final RexNode op1 = convertedCall.getOperands().get(1);
    final RexNode booleanOp;
    final RexNode integerOp;

    if (op0.getType().getSqlTypeName() == SqlTypeName.BOOLEAN
      && SqlTypeName.INT_TYPES.contains(op1.getType().getSqlTypeName())) {
      booleanOp = op0;
      integerOp = op1;
    } else if (SqlTypeName.INT_TYPES.contains(op0.getType().getSqlTypeName())
      && op1.getType().getSqlTypeName() == SqlTypeName.BOOLEAN) {
      booleanOp = op1;
      integerOp = op0;
    } else {
      return convertedCall;
    }

    SqlOperator newOp = (op.getKind() == SqlKind.EQUALS || op.getKind() == SqlKind.NOT_EQUALS) ?
      SqlStdOperatorTable.EQUALS :
      SqlStdOperatorTable.IS_NOT_DISTINCT_FROM;

    return rexBuilder.makeCall(call.getOperator(),
      booleanOp,
      rexBuilder.makeCall(
        SqlStdOperatorTable.CASE,
        rexBuilder.makeCall(
          newOp,
          integerOp,
          rexBuilder.makeZeroLiteral(integerOp.getType())),
        rexBuilder.makeLiteral(false),
        rexBuilder.makeLiteral(true)));
  }
 
Example 9
Source File: RelMdPredicates.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Infers predicates for a project.
 *
 * <ol>
 * <li>create a mapping from input to projection. Map only positions that
 * directly reference an input column.
 * <li>Expressions that only contain above columns are retained in the
 * Project's pullExpressions list.
 * <li>For e.g. expression 'a + e = 9' below will not be pulled up because 'e'
 * is not in the projection list.
 *
 * <blockquote><pre>
 * inputPullUpExprs:      {a &gt; 7, b + c &lt; 10, a + e = 9}
 * projectionExprs:       {a, b, c, e / 2}
 * projectionPullupExprs: {a &gt; 7, b + c &lt; 10}
 * </pre></blockquote>
 *
 * </ol>
 */
public RelOptPredicateList getPredicates(Project project,
    RelMetadataQuery mq) {
  final RelNode input = project.getInput();
  final RexBuilder rexBuilder = project.getCluster().getRexBuilder();
  final RelOptPredicateList inputInfo = mq.getPulledUpPredicates(input);
  final List<RexNode> projectPullUpPredicates = new ArrayList<>();

  ImmutableBitSet.Builder columnsMappedBuilder = ImmutableBitSet.builder();
  Mapping m = Mappings.create(MappingType.PARTIAL_FUNCTION,
      input.getRowType().getFieldCount(),
      project.getRowType().getFieldCount());

  for (Ord<RexNode> expr : Ord.zip(project.getProjects())) {
    if (expr.e instanceof RexInputRef) {
      int sIdx = ((RexInputRef) expr.e).getIndex();
      m.set(sIdx, expr.i);
      columnsMappedBuilder.set(sIdx);
    // Project can also generate constants. We need to include them.
    } else if (RexLiteral.isNullLiteral(expr.e)) {
      projectPullUpPredicates.add(
          rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL,
              rexBuilder.makeInputRef(project, expr.i)));
    } else if (RexUtil.isConstant(expr.e)) {
      final List<RexNode> args =
          ImmutableList.of(rexBuilder.makeInputRef(project, expr.i), expr.e);
      final SqlOperator op = args.get(0).getType().isNullable()
          || args.get(1).getType().isNullable()
          ? SqlStdOperatorTable.IS_NOT_DISTINCT_FROM
          : SqlStdOperatorTable.EQUALS;
      projectPullUpPredicates.add(rexBuilder.makeCall(op, args));
    }
  }

  // Go over childPullUpPredicates. If a predicate only contains columns in
  // 'columnsMapped' construct a new predicate based on mapping.
  final ImmutableBitSet columnsMapped = columnsMappedBuilder.build();
  for (RexNode r : inputInfo.pulledUpPredicates) {
    RexNode r2 = projectPredicate(rexBuilder, input, r, columnsMapped);
    if (!r2.isAlwaysTrue()) {
      r2 = r2.accept(new RexPermuteInputsShuttle(m, input));
      projectPullUpPredicates.add(r2);
    }
  }
  return RelOptPredicateList.of(rexBuilder, projectPullUpPredicates);
}
 
Example 10
Source File: RexUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
static SqlOperator op(SqlKind kind) {
  switch (kind) {
  case IS_FALSE:
    return SqlStdOperatorTable.IS_FALSE;
  case IS_TRUE:
    return SqlStdOperatorTable.IS_TRUE;
  case IS_UNKNOWN:
    return SqlStdOperatorTable.IS_UNKNOWN;
  case IS_NULL:
    return SqlStdOperatorTable.IS_NULL;
  case IS_NOT_FALSE:
    return SqlStdOperatorTable.IS_NOT_FALSE;
  case IS_NOT_TRUE:
    return SqlStdOperatorTable.IS_NOT_TRUE;
  case IS_NOT_NULL:
    return SqlStdOperatorTable.IS_NOT_NULL;
  case IS_DISTINCT_FROM:
    return SqlStdOperatorTable.IS_DISTINCT_FROM;
  case IS_NOT_DISTINCT_FROM:
    return SqlStdOperatorTable.IS_NOT_DISTINCT_FROM;
  case EQUALS:
    return SqlStdOperatorTable.EQUALS;
  case NOT_EQUALS:
    return SqlStdOperatorTable.NOT_EQUALS;
  case LESS_THAN:
    return SqlStdOperatorTable.LESS_THAN;
  case GREATER_THAN:
    return SqlStdOperatorTable.GREATER_THAN;
  case LESS_THAN_OR_EQUAL:
    return SqlStdOperatorTable.LESS_THAN_OR_EQUAL;
  case GREATER_THAN_OR_EQUAL:
    return SqlStdOperatorTable.GREATER_THAN_OR_EQUAL;
  case AND:
    return SqlStdOperatorTable.AND;
  case OR:
    return SqlStdOperatorTable.OR;
  case COALESCE:
    return SqlStdOperatorTable.COALESCE;
  default:
    throw new AssertionError(kind);
  }
}