Search in sources :

Example 6 with ValidationException

use of org.apache.calcite.tools.ValidationException in project druid by apache.

the class DruidPlanner method validateAndGetDataSourceForInsert.

/**
 * Extract target datasource from a {@link SqlInsert}, and also validate that the INSERT is of a form we support.
 * Expects the INSERT target to be either an unqualified name, or a name qualified by the default schema.
 */
private String validateAndGetDataSourceForInsert(final SqlInsert insert) throws ValidationException {
    if (insert.isUpsert()) {
        throw new ValidationException("UPSERT is not supported.");
    }
    if (insert.getTargetColumnList() != null) {
        throw new ValidationException("INSERT with target column list is not supported.");
    }
    final SqlIdentifier tableIdentifier = (SqlIdentifier) insert.getTargetTable();
    final String dataSource;
    if (tableIdentifier.names.isEmpty()) {
        // I don't think this can happen, but include a branch for it just in case.
        throw new ValidationException("INSERT requires target table.");
    } else if (tableIdentifier.names.size() == 1) {
        // Unqualified name.
        dataSource = Iterables.getOnlyElement(tableIdentifier.names);
    } else {
        // Qualified name.
        final String defaultSchemaName = Iterables.getOnlyElement(CalciteSchema.from(frameworkConfig.getDefaultSchema()).path(null));
        if (tableIdentifier.names.size() == 2 && defaultSchemaName.equals(tableIdentifier.names.get(0))) {
            dataSource = tableIdentifier.names.get(1);
        } else {
            throw new ValidationException(StringUtils.format("Cannot INSERT into [%s] because it is not a Druid datasource.", tableIdentifier));
        }
    }
    try {
        IdUtils.validateId("INSERT dataSource", dataSource);
    } catch (IllegalArgumentException e) {
        throw new ValidationException(e.getMessage());
    }
    return dataSource;
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 7 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() 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);
}
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)

Example 8 with ValidationException

use of org.apache.calcite.tools.ValidationException 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);
}
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)

Example 9 with ValidationException

use of org.apache.calcite.tools.ValidationException in project druid by druid-io.

the class DruidPlanner method validateAndGetDataSourceForInsert.

/**
 * Extract target datasource from a {@link SqlInsert}, and also validate that the INSERT is of a form we support.
 * Expects the INSERT target to be either an unqualified name, or a name qualified by the default schema.
 */
private String validateAndGetDataSourceForInsert(final SqlInsert insert) throws ValidationException {
    if (insert.isUpsert()) {
        throw new ValidationException("UPSERT is not supported.");
    }
    if (insert.getTargetColumnList() != null) {
        throw new ValidationException("INSERT with target column list is not supported.");
    }
    final SqlIdentifier tableIdentifier = (SqlIdentifier) insert.getTargetTable();
    final String dataSource;
    if (tableIdentifier.names.isEmpty()) {
        // I don't think this can happen, but include a branch for it just in case.
        throw new ValidationException("INSERT requires target table.");
    } else if (tableIdentifier.names.size() == 1) {
        // Unqualified name.
        dataSource = Iterables.getOnlyElement(tableIdentifier.names);
    } else {
        // Qualified name.
        final String defaultSchemaName = Iterables.getOnlyElement(CalciteSchema.from(frameworkConfig.getDefaultSchema()).path(null));
        if (tableIdentifier.names.size() == 2 && defaultSchemaName.equals(tableIdentifier.names.get(0))) {
            dataSource = tableIdentifier.names.get(1);
        } else {
            throw new ValidationException(StringUtils.format("Cannot INSERT into [%s] because it is not a Druid datasource.", tableIdentifier));
        }
    }
    try {
        IdUtils.validateId("INSERT dataSource", dataSource);
    } catch (IllegalArgumentException e) {
        throw new ValidationException(e.getMessage());
    }
    return dataSource;
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier)

Example 10 with ValidationException

use of org.apache.calcite.tools.ValidationException in project calcite by apache.

the class SqlDdlNodes method populate.

/**
 * Populates the table called {@code name} by executing {@code query}.
 */
protected static void populate(SqlIdentifier name, SqlNode query, CalcitePrepare.Context context) {
    // Generate, prepare and execute an "INSERT INTO table query" statement.
    // (It's a bit inefficient that we convert from SqlNode to SQL and back
    // again.)
    final FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(context.getRootSchema().plus()).build();
    final Planner planner = Frameworks.getPlanner(config);
    try {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw);
        final SqlPrettyWriter w = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, false, pw);
        pw.print("INSERT INTO ");
        name.unparse(w, 0, 0);
        pw.print(" ");
        query.unparse(w, 0, 0);
        pw.flush();
        final String sql = sw.toString();
        final SqlNode query1 = planner.parse(sql);
        final SqlNode query2 = planner.validate(query1);
        final RelRoot r = planner.rel(query2);
        final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
        int rowCount = prepare.executeUpdate();
        Util.discard(rowCount);
        prepare.close();
    } catch (SqlParseException | ValidationException | RelConversionException | SQLException e) {
        throw new RuntimeException(e);
    }
}
Also used : ValidationException(org.apache.calcite.tools.ValidationException) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) SQLException(java.sql.SQLException) RelRoot(org.apache.calcite.rel.RelRoot) PreparedStatement(java.sql.PreparedStatement) RelConversionException(org.apache.calcite.tools.RelConversionException) StringWriter(java.io.StringWriter) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) PrintWriter(java.io.PrintWriter) SqlNode(org.apache.calcite.sql.SqlNode)

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