Search in sources :

Example 6 with AliasedAnalyzedRelation

use of io.crate.analyze.relations.AliasedAnalyzedRelation 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 7 with AliasedAnalyzedRelation

use of io.crate.analyze.relations.AliasedAnalyzedRelation in project crate by crate.

the class SubSelectAnalyzerTest method testSubSelectWithJoins.

@Test
public void testSubSelectWithJoins() throws Exception {
    QueriedSelectRelation relation = analyze("select aliased_sub.a, aliased_sub.b from (select t1.a, t2.b from t1, t2) as aliased_sub");
    AliasedAnalyzedRelation aliasRel = (AliasedAnalyzedRelation) relation.from().get(0);
    QueriedSelectRelation innerRelation = (QueriedSelectRelation) aliasRel.relation();
    assertThat(innerRelation.from().size(), is(2));
    assertThat(innerRelation.outputs(), contains(isReference("a"), isReference("b")));
}
Also used : AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

AliasedAnalyzedRelation (io.crate.analyze.relations.AliasedAnalyzedRelation)7 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)7 Test (org.junit.Test)7 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)2 RelationName (io.crate.metadata.RelationName)2 SessionContext (io.crate.action.sql.SessionContext)1 DocTableRelation (io.crate.analyze.relations.DocTableRelation)1 FullQualifiedNameFieldProvider (io.crate.analyze.relations.FullQualifiedNameFieldProvider)1 TableRelation (io.crate.analyze.relations.TableRelation)1 ArraySliceFunction (io.crate.expression.scalar.ArraySliceFunction)1 ImplicitCastFunction (io.crate.expression.scalar.cast.ImplicitCastFunction)1 CoalesceFunction (io.crate.expression.scalar.conditional.CoalesceFunction)1 AliasSymbol (io.crate.expression.symbol.AliasSymbol)1 FetchStub (io.crate.expression.symbol.FetchStub)1 Function (io.crate.expression.symbol.Function)1 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)1 Symbol (io.crate.expression.symbol.Symbol)1 CoordinatorTxnCtx (io.crate.metadata.CoordinatorTxnCtx)1 Reference (io.crate.metadata.Reference)1 DocTableInfo (io.crate.metadata.doc.DocTableInfo)1