Search in sources :

Example 6 with TableStats

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

the class FetchRewriteTest method test_fetch_rewrite_on_eval_removes_eval_and_extends_replaced_outputs.

@Test
public void test_fetch_rewrite_on_eval_removes_eval_and_extends_replaced_outputs() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (x int)").build();
    DocTableInfo tableInfo = e.resolveTableInfo("tbl");
    var x = e.asSymbol("x");
    var relation = new DocTableRelation(tableInfo);
    var collect = new Collect(relation, List.of(x), WhereClause.MATCH_ALL, 1L, DataTypes.INTEGER.fixedSize());
    var eval = new Eval(collect, List.of(new Function(Signature.scalar("add", DataTypes.INTEGER.getTypeSignature(), DataTypes.INTEGER.getTypeSignature(), DataTypes.INTEGER.getTypeSignature()), List.of(x, x), DataTypes.INTEGER)));
    FetchRewrite fetchRewrite = eval.rewriteToFetch(new TableStats(), List.of());
    assertThat(fetchRewrite, Matchers.notNullValue());
    assertThat(fetchRewrite.newPlan(), isPlan("Collect[doc.tbl | [_fetchid] | true]"));
    assertThat(fetchRewrite.replacedOutputs(), Matchers.hasEntry(isFunction("add", isReference("x"), isReference("x")), isFunction("add", isFetchStub("_doc['x']"), isFetchStub("_doc['x']"))));
    assertThat(List.copyOf(fetchRewrite.replacedOutputs().keySet()), is(eval.outputs()));
}
Also used : SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) Function(io.crate.expression.symbol.Function) DocTableInfo(io.crate.metadata.doc.DocTableInfo) SQLExecutor(io.crate.testing.SQLExecutor) DocTableRelation(io.crate.analyze.relations.DocTableRelation) TableStats(io.crate.statistics.TableStats) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 7 with TableStats

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

the class SelectivityFunctionsCalculationTest method test_collect_operator_adapts_expected_row_count_based_on_selectivity_calculation.

@Test
public void test_collect_operator_adapts_expected_row_count_based_on_selectivity_calculation() throws Throwable {
    var columnStats = new HashMap<ColumnIdent, ColumnStats>();
    long totalNumRows = 20000;
    var numbers = IntStream.range(1, 20001).boxed().collect(Collectors.toList());
    columnStats.put(new ColumnIdent("x"), ColumnStats.fromSortedValues(numbers, DataTypes.INTEGER, 0, totalNumRows));
    Stats stats = new Stats(totalNumRows, DataTypes.INTEGER.fixedSize(), columnStats);
    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 * from doc.tbl where x = 10");
    assertThat(plan.numExpectedRows(), Matchers.is(1L));
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) HashMap(java.util.HashMap) 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 8 with TableStats

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

the class MoveFilterBeneathWindowAggTest method test_filter_on_windows_function_partition_columns_is_moved.

@Test
public void test_filter_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("id = 10");
    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 = "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)

Example 9 with TableStats

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

the class MoveFilterBeneathWindowAggTest method test_filters_combined_by_OR_cannot_be_moved.

@Test
public void test_filters_combined_by_OR_cannot_be_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) = 1 OR id = 10");
    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);
    assertThat(newPlan, nullValue());
}
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)

Example 10 with TableStats

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

the class MoveFilterBeneathWindowAggTest method test_filter_containing_columns_not_part_of_the_window_partition_cannot_be_moved.

@Test
public void test_filter_containing_columns_not_part_of_the_window_partition_cannot_be_moved() {
    var collect = e.logicalPlan("SELECT id, x 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("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);
    assertThat(newPlan, nullValue());
}
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