Search in sources :

Example 1 with DruidQuery

use of org.apache.druid.sql.calcite.rel.DruidQuery in project druid by druid-io.

the class NativeQueryMaker method runQuery.

@Override
public Sequence<Object[]> runQuery(final DruidQuery druidQuery) {
    final Query<?> query = druidQuery.getQuery();
    if (plannerContext.getPlannerConfig().isRequireTimeCondition() && !(druidQuery.getDataSource() instanceof InlineDataSource)) {
        if (Intervals.ONLY_ETERNITY.equals(findBaseDataSourceIntervals(query))) {
            throw new CannotBuildQueryException("requireTimeCondition is enabled, all queries must include a filter condition on the __time column");
        }
    }
    int numFilters = plannerContext.getPlannerConfig().getMaxNumericInFilters();
    // Instead of IN(v1,v2,v3) user should specify IN('v1','v2','v3')
    if (numFilters != PlannerConfig.NUM_FILTER_NOT_USED) {
        if (query.getFilter() instanceof OrDimFilter) {
            OrDimFilter orDimFilter = (OrDimFilter) query.getFilter();
            int numBoundFilters = 0;
            for (DimFilter filter : orDimFilter.getFields()) {
                numBoundFilters += filter instanceof BoundDimFilter ? 1 : 0;
            }
            if (numBoundFilters > numFilters) {
                String dimension = ((BoundDimFilter) (orDimFilter.getFields().get(0))).getDimension();
                throw new UOE(StringUtils.format("The number of values in the IN clause for [%s] in query exceeds configured maxNumericFilter limit of [%s] for INs. Cast [%s] values of IN clause to String", dimension, numFilters, orDimFilter.getFields().size()));
            }
        }
    }
    final List<String> rowOrder;
    if (query instanceof TimeseriesQuery && !druidQuery.getGrouping().getDimensions().isEmpty()) {
        // Hack for timeseries queries: when generating them, DruidQuery.toTimeseriesQuery translates a dimension
        // based on a timestamp_floor expression into a 'granularity'. This is not reflected in the druidQuery's
        // output row signature, so we have to account for it here.
        // TODO: We can remove this once https://github.com/apache/druid/issues/9974 is done.
        final String timeDimension = Iterables.getOnlyElement(druidQuery.getGrouping().getDimensions()).getOutputName();
        rowOrder = druidQuery.getOutputRowSignature().getColumnNames().stream().map(f -> timeDimension.equals(f) ? ColumnHolder.TIME_COLUMN_NAME : f).collect(Collectors.toList());
    } else {
        rowOrder = druidQuery.getOutputRowSignature().getColumnNames();
    }
    final List<SqlTypeName> columnTypes = druidQuery.getOutputRowType().getFieldList().stream().map(f -> f.getType().getSqlTypeName()).collect(Collectors.toList());
    return execute(query, mapColumnList(rowOrder, fieldMapping), mapColumnList(columnTypes, fieldMapping));
}
Also used : Arrays(java.util.Arrays) DimensionHandlerUtils(org.apache.druid.segment.DimensionHandlerUtils) Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap) IAE(org.apache.druid.java.util.common.IAE) DateTimes(org.apache.druid.java.util.common.DateTimes) Sequence(org.apache.druid.java.util.common.guava.Sequence) Collection(java.util.Collection) StringUtils(org.apache.druid.java.util.common.StringUtils) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) ISE(org.apache.druid.java.util.common.ISE) UUID(java.util.UUID) PlannerConfig(org.apache.druid.sql.calcite.planner.PlannerConfig) Collectors(java.util.stream.Collectors) QuerySegmentSpec(org.apache.druid.query.spec.QuerySegmentSpec) List(java.util.List) DimFilter(org.apache.druid.query.filter.DimFilter) Hook(org.apache.calcite.runtime.Hook) DataSourceAnalysis(org.apache.druid.query.planning.DataSourceAnalysis) QueryLifecycleFactory(org.apache.druid.server.QueryLifecycleFactory) Iterables(com.google.common.collect.Iterables) ComparableList(org.apache.druid.segment.data.ComparableList) Intervals(org.apache.druid.java.util.common.Intervals) CannotBuildQueryException(org.apache.druid.sql.calcite.rel.CannotBuildQueryException) QueryLifecycle(org.apache.druid.server.QueryLifecycle) ArrayList(java.util.ArrayList) AuthenticationResult(org.apache.druid.server.security.AuthenticationResult) Interval(org.joda.time.Interval) ColumnHolder(org.apache.druid.segment.column.ColumnHolder) Query(org.apache.druid.query.Query) Pair(org.apache.calcite.util.Pair) PlannerContext(org.apache.druid.sql.calcite.planner.PlannerContext) ComparableStringArray(org.apache.druid.segment.data.ComparableStringArray) UOE(org.apache.druid.java.util.common.UOE) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) Sequences(org.apache.druid.java.util.common.guava.Sequences) DruidQuery(org.apache.druid.sql.calcite.rel.DruidQuery) RelDataType(org.apache.calcite.rel.type.RelDataType) Access(org.apache.druid.server.security.Access) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) NlsString(org.apache.calcite.util.NlsString) InlineDataSource(org.apache.druid.query.InlineDataSource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) QueryToolChest(org.apache.druid.query.QueryToolChest) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Ints(com.google.common.primitives.Ints) Object2IntMap(it.unimi.dsi.fastutil.objects.Object2IntMap) NullHandling(org.apache.druid.common.config.NullHandling) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) Calcites(org.apache.druid.sql.calcite.planner.Calcites) Evals(org.apache.druid.math.expr.Evals) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) TimeseriesQuery(org.apache.druid.query.timeseries.TimeseriesQuery) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) UOE(org.apache.druid.java.util.common.UOE) NlsString(org.apache.calcite.util.NlsString) CannotBuildQueryException(org.apache.druid.sql.calcite.rel.CannotBuildQueryException) InlineDataSource(org.apache.druid.query.InlineDataSource) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter)

Example 2 with DruidQuery

use of org.apache.druid.sql.calcite.rel.DruidQuery in project druid by druid-io.

the class DruidPlanner method explainSqlPlanAsNativeQueries.

/**
 * This method doesn't utilize the Calcite's internal {@link RelOptUtil#dumpPlan} since that tends to be verbose
 * and not indicative of the native Druid Queries which will get executed
 * This method assumes that the Planner has converted the RelNodes to DruidRels, and thereby we can implictly cast it
 *
 * @param rel Instance of the root {@link DruidRel} which is formed by running the planner transformations on it
 * @return A string representing an array of native queries that correspond to the given SQL query, in JSON format
 * @throws JsonProcessingException
 */
private String explainSqlPlanAsNativeQueries(DruidRel<?> rel) throws JsonProcessingException {
    ObjectMapper jsonMapper = plannerContext.getJsonMapper();
    List<DruidQuery> druidQueryList;
    druidQueryList = flattenOutermostRel(rel).stream().map(druidRel -> druidRel.toDruidQuery(false)).collect(Collectors.toList());
    // Putting the queries as object node in an ArrayNode, since directly returning a list causes issues when
    // serializing the "queryType". Another method would be to create a POJO containing query and signature, and then
    // serializing it using normal list method.
    ArrayNode nativeQueriesArrayNode = jsonMapper.createArrayNode();
    for (DruidQuery druidQuery : druidQueryList) {
        Query<?> nativeQuery = druidQuery.getQuery();
        ObjectNode objectNode = jsonMapper.createObjectNode();
        objectNode.put("query", jsonMapper.convertValue(nativeQuery, ObjectNode.class));
        objectNode.put("signature", jsonMapper.convertValue(druidQuery.getOutputRowSignature(), ArrayNode.class));
        nativeQueriesArrayNode.add(objectNode);
    }
    return jsonMapper.writeValueAsString(nativeQueriesArrayNode);
}
Also used : DruidQuery(org.apache.druid.sql.calcite.rel.DruidQuery) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Iterables (com.google.common.collect.Iterables)1 Ints (com.google.common.primitives.Ints)1 Object2IntMap (it.unimi.dsi.fastutil.objects.Object2IntMap)1 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 Hook (org.apache.calcite.runtime.Hook)1 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)1 NlsString (org.apache.calcite.util.NlsString)1 Pair (org.apache.calcite.util.Pair)1