Java Code Examples for org.apache.calcite.sql.validate.SqlMonotonicity#NOT_MONOTONIC

The following examples show how to use org.apache.calcite.sql.validate.SqlMonotonicity#NOT_MONOTONIC . 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: MockCatalogReader.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static List<RelCollation> deduceMonotonicity(
    Prepare.PreparingTable table) {
  final List<RelCollation> collationList = new ArrayList<>();

  // Deduce which fields the table is sorted on.
  int i = -1;
  for (RelDataTypeField field : table.getRowType().getFieldList()) {
    ++i;
    final SqlMonotonicity monotonicity =
        table.getMonotonicity(field.getName());
    if (monotonicity != SqlMonotonicity.NOT_MONOTONIC) {
      final RelFieldCollation.Direction direction =
          monotonicity.isDecreasing()
              ? RelFieldCollation.Direction.DESCENDING
              : RelFieldCollation.Direction.ASCENDING;
      collationList.add(
          RelCollations.of(
              new RelFieldCollation(i, direction)));
    }
  }
  return collationList;
}
 
Example 2
Source File: SqlIdentifier.java    From Bats with Apache License 2.0 6 votes vote down vote up
public SqlMonotonicity getMonotonicity(SqlValidatorScope scope) {
  // for "star" column, whether it's static or dynamic return not_monotonic directly.
  if (Util.last(names).equals("") || DynamicRecordType.isDynamicStarColName(Util.last(names))) {
    return SqlMonotonicity.NOT_MONOTONIC;
  }

  // First check for builtin functions which don't have parentheses,
  // like "LOCALTIME".
  final SqlValidator validator = scope.getValidator();
  SqlCall call =
      SqlUtil.makeCall(
          validator.getOperatorTable(),
          this);
  if (call != null) {
    return call.getMonotonicity(scope);
  }
  final SqlQualified qualified = scope.fullyQualify(this);
  final SqlIdentifier fqId = qualified.identifier;
  return qualified.namespace.resolve().getMonotonicity(Util.last(fqId.names));
}
 
Example 3
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 4
Source File: RexCallBinding.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public SqlMonotonicity getOperandMonotonicity(int ordinal) {
  RexNode operand = operands.get(ordinal);

  if (operand instanceof RexInputRef) {
    for (RelCollation ic : inputCollations) {
      if (ic.getFieldCollations().isEmpty()) {
        continue;
      }

      for (RelFieldCollation rfc : ic.getFieldCollations()) {
        if (rfc.getFieldIndex() == ((RexInputRef) operand).getIndex()) {
          return rfc.direction.monotonicity();
          // TODO: Is it possible to have more than one RelFieldCollation for a RexInputRef?
        }
      }
    }
  } else if (operand instanceof RexCall) {
    final RexCallBinding binding =
        RexCallBinding.create(typeFactory, (RexCall) operand, inputCollations);
    return ((RexCall) operand).getOperator().getMonotonicity(binding);
  }

  return SqlMonotonicity.NOT_MONOTONIC;
}
 
Example 5
Source File: SqlToRelTestBase.java    From calcite with Apache License 2.0 6 votes vote down vote up
private List<RelCollation> deduceMonotonicity(SqlValidatorTable table) {
  final RelDataType rowType = table.getRowType();
  final List<RelCollation> collationList = new ArrayList<>();

  // Deduce which fields the table is sorted on.
  int i = -1;
  for (RelDataTypeField field : rowType.getFieldList()) {
    ++i;
    final SqlMonotonicity monotonicity =
        table.getMonotonicity(field.getName());
    if (monotonicity != SqlMonotonicity.NOT_MONOTONIC) {
      final RelFieldCollation.Direction direction =
          monotonicity.isDecreasing()
              ? RelFieldCollation.Direction.DESCENDING
              : RelFieldCollation.Direction.ASCENDING;
      collationList.add(
          RelCollations.of(new RelFieldCollation(i, direction)));
    }
  }
  return collationList;
}
 
Example 6
Source File: RexCallBinding.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override public SqlMonotonicity getOperandMonotonicity(int ordinal) {
  RexNode operand = operands.get(ordinal);

  if (operand instanceof RexInputRef) {
    for (RelCollation ic : inputCollations) {
      if (ic.getFieldCollations().isEmpty()) {
        continue;
      }

      for (RelFieldCollation rfc : ic.getFieldCollations()) {
        if (rfc.getFieldIndex() == ((RexInputRef) operand).getIndex()) {
          return rfc.direction.monotonicity();
          // TODO: Is it possible to have more than one RelFieldCollation for a RexInputRef?
        }
      }
    }
  } else if (operand instanceof RexCall) {
    final RexCallBinding binding =
        RexCallBinding.create(typeFactory, (RexCall) operand, inputCollations);
    return ((RexCall) operand).getOperator().getMonotonicity(binding);
  }

  return SqlMonotonicity.NOT_MONOTONIC;
}
 
Example 7
Source File: SqlExtractFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  switch (call.getOperandLiteralValue(0, TimeUnitRange.class)) {
  case YEAR:
    return call.getOperandMonotonicity(1).unstrict();
  default:
    return SqlMonotonicity.NOT_MONOTONIC;
  }
}
 
Example 8
Source File: SqlSubstringFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  // SUBSTRING(x FROM 0 FOR constant) has same monotonicity as x
  if (call.getOperandCount() == 3) {
    final SqlMonotonicity mono0 = call.getOperandMonotonicity(0);
    if ((mono0 != SqlMonotonicity.NOT_MONOTONIC)
        && call.getOperandMonotonicity(1) == SqlMonotonicity.CONSTANT
        && call.getOperandLiteralValue(1, BigDecimal.class)
            .equals(BigDecimal.ZERO)
        && call.getOperandMonotonicity(2) == SqlMonotonicity.CONSTANT) {
      return mono0.unstrict();
    }
  }
  return super.getMonotonicity(call);
}
 
Example 9
Source File: RelOptTableImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlMonotonicity getMonotonicity(String columnName) {
  for (RelCollation collation : table.getStatistic().getCollations()) {
    final RelFieldCollation fieldCollation =
        collation.getFieldCollations().get(0);
    final int fieldIndex = fieldCollation.getFieldIndex();
    if (fieldIndex < rowType.getFieldCount()
        && rowType.getFieldNames().get(fieldIndex).equals(columnName)) {
      return fieldCollation.direction.monotonicity();
    }
  }
  return SqlMonotonicity.NOT_MONOTONIC;
}
 
Example 10
Source File: SqlCastFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  RelDataTypeFamily castFrom = call.getOperandType(0).getFamily();
  RelDataTypeFamily castTo = call.getOperandType(1).getFamily();
  if (castFrom instanceof SqlTypeFamily
      && castTo instanceof SqlTypeFamily
      && nonMonotonicCasts.containsEntry(castFrom, castTo)) {
    return SqlMonotonicity.NOT_MONOTONIC;
  } else {
    return call.getOperandMonotonicity(0);
  }
}
 
Example 11
Source File: SqlCastFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  RelDataTypeFamily castFrom = call.getOperandType(0).getFamily();
  RelDataTypeFamily castTo = call.getOperandType(1).getFamily();
  if (castFrom instanceof SqlTypeFamily
      && castTo instanceof SqlTypeFamily
      && nonMonotonicCasts.containsEntry(castFrom, castTo)) {
    return SqlMonotonicity.NOT_MONOTONIC;
  } else {
    return call.getOperandMonotonicity(0);
  }
}
 
Example 12
Source File: SqlExtractFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  switch (call.getOperandLiteralValue(0, TimeUnitRange.class)) {
  case YEAR:
    return call.getOperandMonotonicity(1).unstrict();
  default:
    return SqlMonotonicity.NOT_MONOTONIC;
  }
}
 
Example 13
Source File: SqlSubstringFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  // SUBSTRING(x FROM 0 FOR constant) has same monotonicity as x
  if (call.getOperandCount() == 3) {
    final SqlMonotonicity mono0 = call.getOperandMonotonicity(0);
    if ((mono0 != SqlMonotonicity.NOT_MONOTONIC)
        && call.getOperandMonotonicity(1) == SqlMonotonicity.CONSTANT
        && call.getOperandLiteralValue(1, BigDecimal.class)
            .equals(BigDecimal.ZERO)
        && call.getOperandMonotonicity(2) == SqlMonotonicity.CONSTANT) {
      return mono0.unstrict();
    }
  }
  return super.getMonotonicity(call);
}
 
Example 14
Source File: DremioPrepareTable.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public SqlMonotonicity getMonotonicity(String paramString) {
  return SqlMonotonicity.NOT_MONOTONIC;
}
 
Example 15
Source File: SortProjectTransposeRule.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final Project project = call.rel(1);
  final RelOptCluster cluster = project.getCluster();

  if (sort.getConvention() != project.getConvention()) {
    return;
  }

  // Determine mapping between project input and output fields. If sort
  // relies on non-trivial expressions, we can't push.
  final Mappings.TargetMapping map =
      RelOptUtil.permutationIgnoreCast(
          project.getProjects(), project.getInput().getRowType());
  for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) {
    if (map.getTargetOpt(fc.getFieldIndex()) < 0) {
      return;
    }
    final RexNode node = project.getProjects().get(fc.getFieldIndex());
    if (node.isA(SqlKind.CAST)) {
      // Check whether it is a monotonic preserving cast, otherwise we cannot push
      final RexCall cast = (RexCall) node;
      RelFieldCollation newFc = Objects.requireNonNull(RexUtil.apply(map, fc));
      final RexCallBinding binding =
          RexCallBinding.create(cluster.getTypeFactory(), cast,
              ImmutableList.of(RelCollations.of(newFc)));
      if (cast.getOperator().getMonotonicity(binding) == SqlMonotonicity.NOT_MONOTONIC) {
        return;
      }
    }
  }
  final RelCollation newCollation =
      cluster.traitSet().canonize(
          RexUtil.apply(map, sort.getCollation()));
  final Sort newSort =
      sort.copy(
          sort.getTraitSet().replace(newCollation),
          project.getInput(),
          newCollation,
          sort.offset,
          sort.fetch);
  RelNode newProject =
      project.copy(
          sort.getTraitSet(),
          ImmutableList.of(newSort));
  // Not only is newProject equivalent to sort;
  // newSort is equivalent to project's input
  // (but only if the sort is not also applying an offset/limit).
  Map<RelNode, RelNode> equiv;
  if (sort.offset == null
      && sort.fetch == null
      && cluster.getPlanner().getRelTraitDefs()
          .contains(RelCollationTraitDef.INSTANCE)) {
    equiv = ImmutableMap.of((RelNode) newSort, project.getInput());
  } else {
    equiv = ImmutableMap.of();
  }
  call.transformTo(newProject, equiv);
}
 
Example 16
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlMonotonicity getMonotonicity(String columnName) {
  return monotonicColumnSet.contains(columnName)
      ? SqlMonotonicity.INCREASING
      : SqlMonotonicity.NOT_MONOTONIC;
}
 
Example 17
Source File: SortProjectTransposeRule.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final Sort sort = call.rel(0);
  final Project project = call.rel(1);
  final RelOptCluster cluster = project.getCluster();

  if (sort.getConvention() != project.getConvention()) {
    return;
  }

  // Determine mapping between project input and output fields. If sort
  // relies on non-trivial expressions, we can't push.
  final Mappings.TargetMapping map =
      RelOptUtil.permutationIgnoreCast(
          project.getProjects(), project.getInput().getRowType());
  for (RelFieldCollation fc : sort.getCollation().getFieldCollations()) {
    if (map.getTargetOpt(fc.getFieldIndex()) < 0) {
      return;
    }
    final RexNode node = project.getProjects().get(fc.getFieldIndex());
    if (node.isA(SqlKind.CAST)) {
      // Check whether it is a monotonic preserving cast, otherwise we cannot push
      final RexCall cast = (RexCall) node;
      final RexCallBinding binding =
          RexCallBinding.create(cluster.getTypeFactory(), cast,
              ImmutableList.of(RelCollations.of(RexUtil.apply(map, fc))));
      if (cast.getOperator().getMonotonicity(binding) == SqlMonotonicity.NOT_MONOTONIC) {
        return;
      }
    }
  }
  final RelCollation newCollation =
      cluster.traitSet().canonize(
          RexUtil.apply(map, sort.getCollation()));
  final Sort newSort =
      sort.copy(
          sort.getTraitSet().replace(newCollation),
          project.getInput(),
          newCollation,
          sort.offset,
          sort.fetch);
  RelNode newProject =
      project.copy(
          sort.getTraitSet(),
          ImmutableList.of(newSort));
  // Not only is newProject equivalent to sort;
  // newSort is equivalent to project's input
  // (but only if the sort is not also applying an offset/limit).
  Map<RelNode, RelNode> equiv;
  if (sort.offset == null
      && sort.fetch == null
      && cluster.getPlanner().getRelTraitDefs()
          .contains(RelCollationTraitDef.INSTANCE)) {
    equiv = ImmutableMap.of((RelNode) newSort, project.getInput());
  } else {
    equiv = ImmutableMap.of();
  }
  call.transformTo(newProject, equiv);
}
 
Example 18
Source File: SqlOperatorBinding.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the monotonicity of a bound operand.
 *
 * @param ordinal zero-based ordinal of operand of interest
 * @return monotonicity of operand
 */
public SqlMonotonicity getOperandMonotonicity(int ordinal) {
  return SqlMonotonicity.NOT_MONOTONIC;
}
 
Example 19
Source File: SqlNode.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Returns whether expression is always ascending, descending or constant.
 * This property is useful because it allows to safely aggregate infinite
 * streams of values.
 *
 * <p>The default implementation returns
 * {@link SqlMonotonicity#NOT_MONOTONIC}.
 *
 * @param scope Scope
 */
public SqlMonotonicity getMonotonicity(SqlValidatorScope scope) {
  return SqlMonotonicity.NOT_MONOTONIC;
}
 
Example 20
Source File: SqlNode.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Returns whether expression is always ascending, descending or constant.
 * This property is useful because it allows to safely aggregate infinite
 * streams of values.
 *
 * <p>The default implementation returns
 * {@link SqlMonotonicity#NOT_MONOTONIC}.
 *
 * @param scope Scope
 */
public SqlMonotonicity getMonotonicity(SqlValidatorScope scope) {
  return SqlMonotonicity.NOT_MONOTONIC;
}