Search in sources :

Example 1 with SqlPlanningException

use of org.apache.druid.sql.SqlPlanningException in project druid by druid-io.

the class SqlResource method doPost.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response doPost(final SqlQuery sqlQuery, @Context final HttpServletRequest req) throws IOException {
    final SqlLifecycle lifecycle = sqlLifecycleFactory.factorize();
    final String sqlQueryId = lifecycle.initialize(sqlQuery.getQuery(), sqlQuery.getContext());
    final String remoteAddr = req.getRemoteAddr();
    final String currThreadName = Thread.currentThread().getName();
    try {
        Thread.currentThread().setName(StringUtils.format("sql[%s]", sqlQueryId));
        lifecycle.setParameters(sqlQuery.getParameterList());
        lifecycle.validateAndAuthorize(req);
        // must add after lifecycle is authorized
        sqlLifecycleManager.add(sqlQueryId, lifecycle);
        lifecycle.plan();
        final SqlRowTransformer rowTransformer = lifecycle.createRowTransformer();
        final Sequence<Object[]> sequence = lifecycle.execute();
        final Yielder<Object[]> yielder0 = Yielders.each(sequence);
        try {
            final Response.ResponseBuilder responseBuilder = Response.ok((StreamingOutput) outputStream -> {
                Exception e = null;
                CountingOutputStream os = new CountingOutputStream(outputStream);
                Yielder<Object[]> yielder = yielder0;
                try (final ResultFormat.Writer writer = sqlQuery.getResultFormat().createFormatter(os, jsonMapper)) {
                    writer.writeResponseStart();
                    if (sqlQuery.includeHeader()) {
                        writer.writeHeader(rowTransformer.getRowType(), sqlQuery.includeTypesHeader(), sqlQuery.includeSqlTypesHeader());
                    }
                    while (!yielder.isDone()) {
                        final Object[] row = yielder.get();
                        writer.writeRowStart();
                        for (int i = 0; i < rowTransformer.getFieldList().size(); i++) {
                            final Object value = rowTransformer.transform(row, i);
                            writer.writeRowField(rowTransformer.getFieldList().get(i), value);
                        }
                        writer.writeRowEnd();
                        yielder = yielder.next(null);
                    }
                    writer.writeResponseEnd();
                } catch (Exception ex) {
                    e = ex;
                    log.error(ex, "Unable to send SQL response [%s]", sqlQueryId);
                    throw new RuntimeException(ex);
                } finally {
                    yielder.close();
                    endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, os.getCount());
                }
            }).header(SQL_QUERY_ID_RESPONSE_HEADER, sqlQueryId);
            if (sqlQuery.includeHeader()) {
                responseBuilder.header(SQL_HEADER_RESPONSE_HEADER, SQL_HEADER_VALUE);
            }
            return responseBuilder.build();
        } catch (Throwable e) {
            // make sure to close yielder if anything happened before starting to serialize the response.
            yielder0.close();
            throw new RuntimeException(e);
        }
    } catch (QueryCapacityExceededException cap) {
        endLifecycle(sqlQueryId, lifecycle, cap, remoteAddr, -1);
        return buildNonOkResponse(QueryCapacityExceededException.STATUS_CODE, cap, sqlQueryId);
    } catch (QueryUnsupportedException unsupported) {
        endLifecycle(sqlQueryId, lifecycle, unsupported, remoteAddr, -1);
        return buildNonOkResponse(QueryUnsupportedException.STATUS_CODE, unsupported, sqlQueryId);
    } catch (QueryTimeoutException timeout) {
        endLifecycle(sqlQueryId, lifecycle, timeout, remoteAddr, -1);
        return buildNonOkResponse(QueryTimeoutException.STATUS_CODE, timeout, sqlQueryId);
    } catch (SqlPlanningException | ResourceLimitExceededException e) {
        endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, -1);
        return buildNonOkResponse(BadQueryException.STATUS_CODE, e, sqlQueryId);
    } catch (ForbiddenException e) {
        endLifecycleWithoutEmittingMetrics(sqlQueryId, lifecycle);
        throw (ForbiddenException) serverConfig.getErrorResponseTransformStrategy().transformIfNeeded(// let ForbiddenExceptionMapper handle this
        e);
    } catch (RelOptPlanner.CannotPlanException e) {
        endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, -1);
        SqlPlanningException spe = new SqlPlanningException(SqlPlanningException.PlanningError.UNSUPPORTED_SQL_ERROR, e.getMessage());
        return buildNonOkResponse(BadQueryException.STATUS_CODE, spe, sqlQueryId);
    }// calcite throws a java.lang.AssertionError which is type error not exception. using throwable will catch all
     catch (Throwable e) {
        log.warn(e, "Failed to handle query: %s", sqlQuery);
        endLifecycle(sqlQueryId, lifecycle, e, remoteAddr, -1);
        return buildNonOkResponse(Status.INTERNAL_SERVER_ERROR.getStatusCode(), QueryInterruptedException.wrapIfNeeded(e), sqlQueryId);
    } finally {
        Thread.currentThread().setName(currThreadName);
    }
}
Also used : SqlRowTransformer(org.apache.druid.sql.SqlRowTransformer) StreamingOutput(javax.ws.rs.core.StreamingOutput) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) CountingOutputStream(com.google.common.io.CountingOutputStream) SqlPlanningException(org.apache.druid.sql.SqlPlanningException) ForbiddenException(org.apache.druid.server.security.ForbiddenException) QueryCapacityExceededException(org.apache.druid.query.QueryCapacityExceededException) QueryUnsupportedException(org.apache.druid.query.QueryUnsupportedException) SqlLifecycle(org.apache.druid.sql.SqlLifecycle) BadQueryException(org.apache.druid.query.BadQueryException) QueryCapacityExceededException(org.apache.druid.query.QueryCapacityExceededException) SqlPlanningException(org.apache.druid.sql.SqlPlanningException) ForbiddenException(org.apache.druid.server.security.ForbiddenException) SanitizableException(org.apache.druid.common.exception.SanitizableException) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) QueryTimeoutException(org.apache.druid.query.QueryTimeoutException) ResourceLimitExceededException(org.apache.druid.query.ResourceLimitExceededException) QueryUnsupportedException(org.apache.druid.query.QueryUnsupportedException) Response(javax.ws.rs.core.Response) ResourceLimitExceededException(org.apache.druid.query.ResourceLimitExceededException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 2 with SqlPlanningException

use of org.apache.druid.sql.SqlPlanningException in project druid by druid-io.

the class CalciteInsertDmlTest method testInsertWithoutPartitionedBy.

@Test
public void testInsertWithoutPartitionedBy() {
    SqlPlanningException e = Assert.assertThrows(SqlPlanningException.class, () -> testQuery(StringUtils.format("INSERT INTO dst SELECT * FROM %s", externSql(externalDataSource)), ImmutableList.of(), ImmutableList.of()));
    Assert.assertEquals("INSERT statements must specify PARTITIONED BY clause explictly", e.getMessage());
    didTest = true;
}
Also used : SqlPlanningException(org.apache.druid.sql.SqlPlanningException) Test(org.junit.Test)

Example 3 with SqlPlanningException

use of org.apache.druid.sql.SqlPlanningException in project druid by druid-io.

the class RelParameterizerShuttle method bind.

private RexNode bind(RexNode node, RexBuilder builder, RelDataTypeFactory typeFactory) {
    if (node instanceof RexDynamicParam) {
        RexDynamicParam dynamicParam = (RexDynamicParam) node;
        // if we have a value for dynamic parameter, replace with a literal, else add to list of unbound parameters
        if (plannerContext.getParameters().size() > dynamicParam.getIndex()) {
            TypedValue param = plannerContext.getParameters().get(dynamicParam.getIndex());
            if (param == null) {
                throw new SqlPlanningException(PlanningError.VALIDATION_ERROR, StringUtils.format("Parameter at position[%s] is not bound", dynamicParam.getIndex()));
            }
            if (param.value == null) {
                return builder.makeNullLiteral(typeFactory.createSqlType(SqlTypeName.NULL));
            }
            SqlTypeName typeName = SqlTypeName.getNameForJdbcType(param.type.typeId);
            return builder.makeLiteral(param.value, typeFactory.createSqlType(typeName), true);
        } else {
            throw new SqlPlanningException(PlanningError.VALIDATION_ERROR, StringUtils.format("Parameter at position[%s] is not bound", dynamicParam.getIndex()));
        }
    }
    return node;
}
Also used : SqlPlanningException(org.apache.druid.sql.SqlPlanningException) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) TypedValue(org.apache.calcite.avatica.remote.TypedValue)

Aggregations

SqlPlanningException (org.apache.druid.sql.SqlPlanningException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 CountingOutputStream (com.google.common.io.CountingOutputStream)1 IOException (java.io.IOException)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 TypedValue (org.apache.calcite.avatica.remote.TypedValue)1 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)1 RexDynamicParam (org.apache.calcite.rex.RexDynamicParam)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1 SanitizableException (org.apache.druid.common.exception.SanitizableException)1 BadQueryException (org.apache.druid.query.BadQueryException)1 QueryCapacityExceededException (org.apache.druid.query.QueryCapacityExceededException)1 QueryInterruptedException (org.apache.druid.query.QueryInterruptedException)1 QueryTimeoutException (org.apache.druid.query.QueryTimeoutException)1 QueryUnsupportedException (org.apache.druid.query.QueryUnsupportedException)1 ResourceLimitExceededException (org.apache.druid.query.ResourceLimitExceededException)1