Class CorrelatedSubqueryRule

java.lang.Object
dev.frostlake.executor.CorrelatedSubqueryRule

public final class CorrelatedSubqueryRule extends Object
The correlated subqueries live can evaluate. Its planner turns a correlated subquery into a join, and a shape it cannot turn is refused as Unsupported subquery type cannot be evaluated — when a row first needs the subquery, so an outer query that reads no row never meets the refusal. Every rule below was measured:
  • A scalar subquery must promise one row by its shape. It is either an aggregate with no GROUP BY and no LIMIT, or a FROM-less select with no WHERE, HAVING or ORDER BY and no LIMIT below 1. A set operation never qualifies, and an aggregate may not read the outer row inside its argument: (SELECT SUM(v + fz.id) FROM g) is refused.
  • A filter — the WHERE, HAVING or join ON of a scalar aggregate, and of an EXISTS or IN subquery — may read the outer row only through comparisons whose every operand stays on one side. g.id = fz.id, fz.id > 5, g.id IN (fz.id, 6), g.s LIKE fz.s and ORs of those are accepted. A bare boolean (WHERE fz.b), NOT, CASE, or an operand mixing the two sides (g.id + fz.id = 10) is refused.
  • EXISTS and IN take any other shape — GROUP BY, no FROM — but not a LIMIT or a set operation.

Live's planner answers from metadata first, and two of its shortcuts are mirrored: a subquery over an empty table is pruned away, and an outer relation of at most one row is evaluated row by row (see SubqueryEvaluator); either way nothing is refused.

Correlation is read off the parse tree, through every nested select, each resolving a name in its own relations before the ones around it. A name the rule cannot place leaves the subquery unjudged, so its execution reports the name, as live reports it first.

  • Constructor Details

    • CorrelatedSubqueryRule

      public CorrelatedSubqueryRule(QueryExecutor executor, Set<String> outerNames)
      Parameters:
      outerNames - every name the evaluated row offers the subquery, upper-cased: bare columns and relation-qualified ones, the row's own and the rows further out
  • Method Details