Java Code Examples for org.apache.calcite.util.Litmus#succeed()

The following examples show how to use org.apache.calcite.util.Litmus#succeed() . 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: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether the type of an array of expressions is compatible with a
 * struct type.
 *
 * @param exprs Array of expressions
 * @param type  Type
 * @param litmus What to do if an error is detected (there is a mismatch)
 *
 * @return Whether every expression has the same type as the corresponding
 * member of the struct type
 *
 * @see RelOptUtil#eq(String, RelDataType, String, RelDataType, org.apache.calcite.util.Litmus)
 */
public static boolean compatibleTypes(
    List<RexNode> exprs,
    RelDataType type,
    Litmus litmus) {
  final List<RelDataTypeField> fields = type.getFieldList();
  if (exprs.size() != fields.size()) {
    return litmus.fail("rowtype mismatches expressions");
  }
  for (int i = 0; i < fields.size(); i++) {
    final RelDataType exprType = exprs.get(i).getType();
    final RelDataType fieldType = fields.get(i).getType();
    if (!RelOptUtil.eq("type1", exprType, "type2", fieldType, litmus)) {
      return litmus.fail(null);
    }
  }
  return litmus.succeed();
}
 
Example 2
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether an array of exp contains no aggregate function calls whose
 * arguments are not {@link RexInputRef}s.
 *
 * @param exprs Expressions
 * @param litmus  Whether to assert if there is such a function call
 */
static boolean containNoNonTrivialAggs(List<RexNode> exprs, Litmus litmus) {
    for (RexNode expr : exprs) {
        if (expr instanceof RexCall) {
            RexCall rexCall = (RexCall) expr;
            if (rexCall.getOperator() instanceof SqlAggFunction) {
                for (RexNode operand : rexCall.getOperands()) {
                    if (!(operand instanceof RexLocalRef) && !(operand instanceof RexLiteral)) {
                        return litmus.fail("contains non trivial agg: {}", operand);
                    }
                }
            }
        }
    }
    return litmus.succeed();
}
 
Example 3
Source File: SqlRowTypeNameSpec.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public boolean equalsDeep(SqlTypeNameSpec node, Litmus litmus) {
  if (!(node instanceof SqlRowTypeNameSpec)) {
    return litmus.fail("{} != {}", this, node);
  }
  SqlRowTypeNameSpec that = (SqlRowTypeNameSpec) node;
  if (this.fieldNames.size() != that.fieldNames.size()) {
    return litmus.fail("{} != {}", this, node);
  }
  for (int i = 0; i < fieldNames.size(); i++) {
    if (!this.fieldNames.get(i).equalsDeep(that.fieldNames.get(i), litmus)) {
      return litmus.fail("{} != {}", this, node);
    }
  }
  if (this.fieldTypes.size() != that.fieldTypes.size()) {
    return litmus.fail("{} != {}", this, node);
  }
  for (int i = 0; i < fieldTypes.size(); i++) {
    if (!this.fieldTypes.get(i).equals(that.fieldTypes.get(i))) {
      return litmus.fail("{} != {}", this, node);
    }
  }
  return litmus.succeed();
}
 
Example 4
Source File: RexProgram.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether this program is in canonical form.
 *
 * @param litmus     What to do if an error is detected (program is not in
 *                   canonical form)
 * @param rexBuilder Rex builder
 * @return whether in canonical form
 */
public boolean isNormalized(Litmus litmus, RexBuilder rexBuilder) {
    final RexProgram normalizedProgram = normalize(rexBuilder, null);
    String normalized = normalizedProgram.toString();
    String string = toString();
    if (!normalized.equals(string)) {
        final String message = "Program is not normalized:\n" + "program:    {}\n" + "normalized: {}\n";
        return litmus.fail(message, string, normalized);
    }
    return litmus.succeed();
}
 
Example 5
Source File: SqlLiteral.java    From Bats with Apache License 2.0 5 votes vote down vote up
public boolean equalsDeep(SqlNode node, Litmus litmus) {
  if (!(node instanceof SqlLiteral)) {
    return litmus.fail("{} != {}", this, node);
  }
  SqlLiteral that = (SqlLiteral) node;
  if (!this.equals(that)) {
    return litmus.fail("{} != {}", this, node);
  }
  return litmus.succeed();
}
 
Example 6
Source File: RexUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether an array of expressions contains no forward references.
 * That is, if expression #i contains a {@link RexInputRef} referencing
 * field i or greater.
 *
 * @param exprs        Array of expressions
 * @param inputRowType Input row type
 * @param litmus       What to do if an error is detected (there is a
 *                     forward reference)
 *
 * @return Whether there is a forward reference
 */
public static boolean containNoForwardRefs(List<RexNode> exprs, RelDataType inputRowType, Litmus litmus) {
    final ForwardRefFinder visitor = new ForwardRefFinder(inputRowType);
    for (int i = 0; i < exprs.size(); i++) {
        RexNode expr = exprs.get(i);
        visitor.setLimit(i); // field cannot refer to self or later field
        try {
            expr.accept(visitor);
        } catch (ForwardRefFinder.IllegalForwardRefException e) {
            Util.swallow(e, null);
            return litmus.fail("illegal forward reference in {}", expr);
        }
    }
    return litmus.succeed();
}
 
Example 7
Source File: RexProgram.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether this program is in canonical form.
 *
 * @param litmus     What to do if an error is detected (program is not in
 *                   canonical form)
 * @param rexBuilder Rex builder
 * @return whether in canonical form
 */
public boolean isNormalized(Litmus litmus, RexBuilder rexBuilder) {
  final RexProgram normalizedProgram = normalize(rexBuilder, null);
  String normalized = normalizedProgram.toString();
  String string = toString();
  if (!normalized.equals(string)) {
    final String message = "Program is not normalized:\n"
        + "program:    {}\n"
        + "normalized: {}\n";
    return litmus.fail(message, string, normalized);
  }
  return litmus.succeed();
}
 
Example 8
Source File: RexLiteralImpl.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns whether a value is valid as a constant value, using the same
 * criteria as {@link #valueMatchesType}. */
public static boolean validConstant(Object o, Litmus litmus) {
    if (o == null || o instanceof BigDecimal || o instanceof NlsString || o instanceof ByteString) {
        return litmus.succeed();
    } else if (o instanceof List) {
        List list = (List) o;
        for (Object o1 : list) {
            if (!validConstant(o1, litmus)) {
                return litmus.fail("not a constant: {}", o1);
            }
        }
        return litmus.succeed();
    } else if (o instanceof Map) {
        @SuppressWarnings("unchecked")
        final Map<Object, Object> map = (Map) o;
        for (Map.Entry entry : map.entrySet()) {
            if (!validConstant(entry.getKey(), litmus)) {
                return litmus.fail("not a constant: {}", entry.getKey());
            }
            if (!validConstant(entry.getValue(), litmus)) {
                return litmus.fail("not a constant: {}", entry.getValue());
            }
        }
        return litmus.succeed();
    } else {
        return litmus.fail("not a constant: {}", o);
    }
}
 
Example 9
Source File: SqlDynamicParam.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean equalsDeep(SqlNode node, Litmus litmus) {
  if (!(node instanceof SqlDynamicParam)) {
    return litmus.fail("{} != {}", this, node);
  }
  SqlDynamicParam that = (SqlDynamicParam) node;
  if (this.index != that.index) {
    return litmus.fail("{} != {}", this, node);
  }
  return litmus.succeed();
}
 
Example 10
Source File: VolcanoRuleMatch.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns whether all elements of a given array are not-null;
 * fails if any are null. */
private static <E> boolean allNotNull(E[] es, Litmus litmus) {
  for (E e : es) {
    if (e == null) {
      return litmus.fail("was null", (Object) es);
    }
  }
  return litmus.succeed();
}
 
Example 11
Source File: SqlNode.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns whether two lists of operands are equal. */
public static boolean equalDeep(List<SqlNode> operands0,
    List<SqlNode> operands1, Litmus litmus) {
  if (operands0.size() != operands1.size()) {
    return litmus.fail(null);
  }
  for (int i = 0; i < operands0.size(); i++) {
    if (!SqlNode.equalDeep(operands0.get(i), operands1.get(i), litmus)) {
      return litmus.fail(null);
    }
  }
  return litmus.succeed();
}
 
Example 12
Source File: SqlDataTypeSpec.java    From Bats with Apache License 2.0 5 votes vote down vote up
public boolean equalsDeep(SqlNode node, Litmus litmus) {
  if (!(node instanceof SqlDataTypeSpec)) {
    return litmus.fail("{} != {}", this, node);
  }
  SqlDataTypeSpec that = (SqlDataTypeSpec) node;
  if (!SqlNode.equalDeep(
      this.collectionsTypeName,
      that.collectionsTypeName, litmus)) {
    return litmus.fail(null);
  }
  if (!this.typeName.equalsDeep(that.typeName, litmus)) {
    return litmus.fail(null);
  }
  if (this.precision != that.precision) {
    return litmus.fail("{} != {}", this, node);
  }
  if (this.scale != that.scale) {
    return litmus.fail("{} != {}", this, node);
  }
  if (!Objects.equals(this.timeZone, that.timeZone)) {
    return litmus.fail("{} != {}", this, node);
  }
  if (!Objects.equals(this.charSetName, that.charSetName)) {
    return litmus.fail("{} != {}", this, node);
  }
  return litmus.succeed();
}
 
Example 13
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
static boolean allLessThan(Collection<Integer> integers, int limit, Litmus ret) {
    for (int value : integers) {
        if (value >= limit) {
            return ret.fail("out of range; value: {}, limit: {}", value, limit);
        }
    }
    return ret.succeed();
}
 
Example 14
Source File: RexUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the type of an array of expressions is compatible with a
 * struct type.
 *
 * @param exprs Array of expressions
 * @param type  Type
 * @param litmus What to do if an error is detected (there is a mismatch)
 *
 * @return Whether every expression has the same type as the corresponding
 * member of the struct type
 *
 * @see RelOptUtil#eq(String, RelDataType, String, RelDataType, org.apache.calcite.util.Litmus)
 */
public static boolean compatibleTypes(List<RexNode> exprs, RelDataType type, Litmus litmus) {
    final List<RelDataTypeField> fields = type.getFieldList();
    if (exprs.size() != fields.size()) {
        return litmus.fail("rowtype mismatches expressions");
    }
    for (int i = 0; i < fields.size(); i++) {
        final RelDataType exprType = exprs.get(i).getType();
        final RelDataType fieldType = fields.get(i).getType();
        if (!RelOptUtil.eq("type1", exprType, "type2", fieldType, litmus)) {
            return litmus.fail(null);
        }
    }
    return litmus.succeed();
}
 
Example 15
Source File: Window.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public boolean isValid(Litmus litmus, Context context) {
  // In the window specifications, an aggregate call such as
  // 'SUM(RexInputRef #10)' refers to expression #10 of inputProgram.
  // (Not its projections.)
  final RelDataType childRowType = getInput().getRowType();

  final int childFieldCount = childRowType.getFieldCount();
  final int inputSize = childFieldCount + constants.size();
  final List<RelDataType> inputTypes =
      new AbstractList<RelDataType>() {
        @Override public RelDataType get(int index) {
          return index < childFieldCount
              ? childRowType.getFieldList().get(index).getType()
              : constants.get(index - childFieldCount).getType();
        }

        @Override public int size() {
          return inputSize;
        }
      };

  final RexChecker checker = new RexChecker(inputTypes, context, litmus);
  int count = 0;
  for (Group group : groups) {
    for (RexWinAggCall over : group.aggCalls) {
      ++count;
      if (!checker.isValid(over)) {
        return litmus.fail(null);
      }
    }
  }
  if (count == 0) {
    return litmus.fail("empty");
  }
  return litmus.succeed();
}
 
Example 16
Source File: Filter.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public boolean isValid(Litmus litmus, Context context) {
  if (RexUtil.isNullabilityCast(getCluster().getTypeFactory(), condition)) {
    return litmus.fail("Cast for just nullability not allowed");
  }
  final RexChecker checker =
      new RexChecker(getInput().getRowType(), context, litmus);
  condition.accept(checker);
  if (checker.getFailureCount() > 0) {
    return litmus.fail(null);
  }
  return litmus.succeed();
}
 
Example 17
Source File: SqlIntervalQualifier.java    From calcite with Apache License 2.0 5 votes vote down vote up
public boolean equalsDeep(SqlNode node, Litmus litmus) {
  final String thisString = this.toString();
  final String thatString = node.toString();
  if (!thisString.equals(thatString)) {
    return litmus.fail("{} != {}", this, node);
  }
  return litmus.succeed();
}
 
Example 18
Source File: SqlPrefixOperator.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public boolean validRexOperands(int count, Litmus litmus) {
  if (count != 1) {
    return litmus.fail("wrong operand count {} for {}", count, this);
  }
  return litmus.succeed();
}
 
Example 19
Source File: SqlPostfixOperator.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public boolean validRexOperands(int count, Litmus litmus) {
  if (count != 1) {
    return litmus.fail("wrong operand count {} for {}", count, this);
  }
  return litmus.succeed();
}
 
Example 20
Source File: SqlStdOperatorTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public boolean validRexOperands(int count, Litmus litmus) {
  if (count != 0) {
    return litmus.fail("wrong operand count {} for {}", count, this);
  }
  return litmus.succeed();
}