use of org.apache.calcite.sql.validate.SqlValidator in project druid by druid-io.
the class DruidPlanner method validate.
/**
* Validates a SQL query and populates {@link PlannerContext#getResourceActions()}.
*
* @return set of {@link Resource} corresponding to any Druid datasources or views which are taking part in the query.
*/
public ValidationResult validate() throws SqlParseException, ValidationException {
resetPlanner();
final ParsedNodes parsed = ParsedNodes.create(planner.parse(plannerContext.getSql()));
final SqlValidator validator = getValidator();
final SqlNode validatedQueryNode;
try {
validatedQueryNode = validator.validate(rewriteDynamicParameters(parsed.getQueryNode()));
} catch (RuntimeException e) {
throw new ValidationException(e);
}
SqlResourceCollectorShuttle resourceCollectorShuttle = new SqlResourceCollectorShuttle(validator, plannerContext);
validatedQueryNode.accept(resourceCollectorShuttle);
final Set<ResourceAction> resourceActions = new HashSet<>(resourceCollectorShuttle.getResourceActions());
if (parsed.getInsertNode() != null) {
final String targetDataSource = validateAndGetDataSourceForInsert(parsed.getInsertNode());
resourceActions.add(new ResourceAction(new Resource(targetDataSource, ResourceType.DATASOURCE), Action.WRITE));
}
plannerContext.setResourceActions(resourceActions);
return new ValidationResult(resourceActions);
}
use of org.apache.calcite.sql.validate.SqlValidator in project druid by druid-io.
the class DruidPlanner method prepare.
/**
* Prepare an SQL query for execution, including some initial parsing and validation and any dynamic parameter type
* resolution, to support prepared statements via JDBC.
*
* In some future this could perhaps re-use some of the work done by {@link #validate()}
* instead of repeating it, but that day is not today.
*/
public PrepareResult prepare() throws SqlParseException, ValidationException, RelConversionException {
resetPlanner();
final ParsedNodes parsed = ParsedNodes.create(planner.parse(plannerContext.getSql()));
final SqlNode validatedQueryNode = planner.validate(parsed.getQueryNode());
final RelRoot rootQueryRel = planner.rel(validatedQueryNode);
final SqlValidator validator = getValidator();
final RelDataTypeFactory typeFactory = rootQueryRel.rel.getCluster().getTypeFactory();
final RelDataType parameterTypes = validator.getParameterRowType(validator.validate(validatedQueryNode));
final RelDataType returnedRowType;
if (parsed.getExplainNode() != null) {
returnedRowType = getExplainStructType(typeFactory);
} else {
returnedRowType = buildQueryMaker(rootQueryRel, parsed.getInsertNode()).getResultType();
}
return new PrepareResult(returnedRowType, parameterTypes);
}
use of org.apache.calcite.sql.validate.SqlValidator in project hazelcast by hazelcast.
the class HazelcastCallBinding method newValidationSignatureError.
@Override
public CalciteException newValidationSignatureError() {
SqlOperator operator = getOperator();
SqlValidator validator = getValidator();
SqlCall call = getCall();
String operandTypes = getOperandTypes(validator, call, getScope());
Resources.ExInst<SqlValidatorException> error;
String operatorName = '\'' + operator.getName() + '\'';
switch(operator.getSyntax()) {
case FUNCTION:
case FUNCTION_STAR:
case FUNCTION_ID:
error = RESOURCES.invalidFunctionOperands(operatorName, operandTypes);
break;
default:
error = RESOURCES.invalidOperatorOperands(operatorName, operandTypes);
}
return validator.newValidationError(call, error);
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlTesterImpl method checkFails.
public void checkFails(String expression, String expectedError, boolean runtime) {
if (runtime) {
// We need to test that the expression fails at runtime.
// Ironically, that means that it must succeed at prepare time.
SqlValidator validator = getValidator();
final String sql = buildQuery(expression);
SqlNode n = parseAndValidate(validator, sql);
assertNotNull(n);
} else {
checkQueryFails(buildQuery(expression), expectedError);
}
}
use of org.apache.calcite.sql.validate.SqlValidator in project calcite by apache.
the class SqlTesterImpl method assertExceptionIsThrown.
public void assertExceptionIsThrown(String sql, String expectedMsgPattern) {
SqlValidator validator;
SqlNode sqlNode;
SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
try {
sqlNode = parseQuery(sap.sql);
validator = getValidator();
} catch (SqlParseException e) {
String errMessage = e.getMessage();
if (expectedMsgPattern == null) {
throw new RuntimeException("Error while parsing query:" + sap.sql, e);
} else if (errMessage == null || !errMessage.matches(expectedMsgPattern)) {
throw new RuntimeException("Error did not match expected [" + expectedMsgPattern + "] while parsing query [" + sap.sql + "]", e);
}
return;
} catch (Throwable e) {
throw new RuntimeException("Error while parsing query: " + sap.sql, e);
}
Throwable thrown = null;
try {
validator.validate(sqlNode);
} catch (Throwable ex) {
thrown = ex;
}
SqlValidatorTestCase.checkEx(thrown, expectedMsgPattern, sap);
}
Aggregations