CanonicalPlanner implements Planner { @Override public PlanNode createPlan( QueryContext context, QueryCommand query ) { PlanNode plan = null; if (query instanceof Query) { plan = createCanonicalPlan(context, (Query)query); } else { plan = createCanonicalPlan(context, (SetQuery)query); } return plan; }  @Override PlanNode createPlan( QueryContext context,
                                QueryCommand query );  }
@Test public void shouldProducePlanForSelectStarFromTable() { schemata = schemataBuilder.addTable("__ALLNODES__", "column1", "column2", "column3").build(); query = builder.selectStar().fromAllNodes().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.isEmpty(), is(true)); assertProjectNode(plan, "column1", "column2", "column3"); assertThat(plan.getType(), is(PlanNode.Type.PROJECT)); assertThat(plan.getChildCount(), is(1)); PlanNode source = plan.getFirstChild(); assertSourceNode(source, "__ALLNODES__", null, "column1", "column2", "column3"); assertThat(source.getChildCount(), is(0)); print(plan); }
@Test public void shouldProduceErrorWhenSelectingNonExistantTable() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3").build(); query = builder.selectStar().fromAllNodes().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(true)); }
@Test public void shouldProduceErrorWhenSelectingNonExistantColumnOnExistingTable() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3").build(); query = builder.select("column1", "column4").from("someTable").query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(true)); }
@Test public void shouldProducePlanWhenSelectingAllColumnsOnExistingTable() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3").build(); query = builder.selectStar().from("someTable").query(); initQueryContext(); plan = planner.createPlan(queryContext, query); print(plan); assertThat(problems.hasErrors(), is(false)); assertThat(problems.isEmpty(), is(true)); assertProjectNode(plan, "column1", "column2", "column3"); assertThat(plan.getType(), is(PlanNode.Type.PROJECT)); assertThat(plan.getChildCount(), is(1)); PlanNode source = plan.getFirstChild(); assertSourceNode(source, "someTable", null, "column1", "column2", "column3"); assertThat(source.getChildCount(), is(0)); }
@Test public void shouldProducePlanWhenSelectingColumnsFromTableWithoutAlias() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3").build(); query = builder.select("column1", "column2").from("someTable").where().path("someTable").isEqualTo(1L).end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(false)); assertThat(plan.getType(), is(PlanNode.Type.PROJECT)); assertThat(plan.getSelectors(), is(selectors("someTable"))); }
@Test public void shouldProducePlanWhenSelectingColumnsFromTableWithAlias() { schemata = schemataBuilder.addTable("dna:someTable", "column1", "column2", "column3").build(); query = builder.select("column1", "column2").from("dna:someTable AS t1").where().path("t1").isEqualTo(1L).end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(false)); print(plan); assertThat(plan.getType(), is(PlanNode.Type.PROJECT)); assertThat(plan.getSelectors(), is(selectors("t1"))); }
@Test public void shouldProducePlanWhenSelectingAllColumnsFromTableWithAlias() { schemata = schemataBuilder.addTable("dna:someTable", "column1", "column2", "column3").build(); query = builder.selectStar().from("dna:someTable AS t1").where().path("t1").isEqualTo(1L).end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(false)); print(plan); assertThat(plan.getType(), is(PlanNode.Type.PROJECT)); assertThat(plan.getSelectors(), is(selectors("t1"))); }
@Test public void shouldProduceErrorWhenFullTextSearchingTableWithNoSearchableColumns() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3").build(); query = builder.select("column1", "column2").from("someTable").query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(false)); query = builder.select("column1", "column2").from("someTable").where().search("someTable", "term1").end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(true)); }
@Test public void shouldProducePlanWhenFullTextSearchingTableWithAtLeastOneSearchableColumn() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3").makeSearchable("someTable", "column1") .build(); query = builder.select("column1", "column4").from("someTable").where().search("someTable", "term1").end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(true)); }
@Test public void shouldProduceErrorWhenFullTextSearchingColumnThatIsNotSearchable() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3").build(); query = builder.select("column1", "column2").from("someTable").query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(false)); query = builder.select("column1", "column2").from("someTable").where().search("someTable", "column2", "term1").end() .query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(true)); }
@Test public void shouldProducePlanWhenFullTextSearchingColumnThatIsSearchable() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3").makeSearchable("someTable", "column1") .build(); query = builder.select("column1", "column4").from("someTable").where().search("someTable", "column1", "term1").end() .query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(true)); }
@Test public void shouldProducePlanWhenOrderByClauseIsUsed() { schemata = schemataBuilder.addTable("dna:someTable", "column1", "column2", "column3").build(); query = builder.selectStar().from("dna:someTable AS t1").where().path("t1").isEqualTo(1L).end().orderBy().ascending() .propertyValue("t1", "column1").end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(false)); print(plan); assertThat(plan.getType(), is(PlanNode.Type.SORT)); assertThat(plan.getSelectors(), is(selectors("t1"))); }
@Test public void shouldProducePlanWhenOrderByClauseWithScoreIsUsed() { schemata = schemataBuilder.addTable("dna:someTable", "column1", "column2", "column3").build(); query = builder.selectStar().from("dna:someTable AS t1").where().path("t1").isEqualTo(1L).end().orderBy().ascending() .fullTextSearchScore("t1").end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); assertThat(problems.hasErrors(), is(false)); print(plan); assertThat(plan.getType(), is(PlanNode.Type.SORT)); assertThat(plan.getSelectors(), is(selectors("t1"))); }
@FixFor( "MODE-869" ) @Test public void shouldProducePlanWhenUsingSubquery() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3") .addTable("otherTable", "columnA", "columnB").build(); QueryCommand subquery = builder.select("columnA").from("otherTable").query(); builder = new QueryBuilder(typeSystem); query = builder.selectStar().from("someTable").where().path("someTable").isLike(subquery).end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); print(plan); assertThat(problems.hasErrors(), is(false)); assertThat(problems.isEmpty(), is(true)); assertThat(plan.getType(), is(Type.DEPENDENT_QUERY)); assertThat(plan.getChildCount(), is(2)); PlanNode subqueryPlan = plan.getFirstChild(); assertProjectNode(subqueryPlan, "columnA"); assertThat(subqueryPlan.getProperty(Property.VARIABLE_NAME, String.class), is(Subquery.VARIABLE_PREFIX + "1")); assertThat(subqueryPlan.getChildCount(), is(1)); assertThat(subqueryPlan.getSelectors(), is(selectors("otherTable"))); PlanNode subquerySource = subqueryPlan.getFirstChild(); assertSourceNode(subquerySource, "otherTable", null, "columnA", "columnB"); assertThat(subquerySource.getChildCount(), is(0)); PlanNode queryPlan = plan.getLastChild(); assertProjectNode(queryPlan, "column1", "column2", "column3"); assertThat(queryPlan.getType(), is(PlanNode.Type.PROJECT)); assertThat(queryPlan.getChildCount(), is(1)); assertThat(queryPlan.getSelectors(), is(selectors("someTable"))); PlanNode criteriaNode = queryPlan.getFirstChild(); assertThat(criteriaNode.getType(), is(PlanNode.Type.SELECT)); assertThat(criteriaNode.getChildCount(), is(1)); assertThat(criteriaNode.getSelectors(), is(selectors("someTable"))); assertThat(criteriaNode.getProperty(Property.SELECT_CRITERIA), is((Object)like(nodePath("someTable"), var(Subquery.VARIABLE_PREFIX + "1")))); PlanNode source = criteriaNode.getFirstChild(); assertSourceNode(source, "someTable", null, "column1", "column2", "column3"); assertThat(source.getChildCount(), is(0)); }
@FixFor( "MODE-869" ) @Test public void shouldProducePlanWhenUsingSubqueryInSubquery() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3") .addTable("otherTable", "columnA", "columnB").addTable("stillOther", "columnX", "columnY") .build(); QueryCommand subquery2 = builder.select("columnY").from("stillOther").where().propertyValue("stillOther", "columnX") .isLessThan().cast(3).asLong().end().query(); builder = new QueryBuilder(typeSystem); QueryCommand subquery1 = builder.select("columnA").from("otherTable").where().propertyValue("otherTable", "columnB") .isEqualTo(subquery2).end().query(); builder = new QueryBuilder(typeSystem); query = builder.selectStar().from("someTable").where().path("someTable").isLike(subquery1).end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); print(plan); assertThat(problems.hasErrors(), is(false)); assertThat(problems.isEmpty(), is(true)); assertThat(plan.getType(), is(Type.DEPENDENT_QUERY)); assertThat(plan.getChildCount(), is(2)); PlanNode depQuery1 = plan.getFirstChild(); assertThat(depQuery1.getType(), is(PlanNode.Type.DEPENDENT_QUERY)); assertThat(depQuery1.getChildCount(), is(2)); PlanNode subqueryPlan2 = depQuery1.getFirstChild(); assertProjectNode(subqueryPlan2, "columnY"); assertThat(subqueryPlan2.getProperty(Property.VARIABLE_NAME, String.class), is(Subquery.VARIABLE_PREFIX + "2")); assertThat(subqueryPlan2.getChildCount(), is(1)); assertThat(subqueryPlan2.getSelectors(), is(selectors("stillOther"))); PlanNode criteriaNode2 = subqueryPlan2.getFirstChild(); assertThat(criteriaNode2.getType(), is(PlanNode.Type.SELECT)); assertThat(criteriaNode2.getChildCount(), is(1)); assertThat(criteriaNode2.getSelectors(), is(selectors("stillOther"))); assertThat(criteriaNode2.getProperty(Property.SELECT_CRITERIA), is((Object)lessThan(property("stillOther", "columnX"), literal(3L)))); PlanNode subquerySource2 = criteriaNode2.getFirstChild(); assertSourceNode(subquerySource2, "stillOther", null, "columnX", "columnY"); assertThat(subquerySource2.getChildCount(), is(0)); PlanNode subqueryPlan1 = depQuery1.getLastChild(); assertProjectNode(subqueryPlan1, "columnA"); assertThat(subqueryPlan1.getProperty(Property.VARIABLE_NAME, String.class), is(Subquery.VARIABLE_PREFIX + "1")); assertThat(subqueryPlan1.getChildCount(), is(1)); assertThat(subqueryPlan1.getSelectors(), is(selectors("otherTable"))); PlanNode criteriaNode1 = subqueryPlan1.getFirstChild(); assertThat(criteriaNode1.getType(), is(PlanNode.Type.SELECT)); assertThat(criteriaNode1.getChildCount(), is(1)); assertThat(criteriaNode1.getSelectors(), is(selectors("otherTable"))); assertThat(criteriaNode1.getProperty(Property.SELECT_CRITERIA), is((Object)equals(property("otherTable", "columnB"), var(Subquery.VARIABLE_PREFIX + "2")))); PlanNode subquerySource1 = criteriaNode1.getFirstChild(); assertSourceNode(subquerySource1, "otherTable", null, "columnA", "columnB"); assertThat(subquerySource1.getChildCount(), is(0)); PlanNode queryPlan = plan.getLastChild(); assertProjectNode(queryPlan, "column1", "column2", "column3"); assertThat(queryPlan.getType(), is(PlanNode.Type.PROJECT)); assertThat(queryPlan.getChildCount(), is(1)); assertThat(queryPlan.getSelectors(), is(selectors("someTable"))); PlanNode criteriaNode = queryPlan.getFirstChild(); assertThat(criteriaNode.getType(), is(PlanNode.Type.SELECT)); assertThat(criteriaNode.getChildCount(), is(1)); assertThat(criteriaNode.getSelectors(), is(selectors("someTable"))); assertThat(criteriaNode.getProperty(Property.SELECT_CRITERIA), is((Object)like(nodePath("someTable"), var(Subquery.VARIABLE_PREFIX + "1")))); PlanNode source = criteriaNode.getFirstChild(); assertSourceNode(source, "someTable", null, "column1", "column2", "column3"); assertThat(source.getChildCount(), is(0)); }
@FixFor( "MODE-869" ) @Test public void shouldProducePlanWhenUsingTwoSubqueries() { schemata = schemataBuilder.addTable("someTable", "column1", "column2", "column3") .addTable("otherTable", "columnA", "columnB").addTable("stillOther", "columnX", "columnY") .build(); QueryCommand subquery1 = builder.select("columnA").from("otherTable").where().propertyValue("otherTable", "columnB") .isEqualTo("winner").end().query(); builder = new QueryBuilder(typeSystem); QueryCommand subquery2 = builder.select("columnY").from("stillOther").where().propertyValue("stillOther", "columnX") .isLessThan().cast(3).asLong().end().query(); builder = new QueryBuilder(typeSystem); query = builder.selectStar().from("someTable").where().path("someTable").isLike(subquery2).and() .propertyValue("someTable", "column3").isInSubquery(subquery1).end().query(); initQueryContext(); plan = planner.createPlan(queryContext, query); print(plan); assertThat(problems.hasErrors(), is(false)); assertThat(problems.isEmpty(), is(true)); assertThat(plan.getType(), is(Type.DEPENDENT_QUERY)); assertThat(plan.getChildCount(), is(2)); PlanNode subqueryPlan1 = plan.getFirstChild(); assertProjectNode(subqueryPlan1, "columnA"); assertThat(subqueryPlan1.getProperty(Property.VARIABLE_NAME, String.class), is(Subquery.VARIABLE_PREFIX + "1")); assertThat(subqueryPlan1.getChildCount(), is(1)); assertThat(subqueryPlan1.getSelectors(), is(selectors("otherTable"))); PlanNode criteriaNode1 = subqueryPlan1.getFirstChild(); assertThat(criteriaNode1.getType(), is(PlanNode.Type.SELECT)); assertThat(criteriaNode1.getChildCount(), is(1)); assertThat(criteriaNode1.getSelectors(), is(selectors("otherTable"))); assertThat(criteriaNode1.getProperty(Property.SELECT_CRITERIA), is((Object)equals(property("otherTable", "columnB"), literal("winner")))); PlanNode subquerySource1 = criteriaNode1.getFirstChild(); assertSourceNode(subquerySource1, "otherTable", null, "columnA", "columnB"); assertThat(subquerySource1.getChildCount(), is(0)); PlanNode depQuery2 = plan.getLastChild(); assertThat(depQuery2.getType(), is(PlanNode.Type.DEPENDENT_QUERY)); assertThat(depQuery2.getChildCount(), is(2)); PlanNode subqueryPlan2 = depQuery2.getFirstChild(); assertProjectNode(subqueryPlan2, "columnY"); assertThat(subqueryPlan2.getProperty(Property.VARIABLE_NAME, String.class), is(Subquery.VARIABLE_PREFIX + "2")); assertThat(subqueryPlan2.getChildCount(), is(1)); assertThat(subqueryPlan2.getSelectors(), is(selectors("stillOther"))); PlanNode criteriaNode2 = subqueryPlan2.getFirstChild(); assertThat(criteriaNode2.getType(), is(PlanNode.Type.SELECT)); assertThat(criteriaNode2.getChildCount(), is(1)); assertThat(criteriaNode2.getSelectors(), is(selectors("stillOther"))); assertThat(criteriaNode2.getProperty(Property.SELECT_CRITERIA), is((Object)lessThan(property("stillOther", "columnX"), literal(3L)))); PlanNode subquerySource2 = criteriaNode2.getFirstChild(); assertSourceNode(subquerySource2, "stillOther", null, "columnX", "columnY"); assertThat(subquerySource2.getChildCount(), is(0)); PlanNode queryPlan = depQuery2.getLastChild(); assertProjectNode(queryPlan, "column1", "column2", "column3"); assertThat(queryPlan.getType(), is(PlanNode.Type.PROJECT)); assertThat(queryPlan.getChildCount(), is(1)); assertThat(queryPlan.getSelectors(), is(selectors("someTable"))); PlanNode criteriaNode3 = queryPlan.getFirstChild(); assertThat(criteriaNode3.getType(), is(PlanNode.Type.SELECT)); assertThat(criteriaNode3.getChildCount(), is(1)); assertThat(criteriaNode3.getSelectors(), is(selectors("someTable"))); assertThat(criteriaNode3.getProperty(Property.SELECT_CRITERIA), is((Object)like(nodePath("someTable"), var(Subquery.VARIABLE_PREFIX + "2")))); PlanNode criteriaNode4 = criteriaNode3.getFirstChild(); assertThat(criteriaNode4.getProperty(Property.SELECT_CRITERIA), is((Object)in(property("someTable", "column3"), var(Subquery.VARIABLE_PREFIX + "1")))); PlanNode source = criteriaNode4.getFirstChild(); assertSourceNode(source, "someTable", null, "column1", "column2", "column3"); assertThat(source.getChildCount(), is(0)); }