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

The following examples show how to use org.apache.calcite.sql.validate.SqlMonotonicity. 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: Bats   Source File: RelFieldCollation.java    License: Apache License 2.0 6 votes vote down vote up
/** Converts the direction to a
 * {@link org.apache.calcite.sql.validate.SqlMonotonicity}. */
public SqlMonotonicity monotonicity() {
  switch (this) {
  case ASCENDING:
    return SqlMonotonicity.INCREASING;
  case STRICTLY_ASCENDING:
    return SqlMonotonicity.STRICTLY_INCREASING;
  case DESCENDING:
    return SqlMonotonicity.DECREASING;
  case STRICTLY_DESCENDING:
    return SqlMonotonicity.STRICTLY_DECREASING;
  case CLUSTERED:
    return SqlMonotonicity.MONOTONIC;
  default:
    throw new AssertionError("unknown: " + this);
  }
}
 
Example 2
Source Project: Bats   Source File: RelFieldCollation.java    License: Apache License 2.0 6 votes vote down vote up
/** Converts a {@link SqlMonotonicity} to a direction. */
public static Direction of(SqlMonotonicity monotonicity) {
  switch (monotonicity) {
  case INCREASING:
    return ASCENDING;
  case DECREASING:
    return DESCENDING;
  case STRICTLY_INCREASING:
    return STRICTLY_ASCENDING;
  case STRICTLY_DECREASING:
    return STRICTLY_DESCENDING;
  case MONOTONIC:
    return CLUSTERED;
  default:
    throw new AssertionError("unknown: " + monotonicity);
  }
}
 
Example 3
Source Project: Bats   Source File: SqlIdentifier.java    License: 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 4
Source Project: Bats   Source File: SqlBinaryOperator.java    License: Apache License 2.0 6 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  if (getName().equals("/")) {
    final SqlMonotonicity mono0 = call.getOperandMonotonicity(0);
    final SqlMonotonicity mono1 = call.getOperandMonotonicity(1);
    if (mono1 == SqlMonotonicity.CONSTANT) {
      if (call.isOperandLiteral(1, false)) {
        switch (call.getOperandLiteralValue(1, BigDecimal.class).signum()) {
        case -1:

          // mono / -ve constant --> reverse mono, unstrict
          return mono0.reverse().unstrict();
        case 0:

          // mono / zero --> constant (infinity!)
          return SqlMonotonicity.CONSTANT;
        default:

          // mono / +ve constant * mono1 --> mono, unstrict
          return mono0.unstrict();
        }
      }
    }
  }

  return super.getMonotonicity(call);
}
 
Example 5
Source Project: Bats   Source File: RexCallBinding.java    License: 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 6
Source Project: calcite   Source File: RelFieldCollation.java    License: Apache License 2.0 6 votes vote down vote up
/** Converts the direction to a
 * {@link org.apache.calcite.sql.validate.SqlMonotonicity}. */
public SqlMonotonicity monotonicity() {
  switch (this) {
  case ASCENDING:
    return SqlMonotonicity.INCREASING;
  case STRICTLY_ASCENDING:
    return SqlMonotonicity.STRICTLY_INCREASING;
  case DESCENDING:
    return SqlMonotonicity.DECREASING;
  case STRICTLY_DESCENDING:
    return SqlMonotonicity.STRICTLY_DECREASING;
  case CLUSTERED:
    return SqlMonotonicity.MONOTONIC;
  default:
    throw new AssertionError("unknown: " + this);
  }
}
 
Example 7
Source Project: calcite   Source File: RelFieldCollation.java    License: Apache License 2.0 6 votes vote down vote up
/** Converts a {@link SqlMonotonicity} to a direction. */
public static Direction of(SqlMonotonicity monotonicity) {
  switch (monotonicity) {
  case INCREASING:
    return ASCENDING;
  case DECREASING:
    return DESCENDING;
  case STRICTLY_INCREASING:
    return STRICTLY_ASCENDING;
  case STRICTLY_DECREASING:
    return STRICTLY_DESCENDING;
  case MONOTONIC:
    return CLUSTERED;
  default:
    throw new AssertionError("unknown: " + monotonicity);
  }
}
 
Example 8
Source Project: calcite   Source File: EnumerableTraitsUtils.java    License: 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 9
Source Project: calcite   Source File: SqlBinaryOperator.java    License: Apache License 2.0 6 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  if (getName().equals("/")) {
    final SqlMonotonicity mono0 = call.getOperandMonotonicity(0);
    final SqlMonotonicity mono1 = call.getOperandMonotonicity(1);
    if (mono1 == SqlMonotonicity.CONSTANT) {
      if (call.isOperandLiteral(1, false)) {
        switch (call.getOperandLiteralValue(1, BigDecimal.class).signum()) {
        case -1:

          // mono / -ve constant --> reverse mono, unstrict
          return mono0.reverse().unstrict();
        case 0:

          // mono / zero --> constant (infinity!)
          return SqlMonotonicity.CONSTANT;
        default:

          // mono / +ve constant * mono1 --> mono, unstrict
          return mono0.unstrict();
        }
      }
    }
  }

  return super.getMonotonicity(call);
}
 
Example 10
Source Project: calcite   Source File: RexCallBinding.java    License: 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 11
Source Project: calcite   Source File: SqlToRelTestBase.java    License: 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 12
Source Project: Bats   Source File: SqlGroupedWindowFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  // Monotonic iff its first argument is, but not strict.
  //
  // Note: This strategy happens to works for all current group functions
  // (HOP, TUMBLE, SESSION). When there are exceptions to this rule, we'll
  // make the method abstract.
  return call.getOperandMonotonicity(0).unstrict();
}
 
Example 13
Source Project: Bats   Source File: SqlSubstringFunction.java    License: 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 Project: Bats   Source File: SqlCastFunction.java    License: 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 15
Source Project: Bats   Source File: SqlPrefixOperator.java    License: Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  if (getName().equals("-")) {
    return call.getOperandMonotonicity(0).reverse();
  }

  return super.getMonotonicity(call);
}
 
Example 16
Source Project: Bats   Source File: RelOptTableImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
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 17
Source Project: calcite   Source File: RelOptTableImpl.java    License: 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 18
Source Project: calcite   Source File: SqlGroupedWindowFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  // Monotonic iff its first argument is, but not strict.
  //
  // Note: This strategy happens to works for all current group functions
  // (HOP, TUMBLE, SESSION). When there are exceptions to this rule, we'll
  // make the method abstract.
  return call.getOperandMonotonicity(0).unstrict();
}
 
Example 19
Source Project: calcite   Source File: SqlSubstringFunction.java    License: 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 20
Source Project: calcite   Source File: SqlExtractFunction.java    License: 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 21
Source Project: calcite   Source File: SqlCastFunction.java    License: 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 22
Source Project: calcite   Source File: SqlPrefixOperator.java    License: Apache License 2.0 5 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  if (getName().equals("-")) {
    return call.getOperandMonotonicity(0).reverse();
  }

  return super.getMonotonicity(call);
}
 
Example 23
Source Project: calcite   Source File: AbstractSqlTester.java    License: Apache License 2.0 5 votes vote down vote up
public void checkMonotonic(String query,
    SqlMonotonicity expectedMonotonicity) {
  SqlValidator validator = getValidator();
  SqlNode n = parseAndValidate(validator, query);
  final RelDataType rowType = validator.getValidatedNodeType(n);
  final SqlValidatorNamespace selectNamespace = validator.getNamespace(n);
  final String field0 = rowType.getFieldList().get(0).getName();
  final SqlMonotonicity monotonicity =
      selectNamespace.getMonotonicity(field0);
  assertThat(monotonicity, equalTo(expectedMonotonicity));
}
 
Example 24
Source Project: Bats   Source File: SortProjectTransposeRule.java    License: 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 25
Source Project: Bats   Source File: SqlDynamicParam.java    License: Apache License 2.0 4 votes vote down vote up
public SqlMonotonicity getMonotonicity(SqlValidatorScope scope) {
  return SqlMonotonicity.CONSTANT;
}
 
Example 26
Source Project: Bats   Source File: SqlAsOperator.java    License: Apache License 2.0 4 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  return call.getOperandMonotonicity(0);
}
 
Example 27
Source Project: Bats   Source File: SqlCallBinding.java    License: Apache License 2.0 4 votes vote down vote up
public SqlMonotonicity getOperandMonotonicity(int ordinal) {
  return call.getOperandList().get(ordinal).getMonotonicity(scope);
}
 
Example 28
Source Project: Bats   Source File: SqlCurrentDateFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  return SqlMonotonicity.INCREASING;
}
 
Example 29
Source Project: calcite   Source File: MockCatalogReader.java    License: 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 30
Source Project: Bats   Source File: SqlMonotonicUnaryFunction.java    License: Apache License 2.0 4 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  return call.getOperandMonotonicity(0).unstrict();
}