Search in sources :

Example 1 with Stats

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"));
}
Also used : HashMap(java.util.HashMap) Stats(io.crate.statistics.Stats) TableStats(io.crate.statistics.TableStats) RelationName(io.crate.metadata.RelationName) TableStats(io.crate.statistics.TableStats) Test(org.junit.Test)

Example 2 with Stats

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));
}
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 3 with Stats

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())));
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Row1(io.crate.data.Row1) Symbol(io.crate.expression.symbol.Symbol) ColumnStats(io.crate.statistics.ColumnStats) ColumnStats(io.crate.statistics.ColumnStats) Stats(io.crate.statistics.Stats) SqlExpressions(io.crate.testing.SqlExpressions) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 4 with Stats

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));
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Symbol(io.crate.expression.symbol.Symbol) ColumnStats(io.crate.statistics.ColumnStats) Stats(io.crate.statistics.Stats) SqlExpressions(io.crate.testing.SqlExpressions) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 5 with Stats

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));
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) Symbol(io.crate.expression.symbol.Symbol) ColumnStats(io.crate.statistics.ColumnStats) ColumnStats(io.crate.statistics.ColumnStats) Stats(io.crate.statistics.Stats) SqlExpressions(io.crate.testing.SqlExpressions) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

Stats (io.crate.statistics.Stats)12 ColumnStats (io.crate.statistics.ColumnStats)10 ColumnIdent (io.crate.metadata.ColumnIdent)9 Test (org.junit.Test)9 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)8 Symbol (io.crate.expression.symbol.Symbol)7 SqlExpressions (io.crate.testing.SqlExpressions)7 TableStats (io.crate.statistics.TableStats)6 RelationName (io.crate.metadata.RelationName)4 HashMap (java.util.HashMap)4 SQLExecutor (io.crate.testing.SQLExecutor)2 WhereClause (io.crate.analyze.WhereClause)1 Row1 (io.crate.data.Row1)1 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)1 Reference (io.crate.metadata.Reference)1 Collect (io.crate.planner.operators.Collect)1 Before (org.junit.Before)1