Skip to content

Commit

Permalink
[CALCITE-4215] Avoid NPE when monotonicity returns null
Browse files Browse the repository at this point in the history
  • Loading branch information
vlsi committed Oct 1, 2020
1 parent 4787123 commit 58adcae
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ protected RelDataType adjustType(

@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
if (getName().equals("-")) {
return call.getOperandMonotonicity(0).reverse();
SqlMonotonicity monotonicity = call.getOperandMonotonicity(0);
return monotonicity == null ? null : monotonicity.reverse();
}

return super.getMonotonicity(call);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public void unparse(
@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
switch (call.getOperandLiteralValue(0, TimeUnitRange.class)) {
case YEAR:
return call.getOperandMonotonicity(1).unstrict();
SqlMonotonicity monotonicity = call.getOperandMonotonicity(1);
return monotonicity == null ? null : monotonicity.unstrict();
default:
return SqlMonotonicity.NOT_MONOTONIC;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public SqlFloorFunction(SqlKind kind) {

@Override public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
// Monotonic iff its first argument is, but not strict.
return call.getOperandMonotonicity(0).unstrict();
SqlMonotonicity monotonicity = call.getOperandMonotonicity(0);
return monotonicity == null ? null : monotonicity.unstrict();
}

@Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public SqlMonotonicBinaryOperator(
final SqlMonotonicity mono0 = call.getOperandMonotonicity(0);
final SqlMonotonicity mono1 = call.getOperandMonotonicity(1);

// unknown <op> unknown --> unknown
if (mono0 == null || mono1 == null) {
return null;
}
// constant <op> constant --> constant
if ((mono1 == SqlMonotonicity.CONSTANT)
&& (mono0 == SqlMonotonicity.CONSTANT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,8 @@ public static boolean containsMonotonic(SqlValidatorScope scope) {
for (SqlValidatorNamespace ns : children(scope)) {
ns = ns.resolve();
for (String field : ns.getRowType().getFieldNames()) {
if (!ns.getMonotonicity(field).mayRepeat()) {
SqlMonotonicity monotonicity = ns.getMonotonicity(field);
if (monotonicity != null && !monotonicity.mayRepeat()) {
return true;
}
}
Expand Down

0 comments on commit 58adcae

Please sign in to comment.