Search in sources :

Example 1 with ValidationException

use of org.apache.beam.vendor.calcite.v1_28_0.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)

Example 2 with ValidationException

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.ValidationException in project drill by axbaretto.

the class RefreshMetadataHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
    final SqlRefreshMetadata refreshTable = unwrap(sqlNode, SqlRefreshMetadata.class);
    try {
        final SchemaPlus schema = findSchema(config.getConverter().getDefaultSchema(), refreshTable.getSchemaPath());
        if (schema == null) {
            return direct(false, "Storage plugin or workspace does not exist [%s]", SchemaUtilites.SCHEMA_PATH_JOINER.join(refreshTable.getSchemaPath()));
        }
        final String tableName = refreshTable.getName();
        if (tableName.contains("*") || tableName.contains("?")) {
            return direct(false, "Glob path %s not supported for metadata refresh", tableName);
        }
        final Table table = schema.getTable(tableName);
        if (table == null) {
            return direct(false, "Table %s does not exist.", tableName);
        }
        if (!(table instanceof DrillTable)) {
            return notSupported(tableName);
        }
        final DrillTable drillTable = (DrillTable) table;
        final Object selection = drillTable.getSelection();
        if (selection instanceof FileSelection && ((FileSelection) selection).isEmptyDirectory()) {
            return direct(false, "Table %s is empty and doesn't contain any parquet files.", tableName);
        }
        if (!(selection instanceof FormatSelection)) {
            return notSupported(tableName);
        }
        FormatSelection formatSelection = (FormatSelection) selection;
        FormatPluginConfig formatConfig = formatSelection.getFormat();
        if (!((formatConfig instanceof ParquetFormatConfig) || ((formatConfig instanceof NamedFormatPluginConfig) && ((NamedFormatPluginConfig) formatConfig).name.equals("parquet")))) {
            return notSupported(tableName);
        }
        FileSystemPlugin plugin = (FileSystemPlugin) drillTable.getPlugin();
        DrillFileSystem fs = new DrillFileSystem(plugin.getFormatPlugin(formatSelection.getFormat()).getFsConf());
        String selectionRoot = formatSelection.getSelection().selectionRoot;
        if (!fs.getFileStatus(new Path(selectionRoot)).isDirectory()) {
            return notSupported(tableName);
        }
        if (!(formatConfig instanceof ParquetFormatConfig)) {
            formatConfig = new ParquetFormatConfig();
        }
        Metadata.createMeta(fs, selectionRoot, (ParquetFormatConfig) formatConfig);
        return direct(true, "Successfully updated metadata for table %s.", tableName);
    } catch (Exception e) {
        logger.error("Failed to update metadata for table '{}'", refreshTable.getName(), e);
        return DirectPlan.createDirectPlan(context, false, String.format("Error: %s", e.getMessage()));
    }
}
Also used : FileSelection(org.apache.drill.exec.store.dfs.FileSelection) Path(org.apache.hadoop.fs.Path) FileSystemPlugin(org.apache.drill.exec.store.dfs.FileSystemPlugin) Table(org.apache.calcite.schema.Table) DrillTable(org.apache.drill.exec.planner.logical.DrillTable) DrillTable(org.apache.drill.exec.planner.logical.DrillTable) SchemaPlus(org.apache.calcite.schema.SchemaPlus) FormatSelection(org.apache.drill.exec.store.dfs.FormatSelection) SqlRefreshMetadata(org.apache.drill.exec.planner.sql.parser.SqlRefreshMetadata) IOException(java.io.IOException) ValidationException(org.apache.calcite.tools.ValidationException) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) RelConversionException(org.apache.calcite.tools.RelConversionException) NamedFormatPluginConfig(org.apache.drill.exec.store.dfs.NamedFormatPluginConfig) DrillFileSystem(org.apache.drill.exec.store.dfs.DrillFileSystem) NamedFormatPluginConfig(org.apache.drill.exec.store.dfs.NamedFormatPluginConfig) FormatPluginConfig(org.apache.drill.common.logical.FormatPluginConfig) ParquetFormatConfig(org.apache.drill.exec.store.parquet.ParquetFormatConfig)

Example 3 with ValidationException

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.ValidationException in project drill by axbaretto.

the class DrillSqlWorker method getQueryPlan.

/**
 * Converts sql query string into query physical plan.
 *
 * @param context query context
 * @param sql sql query
 * @param textPlan text plan
 * @return query physical plan
 */
private static PhysicalPlan getQueryPlan(QueryContext context, String sql, Pointer<String> textPlan) throws ForemanSetupException {
    final SqlConverter parser = new SqlConverter(context);
    injector.injectChecked(context.getExecutionControls(), "sql-parsing", ForemanSetupException.class);
    final SqlNode sqlNode = parser.parse(sql);
    final AbstractSqlHandler handler;
    final SqlHandlerConfig config = new SqlHandlerConfig(context, parser);
    switch(sqlNode.getKind()) {
        case EXPLAIN:
            handler = new ExplainHandler(config, textPlan);
            break;
        case SET_OPTION:
            handler = new SetOptionHandler(context);
            break;
        case DESCRIBE_TABLE:
            if (sqlNode instanceof DrillSqlDescribeTable) {
                handler = new DescribeTableHandler(config);
                break;
            }
        case DESCRIBE_SCHEMA:
            if (sqlNode instanceof SqlDescribeSchema) {
                handler = new DescribeSchemaHandler(config);
                break;
            }
        case OTHER:
            if (sqlNode instanceof SqlCreateTable) {
                handler = ((DrillSqlCall) sqlNode).getSqlHandler(config, textPlan);
                break;
            }
            if (sqlNode instanceof DrillSqlCall) {
                handler = ((DrillSqlCall) sqlNode).getSqlHandler(config);
                break;
            }
        // fallthrough
        default:
            handler = new DefaultSqlHandler(config, textPlan);
    }
    try {
        return handler.getPlan(sqlNode);
    } catch (ValidationException e) {
        String errorMessage = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        throw UserException.validationError(e).message(errorMessage).build(logger);
    } catch (AccessControlException e) {
        throw UserException.permissionError(e).build(logger);
    } catch (SqlUnsupportedException e) {
        throw UserException.unsupportedError(e).build(logger);
    } catch (IOException | RelConversionException e) {
        throw new QueryInputException("Failure handling SQL.", e);
    }
}
Also used : DefaultSqlHandler(org.apache.drill.exec.planner.sql.handlers.DefaultSqlHandler) ExplainHandler(org.apache.drill.exec.planner.sql.handlers.ExplainHandler) ValidationException(org.apache.calcite.tools.ValidationException) DescribeTableHandler(org.apache.drill.exec.planner.sql.handlers.DescribeTableHandler) SetOptionHandler(org.apache.drill.exec.planner.sql.handlers.SetOptionHandler) AccessControlException(org.apache.hadoop.security.AccessControlException) SqlHandlerConfig(org.apache.drill.exec.planner.sql.handlers.SqlHandlerConfig) IOException(java.io.IOException) SqlDescribeSchema(org.apache.calcite.sql.SqlDescribeSchema) DescribeSchemaHandler(org.apache.drill.exec.planner.sql.handlers.DescribeSchemaHandler) SqlCreateTable(org.apache.drill.exec.planner.sql.parser.SqlCreateTable) RelConversionException(org.apache.calcite.tools.RelConversionException) DrillSqlDescribeTable(org.apache.drill.exec.planner.sql.parser.DrillSqlDescribeTable) AbstractSqlHandler(org.apache.drill.exec.planner.sql.handlers.AbstractSqlHandler) SqlUnsupportedException(org.apache.drill.exec.work.foreman.SqlUnsupportedException) DrillSqlCall(org.apache.drill.exec.planner.sql.parser.DrillSqlCall) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with ValidationException

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.ValidationException in project beam by apache.

the class CalciteQueryPlanner method convertToBeamRel.

/**
 * It parses and validate the input query, then convert into a {@link BeamRelNode} tree. Note that
 * query parameters are not yet supported.
 */
@Override
public BeamRelNode convertToBeamRel(String sqlStatement, QueryParameters queryParameters) throws ParseException, SqlConversionException {
    Preconditions.checkArgument(queryParameters.getKind() == Kind.NONE, "Beam SQL Calcite dialect does not yet support query parameters.");
    BeamRelNode beamRelNode;
    try {
        SqlNode parsed = planner.parse(sqlStatement);
        TableResolutionUtils.setupCustomTableResolution(connection, parsed);
        SqlNode validated = planner.validate(parsed);
        LOG.info("SQL:\n" + validated);
        // root of original logical plan
        RelRoot root = planner.rel(validated);
        LOG.info("SQLPlan>\n" + RelOptUtil.toString(root.rel));
        RelTraitSet desiredTraits = root.rel.getTraitSet().replace(BeamLogicalConvention.INSTANCE).replace(root.collation).simplify();
        // beam physical plan
        root.rel.getCluster().setMetadataProvider(ChainedRelMetadataProvider.of(ImmutableList.of(NonCumulativeCostImpl.SOURCE, RelMdNodeStats.SOURCE, root.rel.getCluster().getMetadataProvider())));
        root.rel.getCluster().setMetadataQuerySupplier(BeamRelMetadataQuery::instance);
        RelMetadataQuery.THREAD_PROVIDERS.set(JaninoRelMetadataProvider.of(root.rel.getCluster().getMetadataProvider()));
        root.rel.getCluster().invalidateMetadataQuery();
        beamRelNode = (BeamRelNode) planner.transform(0, desiredTraits, root.rel);
        LOG.info("BEAMPlan>\n" + RelOptUtil.toString(beamRelNode));
    } catch (RelConversionException | CannotPlanException e) {
        throw new SqlConversionException(String.format("Unable to convert query %s", sqlStatement), e);
    } catch (SqlParseException | ValidationException e) {
        throw new ParseException(String.format("Unable to parse query %s", sqlStatement), e);
    } finally {
        planner.close();
    }
    return beamRelNode;
}
Also used : BeamRelNode(org.apache.beam.sdk.extensions.sql.impl.rel.BeamRelNode) BeamRelMetadataQuery(org.apache.beam.sdk.extensions.sql.impl.planner.BeamRelMetadataQuery) ValidationException(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.ValidationException) CannotPlanException(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelOptPlanner.CannotPlanException) SqlParseException(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParseException) RelRoot(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rel.RelRoot) RelTraitSet(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.plan.RelTraitSet) RelConversionException(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.tools.RelConversionException) SqlParseException(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParseException) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)

Example 5 with ValidationException

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

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

Aggregations

ValidationException (org.apache.calcite.tools.ValidationException)11 SqlParseException (org.apache.calcite.sql.parser.SqlParseException)6 RelConversionException (org.apache.calcite.tools.RelConversionException)6 SqlNode (org.apache.calcite.sql.SqlNode)5 RelRoot (org.apache.calcite.rel.RelRoot)3 IOException (java.io.IOException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 MetadataStorageManagerException (herddb.metadata.MetadataStorageManagerException)1 ExecutionPlan (herddb.model.ExecutionPlan)1 StatementExecutionException (herddb.model.StatementExecutionException)1 Table (herddb.model.Table)1 GetStatement (herddb.model.commands.GetStatement)1 SQLPlannedOperationStatement (herddb.model.commands.SQLPlannedOperationStatement)1 ScanStatement (herddb.model.commands.ScanStatement)1 LimitOp (herddb.model.planner.LimitOp)1 PlannerOp (herddb.model.planner.PlannerOp)1 ValuesOp (herddb.model.planner.ValuesOp)1 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)1 ConstantExpression (herddb.sql.expressions.ConstantExpression)1 PrintWriter (java.io.PrintWriter)1