use of org.apache.calcite.tools.ValidationException in project druid by apache.
the class DruidSqlParserUtils method parseTimeStampWithTimeZone.
/**
* Converts a {@link SqlNode} into a timestamp, taking into account the timezone
*
* @param sqlNode the sql node
* @param timeZone timezone
* @return the timestamp string as milliseconds from epoch
* @throws ValidationException if the sql node is not a SqlTimestampLiteral
*/
public static String parseTimeStampWithTimeZone(SqlNode sqlNode, DateTimeZone timeZone) throws ValidationException {
if (!(sqlNode instanceof SqlTimestampLiteral)) {
throw new ValidationException("Expressions must be of the form __time <operator> TIMESTAMP");
}
Timestamp sqlTimestamp = Timestamp.valueOf(((SqlTimestampLiteral) sqlNode).toFormattedString());
ZonedDateTime zonedTimestamp = sqlTimestamp.toLocalDateTime().atZone(timeZone.toTimeZone().toZoneId());
return String.valueOf(zonedTimestamp.toInstant().toEpochMilli());
}
use of org.apache.calcite.tools.ValidationException in project druid by druid-io.
the class DruidPlanner method plan.
public PlannerResult plan(final String sql) throws SqlParseException, ValidationException, RelConversionException {
SqlExplain explain = null;
SqlNode parsed = planner.parse(sql);
if (parsed.getKind() == SqlKind.EXPLAIN) {
explain = (SqlExplain) parsed;
parsed = explain.getExplicandum();
}
final SqlNode validated = planner.validate(parsed);
final RelRoot root = planner.rel(validated);
try {
return planWithDruidConvention(explain, root);
} catch (RelOptPlanner.CannotPlanException e) {
// Try again with BINDABLE convention. Used for querying Values, metadata tables, and fallback.
try {
return planWithBindableConvention(explain, root);
} catch (Exception e2) {
e.addSuppressed(e2);
throw e;
}
}
}
use of org.apache.calcite.tools.ValidationException in project druid by apache.
the class SqlLifecycle method plan.
/**
* Plan the query to enable execution.
*
* If successful, the lifecycle will first transition from {@link State#AUTHORIZED} to {@link State#PLANNED}.
*/
public void plan() throws RelConversionException {
transition(State.AUTHORIZED, State.PLANNED);
Preconditions.checkNotNull(plannerContext, "Cannot plan, plannerContext is null");
try (DruidPlanner planner = plannerFactory.createPlannerWithContext(plannerContext)) {
this.plannerResult = planner.plan();
}// we can't collapse catch clauses since SqlPlanningException has type-sensitive constructors.
catch (SqlParseException e) {
throw new SqlPlanningException(e);
} catch (ValidationException e) {
throw new SqlPlanningException(e);
}
}
use of org.apache.calcite.tools.ValidationException in project druid by apache.
the class SqlLifecycle method validate.
private ValidationResult validate(AuthenticationResult authenticationResult) {
try (DruidPlanner planner = plannerFactory.createPlanner(sql, queryContext)) {
// set planner context for logs/metrics in case something explodes early
this.plannerContext = planner.getPlannerContext();
this.plannerContext.setAuthenticationResult(authenticationResult);
// set parameters on planner context, if parameters have already been set
this.plannerContext.setParameters(parameters);
this.validationResult = planner.validate(authConfig.authorizeQueryContextParams());
return validationResult;
}// we can't collapse catch clauses since SqlPlanningException has type-sensitive constructors.
catch (SqlParseException e) {
throw new SqlPlanningException(e);
} catch (ValidationException e) {
throw new SqlPlanningException(e);
}
}
use of org.apache.calcite.tools.ValidationException in project druid by apache.
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(boolean authorizeContextParams) throws SqlParseException, ValidationException {
resetPlanner();
final ParsedNodes parsed = ParsedNodes.create(planner.parse(plannerContext.getSql()), plannerContext.getTimeZone());
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.getInsertOrReplace() != null) {
final String targetDataSource = validateAndGetDataSourceForIngest(parsed.getInsertOrReplace());
resourceActions.add(new ResourceAction(new Resource(targetDataSource, ResourceType.DATASOURCE), Action.WRITE));
}
if (authorizeContextParams) {
plannerContext.getQueryContext().getUserParams().keySet().forEach(contextParam -> resourceActions.add(new ResourceAction(new Resource(contextParam, ResourceType.QUERY_CONTEXT), Action.WRITE)));
}
plannerContext.setResourceActions(resourceActions);
return new ValidationResult(resourceActions);
}
Aggregations