Search in sources :

Example 61 with OrderBy

use of io.crate.analyze.OrderBy in project crate by crate.

the class WindowAggTest method testOrderByIsPartitionByWithoutExplicitOrderBy.

@Test
public void testOrderByIsPartitionByWithoutExplicitOrderBy() {
    OrderBy orderBy = WindowAgg.createOrderByInclPartitionBy(wd("avg(x) OVER (PARTITION BY x)"));
    assertThat(orderBy, notNullValue());
    assertThat(orderBy.orderBySymbols(), contains(isReference("x")));
}
Also used : OrderBy(io.crate.analyze.OrderBy) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 62 with OrderBy

use of io.crate.analyze.OrderBy in project crate by crate.

the class OrderedLuceneBatchIteratorBenchmark method createLuceneBatchIterator.

@Setup
public void createLuceneBatchIterator() throws Exception {
    IndexWriter iw = new IndexWriter(new ByteBuffersDirectory(), new IndexWriterConfig(new StandardAnalyzer()));
    dummyShardId = new ShardId("dummy", UUIDs.randomBase64UUID(), 1);
    columnName = "x";
    for (int i = 0; i < 10_000_000; i++) {
        Document doc = new Document();
        doc.add(new NumericDocValuesField(columnName, i));
        iw.addDocument(doc);
    }
    iw.commit();
    iw.forceMerge(1, true);
    indexSearcher = new IndexSearcher(DirectoryReader.open(iw, true, true));
    collectorContext = new CollectorContext();
    reference = new Reference(new ReferenceIdent(new RelationName(Schemas.DOC_SCHEMA_NAME, "dummyTable"), columnName), RowGranularity.DOC, DataTypes.INTEGER, 1, null);
    orderBy = new OrderBy(Collections.singletonList(reference), reverseFlags, nullsFirst);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) OrderBy(io.crate.analyze.OrderBy) Reference(io.crate.metadata.Reference) Document(org.apache.lucene.document.Document) ReferenceIdent(io.crate.metadata.ReferenceIdent) ShardId(org.elasticsearch.index.shard.ShardId) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) RelationName(io.crate.metadata.RelationName) CollectorContext(io.crate.expression.reference.doc.lucene.CollectorContext) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Setup(org.openjdk.jmh.annotations.Setup)

Example 63 with OrderBy

use of io.crate.analyze.OrderBy in project crate by crate.

the class DocInputFactory method extractImplementations.

public InputFactory.Context<? extends LuceneCollectorExpression<?>> extractImplementations(TransactionContext txnCtx, RoutedCollectPhase phase) {
    OrderBy orderBy = phase.orderBy();
    ReferenceResolver<? extends LuceneCollectorExpression<?>> refResolver;
    if (orderBy == null) {
        refResolver = referenceResolver;
    } else {
        refResolver = ref -> {
            if (orderBy.orderBySymbols().contains(ref)) {
                DataType<?> dataType = ref.valueType();
                return new OrderByCollectorExpression(ref, orderBy, dataType::sanitizeValue);
            }
            return referenceResolver.getImplementation(ref);
        };
    }
    InputFactory.Context<? extends LuceneCollectorExpression<?>> ctx = inputFactory.ctxForRefs(txnCtx, refResolver);
    ctx.add(phase.toCollect());
    return ctx;
}
Also used : OrderBy(io.crate.analyze.OrderBy) InputFactory(io.crate.expression.InputFactory) OrderByCollectorExpression(io.crate.expression.reference.doc.lucene.OrderByCollectorExpression)

Example 64 with OrderBy

use of io.crate.analyze.OrderBy in project crate by crate.

the class WindowFunction method toString.

@Override
public String toString(Style style) {
    var builder = new StringBuilder(super.toString(style));
    if (ignoreNulls != null) {
        if (ignoreNulls) {
            builder.append(" IGNORE NULLS");
        } else {
            builder.append(" RESPECT NULLS");
        }
    }
    builder.append(" OVER (");
    var partitions = windowDefinition.partitions();
    if (!partitions.isEmpty()) {
        builder.append("PARTITION BY ");
        builder.append(Lists2.joinOn(", ", partitions, x -> x.toString(style)));
    }
    var orderBy = windowDefinition.orderBy();
    if (orderBy != null) {
        if (!partitions.isEmpty()) {
            builder.append(" ");
        }
        builder.append("ORDER BY ");
        OrderBy.explainRepresentation(builder, orderBy.orderBySymbols(), orderBy.reverseFlags(), orderBy.nullsFirst(), x -> x.toString(style));
    }
    WindowFrameDefinition frameDefinition = windowDefinition.windowFrameDefinition();
    if (frameDefinition != WindowDefinition.RANGE_UNBOUNDED_PRECEDING_CURRENT_ROW) {
        builder.append(" ");
        builder.append(frameDefinition.mode().name());
        builder.append(" BETWEEN ");
        appendFrameBound(builder, style, frameDefinition.start());
        builder.append(" AND ");
        appendFrameBound(builder, style, frameDefinition.end());
    }
    builder.append(")");
    return builder.toString();
}
Also used : StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) AGGREGATE(io.crate.metadata.FunctionType.AGGREGATE) DataType(io.crate.types.DataType) IOException(java.io.IOException) Signature(io.crate.metadata.functions.Signature) FrameBoundDefinition(io.crate.analyze.FrameBoundDefinition) Style(io.crate.expression.symbol.format.Style) WINDOW(io.crate.metadata.FunctionType.WINDOW) Lists2(io.crate.common.collections.Lists2) Objects(java.util.Objects) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) Version(org.elasticsearch.Version) WindowDefinition(io.crate.analyze.WindowDefinition) StreamInput(org.elasticsearch.common.io.stream.StreamInput) WindowFrameDefinition(io.crate.analyze.WindowFrameDefinition) Nullable(javax.annotation.Nullable) WindowFrameDefinition(io.crate.analyze.WindowFrameDefinition)

Aggregations

OrderBy (io.crate.analyze.OrderBy)64 Test (org.junit.Test)29 Symbol (io.crate.expression.symbol.Symbol)16 ArrayList (java.util.ArrayList)14 QuerySpec (io.crate.analyze.QuerySpec)13 List (java.util.List)13 Row (io.crate.data.Row)12 Reference (io.crate.metadata.Reference)10 Nullable (javax.annotation.Nullable)9 QueriedDocTable (io.crate.analyze.relations.QueriedDocTable)8 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)8 Map (java.util.Map)8 ShardId (org.elasticsearch.index.shard.ShardId)8 Symbol (io.crate.analyze.symbol.Symbol)7 InputFactory (io.crate.expression.InputFactory)7 RelationName (io.crate.metadata.RelationName)7 WindowDefinition (io.crate.analyze.WindowDefinition)6 TransactionContext (io.crate.metadata.TransactionContext)6 DataType (io.crate.types.DataType)6 Lists2 (io.crate.common.collections.Lists2)5