use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlSubstringFunction method checkOperandTypes.
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
SqlValidator validator = callBinding.getValidator();
SqlValidatorScope scope = callBinding.getScope();
final List<SqlNode> operands = callBinding.operands();
int n = operands.size();
assert (3 == n) || (2 == n);
if (!OperandTypes.STRING.checkSingleOperandType(callBinding, operands.get(0), 0, throwOnFailure)) {
return false;
}
if (2 == n) {
if (!OperandTypes.NUMERIC.checkSingleOperandType(callBinding, operands.get(1), 0, throwOnFailure)) {
return false;
}
} else {
RelDataType t1 = validator.deriveType(scope, operands.get(1));
RelDataType t2 = validator.deriveType(scope, operands.get(2));
if (SqlTypeUtil.inCharFamily(t1)) {
if (!OperandTypes.STRING.checkSingleOperandType(callBinding, operands.get(1), 0, throwOnFailure)) {
return false;
}
if (!OperandTypes.STRING.checkSingleOperandType(callBinding, operands.get(2), 0, throwOnFailure)) {
return false;
}
if (!SqlTypeUtil.isCharTypeComparable(callBinding, operands, throwOnFailure)) {
return false;
}
} else {
if (!OperandTypes.NUMERIC.checkSingleOperandType(callBinding, operands.get(1), 0, throwOnFailure)) {
return false;
}
if (!OperandTypes.NUMERIC.checkSingleOperandType(callBinding, operands.get(2), 0, throwOnFailure)) {
return false;
}
}
if (!SqlTypeUtil.inSameFamily(t1, t2)) {
if (throwOnFailure) {
throw callBinding.newValidationSignatureError();
}
return false;
}
}
return true;
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlTesterImpl method getResultType.
public RelDataType getResultType(String sql) {
SqlValidator validator = getValidator();
SqlNode n = parseAndValidate(validator, sql);
return validator.getValidatedNodeType(n);
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlTesterImpl method checkFieldOrigin.
public void checkFieldOrigin(String sql, String fieldOriginList) {
SqlValidator validator = getValidator();
SqlNode n = parseAndValidate(validator, sql);
final List<List<String>> list = validator.getFieldOrigins(n);
final StringBuilder buf = new StringBuilder("{");
int i = 0;
for (List<String> strings : list) {
if (i++ > 0) {
buf.append(", ");
}
if (strings == null) {
buf.append("null");
} else {
int j = 0;
for (String s : strings) {
if (j++ > 0) {
buf.append('.');
}
buf.append(s);
}
}
}
buf.append("}");
assertEquals(fieldOriginList, buf.toString());
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlTesterImpl method checkIntervalConv.
public void checkIntervalConv(String sql, String expected) {
SqlValidator validator = getValidator();
final SqlCall n = (SqlCall) parseAndValidate(validator, sql);
SqlNode node = null;
for (int i = 0; i < n.operandCount(); i++) {
node = stripAs(n.operand(i));
if (node instanceof SqlCall) {
node = ((SqlCall) node).operand(0);
break;
}
}
assertNotNull(node);
SqlIntervalLiteral intervalLiteral = (SqlIntervalLiteral) node;
SqlIntervalLiteral.IntervalValue interval = (SqlIntervalLiteral.IntervalValue) intervalLiteral.getValue();
long l = interval.getIntervalQualifier().isYearMonth() ? SqlParserUtil.intervalToMonths(interval) : SqlParserUtil.intervalToMillis(interval);
String actual = l + "";
assertEquals(expected, actual);
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class CalcitePrepareImpl method parse_.
/**
* Shared implementation for {@link #parse}, {@link #convert} and
* {@link #analyzeView}.
*/
private ParseResult parse_(Context context, String sql, boolean convert, boolean analyze, boolean fail) {
final JavaTypeFactory typeFactory = context.getTypeFactory();
CalciteCatalogReader catalogReader = new CalciteCatalogReader(context.getRootSchema(), context.getDefaultSchemaPath(), typeFactory, context.config());
SqlParser parser = createParser(sql);
SqlNode sqlNode;
try {
sqlNode = parser.parseStmt();
} catch (SqlParseException e) {
throw new RuntimeException("parse failed", e);
}
final SqlValidator validator = createSqlValidator(context, catalogReader);
SqlNode sqlNode1 = validator.validate(sqlNode);
if (convert) {
return convert_(context, sql, analyze, fail, catalogReader, validator, sqlNode1);
}
return new ParseResult(this, validator, sql, sqlNode1, validator.getValidatedNodeType(sqlNode1));
}
Aggregations