Search in sources :

Example 6 with SqlParseException

use of org.apache.beam.vendor.calcite.v1_28_0.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.beam.vendor.calcite.v1_28_0.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.beam.vendor.calcite.v1_28_0.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.beam.vendor.calcite.v1_28_0.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.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParseException in project beam by apache.

the class QueryReader method getQueryIdentifiers.

/**
 * Parse query and get all its identifiers.
 *
 * @param queryString
 * @return Set of SQL query identifiers as strings.
 * @throws SqlParseException
 */
public static Set<String> getQueryIdentifiers(String queryString) throws SqlParseException {
    SqlParser parser = SqlParser.create(queryString);
    SqlNode parsedQuery = parser.parseQuery();
    SqlTransformRunner.SqlIdentifierVisitor sqlVisitor = new SqlTransformRunner.SqlIdentifierVisitor();
    parsedQuery.accept(sqlVisitor);
    return sqlVisitor.getIdentifiers();
}
Also used : SqlParser(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.parser.SqlParser) SqlNode(org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)

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 SqlNode (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlNode)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