Search in sources :

Example 46 with OrderBy

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

the class NodeStatsIteratorTest method testNodeStatsIteratorContrat.

@Test
public void testNodeStatsIteratorContrat() throws Exception {
    List<Symbol> toCollect = new ArrayList<>();
    toCollect.add(idRef);
    when(collectPhase.toCollect()).thenReturn(toCollect);
    when(collectPhase.whereClause()).thenReturn(WhereClause.MATCH_ALL);
    when(collectPhase.orderBy()).thenReturn(new OrderBy(Collections.singletonList(idRef), new boolean[] { false }, new Boolean[] { true }));
    List<Object[]> expectedResult = Arrays.asList(new Object[] { new BytesRef("nodeOne") }, new Object[] { new BytesRef("nodeTwo") });
    BatchIteratorTester tester = new BatchIteratorTester(() -> NodeStatsIterator.newInstance(transportNodeStatsAction, collectPhase, nodes, new InputFactory(getFunctions())));
    tester.verifyResultAndEdgeCaseBehaviour(expectedResult);
}
Also used : OrderBy(io.crate.analyze.OrderBy) InputFactory(io.crate.operation.InputFactory) Symbol(io.crate.analyze.symbol.Symbol) BatchIteratorTester(io.crate.testing.BatchIteratorTester) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 47 with OrderBy

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

the class OrderedLuceneBatchIteratorFactoryTest method prepareSearchers.

@Before
public void prepareSearchers() throws Exception {
    IndexWriter iw1 = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(new StandardAnalyzer()));
    IndexWriter iw2 = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(new StandardAnalyzer()));
    expectedResult = LongStream.range(0, 20).mapToObj(i -> new Object[] { i }).collect(Collectors.toList());
    // expect descending order to differentiate between insert order
    expectedResult.sort(Comparator.comparingLong((Object[] o) -> ((long) o[0])).reversed());
    for (int i = 0; i < 20; i++) {
        Document doc = new Document();
        doc.add(new NumericDocValuesField(columnName, i));
        if (i % 2 == 0) {
            iw1.addDocument(doc);
        } else {
            iw2.addDocument(doc);
        }
    }
    iw1.commit();
    iw2.commit();
    searcher1 = new IndexSearcher(DirectoryReader.open(iw1, true));
    searcher2 = new IndexSearcher(DirectoryReader.open(iw2, true));
    fieldTypeLookup = columnName -> {
        LongFieldMapper.LongFieldType longFieldType = new LongFieldMapper.LongFieldType();
        longFieldType.setNames(new MappedFieldType.Names(columnName));
        return longFieldType;
    };
    orderBy = new OrderBy(Collections.singletonList(reference), reverseFlags, nullsFirst);
}
Also used : OrderBy(io.crate.analyze.OrderBy) LongFieldMapper(org.elasticsearch.index.mapper.core.LongFieldMapper) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Before(org.junit.Before)

Example 48 with OrderBy

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

the class WindowAgg method create.

@VisibleForTesting
public static LogicalPlan create(LogicalPlan source, List<WindowFunction> windowFunctions) {
    if (windowFunctions.isEmpty()) {
        return source;
    }
    LinkedHashMap<WindowDefinition, ArrayList<WindowFunction>> groupedFunctions = new LinkedHashMap<>();
    for (WindowFunction windowFunction : windowFunctions) {
        WindowDefinition windowDefinition = windowFunction.windowDefinition();
        ArrayList<WindowFunction> functions = groupedFunctions.computeIfAbsent(windowDefinition, w -> new ArrayList<>());
        functions.add(windowFunction);
    }
    LogicalPlan lastWindowAgg = source;
    for (Map.Entry<WindowDefinition, ArrayList<WindowFunction>> entry : groupedFunctions.entrySet()) {
        /*
             * Pass along the source outputs as standalone symbols as they might be required in cases like:
             *      select x, avg(x) OVER() from t;
             */
        ArrayList<WindowFunction> functions = entry.getValue();
        WindowDefinition windowDefinition = entry.getKey();
        OrderBy orderBy = windowDefinition.orderBy();
        if (orderBy == null || lastWindowAgg.outputs().containsAll(orderBy.orderBySymbols())) {
            lastWindowAgg = new WindowAgg(lastWindowAgg, windowDefinition, functions, lastWindowAgg.outputs());
        } else {
            // ``WindowProjector.createUpdateProbeValueFunction` expects that all OrderBY symbols are `InputColumn`
            // Here we have a case where there is a function or something in the orderBy expression that is *not*
            // already provided by the source.
            // -> Inject `eval` so that the `orderBy` of the window-function will turn into a `InputColumn`
            Eval eval = new Eval(lastWindowAgg, Lists2.concatUnique(lastWindowAgg.outputs(), orderBy.orderBySymbols()));
            lastWindowAgg = new WindowAgg(eval, windowDefinition, functions, eval.outputs());
        }
    }
    return lastWindowAgg;
}
Also used : OrderBy(io.crate.analyze.OrderBy) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) WindowFunction(io.crate.expression.symbol.WindowFunction) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) WindowDefinition(io.crate.analyze.WindowDefinition) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 49 with OrderBy

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

the class NodeStatsTest method testNodeStatsIteratorContrat.

@Test
public void testNodeStatsIteratorContrat() throws Exception {
    List<Symbol> toCollect = new ArrayList<>();
    toCollect.add(idRef);
    when(collectPhase.toCollect()).thenReturn(toCollect);
    when(collectPhase.where()).thenReturn(Literal.BOOLEAN_TRUE);
    when(collectPhase.orderBy()).thenReturn(new OrderBy(Collections.singletonList(idRef)));
    List<Object[]> expectedResult = Arrays.asList(new Object[] { "nodeOne" }, new Object[] { "nodeTwo" });
    BatchIteratorTester tester = new BatchIteratorTester(() -> NodeStats.newInstance(transportNodeStatsAction, collectPhase, nodes, txnCtx, new InputFactory(nodeCtx)));
    tester.verifyResultAndEdgeCaseBehaviour(expectedResult);
}
Also used : OrderBy(io.crate.analyze.OrderBy) InputFactory(io.crate.expression.InputFactory) Symbol(io.crate.expression.symbol.Symbol) ArrayList(java.util.ArrayList) BatchIteratorTester(io.crate.testing.BatchIteratorTester) Test(org.junit.Test)

Example 50 with OrderBy

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

the class LuceneOrderedDocCollectorTest method testSearchAfterWithSystemColumn.

@Test
public void testSearchAfterWithSystemColumn() {
    Reference sysColReference = new Reference(new ReferenceIdent(new RelationName(Schemas.DOC_SCHEMA_NAME, "table"), DocSysColumns.SCORE), RowGranularity.DOC, DataTypes.FLOAT, 0, null);
    OrderBy orderBy = new OrderBy(List.of(sysColReference, REFERENCE), new boolean[] { false, false }, new boolean[] { false, false });
    FieldDoc lastCollected = new FieldDoc(0, 0, new Object[] { 2L });
    OptimizeQueryForSearchAfter queryForSearchAfter = new OptimizeQueryForSearchAfter(orderBy, mock(QueryShardContext.class), name -> valueFieldType);
    Query nextPageQuery = queryForSearchAfter.apply(lastCollected);
    // returns null which leads to reuse of old query without paging optimization
    assertNull(nextPageQuery);
}
Also used : OrderBy(io.crate.analyze.OrderBy) FieldDoc(org.apache.lucene.search.FieldDoc) Query(org.apache.lucene.search.Query) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) Reference(io.crate.metadata.Reference) RelationName(io.crate.metadata.RelationName) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) ReferenceIdent(io.crate.metadata.ReferenceIdent) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest) Test(org.junit.Test)

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