use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlTesterImpl method check.
public void check(String query, TypeChecker typeChecker, ParameterChecker parameterChecker, ResultChecker resultChecker) {
if (typeChecker == null) {
// Parse and validate. There should be no errors.
Util.discard(getResultType(query));
} else {
// Parse and validate. There should be no errors.
// There must be 1 column. Get its type.
RelDataType actualType = getColumnType(query);
// Check result type.
typeChecker.checkType(actualType);
}
SqlValidator validator = getValidator();
SqlNode n = parseAndValidate(validator, query);
final RelDataType parameterRowType = validator.getParameterRowType(n);
parameterChecker.checkParameters(parameterRowType);
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlTesterImpl method checkMonotonic.
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));
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlTesterImpl method getMonotonicity.
public SqlMonotonicity getMonotonicity(String sql) {
final SqlValidator validator = getValidator();
final SqlNode node = parseAndValidate(validator, sql);
final SqlSelect select = (SqlSelect) node;
final SqlNode selectItem0 = select.getSelectList().get(0);
final SqlValidatorScope scope = validator.getSelectScope(select);
return selectItem0.getMonotonicity(scope);
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlTypeUtil method isCharTypeComparable.
/**
* Returns whether the operands to a call are char type-comparable.
*
* @param binding Binding of call to operands
* @param operands Operands to check for compatibility; usually the
* operands of the bound call, but not always
* @param throwOnFailure Whether to throw an exception on failure
* @return whether operands are valid
*/
public static boolean isCharTypeComparable(SqlCallBinding binding, List<SqlNode> operands, boolean throwOnFailure) {
final SqlValidator validator = binding.getValidator();
final SqlValidatorScope scope = binding.getScope();
assert operands != null;
assert operands.size() >= 2;
if (!isCharTypeComparable(deriveAndCollectTypes(validator, scope, operands))) {
if (throwOnFailure) {
String msg = "";
for (int i = 0; i < operands.size(); i++) {
if (i > 0) {
msg += ", ";
}
msg += operands.get(i).toString();
}
throw binding.newError(RESOURCE.operandNotComparable(msg));
}
return false;
}
return true;
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class RelOptRulesTest method testSemiJoinTrim.
@Test
public void testSemiJoinTrim() throws Exception {
final DiffRepository diffRepos = getDiffRepos();
String sql = diffRepos.expand(null, "${sql}");
TesterImpl t = (TesterImpl) tester;
final RelDataTypeFactory typeFactory = t.getTypeFactory();
final Prepare.CatalogReader catalogReader = t.createCatalogReader(typeFactory);
final SqlValidator validator = t.createValidator(catalogReader, typeFactory);
SqlToRelConverter converter = t.createSqlToRelConverter(validator, catalogReader, typeFactory, SqlToRelConverter.Config.DEFAULT);
final SqlNode sqlQuery = t.parseQuery(sql);
final SqlNode validatedQuery = validator.validate(sqlQuery);
RelRoot root = converter.convertQuery(validatedQuery, false, true);
root = root.withRel(converter.decorrelate(sqlQuery, root.rel));
final HepProgram program = HepProgram.builder().addRuleInstance(FilterProjectTransposeRule.INSTANCE).addRuleInstance(FilterJoinRule.FILTER_ON_JOIN).addRuleInstance(ProjectMergeRule.INSTANCE).addRuleInstance(SemiJoinRule.PROJECT).build();
HepPlanner planner = new HepPlanner(program);
planner.setRoot(root.rel);
root = root.withRel(planner.findBestExp());
String planBefore = NL + RelOptUtil.toString(root.rel);
diffRepos.assertEquals("planBefore", "${planBefore}", planBefore);
converter = t.createSqlToRelConverter(validator, catalogReader, typeFactory, SqlToRelConverter.configBuilder().withTrimUnusedFields(true).build());
root = root.withRel(converter.trimUnusedFields(false, root.rel));
String planAfter = NL + RelOptUtil.toString(root.rel);
diffRepos.assertEquals("planAfter", "${planAfter}", planAfter);
}
Aggregations