use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.
the class SqlCastFunction method inferReturnType.
// ~ Methods ----------------------------------------------------------------
public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
assert opBinding.getOperandCount() == 2;
RelDataType ret = opBinding.getOperandType(1);
RelDataType firstType = opBinding.getOperandType(0);
ret = opBinding.getTypeFactory().createTypeWithNullability(ret, firstType.isNullable());
if (opBinding instanceof SqlCallBinding) {
SqlCallBinding callBinding = (SqlCallBinding) opBinding;
SqlNode operand0 = callBinding.operand(0);
// to them using the type they are casted to.
if (((operand0 instanceof SqlLiteral) && (((SqlLiteral) operand0).getValue() == null)) || (operand0 instanceof SqlDynamicParam)) {
final SqlValidatorImpl validator = (SqlValidatorImpl) callBinding.getValidator();
validator.setValidatedNodeType(operand0, ret);
}
}
return ret;
}
use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.
the class SqlOperatorBaseTest method testArgumentBounds.
/**
* Test that calls all operators with all possible argument types, and for
* each type, with a set of tricky values.
*/
@Test
public void testArgumentBounds() {
if (!CalciteAssert.ENABLE_SLOW) {
return;
}
final SqlValidatorImpl validator = (SqlValidatorImpl) tester.getValidator();
final SqlValidatorScope scope = validator.getEmptyScope();
final RelDataTypeFactory typeFactory = validator.getTypeFactory();
final Builder builder = new Builder(typeFactory);
builder.add0(SqlTypeName.BOOLEAN, true, false);
builder.add0(SqlTypeName.TINYINT, 0, 1, -3, Byte.MAX_VALUE, Byte.MIN_VALUE);
builder.add0(SqlTypeName.SMALLINT, 0, 1, -4, Short.MAX_VALUE, Short.MIN_VALUE);
builder.add0(SqlTypeName.INTEGER, 0, 1, -2, Integer.MIN_VALUE, Integer.MAX_VALUE);
builder.add0(SqlTypeName.BIGINT, 0, 1, -5, Integer.MAX_VALUE, Long.MAX_VALUE, Long.MIN_VALUE);
builder.add1(SqlTypeName.VARCHAR, 11, "", " ", "hello world");
builder.add1(SqlTypeName.CHAR, 5, "", "e", "hello");
builder.add0(SqlTypeName.TIMESTAMP, 0L, DateTimeUtils.MILLIS_PER_DAY);
for (SqlOperator op : SqlStdOperatorTable.instance().getOperatorList()) {
switch(op.getKind()) {
// can't handle the flag argument
case TRIM:
case EXISTS:
continue;
}
switch(op.getSyntax()) {
case SPECIAL:
continue;
}
final SqlOperandTypeChecker typeChecker = op.getOperandTypeChecker();
if (typeChecker == null) {
continue;
}
final SqlOperandCountRange range = typeChecker.getOperandCountRange();
for (int n = range.getMin(), max = range.getMax(); n <= max; n++) {
final List<List<ValueType>> argValues = Collections.nCopies(n, builder.values);
for (final List<ValueType> args : Linq4j.product(argValues)) {
SqlNodeList nodeList = new SqlNodeList(SqlParserPos.ZERO);
int nullCount = 0;
for (ValueType arg : args) {
if (arg.value == null) {
++nullCount;
}
nodeList.add(arg.node);
}
final SqlCall call = op.createCall(nodeList);
final SqlCallBinding binding = new SqlCallBinding(validator, scope, call);
if (!typeChecker.checkOperandTypes(binding, false)) {
continue;
}
final SqlPrettyWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT);
op.unparse(writer, call, 0, 0);
final String s = writer.toSqlString().toString();
if (s.startsWith("OVERLAY(") || s.contains(" / 0") || s.matches("MOD\\(.*, 0\\)")) {
continue;
}
final Strong.Policy policy = Strong.policy(op.kind);
try {
if (nullCount > 0 && policy == Strong.Policy.ANY) {
tester.checkNull(s);
} else {
final String query;
if (op instanceof SqlAggFunction) {
if (op.requiresOrder()) {
query = "SELECT " + s + " OVER () FROM (VALUES (1))";
} else {
query = "SELECT " + s + " FROM (VALUES (1))";
}
} else {
query = SqlTesterImpl.buildQuery(s);
}
tester.check(query, SqlTests.ANY_TYPE_CHECKER, SqlTests.ANY_PARAMETER_CHECKER, SqlTests.ANY_RESULT_CHECKER);
}
} catch (Error e) {
System.out.println(s + ": " + e.getMessage());
throw e;
} catch (Exception e) {
System.out.println("Failed: " + s + ": " + e.getMessage());
}
}
}
}
}
use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.
the class SqlCall method findValidOptions.
public void findValidOptions(SqlValidator validator, SqlValidatorScope scope, SqlParserPos pos, Collection<SqlMoniker> hintList) {
for (SqlNode operand : getOperandList()) {
if (operand instanceof SqlIdentifier) {
SqlIdentifier id = (SqlIdentifier) operand;
SqlParserPos idPos = id.getParserPosition();
if (idPos.toString().equals(pos.toString())) {
((SqlValidatorImpl) validator).lookupNameCompletionHints(scope, id.names, pos, hintList);
return;
}
}
}
// no valid options
}
use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.
the class SqlJdbcFunctionCall method deriveType.
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
for (SqlNode operand : call.getOperandList()) {
RelDataType nodeType = validator.deriveType(scope, operand);
((SqlValidatorImpl) validator).setValidatedNodeType(operand, nodeType);
}
return validateOperands(validator, scope, call);
}
use of org.apache.calcite.sql.validate.SqlValidatorImpl in project calcite by apache.
the class SqlFilterOperator method deriveType.
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
// Validate type of the inner aggregate call
validateOperands(validator, scope, call);
// Assume the first operand is an aggregate call and derive its type.
SqlNode agg = call.operand(0);
if (!(agg instanceof SqlCall)) {
throw new IllegalStateException("Argument to SqlOverOperator" + " should be SqlCall, got " + agg.getClass() + ": " + agg);
}
final SqlCall aggCall = (SqlCall) agg;
// Pretend that group-count is 0. This tells the aggregate function that it
// might be invoked with 0 rows in a group. Most aggregate functions will
// return NULL in this case.
SqlCallBinding opBinding = new SqlCallBinding(validator, scope, aggCall) {
@Override
public int getGroupCount() {
return 0;
}
};
RelDataType ret = aggCall.getOperator().inferReturnType(opBinding);
// Copied from validateOperands
((SqlValidatorImpl) validator).setValidatedNodeType(call, ret);
((SqlValidatorImpl) validator).setValidatedNodeType(agg, ret);
return ret;
}
Aggregations