Search in sources :

Example 16 with ValidationException

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());
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) ZonedDateTime(java.time.ZonedDateTime) Timestamp(java.sql.Timestamp) SqlTimestampLiteral(org.apache.calcite.sql.SqlTimestampLiteral)

Example 17 with ValidationException

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;
        }
    }
}
Also used : SqlExplain(org.apache.calcite.sql.SqlExplain) RelRoot(org.apache.calcite.rel.RelRoot) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) ValidationException(org.apache.calcite.tools.ValidationException) RelConversionException(org.apache.calcite.tools.RelConversionException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 18 with ValidationException

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);
    }
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) DruidPlanner(org.apache.druid.sql.calcite.planner.DruidPlanner)

Example 19 with ValidationException

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);
    }
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) DruidPlanner(org.apache.druid.sql.calcite.planner.DruidPlanner)

Example 20 with ValidationException

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);
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlValidator(org.apache.calcite.sql.validate.SqlValidator) Resource(org.apache.druid.server.security.Resource) SqlNode(org.apache.calcite.sql.SqlNode) ResourceAction(org.apache.druid.server.security.ResourceAction) HashSet(java.util.HashSet)

Aggregations

ValidationException (org.apache.calcite.tools.ValidationException)35 SqlNode (org.apache.calcite.sql.SqlNode)16 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)16 RelConversionException (org.apache.calcite.tools.RelConversionException)12 RelRoot (org.apache.calcite.rel.RelRoot)7 Planner (org.apache.calcite.tools.Planner)6 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)5 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 ZonedDateTime (java.time.ZonedDateTime)4 SqlPrettyWriter (org.apache.calcite.sql.pretty.SqlPrettyWriter)4 AndDimFilter (org.apache.druid.query.filter.AndDimFilter)4 BoundDimFilter (org.apache.druid.query.filter.BoundDimFilter)4 DimFilter (org.apache.druid.query.filter.DimFilter)4 NotDimFilter (org.apache.druid.query.filter.NotDimFilter)4 OrDimFilter (org.apache.druid.query.filter.OrDimFilter)4 SQLPlannedOperationStatement (herddb.model.commands.SQLPlannedOperationStatement)3 ScanStatement (herddb.model.commands.ScanStatement)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3