Search in sources :

Example 41 with SQLExecutor

use of io.crate.testing.SQLExecutor 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 42 with SQLExecutor

use of io.crate.testing.SQLExecutor in project crate by crate.

the class MapBackedSymbolReplacerTest method test_convert_symbol_by_traversing.

@Test
public void test_convert_symbol_by_traversing() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (x int)").build();
    Symbol x = e.asSymbol("x");
    Symbol x_source_lookup = e.asSymbol("_doc['x']");
    Symbol alias = new AliasSymbol("a_alias", x);
    Symbol alias_source_lookup = new AliasSymbol("a_alias", x_source_lookup);
    // Mapping does not contain the requested symbol, but a child of it. Fallback to symbol traversing.
    Map<Symbol, Symbol> mapping = Map.of(x, x_source_lookup);
    assertThat(MapBackedSymbolReplacer.convert(alias, mapping), is(alias_source_lookup));
}
Also used : AliasSymbol(io.crate.expression.symbol.AliasSymbol) SQLExecutor(io.crate.testing.SQLExecutor) AliasSymbol(io.crate.expression.symbol.AliasSymbol) Symbol(io.crate.expression.symbol.Symbol) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 43 with SQLExecutor

use of io.crate.testing.SQLExecutor 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 44 with SQLExecutor

use of io.crate.testing.SQLExecutor in project crate by crate.

the class OptimizerTest method test_cast_is_not_swapped_when_column_explicitly_casted.

@Test
public void test_cast_is_not_swapped_when_column_explicitly_casted() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable("create table tbl (strCol string, intCol int)").build();
    Symbol symbol = Optimizer.optimizeCasts(e.asSymbol("strCol::bigint > 3"), e.getPlannerContext(clusterService.state()));
    assertThat(symbol, SymbolMatchers.isFunction(GtOperator.NAME, SymbolMatchers.isFunction("cast"), SymbolMatchers.isLiteral(3L)));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) Symbol(io.crate.expression.symbol.Symbol) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 45 with SQLExecutor

use of io.crate.testing.SQLExecutor in project crate by crate.

the class SymbolToColumnDefinitionConverterTest method testScopedNameToColumnDefinition.

@Test
public void testScopedNameToColumnDefinition() throws IOException {
    String createTableStmt = "create table tbl (" + "   col_default_object object as (" + "       col_nested_integer integer," + "       col_nested_object object as (" + "           col_nested_timestamp_with_time_zone timestamp with time zone" + "       )" + "   )" + ")";
    SQLExecutor e = SQLExecutor.builder(clusterService).addTable(createTableStmt).build();
    String selectStmt = "select A.col_default_object['col_nested_integer'], " + "   A.col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone'], " + "   A.col_default_object['col_nested_object'] " + "from " + "   (select " + "       col_default_object['col_nested_integer'], " + "       col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone'], " + "       col_default_object['col_nested_object']" + "   from tbl) as A";
    var analyzedRelation = e.analyze(selectStmt);
    var actual = Lists2.map(analyzedRelation.outputs(), Symbols::toColumnDefinition);
    assertThat(actual, containsInAnyOrder(isColumnDefinition("col_default_object['col_nested_integer']", isColumnType(DataTypes.INTEGER.getName())), isColumnDefinition("col_default_object['col_nested_object']['col_nested_timestamp_with_time_zone']", isColumnType(DataTypes.TIMESTAMPZ.getName())), isColumnDefinition("col_default_object['col_nested_object']", isObjectColumnType(DataTypes.UNTYPED_OBJECT.getName(), isColumnPolicy(OBJECT_TYPE_DEFAULT_COLUMN_POLICY), contains(isColumnDefinition("col_nested_timestamp_with_time_zone", isColumnType(DataTypes.TIMESTAMPZ.getName())))))));
}
Also used : SQLExecutor(io.crate.testing.SQLExecutor) Symbols(io.crate.expression.symbol.Symbols) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

SQLExecutor (io.crate.testing.SQLExecutor)64 Test (org.junit.Test)55 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)54 DocTableInfo (io.crate.metadata.doc.DocTableInfo)24 RelationName (io.crate.metadata.RelationName)9 AnalyzedUpdateStatement (io.crate.analyze.AnalyzedUpdateStatement)8 Doc (io.crate.expression.reference.Doc)8 Assignments (io.crate.expression.symbol.Assignments)7 Symbols (io.crate.expression.symbol.Symbols)7 CreateTable (io.crate.sql.tree.CreateTable)7 TableStats (io.crate.statistics.TableStats)7 Before (org.junit.Before)7 Symbol (io.crate.expression.symbol.Symbol)6 ViewsMetadataTest (io.crate.metadata.view.ViewsMetadataTest)6 QualifiedName (io.crate.sql.tree.QualifiedName)6 DocTableRelation (io.crate.analyze.relations.DocTableRelation)5 SessionContext (io.crate.action.sql.SessionContext)4 AliasSymbol (io.crate.expression.symbol.AliasSymbol)4 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)3 PlannerContext (io.crate.planner.PlannerContext)3