Search in sources :

Example 56 with OrderBy

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

the class MoveOrderBeneathRename method apply.

@Override
public LogicalPlan apply(Order plan, Captures captures, TableStats tableStats, TransactionContext txnCtx, NodeContext nodeCtx) {
    Rename rename = captures.get(renameCapture);
    Function<? super Symbol, ? extends Symbol> mapField = FieldReplacer.bind(rename::resolveField);
    OrderBy mappedOrderBy = plan.orderBy().map(mapField);
    if (rename.source().outputs().containsAll(mappedOrderBy.orderBySymbols())) {
        Order newOrder = new Order(rename.source(), mappedOrderBy);
        return rename.replaceSources(List.of(newOrder));
    } else {
        return null;
    }
}
Also used : OrderBy(io.crate.analyze.OrderBy) Order(io.crate.planner.operators.Order) Rename(io.crate.planner.operators.Rename)

Example 57 with OrderBy

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

the class MoveOrderBeneathNestedLoop method apply.

@Override
public LogicalPlan apply(Order order, Captures captures, TableStats tableStats, TransactionContext txnCtx, NodeContext nodeCtx) {
    NestedLoopJoin nestedLoop = captures.get(nlCapture);
    Set<RelationName> relationsInOrderBy = Collections.newSetFromMap(new IdentityHashMap<>());
    Consumer<ScopedSymbol> gatherRelationsFromField = f -> relationsInOrderBy.add(f.relation());
    Consumer<Reference> gatherRelationsFromRef = r -> relationsInOrderBy.add(r.ident().tableIdent());
    OrderBy orderBy = order.orderBy();
    for (Symbol orderExpr : orderBy.orderBySymbols()) {
        FieldsVisitor.visitFields(orderExpr, gatherRelationsFromField);
        RefVisitor.visitRefs(orderExpr, gatherRelationsFromRef);
    }
    if (relationsInOrderBy.size() == 1) {
        RelationName relationInOrderBy = relationsInOrderBy.iterator().next();
        if (relationInOrderBy == nestedLoop.topMostLeftRelation().relationName()) {
            LogicalPlan lhs = nestedLoop.sources().get(0);
            LogicalPlan newLhs = order.replaceSources(List.of(lhs));
            return new NestedLoopJoin(newLhs, nestedLoop.sources().get(1), nestedLoop.joinType(), nestedLoop.joinCondition(), nestedLoop.isFiltered(), nestedLoop.topMostLeftRelation(), true, nestedLoop.isRewriteFilterOnOuterJoinToInnerJoinDone());
        }
    }
    return null;
}
Also used : TransactionContext(io.crate.metadata.TransactionContext) NodeContext(io.crate.metadata.NodeContext) LogicalPlan(io.crate.planner.operators.LogicalPlan) Captures(io.crate.planner.optimizer.matcher.Captures) IdentityHashMap(java.util.IdentityHashMap) RelationName(io.crate.metadata.RelationName) Reference(io.crate.metadata.Reference) Pattern(io.crate.planner.optimizer.matcher.Pattern) Set(java.util.Set) NestedLoopJoin(io.crate.planner.operators.NestedLoopJoin) Rule(io.crate.planner.optimizer.Rule) Consumer(java.util.function.Consumer) Order(io.crate.planner.operators.Order) RefVisitor(io.crate.expression.symbol.RefVisitor) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) TableStats(io.crate.statistics.TableStats) Symbol(io.crate.expression.symbol.Symbol) Pattern.typeOf(io.crate.planner.optimizer.matcher.Pattern.typeOf) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Patterns.source(io.crate.planner.optimizer.matcher.Patterns.source) Collections(java.util.Collections) FieldsVisitor(io.crate.expression.symbol.FieldsVisitor) Capture(io.crate.planner.optimizer.matcher.Capture) OrderBy(io.crate.analyze.OrderBy) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) NestedLoopJoin(io.crate.planner.operators.NestedLoopJoin) RelationName(io.crate.metadata.RelationName) LogicalPlan(io.crate.planner.operators.LogicalPlan) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol)

Example 58 with OrderBy

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

the class RamAccountingPageIteratorTest method testRamAccountingWrappingAppliedForOrderedIterators.

@Test
public void testRamAccountingWrappingAppliedForOrderedIterators() {
    PositionalOrderBy orderBy = PositionalOrderBy.of(new OrderBy(Collections.singletonList(Literal.of(1)), new boolean[] { false }, new boolean[] { false }), Collections.singletonList(Literal.of(1)));
    PagingIterator<Integer, Row> repeatingSortedPagingIterator = PagingIterator.create(2, true, orderBy, () -> null);
    assertThat(repeatingSortedPagingIterator, instanceOf(RamAccountingPageIterator.class));
    assertThat(((RamAccountingPageIterator) repeatingSortedPagingIterator).delegatePagingIterator, instanceOf(SortedPagingIterator.class));
    PagingIterator<Integer, Row> nonRepeatingSortedPagingIterator = PagingIterator.create(2, false, orderBy, () -> null);
    assertThat(nonRepeatingSortedPagingIterator, instanceOf(RamAccountingPageIterator.class));
    assertThat(((RamAccountingPageIterator) nonRepeatingSortedPagingIterator).delegatePagingIterator, instanceOf(SortedPagingIterator.class));
}
Also used : PositionalOrderBy(io.crate.planner.PositionalOrderBy) OrderBy(io.crate.analyze.OrderBy) PositionalOrderBy(io.crate.planner.PositionalOrderBy) Row(io.crate.data.Row) Test(org.junit.Test) RowAccountingWithEstimatorsTest(io.crate.breaker.RowAccountingWithEstimatorsTest)

Example 59 with OrderBy

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

the class WindowAggTest method testOrderByIsMergedWithPartitionByWithPartialColumnOverlap.

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

Example 60 with OrderBy

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

the class WindowAggTest method testOrderByIsMergedWithPartitionByWithPartialColumnOverlapButReverseOrder.

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

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