Search in sources :

Example 1 with UOE

use of org.apache.druid.java.util.common.UOE in project druid by druid-io.

the class LocalDataSegmentPuller method buildFileObject.

public static FileObject buildFileObject(final URI uri) {
    final Path path = Paths.get(uri);
    final File file = path.toFile();
    return new FileObject() {

        @Override
        public URI toUri() {
            return uri;
        }

        @Override
        public String getName() {
            return path.getFileName().toString();
        }

        @Override
        public InputStream openInputStream() throws IOException {
            return new FileInputStream(file);
        }

        @Override
        public OutputStream openOutputStream() throws IOException {
            return new FileOutputStream(file);
        }

        @Override
        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
            return Files.newReader(file, Charset.defaultCharset());
        }

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            throw new UOE("CharSequence not supported");
        }

        @Override
        public Writer openWriter() throws IOException {
            return Files.newWriter(file, Charset.defaultCharset());
        }

        @Override
        public long getLastModified() {
            return file.lastModified();
        }

        @Override
        public boolean delete() {
            return file.delete();
        }
    };
}
Also used : Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) UOE(org.apache.druid.java.util.common.UOE) FileObject(javax.tools.FileObject) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 2 with UOE

use of org.apache.druid.java.util.common.UOE 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 3 with UOE

use of org.apache.druid.java.util.common.UOE in project druid by druid-io.

the class ScanQueryLimitRowIterator method next.

@Override
public ScanResultValue next() {
    if (ScanQuery.ResultFormat.RESULT_FORMAT_VALUE_VECTOR.equals(resultFormat)) {
        throw new UOE(ScanQuery.ResultFormat.RESULT_FORMAT_VALUE_VECTOR + " is not supported yet");
    }
    // inner-level if we are time-ordering
    if (query.getTimeOrder() == ScanQuery.Order.NONE || !query.getContextBoolean(ScanQuery.CTX_KEY_OUTERMOST, true)) {
        ScanResultValue batch = yielder.get();
        List events = (List) batch.getEvents();
        if (events.size() <= limit - count) {
            count += events.size();
            yielder = yielder.next(null);
            return batch;
        } else {
            // last batch
            // single batch length is <= Integer.MAX_VALUE, so this should not overflow
            int numLeft = (int) (limit - count);
            count = limit;
            return new ScanResultValue(batch.getSegmentId(), batch.getColumns(), events.subList(0, numLeft));
        }
    } else {
        // Perform single-event ScanResultValue batching at the outer level.  Each scan result value from the yielder
        // in this case will only have one event so there's no need to iterate through events.
        int batchSize = query.getBatchSize();
        List<Object> eventsToAdd = new ArrayList<>(batchSize);
        List<String> columns = new ArrayList<>();
        while (eventsToAdd.size() < batchSize && !yielder.isDone() && count < limit) {
            ScanResultValue srv = yielder.get();
            // Only replace once using the columns from the first event
            columns = columns.isEmpty() ? srv.getColumns() : columns;
            eventsToAdd.add(Iterables.getOnlyElement((List<Object>) srv.getEvents()));
            yielder = yielder.next(null);
            count++;
        }
        return new ScanResultValue(null, columns, eventsToAdd);
    }
}
Also used : ArrayList(java.util.ArrayList) UOE(org.apache.druid.java.util.common.UOE) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with UOE

use of org.apache.druid.java.util.common.UOE in project druid by druid-io.

the class ScanQueryQueryToolChest method resultsAsArrays.

@Override
public Sequence<Object[]> resultsAsArrays(final ScanQuery query, final Sequence<ScanResultValue> resultSequence) {
    final List<String> fields = resultArraySignature(query).getColumnNames();
    final Function<?, Object[]> mapper;
    switch(query.getResultFormat()) {
        case RESULT_FORMAT_LIST:
            mapper = (Map<String, Object> row) -> {
                final Object[] rowArray = new Object[fields.size()];
                for (int i = 0; i < fields.size(); i++) {
                    rowArray[i] = row.get(fields.get(i));
                }
                return rowArray;
            };
            break;
        case RESULT_FORMAT_COMPACTED_LIST:
            mapper = (List<Object> row) -> {
                if (row.size() == fields.size()) {
                    return row.toArray();
                } else if (fields.isEmpty()) {
                    return new Object[0];
                } else {
                    // the result row here.
                    throw new ISE("Mismatch in expected[%d] vs actual[%s] field count", fields.size(), row.size());
                }
            };
            break;
        default:
            throw new UOE("Unsupported resultFormat for array-based results: %s", query.getResultFormat());
    }
    return resultSequence.flatMap(result -> {
        // Generics? Where we're going, we don't need generics.
        final List rows = (List) result.getEvents();
        final Iterable arrays = Iterables.transform(rows, (Function) mapper);
        return Sequences.simple(arrays);
    });
}
Also used : List(java.util.List) ISE(org.apache.druid.java.util.common.ISE) UOE(org.apache.druid.java.util.common.UOE) Map(java.util.Map)

Example 5 with UOE

use of org.apache.druid.java.util.common.UOE in project druid by druid-io.

the class ExpressionFilter method makeVectorMatcher.

@Override
public VectorValueMatcher makeVectorMatcher(VectorColumnSelectorFactory factory) {
    final Expr theExpr = expr.get();
    DruidPredicateFactory predicateFactory = new DruidPredicateFactory() {

        @Override
        public Predicate<String> makeStringPredicate() {
            return Evals::asBoolean;
        }

        @Override
        public DruidLongPredicate makeLongPredicate() {
            return Evals::asBoolean;
        }

        @Override
        public DruidFloatPredicate makeFloatPredicate() {
            return Evals::asBoolean;
        }

        @Override
        public DruidDoublePredicate makeDoublePredicate() {
            return Evals::asBoolean;
        }

        // The hashcode and equals are to make SubclassesMustOverrideEqualsAndHashCodeTest stop complaining..
        // DruidPredicateFactory currently doesn't really need equals or hashcode since 'toString' method that is actually
        // called when testing equality of DimensionPredicateFilter, so it's the truly required method, but that seems
        // a bit strange. DimensionPredicateFilter should probably be reworked to use equals from DruidPredicateFactory
        // instead of using toString.
        @Override
        public int hashCode() {
            return super.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            return super.equals(obj);
        }
    };
    final ExpressionType outputType = theExpr.getOutputType(factory);
    // effectively constant
    if (outputType == null) {
        // false matcher
        if (NullHandling.sqlCompatible()) {
            return BooleanVectorValueMatcher.of(factory.getReadableVectorInspector(), false);
        }
        // or not.
        return BooleanVectorValueMatcher.of(factory.getReadableVectorInspector(), theExpr.eval(InputBindings.nilBindings()).asBoolean());
    }
    // if we got here, we really have to evaluate the expressions to match
    switch(outputType.getType()) {
        case LONG:
            return VectorValueMatcherColumnProcessorFactory.instance().makeLongProcessor(ColumnCapabilitiesImpl.createSimpleNumericColumnCapabilities(ColumnType.LONG), ExpressionVectorSelectors.makeVectorValueSelector(factory, theExpr)).makeMatcher(predicateFactory);
        case DOUBLE:
            return VectorValueMatcherColumnProcessorFactory.instance().makeDoubleProcessor(ColumnCapabilitiesImpl.createSimpleNumericColumnCapabilities(ColumnType.DOUBLE), ExpressionVectorSelectors.makeVectorValueSelector(factory, theExpr)).makeMatcher(predicateFactory);
        case STRING:
            return VectorValueMatcherColumnProcessorFactory.instance().makeObjectProcessor(ColumnCapabilitiesImpl.createSimpleSingleValueStringColumnCapabilities(), ExpressionVectorSelectors.makeVectorObjectSelector(factory, theExpr)).makeMatcher(predicateFactory);
        default:
            throw new UOE("Vectorized expression matchers not implemented for type: [%s]", outputType);
    }
}
Also used : Expr(org.apache.druid.math.expr.Expr) UOE(org.apache.druid.java.util.common.UOE) ExpressionType(org.apache.druid.math.expr.ExpressionType) DruidPredicateFactory(org.apache.druid.query.filter.DruidPredicateFactory)

Aggregations

UOE (org.apache.druid.java.util.common.UOE)19 ArrayList (java.util.ArrayList)7 ISE (org.apache.druid.java.util.common.ISE)7 List (java.util.List)6 Interval (org.joda.time.Interval)6 Map (java.util.Map)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 File (java.io.File)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Iterator (java.util.Iterator)3 Nullable (javax.annotation.Nullable)3 DynamicPartitionsSpec (org.apache.druid.indexer.partitions.DynamicPartitionsSpec)3 IAE (org.apache.druid.java.util.common.IAE)3 StringUtils (org.apache.druid.java.util.common.StringUtils)3 Sequence (org.apache.druid.java.util.common.guava.Sequence)3 Optional (com.google.common.base.Optional)2 Preconditions (com.google.common.base.Preconditions)2 ImmutableList (com.google.common.collect.ImmutableList)2 Collections (java.util.Collections)2