Search in sources :

Example 1 with FetchStub

use of io.crate.expression.symbol.FetchStub 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)

Aggregations

AliasedAnalyzedRelation (io.crate.analyze.relations.AliasedAnalyzedRelation)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 AliasSymbol (io.crate.expression.symbol.AliasSymbol)1 FetchStub (io.crate.expression.symbol.FetchStub)1 Symbol (io.crate.expression.symbol.Symbol)1 Reference (io.crate.metadata.Reference)1 RelationName (io.crate.metadata.RelationName)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1 TableStats (io.crate.statistics.TableStats)1 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 SQLExecutor (io.crate.testing.SQLExecutor)1 SymbolMatchers.isFetchStub (io.crate.testing.SymbolMatchers.isFetchStub)1 SymbolMatchers.isReference (io.crate.testing.SymbolMatchers.isReference)1 Test (org.junit.Test)1