use of io.crate.statistics.Stats in project crate by crate.
the class SubSelectIntegrationTest method testSingleRowSubSelectWorksWithJoins.
@Test
public void testSingleRowSubSelectWorksWithJoins() throws Exception {
execute("create table t (x long primary key)");
ensureYellow();
execute("insert into t (x) values (1), (2)");
execute("refresh table t");
for (TableStats tableStats : internalCluster().getInstances(TableStats.class)) {
Map<RelationName, Stats> newStats = new HashMap<>();
newStats.put(new RelationName(sqlExecutor.getCurrentSchema(), "t"), new Stats(100, 64, Map.of()));
tableStats.updateTableStats(newStats);
}
// Left table is expected to be one row, due to the single row subselect in the where clause.
execute("select * from t as t1, t as t2 where t1.x = (select 1) order by t2.x");
assertThat(printedTable(response.rows()), is("1| 1\n1| 2\n"));
// Left table is expected to be bigger due to the table stats stating it being 100 rows
execute("select * from t as t2, t as t1 where t1.x = (select 1) order by t2.x");
assertThat(printedTable(response.rows()), is("1| 1\n2| 1\n"));
}
use of io.crate.statistics.Stats 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));
}
use of io.crate.statistics.Stats in project crate by crate.
the class SelectivityFunctionsTest method test_eq_value_that_is_present_in_mcv_uses_mcv_frequency_as_selectivity.
@Test
public void test_eq_value_that_is_present_in_mcv_uses_mcv_frequency_as_selectivity() {
SqlExpressions expressions = new SqlExpressions(T3.sources(clusterService));
Symbol query = expressions.asSymbol("x = ?");
var numbers = Lists2.concat(List.of(1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 10), IntStream.range(11, 15).boxed().collect(Collectors.toList()));
var columnStats = ColumnStats.fromSortedValues(numbers, DataTypes.INTEGER, 0, numbers.size());
double frequencyOf10 = columnStats.mostCommonValues().frequencies()[0];
var statsByColumn = Map.<ColumnIdent, ColumnStats>of(new ColumnIdent("x"), columnStats);
Stats stats = new Stats(numbers.size(), 16, statsByColumn);
assertThat(SelectivityFunctions.estimateNumRows(stats, query, new Row1(10)), Matchers.is((long) (frequencyOf10 * numbers.size())));
}
use of io.crate.statistics.Stats in project crate by crate.
the class SelectivityFunctionsTest method test_not_reverses_selectivity_of_inner_function.
@Test
public void test_not_reverses_selectivity_of_inner_function() {
SqlExpressions expressions = new SqlExpressions(T3.sources(clusterService));
Symbol query = expressions.asSymbol("NOT (x = 10)");
var numbers = IntStream.range(1, 20_001).boxed().collect(Collectors.toList());
var columnStats = ColumnStats.fromSortedValues(numbers, DataTypes.INTEGER, 0, 20_000L);
Stats stats = new Stats(20_000, 16, Map.of(new ColumnIdent("x"), columnStats));
assertThat(SelectivityFunctions.estimateNumRows(stats, query, null), Matchers.is(19999L));
}
use of io.crate.statistics.Stats in project crate by crate.
the class SelectivityFunctionsTest method test_column_eq_column_uses_approx_distinct_for_selectivity_approximation.
@Test
public void test_column_eq_column_uses_approx_distinct_for_selectivity_approximation() {
SqlExpressions expressions = new SqlExpressions(T3.sources(clusterService));
Symbol query = expressions.asSymbol("x = y");
var numbers = Lists2.concat(List.of(1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 10), IntStream.range(11, 15).boxed().collect(Collectors.toList()));
var columnStats = ColumnStats.fromSortedValues(numbers, DataTypes.INTEGER, 0, numbers.size());
var statsByColumn = Map.<ColumnIdent, ColumnStats>of(new ColumnIdent("x"), columnStats);
Stats stats = new Stats(numbers.size(), 16, statsByColumn);
assertThat(SelectivityFunctions.estimateNumRows(stats, query, null), Matchers.is(3L));
}
Aggregations