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())));
}
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());
}
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")));
}
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)"));
}
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")));
}
Aggregations