Search in sources :

Example 1 with AliasedAnalyzedRelation

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

the class ExpressionAnalyzerTest method testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation.

@Test
public void testInSelfJoinCaseFunctionsThatLookTheSameMustNotReuseFunctionAllocation() throws Exception {
    TableInfo t1 = executor.resolveTableInfo("t1");
    TableRelation relation = new TableRelation(t1);
    RelationName a1 = new RelationName(null, "a1");
    RelationName a2 = new RelationName(null, "a2");
    Map<RelationName, AnalyzedRelation> sources = Map.of(a1, new AliasedAnalyzedRelation(relation, a1), a2, new AliasedAnalyzedRelation(relation, a2));
    SessionContext sessionContext = SessionContext.systemSessionContext();
    CoordinatorTxnCtx coordinatorTxnCtx = new CoordinatorTxnCtx(sessionContext);
    ExpressionAnalyzer expressionAnalyzer = new ExpressionAnalyzer(coordinatorTxnCtx, expressions.nodeCtx, paramTypeHints, new FullQualifiedNameFieldProvider(sources, ParentRelations.NO_PARENTS, sessionContext.searchPath().currentSchema()), null);
    Function andFunction = (Function) expressionAnalyzer.convert(SqlParser.createExpression("not a1.x = 1 and not a2.x = 1"), context);
    ScopedSymbol t1Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(0)).arguments().get(0)).arguments().get(0));
    ScopedSymbol t2Id = ((ScopedSymbol) ((Function) ((Function) andFunction.arguments().get(1)).arguments().get(0)).arguments().get(0));
    assertThat(t1Id.relation(), is(not(t2Id.relation())));
}
Also used : CoalesceFunction(io.crate.expression.scalar.conditional.CoalesceFunction) SymbolMatchers.isFunction(io.crate.testing.SymbolMatchers.isFunction) ArraySliceFunction(io.crate.expression.scalar.ArraySliceFunction) ImplicitCastFunction(io.crate.expression.scalar.cast.ImplicitCastFunction) Function(io.crate.expression.symbol.Function) CoordinatorTxnCtx(io.crate.metadata.CoordinatorTxnCtx) RelationName(io.crate.metadata.RelationName) SessionContext(io.crate.action.sql.SessionContext) TableInfo(io.crate.metadata.table.TableInfo) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) TableRelation(io.crate.analyze.relations.TableRelation) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) FullQualifiedNameFieldProvider(io.crate.analyze.relations.FullQualifiedNameFieldProvider) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Example 2 with AliasedAnalyzedRelation

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

the class SubSelectAnalyzerTest method testJoinOnSubSelectsWithOrder.

@Test
public void testJoinOnSubSelectsWithOrder() throws Exception {
    QueriedSelectRelation relation = analyze("select * from " + " (select a, i from t1 order by a) t1, " + " (select b, i from t2 where b > '10') t2 " + "where t1.a > '50' and t2.b > '100' " + "order by 2 limit 10");
    assertThat(relation.outputs(), contains(isField("a"), isField("i"), isField("b"), isField("i")));
    assertThat(relation.where(), isSQL("((t1.a > '50') AND (t2.b > '100'))"));
    assertThat(relation.orderBy().orderBySymbols(), contains(isField("i")));
    assertThat(relation.limit(), isLiteral(10L));
    AliasedAnalyzedRelation t1Alias = (AliasedAnalyzedRelation) relation.from().get(0);
    QueriedSelectRelation t1 = ((QueriedSelectRelation) t1Alias.relation());
    assertThat(t1.where(), isSQL("true"));
    assertThat(t1.orderBy(), isSQL("doc.t1.a"));
    AliasedAnalyzedRelation t2Alias = (AliasedAnalyzedRelation) relation.from().get(1);
    QueriedSelectRelation t2 = ((QueriedSelectRelation) t2Alias.relation());
    assertThat(t2.where(), isFunction("op_>", isReference("b"), isLiteral("10")));
    assertThat(t2.orderBy(), Matchers.nullValue());
}
Also used : AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 3 with AliasedAnalyzedRelation

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

the class SubSelectAnalyzerTest method testJoinOnSubSelectsWithGlobalAggregationsAndLimitNotPushedDown.

@Test
public void testJoinOnSubSelectsWithGlobalAggregationsAndLimitNotPushedDown() throws Exception {
    QueriedSelectRelation relation = analyze("select count(*) from " + " (select a, i from (" + "     select * from t1 order by a desc limit 5) a" + "  order by a limit 10) t1 " + "join" + " (select b, i from t2 where b > '10') t2 " + "on t1.i = t2.i " + "order by 1 limit 10");
    assertThat(relation.outputs(), contains(isFunction("count")));
    assertThat(relation.orderBy().orderBySymbols(), contains(isFunction("count")));
    assertThat(relation.limit(), isLiteral(10L));
    assertThat(relation.joinPairs().get(0).condition(), isFunction("op_=", isField("i"), isField("i")));
    AliasedAnalyzedRelation t1Alias = (AliasedAnalyzedRelation) relation.from().get(0);
    QueriedSelectRelation t1Sub = ((QueriedSelectRelation) t1Alias.relation());
    assertThat(t1Sub.outputs(), contains(isField("a"), isField("i")));
    assertThat(t1Sub.orderBy().orderBySymbols(), contains(isField("a")));
    assertThat(t1Sub.limit(), isLiteral(10L));
    AliasedAnalyzedRelation t1NestedAlias = (AliasedAnalyzedRelation) t1Sub.from().get(0);
    QueriedSelectRelation t1 = ((QueriedSelectRelation) t1NestedAlias.relation());
    assertThat(t1.orderBy().orderBySymbols(), contains(isReference("a")));
    assertThat(t1.limit(), isLiteral(5L));
    AliasedAnalyzedRelation t2Alias = (AliasedAnalyzedRelation) relation.from().get(1);
    QueriedSelectRelation t2sub = (QueriedSelectRelation) t2Alias.relation();
    assertThat(t2sub.where(), isFunction("op_>", isReference("b"), isLiteral("10")));
}
Also used : AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 4 with AliasedAnalyzedRelation

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

the class SubSelectAnalyzerTest method testSubSelectWithOuterJoinAndAggregations.

@Test
public void testSubSelectWithOuterJoinAndAggregations() throws Exception {
    QueriedSelectRelation relation = analyze("select * from " + " (select max(a) ma, i from t1 group by i) t1 " + "left join" + " (select max(b) mb, i from t2 group by i having i > 10::int) t2 " + "on t1.i = t2.i where t1.ma > '50' and t2.mb > '100'");
    assertThat(relation.outputs(), isSQL("t1.ma, t1.i, t2.mb, t2.i"));
    assertThat(relation.joinPairs().get(0).condition(), isSQL("(t1.i = t2.i)"));
    assertThat(relation.where(), isSQL("((t1.ma > '50') AND (t2.mb > '100'))"));
    AliasedAnalyzedRelation t1Alias = (AliasedAnalyzedRelation) relation.from().get(0);
    QueriedSelectRelation t1Sel = (QueriedSelectRelation) t1Alias.relation();
    assertThat(t1Sel.outputs(), contains(isAlias("ma", isFunction("max")), isReference("i")));
    assertThat(t1Sel.groupBy(), isSQL("doc.t1.i"));
    assertThat(t1Sel.having(), Matchers.nullValue());
    AliasedAnalyzedRelation t2Alias = (AliasedAnalyzedRelation) relation.from().get(1);
    QueriedSelectRelation t2Sel = (QueriedSelectRelation) t2Alias.relation();
    assertThat(t2Sel.outputs(), contains(isAlias("mb", isFunction("max", isReference("b"))), isReference("i")));
    assertThat(t2Sel.groupBy(), isSQL("doc.t2.i"));
    assertThat(t2Sel.having(), isSQL("(doc.t2.i > 10)"));
}
Also used : AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 5 with AliasedAnalyzedRelation

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

the class GroupByAnalyzerTest method testSelectDistinctWithGroupByOnSubSelectInner.

@Test
public void testSelectDistinctWithGroupByOnSubSelectInner() {
    AnalyzedRelation relation = analyze("select * from (" + "  select distinct id from users group by id, name order by 1" + ") t order by 1 desc");
    assertThat(relation, instanceOf(QueriedSelectRelation.class));
    QueriedSelectRelation outerRelation = (QueriedSelectRelation) relation;
    assertThat(outerRelation.outputs(), contains(isField("id")));
    assertThat(outerRelation.groupBy(), Matchers.empty());
    assertThat(outerRelation.orderBy().orderBySymbols(), contains(isField("id")));
    AliasedAnalyzedRelation aliasedRelation = (AliasedAnalyzedRelation) outerRelation.from().get(0);
    QueriedSelectRelation innerRelation = (QueriedSelectRelation) aliasedRelation.relation();
    assertThat(innerRelation.isDistinct(), is(true));
    assertThat(innerRelation.groupBy(), contains(isReference("id"), isReference("name")));
}
Also used : AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) AliasedAnalyzedRelation(io.crate.analyze.relations.AliasedAnalyzedRelation) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

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