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

The following examples show how to use org.apache.calcite.sql.validate.SqlMonotonicity. 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: 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 #2
Source File: RelFieldCollation.java    From Bats with 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 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 #4
Source File: RelFieldCollation.java    From Bats with 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 #5
Source File: SqlBinaryOperator.java    From Bats with 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 #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: RelFieldCollation.java    From calcite with 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 #8
Source File: RelFieldCollation.java    From calcite with 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 #9
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 #10
Source File: SqlBinaryOperator.java    From calcite with 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 #11
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 #12
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 #13
Source File: SqlPrefixOperator.java    From calcite with 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 #14
Source File: AbstractSqlTester.java    From calcite with 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 #15
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 #16
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 #17
Source File: SqlGroupedWindowFunction.java    From Bats with 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 #18
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 #19
Source File: SqlGroupedWindowFunction.java    From calcite with 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 #20
Source File: SqlPrefixOperator.java    From Bats with 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 #21
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 #22
Source File: RelOptTableImpl.java    From Bats with 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 #23
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 #24
Source File: SqlCallBinding.java    From Bats with Apache License 2.0 4 votes vote down vote up
public SqlMonotonicity getOperandMonotonicity(int ordinal) {
  return call.getOperandList().get(ordinal).getMonotonicity(scope);
}
 
Example #25
Source File: SqlCurrentDateFunction.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  return SqlMonotonicity.INCREASING;
}
 
Example #26
Source File: FlinkSqlOperatorTable.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
	return SqlMonotonicity.INCREASING;
}
 
Example #27
Source File: SqlAsOperator.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
  return call.getOperandMonotonicity(0);
}
 
Example #28
Source File: SqlDynamicParam.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlMonotonicity getMonotonicity(SqlValidatorScope scope) {
  return SqlMonotonicity.CONSTANT;
}
 
Example #29
Source File: SqlValidatorTestCase.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Sql monotonic(SqlMonotonicity expectedMonotonicity) {
  tester.checkMonotonic(toSql(false), expectedMonotonicity);
  return this;
}
 
Example #30
Source File: SqlDynamicParam.java    From Bats with Apache License 2.0 4 votes vote down vote up
public SqlMonotonicity getMonotonicity(SqlValidatorScope scope) {
  return SqlMonotonicity.CONSTANT;
}