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)));
}
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()));
}
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)));
}
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));
}
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));
}
Aggregations