Search in sources :

Example 6 with SqlParseException

use of org.apache.calcite.sql.parser.SqlParseException in project samza by apache.

the class SamzaSqlQueryParser method parseQuery.

public static QueryInfo parseQuery(String sql) {
    Planner planner = createPlanner();
    SqlNode sqlNode;
    // Having semi-colons at the end of sql statement is a valid syntax in standard sql but not for Calcite parser.
    // Hence, removing trailing semi-colon before passing sql statement to Calcite parser.
    sql = sql.replaceAll(TRAILING_SEMI_COLON_REGEX, "");
    try {
        sqlNode = planner.parse(sql);
    } catch (SqlParseException e) {
        String errorMsg = SamzaSqlValidator.formatErrorString(sql, e);
        LOG.error(errorMsg, e);
        throw new SamzaException(errorMsg, e);
    }
    String sink;
    String selectQuery;
    ArrayList<String> sources;
    if (sqlNode instanceof SqlInsert) {
        SqlInsert sqlInsert = (SqlInsert) sqlNode;
        sink = sqlInsert.getTargetTable().toString();
        if (sqlInsert.getSource() instanceof SqlSelect) {
            SqlSelect sqlSelect = (SqlSelect) sqlInsert.getSource();
            selectQuery = sqlSelect.toString();
            LOG.info("Parsed select query {} from sql {}", selectQuery, sql);
            sources = getSourcesFromSelectQuery(sqlSelect);
        } else {
            String msg = String.format("Sql query is not of the expected format. Select node expected, found %s", sqlInsert.getSource().getClass().toString());
            LOG.error(msg);
            throw new SamzaException(msg);
        }
    } else {
        String msg = String.format("Sql query is not of the expected format. Insert node expected, found %s", sqlNode.getClass().toString());
        LOG.error(msg);
        throw new SamzaException(msg);
    }
    return new QueryInfo(selectQuery, sources, sink, sql);
}
Also used : SqlSelect(org.apache.calcite.sql.SqlSelect) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) Planner(org.apache.calcite.tools.Planner) SqlInsert(org.apache.calcite.sql.SqlInsert) SamzaException(org.apache.samza.SamzaException) SqlNode(org.apache.calcite.sql.SqlNode)

Example 7 with SqlParseException

use of org.apache.calcite.sql.parser.SqlParseException 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 8 with SqlParseException

use of org.apache.calcite.sql.parser.SqlParseException 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)

Example 9 with SqlParseException

use of org.apache.calcite.sql.parser.SqlParseException 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 10 with SqlParseException

use of org.apache.calcite.sql.parser.SqlParseException in project calcite by apache.

the class CassandraSchema method addMaterializedViews.

/**
 * Add all materialized views defined in the schema to this column family
 */
private void addMaterializedViews() {
    // Close the hook use to get us here
    hook.close();
    for (MaterializedViewMetadata view : getKeyspace().getMaterializedViews()) {
        String tableName = view.getBaseTable().getName();
        StringBuilder queryBuilder = new StringBuilder("SELECT ");
        // Add all the selected columns to the query
        List<String> columnNames = new ArrayList<String>();
        for (ColumnMetadata column : view.getColumns()) {
            columnNames.add("\"" + column.getName() + "\"");
        }
        queryBuilder.append(Util.toString(columnNames, "", ", ", ""));
        queryBuilder.append(" FROM \"" + tableName + "\"");
        // Get the where clause from the system schema
        String whereQuery = "SELECT where_clause from system_schema.views " + "WHERE keyspace_name='" + keyspace + "' AND view_name='" + view.getName() + "'";
        queryBuilder.append(" WHERE " + session.execute(whereQuery).one().getString(0));
        // Parse and unparse the view query to get properly quoted field names
        String query = queryBuilder.toString();
        SqlParser.ConfigBuilder configBuilder = SqlParser.configBuilder();
        configBuilder.setUnquotedCasing(Casing.UNCHANGED);
        SqlSelect parsedQuery;
        try {
            parsedQuery = (SqlSelect) SqlParser.create(query, configBuilder.build()).parseQuery();
        } catch (SqlParseException e) {
            LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName());
            continue;
        }
        StringWriter stringWriter = new StringWriter(query.length());
        PrintWriter printWriter = new PrintWriter(stringWriter);
        SqlWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, true, printWriter);
        parsedQuery.unparse(writer, 0, 0);
        query = stringWriter.toString();
        // Add the view for this query
        String viewName = "$" + getTableNames().size();
        SchemaPlus schema = parentSchema.getSubSchema(name);
        CalciteSchema calciteSchema = CalciteSchema.from(schema);
        List<String> viewPath = calciteSchema.path(viewName);
        schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName(), true));
    }
}
Also used : ColumnMetadata(com.datastax.driver.core.ColumnMetadata) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) ArrayList(java.util.ArrayList) SqlParser(org.apache.calcite.sql.parser.SqlParser) SchemaPlus(org.apache.calcite.schema.SchemaPlus) MaterializedViewMetadata(com.datastax.driver.core.MaterializedViewMetadata) SqlSelect(org.apache.calcite.sql.SqlSelect) StringWriter(java.io.StringWriter) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) PrintWriter(java.io.PrintWriter)

Aggregations

SqlParseException (org.apache.calcite.sql.parser.SqlParseException)16 SqlNode (org.apache.calcite.sql.SqlNode)11 ValidationException (org.apache.calcite.tools.ValidationException)6 SqlParser (org.apache.calcite.sql.parser.SqlParser)4 ArrayList (java.util.ArrayList)3 RelRoot (org.apache.calcite.rel.RelRoot)3 RelConversionException (org.apache.calcite.tools.RelConversionException)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)2 RelOptTable (org.apache.calcite.plan.RelOptTable)2 CalciteContextException (org.apache.calcite.runtime.CalciteContextException)2 SqlSelect (org.apache.calcite.sql.SqlSelect)2 SqlValidator (org.apache.calcite.sql.validate.SqlValidator)2 Planner (org.apache.calcite.tools.Planner)2 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)1 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ImmutableList (com.google.common.collect.ImmutableList)1