Search in sources :

Example 21 with SqlNode

use of org.apache.calcite.sql.SqlNode in project drill by apache.

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 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) 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) SqlCreateTable(org.apache.drill.exec.planner.sql.parser.SqlCreateTable) RelConversionException(org.apache.calcite.tools.RelConversionException) 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 22 with SqlNode

use of org.apache.calcite.sql.SqlNode in project drill by apache.

the class DefaultSqlHandler method validateNode.

private TypedSqlNode validateNode(SqlNode sqlNode) throws ValidationException, RelConversionException, ForemanSetupException {
    final SqlNode sqlNodeValidated = config.getConverter().validate(sqlNode);
    final TypedSqlNode typedSqlNode = new TypedSqlNode(sqlNodeValidated, config.getConverter().getOutputType(sqlNodeValidated));
    // Check if the unsupported functionality is used
    UnsupportedOperatorsVisitor visitor = UnsupportedOperatorsVisitor.createVisitor(context);
    try {
        sqlNodeValidated.accept(visitor);
    } catch (UnsupportedOperationException ex) {
        // If the exception due to the unsupported functionalities
        visitor.convertException();
        // If it is not, let this exception move forward to higher logic
        throw ex;
    }
    return typedSqlNode;
}
Also used : UnsupportedOperatorsVisitor(org.apache.drill.exec.planner.sql.parser.UnsupportedOperatorsVisitor) SqlNode(org.apache.calcite.sql.SqlNode) TypedSqlNode(org.apache.calcite.sql.TypedSqlNode) TypedSqlNode(org.apache.calcite.sql.TypedSqlNode)

Example 23 with SqlNode

use of org.apache.calcite.sql.SqlNode in project drill by apache.

the class DrillAvgVarianceConvertlet method expandAvg.

private SqlNode expandAvg(final SqlNode arg) {
    final SqlParserPos pos = SqlParserPos.ZERO;
    final SqlNode sum = SqlStdOperatorTable.SUM.createCall(pos, arg);
    final SqlNode count = SqlStdOperatorTable.COUNT.createCall(pos, arg);
    final SqlNode sumAsDouble = CastHighOp.createCall(pos, sum);
    return SqlStdOperatorTable.DIVIDE.createCall(pos, sumAsDouble, count);
}
Also used : SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlNode (org.apache.calcite.sql.SqlNode)23 RelNode (org.apache.calcite.rel.RelNode)6 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)5 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)5 Planner (org.apache.calcite.tools.Planner)5 SchemaPlus (org.apache.calcite.schema.SchemaPlus)4 SqlSelect (org.apache.calcite.sql.SqlSelect)4 HashMap (java.util.HashMap)3 SqlNodeList (org.apache.calcite.sql.SqlNodeList)3 RelConversionException (org.apache.calcite.tools.RelConversionException)3 SqlCreateFunction (org.apache.storm.sql.parser.SqlCreateFunction)3 SqlCreateTable (org.apache.storm.sql.parser.SqlCreateTable)3 StormParser (org.apache.storm.sql.parser.StormParser)3 QueryPlanner (org.apache.storm.sql.planner.trident.QueryPlanner)3 ISqlTridentDataSource (org.apache.storm.sql.runtime.ISqlTridentDataSource)3 ArrayList (java.util.ArrayList)2 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)2 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)2 CalciteCatalogReader (org.apache.calcite.prepare.CalciteCatalogReader)2 StreamableTable (org.apache.calcite.schema.StreamableTable)2