Search in sources :

Example 16 with TableStats

use of io.crate.statistics.TableStats in project crate by crate.

the class LimitTest method testLimitOnLimitOperator.

@Test
public void testLimitOnLimitOperator() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    QueriedSelectRelation queriedDocTable = e.analyze("select name from users");
    LogicalPlan plan = Limit.create(Limit.create(Collect.create(((AbstractTableRelation<?>) queriedDocTable.from().get(0)), queriedDocTable.outputs(), new WhereClause(queriedDocTable.where()), new TableStats(), null), Literal.of(10L), Literal.of(5L)), Literal.of(20L), Literal.of(7L));
    assertThat(plan, isPlan("Limit[20::bigint;7::bigint]\n" + "  └ Limit[10::bigint;5::bigint]\n" + "    └ Collect[doc.users | [name] | true]"));
    PlannerContext ctx = e.getPlannerContext(clusterService.state());
    Merge merge = (Merge) plan.build(ctx, Set.of(), new ProjectionBuilder(e.nodeCtx), TopN.NO_LIMIT, 0, null, null, Row.EMPTY, SubQueryResults.EMPTY);
    io.crate.planner.node.dql.Collect collect = (io.crate.planner.node.dql.Collect) merge.subPlan();
    assertThat(collect.collectPhase().projections(), contains(ProjectionMatchers.isTopN(15, 0)));
    // noinspection unchecked
    assertThat(merge.mergePhase().projections(), contains(ProjectionMatchers.isTopN(10, 5), ProjectionMatchers.isTopN(20, 7)));
}
Also used : WhereClause(io.crate.analyze.WhereClause) TableStats(io.crate.statistics.TableStats) PlannerContext(io.crate.planner.PlannerContext) Merge(io.crate.planner.Merge) SQLExecutor(io.crate.testing.SQLExecutor) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 17 with TableStats

use of io.crate.statistics.TableStats in project crate by crate.

the class CollectTest method test.

@Test
public void test() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("CREATE TABLE t (x int)").build();
    TableStats tableStats = new TableStats();
    PlannerContext plannerCtx = e.getPlannerContext(clusterService.state());
    ProjectionBuilder projectionBuilder = new ProjectionBuilder(e.nodeCtx);
    QueriedSelectRelation analyzedRelation = e.analyze("SELECT 123 AS alias, 456 AS alias2 FROM t ORDER BY alias, 2");
    LogicalPlanner logicalPlanner = new LogicalPlanner(e.nodeCtx, tableStats, () -> clusterService.state().nodes().getMinNodeVersion());
    LogicalPlan operator = logicalPlanner.plan(analyzedRelation, plannerCtx);
    ExecutionPlan build = operator.build(plannerCtx, Set.of(), projectionBuilder, -1, 0, null, null, Row.EMPTY, SubQueryResults.EMPTY);
    assertThat((((RoutedCollectPhase) ((io.crate.planner.node.dql.Collect) build).collectPhase())).orderBy(), is(nullValue()));
}
Also used : PlannerContext(io.crate.planner.PlannerContext) ExecutionPlan(io.crate.planner.ExecutionPlan) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) TableStats(io.crate.statistics.TableStats) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 18 with TableStats

use of io.crate.statistics.TableStats in project crate by crate.

the class FetchRewriteTest method test_fetchrewrite_on_rename_puts_fetch_marker_into_alias_scope.

@Test
public void test_fetchrewrite_on_rename_puts_fetch_marker_into_alias_scope() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (x int)").build();
    DocTableInfo tableInfo = e.resolveTableInfo("tbl");
    Reference x = (Reference) e.asSymbol("x");
    var relation = new DocTableRelation(tableInfo);
    var alias = new AliasedAnalyzedRelation(relation, new RelationName(null, "t1"));
    var collect = new Collect(relation, List.of(x), WhereClause.MATCH_ALL, 1L, DataTypes.INTEGER.fixedSize());
    Symbol t1X = alias.getField(x.column(), Operation.READ, true);
    assertThat(t1X, Matchers.notNullValue());
    var rename = new Rename(List.of(t1X), alias.relationName(), alias, collect);
    FetchRewrite fetchRewrite = rename.rewriteToFetch(new TableStats(), List.of());
    assertThat(fetchRewrite, Matchers.notNullValue());
    LogicalPlan newRename = fetchRewrite.newPlan();
    assertThat(newRename, isPlan("Rename[t1._fetchid] AS t1\n" + "  └ Collect[doc.tbl | [_fetchid] | true]"));
    assertThat("fetchRewrite replacedOutputs.keySet() must always match the outputs of the operator prior to the rewrite", List.copyOf(fetchRewrite.replacedOutputs().keySet()), is(rename.outputs()));
    assertThat(fetchRewrite.replacedOutputs(), Matchers.hasEntry(isField("x", alias.relationName()), isFetchStub("_doc['x']")));
    assertThat(newRename.outputs(), contains(isFetchMarker(alias.relationName(), contains(isReference("_doc['x']")))));
    FetchStub fetchStub = (FetchStub) fetchRewrite.replacedOutputs().entrySet().iterator().next().getValue();
    assertThat("FetchStub fetchMarker must be changed to the aliased marker", fetchStub.fetchMarker(), Matchers.sameInstance(newRename.outputs().get(0)));
}
Also used : DocTableInfo(io.crate.metadata.doc.DocTableInfo) Reference(io.crate.metadata.Reference) SymbolMatchers.isReference(io.crate.testing.SymbolMatchers.isReference) AliasSymbol(io.crate.expression.symbol.AliasSymbol) Symbol(io.crate.expression.symbol.Symbol) TableStats(io.crate.statistics.TableStats) SQLExecutor(io.crate.testing.SQLExecutor) SymbolMatchers.isFetchStub(io.crate.testing.SymbolMatchers.isFetchStub) FetchStub(io.crate.expression.symbol.FetchStub) DocTableRelation(io.crate.analyze.relations.DocTableRelation) RelationName(io.crate.metadata.RelationName) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 19 with TableStats

use of io.crate.statistics.TableStats in project crate by crate.

the class SelectivityFunctionsCalculationTest method test_group_operator_adapt_expected_row_count_based_on_column_stats.

@Test
public void test_group_operator_adapt_expected_row_count_based_on_column_stats() throws Throwable {
    var samples = IntStream.concat(IntStream.generate(() -> 10).limit(50), IntStream.generate(() -> 20).limit(50)).boxed().collect(Collectors.toList());
    long numDocs = 2_000L;
    Stats stats = new Stats(numDocs, DataTypes.INTEGER.fixedSize(), Map.of(new ColumnIdent("x"), ColumnStats.fromSortedValues(samples, DataTypes.INTEGER, 0, numDocs)));
    TableStats tableStats = new TableStats();
    tableStats.updateTableStats(Map.of(new RelationName("doc", "tbl"), stats));
    SQLExecutor e = SQLExecutor.builder(clusterService).setTableStats(tableStats).addTable("create table doc.tbl (x int)").build();
    LogicalPlan plan = e.logicalPlan("select x, count(*) from doc.tbl group by x");
    assertThat(plan.numExpectedRows(), Matchers.is(2L));
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) SQLExecutor(io.crate.testing.SQLExecutor) ColumnStats(io.crate.statistics.ColumnStats) Stats(io.crate.statistics.Stats) TableStats(io.crate.statistics.TableStats) RelationName(io.crate.metadata.RelationName) TableStats(io.crate.statistics.TableStats) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 20 with TableStats

use of io.crate.statistics.TableStats in project crate by crate.

the class MoveFilterBeneathWindowAggTest method test_filter_part_on_windows_function_partition_columns_is_moved.

@Test
public void test_filter_part_on_windows_function_partition_columns_is_moved() {
    var collect = e.logicalPlan("SELECT id FROM t1");
    WindowFunction windowFunction = (WindowFunction) e.asSymbol("ROW_NUMBER() OVER(PARTITION BY id)");
    WindowAgg windowAgg = (WindowAgg) WindowAgg.create(collect, List.of(windowFunction));
    Symbol query = e.asSymbol("ROW_NUMBER() OVER(PARTITION BY id) = 2 AND id = 10 AND x = 1");
    Filter filter = new Filter(windowAgg, query);
    var rule = new MoveFilterBeneathWindowAgg();
    Match<Filter> match = rule.pattern().accept(filter, Captures.empty());
    assertThat(match.isPresent(), is(true));
    assertThat(match.value(), Matchers.sameInstance(filter));
    LogicalPlan newPlan = rule.apply(match.value(), match.captures(), new TableStats(), CoordinatorTxnCtx.systemTransactionContext(), e.nodeCtx);
    var expectedPlan = "Filter[((row_number() OVER (PARTITION BY id) = 2) AND (x = 1))]\n" + "  └ WindowAgg[id, row_number() OVER (PARTITION BY id)]\n" + "    └ Filter[(id = 10)]\n" + "      └ Collect[doc.t1 | [id] | true]";
    assertThat(newPlan, isPlan(expectedPlan));
}
Also used : WindowFunction(io.crate.expression.symbol.WindowFunction) WindowAgg(io.crate.planner.operators.WindowAgg) Filter(io.crate.planner.operators.Filter) Symbol(io.crate.expression.symbol.Symbol) LogicalPlan(io.crate.planner.operators.LogicalPlan) TableStats(io.crate.statistics.TableStats) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

TableStats (io.crate.statistics.TableStats)23 Test (org.junit.Test)17 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)15 Symbol (io.crate.expression.symbol.Symbol)11 RelationName (io.crate.metadata.RelationName)8 Filter (io.crate.planner.operators.Filter)7 LogicalPlan (io.crate.planner.operators.LogicalPlan)7 SQLExecutor (io.crate.testing.SQLExecutor)7 WindowFunction (io.crate.expression.symbol.WindowFunction)6 WindowAgg (io.crate.planner.operators.WindowAgg)6 Stats (io.crate.statistics.Stats)6 DocTableRelation (io.crate.analyze.relations.DocTableRelation)4 PlannerContext (io.crate.planner.PlannerContext)4 ProjectionBuilder (io.crate.execution.dsl.projection.builder.ProjectionBuilder)3 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)3 ColumnIdent (io.crate.metadata.ColumnIdent)3 Reference (io.crate.metadata.Reference)3 DocTableInfo (io.crate.metadata.doc.DocTableInfo)3 ColumnStats (io.crate.statistics.ColumnStats)3 ArrayList (java.util.ArrayList)3