@Test public void shouldOptimizePlanForQueryWithOrderByClauseThatUsesScoreFunction() { node = optimize("SELECT v2.c11 AS c1 FROM v2 WHERE v2.c11 = 'x' AND v2.c12 = 'y' ORDER BY SCORE(v2) ASC"); PlanNode sort = new PlanNode(Type.SORT, selector("t1"), selector("t2")); sort.setProperty(Property.SORT_ORDER_BY, orderings(ascendingScore("t1", "t2"))); PlanNode project = new PlanNode(Type.PROJECT, sort, selector("t1")); project.setProperty(Property.PROJECT_COLUMNS, columns(column("t1", "c11", "c1"))); PlanNode join = new PlanNode(Type.JOIN, project, selector("t2"), selector("t1")); join.setProperty(Property.JOIN_ALGORITHM, JoinAlgorithm.NESTED_LOOP); join.setProperty(Property.JOIN_TYPE, JoinType.INNER); join.setProperty(Property.JOIN_CONDITION, new EquiJoinCondition(selector("t1"), "c11", selector("t2"), "c21")); PlanNode leftAccess = new PlanNode(Type.ACCESS, join, selector("t1")); PlanNode leftProject = new PlanNode(Type.PROJECT, leftAccess, selector("t1")); leftProject.setProperty(Property.PROJECT_COLUMNS, columns(column("t1", "c11", "c1"))); PlanNode leftSelect1 = new PlanNode(Type.SELECT, leftProject, selector("t1")); leftSelect1.setProperty(Property.SELECT_CRITERIA, new Comparison(new PropertyValue(selector("t1"), "c11"), Operator.EQUAL_TO, new Literal("x"))); PlanNode leftSelect2 = new PlanNode(Type.SELECT, leftSelect1, selector("t1")); leftSelect2.setProperty(Property.SELECT_CRITERIA, new Comparison(new PropertyValue(selector("t1"), "c12"), Operator.EQUAL_TO, new Literal("y"))); PlanNode leftSource = new PlanNode(Type.SOURCE, leftSelect2, selector("t1")); leftSource.setProperty(Property.SOURCE_NAME, selector("t1")); leftSource.setProperty(Property.SOURCE_COLUMNS, context.getSchemata().getTable(selector("t1")).getColumns()); PlanNode rightAccess = new PlanNode(Type.ACCESS, join, selector("t2")); PlanNode rightProject = new PlanNode(Type.PROJECT, rightAccess, selector("t2")); rightProject.setProperty(Property.PROJECT_COLUMNS, columns(column("t2", "c21"))); PlanNode rightSelect1 = new PlanNode(Type.SELECT, rightProject, selector("t2")); rightSelect1.setProperty(Property.SELECT_CRITERIA, new Comparison(new PropertyValue(selector("t2"), "c21"), Operator.EQUAL_TO, new Literal("x"))); PlanNode rightSource = new PlanNode(Type.SOURCE, rightSelect1, selector("t2")); rightSource.setProperty(Property.SOURCE_NAME, selector("t2")); rightSource.setProperty(Property.SOURCE_COLUMNS, context.getSchemata().getTable(selector("t2")).getColumns()); assertPlanMatches(sort); }
@Override public PlanNode optimize( QueryContext context, PlanNode plan ) { LinkedList<OptimizerRule> rules = new LinkedList<OptimizerRule>(); populateRuleStack(rules, context.getHints()); Problems problems = context.getProblems(); while (rules.peek() != null && !problems.hasErrors()) { OptimizerRule nextRule = rules.poll(); LOGGER.trace("Running query optimizer rule {0}", nextRule); plan = nextRule.execute(context, plan, rules); LOGGER.trace("Plan after running query optimizer rule {0}: \n{1}", nextRule, plan); } return plan; }
RuleBasedOptimizer implements Optimizer { @Override public PlanNode optimize( QueryContext context, PlanNode plan ) { LinkedList<OptimizerRule> rules = new LinkedList<OptimizerRule>(); populateRuleStack(rules, context.getHints()); Problems problems = context.getProblems(); while (rules.peek() != null && !problems.hasErrors()) { OptimizerRule nextRule = rules.poll(); LOGGER.trace("Running query optimizer rule {0}", nextRule); plan = nextRule.execute(context, plan, rules); LOGGER.trace("Plan after running query optimizer rule {0}: \n{1}", nextRule, plan); } return plan; } }
RuleBasedOptimizer implements Optimizer { @Override public PlanNode optimize( QueryContext context, PlanNode plan ) { LinkedList<OptimizerRule> rules = new LinkedList<OptimizerRule>(); populateRuleStack(rules, context.getHints()); Problems problems = context.getProblems(); while (rules.peek() != null && !problems.hasErrors()) { OptimizerRule nextRule = rules.poll(); LOGGER.trace("Running query optimizer rule {0}", nextRule); plan = nextRule.execute(context, plan, rules); LOGGER.trace("Plan after running query optimizer rule {0}: \n{1}", nextRule, plan); } return plan; }  }
RuleBasedOptimizer implements Optimizer { @Override public PlanNode optimize( QueryContext context, PlanNode plan ) { LinkedList<OptimizerRule> rules = new LinkedList<OptimizerRule>(); populateRuleStack(rules, context.getHints()); Problems problems = context.getProblems(); while (rules.peek() != null && !problems.hasErrors()) { OptimizerRule nextRule = rules.poll(); LOGGER.trace("Running query optimizer rule {0}", nextRule); plan = nextRule.execute(context, plan, rules); LOGGER.trace("Plan after running query optimizer rule {0}: \n{1}", nextRule, plan); } return plan; }  @Override PlanNode optimize( QueryContext context,
                              PlanNode plan ); }
RuleBasedOptimizer implements Optimizer { @Override public PlanNode optimize( QueryContext context, PlanNode plan ) { LinkedList<OptimizerRule> rules = new LinkedList<OptimizerRule>(); populateRuleStack(rules, context.getHints()); Problems problems = context.getProblems(); while (rules.peek() != null && !problems.hasErrors()) { OptimizerRule nextRule = rules.poll(); LOGGER.trace("Running query optimizer rule {0}", nextRule); plan = nextRule.execute(context, plan, rules); LOGGER.trace("Plan after running query optimizer rule {0}: \n{1}", nextRule, plan); } return plan; }  @Override PlanNode optimize( QueryContext context,
                              PlanNode plan );  }