Search in sources :

Example 11 with QueriedSelectRelation

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

the class WhereClauseOptimizerTest method optimize.

private WhereClauseOptimizer.DetailedQuery optimize(String statement) {
    QueriedSelectRelation queriedTable = e.analyze(statement);
    DocTableRelation table = ((DocTableRelation) queriedTable.from().get(0));
    EvaluatingNormalizer normalizer = new EvaluatingNormalizer(e.nodeCtx, RowGranularity.CLUSTER, null, table);
    return WhereClauseOptimizer.optimize(normalizer, queriedTable.where(), table.tableInfo(), e.getPlannerContext(clusterService.state()).transactionContext(), e.nodeCtx);
}
Also used : EvaluatingNormalizer(io.crate.expression.eval.EvaluatingNormalizer) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) DocTableRelation(io.crate.analyze.relations.DocTableRelation)

Example 12 with QueriedSelectRelation

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

the class RelationNormalizerTest method testOrderByLimitsNotMerged.

@Test
public void testOrderByLimitsNotMerged() throws Exception {
    QueriedRelation relation = normalize("select * from (" + "select * from (" + "select * from t1 order by a limit 10 offset 5" + ") as tt" + ") as ttt order by a desc limit 5");
    assertThat(relation, instanceOf(QueriedSelectRelation.class));
    QueriedSelectRelation outerRelation = (QueriedSelectRelation) relation;
    assertThat(outerRelation.querySpec(), isSQL("SELECT io.crate.analyze.QueriedSelectRelation.a, " + "io.crate.analyze.QueriedSelectRelation.x, " + "io.crate.analyze.QueriedSelectRelation.i " + "ORDER BY io.crate.analyze.QueriedSelectRelation.a DESC LIMIT 5"));
    assertThat(outerRelation.subRelation(), instanceOf(QueriedDocTable.class));
    assertThat(outerRelation.subRelation().querySpec(), isSQL("SELECT doc.t1.a, doc.t1.x, doc.t1.i ORDER BY doc.t1.a LIMIT 10 OFFSET 5"));
}
Also used : QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 13 with QueriedSelectRelation

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

the class RelationNormalizerTest method testOrderByLimitsOnInnerNotMerged.

@Test
public void testOrderByLimitsOnInnerNotMerged() throws Exception {
    QueriedRelation relation = normalize("select * from (" + "select * from (" + "select * from t1 order by a limit 10" + ") as tt" + ") as ttt order by x");
    assertThat(relation, instanceOf(QueriedSelectRelation.class));
    QueriedSelectRelation outerRelation = (QueriedSelectRelation) relation;
    assertThat(outerRelation.querySpec(), isSQL("SELECT io.crate.analyze.QueriedSelectRelation.a, " + "io.crate.analyze.QueriedSelectRelation.x, " + "io.crate.analyze.QueriedSelectRelation.i ORDER BY io.crate.analyze.QueriedSelectRelation.x"));
    assertThat(outerRelation.subRelation(), instanceOf(QueriedDocTable.class));
    assertThat(outerRelation.subRelation().querySpec(), isSQL("SELECT doc.t1.a, doc.t1.x, doc.t1.i ORDER BY doc.t1.a LIMIT 10"));
}
Also used : QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 14 with QueriedSelectRelation

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

the class LimitTest method testLimitOnLimitOperator.

@Test
public void testLimitOnLimitOperator() throws Exception {
    SQLExecutor e = SQLExecutor.builder(clusterService, 2, RandomizedTest.getRandom(), List.of()).addTable(TableDefinitions.USER_TABLE_DEFINITION).build();
    QueriedSelectRelation queriedDocTable = e.analyze("select name from users");
    LogicalPlan plan = Limit.create(Limit.create(Collect.create(((AbstractTableRelation<?>) queriedDocTable.from().get(0)), queriedDocTable.outputs(), new WhereClause(queriedDocTable.where()), new TableStats(), null), Literal.of(10L), Literal.of(5L)), Literal.of(20L), Literal.of(7L));
    assertThat(plan, isPlan("Limit[20::bigint;7::bigint]\n" + "  └ Limit[10::bigint;5::bigint]\n" + "    └ Collect[doc.users | [name] | true]"));
    PlannerContext ctx = e.getPlannerContext(clusterService.state());
    Merge merge = (Merge) plan.build(ctx, Set.of(), new ProjectionBuilder(e.nodeCtx), TopN.NO_LIMIT, 0, null, null, Row.EMPTY, SubQueryResults.EMPTY);
    io.crate.planner.node.dql.Collect collect = (io.crate.planner.node.dql.Collect) merge.subPlan();
    assertThat(collect.collectPhase().projections(), contains(ProjectionMatchers.isTopN(15, 0)));
    // noinspection unchecked
    assertThat(merge.mergePhase().projections(), contains(ProjectionMatchers.isTopN(10, 5), ProjectionMatchers.isTopN(20, 7)));
}
Also used : WhereClause(io.crate.analyze.WhereClause) TableStats(io.crate.statistics.TableStats) PlannerContext(io.crate.planner.PlannerContext) Merge(io.crate.planner.Merge) SQLExecutor(io.crate.testing.SQLExecutor) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) AbstractTableRelation(io.crate.analyze.relations.AbstractTableRelation) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) RandomizedTest(com.carrotsearch.randomizedtesting.RandomizedTest)

Example 15 with QueriedSelectRelation

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

the class CollectTest method test.

@Test
public void test() throws Exception {
    var e = SQLExecutor.builder(clusterService).addTable("CREATE TABLE t (x int)").build();
    TableStats tableStats = new TableStats();
    PlannerContext plannerCtx = e.getPlannerContext(clusterService.state());
    ProjectionBuilder projectionBuilder = new ProjectionBuilder(e.nodeCtx);
    QueriedSelectRelation analyzedRelation = e.analyze("SELECT 123 AS alias, 456 AS alias2 FROM t ORDER BY alias, 2");
    LogicalPlanner logicalPlanner = new LogicalPlanner(e.nodeCtx, tableStats, () -> clusterService.state().nodes().getMinNodeVersion());
    LogicalPlan operator = logicalPlanner.plan(analyzedRelation, plannerCtx);
    ExecutionPlan build = operator.build(plannerCtx, Set.of(), projectionBuilder, -1, 0, null, null, Row.EMPTY, SubQueryResults.EMPTY);
    assertThat((((RoutedCollectPhase) ((io.crate.planner.node.dql.Collect) build).collectPhase())).orderBy(), is(nullValue()));
}
Also used : PlannerContext(io.crate.planner.PlannerContext) ExecutionPlan(io.crate.planner.ExecutionPlan) QueriedSelectRelation(io.crate.analyze.QueriedSelectRelation) TableStats(io.crate.statistics.TableStats) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) RoutedCollectPhase(io.crate.execution.dsl.phases.RoutedCollectPhase) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

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