Search in sources :

Example 6 with QueriedSelectRelation

use of io.crate.analyze.QueriedSelectRelation in project crate by crate.

the class SplitPointsTest method testSplitPointsCreationWithFunctionInAggregation.

@Test
public void testSplitPointsCreationWithFunctionInAggregation() throws Exception {
    QueriedSelectRelation relation = e.analyze("select sum(coalesce(x, 0)) + 10 from t1");
    SplitPoints splitPoints = SplitPointsBuilder.create(relation);
    assertThat(splitPoints.toCollect(), contains(isFunction("coalesce")));
    assertThat(splitPoints.aggregates(), contains(isFunction("sum")));
}
Also used : QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 7 with QueriedSelectRelation

use of io.crate.analyze.QueriedSelectRelation in project crate by crate.

the class SplitPointsTest method testSplitPointsCreationSelectItemAggregationsAreAlwaysAdded.

@Test
public void testSplitPointsCreationSelectItemAggregationsAreAlwaysAdded() throws Exception {
    QueriedSelectRelation relation = e.analyze("select" + "   sum(coalesce(x, 0::integer)), " + "   sum(coalesce(x, 0::integer)) + 10 " + "from t1");
    SplitPoints splitPoints = SplitPointsBuilder.create(relation);
    assertThat(splitPoints.toCollect(), contains(isFunction("coalesce")));
    assertThat(splitPoints.aggregates(), contains(isFunction("sum")));
}
Also used : QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 8 with QueriedSelectRelation

use of io.crate.analyze.QueriedSelectRelation in project crate by crate.

the class SplitPointsTest method testAggregationPlusTableFunctionUsingAggregation.

@Test
public void testAggregationPlusTableFunctionUsingAggregation() throws Exception {
    QueriedSelectRelation relation = e.analyze("select max(x), generate_series(0, max(x)) from t1");
    SplitPoints splitPoints = SplitPointsBuilder.create(relation);
    assertThat(splitPoints.toCollect(), contains(isReference("x")));
    assertThat(splitPoints.aggregates(), contains(isFunction("max")));
    assertThat(splitPoints.tableFunctions(), contains(isFunction("generate_series")));
}
Also used : QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 9 with QueriedSelectRelation

use of io.crate.analyze.QueriedSelectRelation in project crate by crate.

the class SplitPointsTest method testTableFunctionArgsAndStandaloneColumnsAreAddedToCollect.

@Test
public void testTableFunctionArgsAndStandaloneColumnsAreAddedToCollect() throws Exception {
    QueriedSelectRelation relation = e.analyze("select unnest(xs), x from t2");
    SplitPoints splitPoints = SplitPointsBuilder.create(relation);
    assertThat(splitPoints.toCollect(), contains(isReference("xs"), isReference("x")));
    assertThat(splitPoints.tableFunctions(), contains(isFunction("unnest")));
}
Also used : QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 10 with QueriedSelectRelation

use of io.crate.analyze.QueriedSelectRelation in project crate by crate.

the class SQLPrinter method print.

public static String print(QueriedSelectRelation relation) {
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT ");
    sb.append(Lists2.joinOn(", ", relation.outputs(), x -> x.toString(Style.QUALIFIED)));
    if (relation.where() != Literal.BOOLEAN_TRUE) {
        sb.append(" WHERE ");
        sb.append(relation.where().toString(Style.QUALIFIED));
    }
    if (!relation.groupBy().isEmpty()) {
        sb.append(" GROUP BY ");
        sb.append(Lists2.joinOn(", ", relation.groupBy(), x -> x.toString(Style.QUALIFIED)));
    }
    Symbol having = relation.having();
    if (having != null) {
        sb.append(" HAVING ");
        sb.append(having.toString(Style.QUALIFIED));
    }
    OrderBy orderBy = relation.orderBy();
    if (orderBy != null) {
        sb.append(" ORDER BY ");
        process(orderBy, sb);
    }
    Symbol limit = relation.limit();
    if (limit != null) {
        sb.append(" LIMIT ");
        sb.append(print(limit));
    }
    Symbol offset = relation.offset();
    if (offset != null) {
        sb.append(" OFFSET ");
        sb.append(print(offset));
    }
    return sb.toString();
}
Also used : HashSet(java.util.HashSet) OrderBy(io.crate.analyze.OrderBy) Literal(io.crate.expression.symbol.Literal) Symbol(io.crate.expression.symbol.Symbol) WhereClause(io.crate.analyze.WhereClause) Collection(java.util.Collection) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) Style(io.crate.expression.symbol.format.Style) Lists2(io.crate.common.collections.Lists2) Ordering(io.crate.common.collections.Ordering) OrderBy(io.crate.analyze.OrderBy) Symbol(io.crate.expression.symbol.Symbol)

Aggregations

QueriedSelectRelation (io.crate.analyze.QueriedSelectRelation)18 Test (org.junit.Test)12 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)10 Symbol (io.crate.expression.symbol.Symbol)4 WhereClause (io.crate.analyze.WhereClause)3 DocTableRelation (io.crate.analyze.relations.DocTableRelation)3 OrderBy (io.crate.analyze.OrderBy)2 ExpressionAnalysisContext (io.crate.analyze.expressions.ExpressionAnalysisContext)2 ExpressionAnalyzer (io.crate.analyze.expressions.ExpressionAnalyzer)2 SubqueryAnalyzer (io.crate.analyze.expressions.SubqueryAnalyzer)2 SelectAnalysis (io.crate.analyze.relations.select.SelectAnalysis)2 ProjectionBuilder (io.crate.execution.dsl.projection.builder.ProjectionBuilder)2 EvaluatingNormalizer (io.crate.expression.eval.EvaluatingNormalizer)2 PlannerContext (io.crate.planner.PlannerContext)2 TableStats (io.crate.statistics.TableStats)2 CrateUnitTest (io.crate.test.integration.CrateUnitTest)2 SQLExecutor (io.crate.testing.SQLExecutor)2 Map (java.util.Map)2 RandomizedTest (com.carrotsearch.randomizedtesting.RandomizedTest)1 AbstractTableRelation (io.crate.analyze.relations.AbstractTableRelation)1