afs opened a new issue, #1615:
URL: https://github.com/apache/jena/issues/1615

   ### Version
   
   4.6.1
   
   ### Feature
   
   # LATERAL join
   
   _Proposed experimental feature_
   
   _This issue description may be edited in-place._
   
   A `LATERAL` join is like a foreach loop, looping on the results from the 
left-hand side (LHS), the pattern before the `LATERAL` keyword, and executing 
the right-hand side (RHS) query pattern once for each row, with the variables 
from the RHS in-scope during each RHS evaluation.
   
   A regular join only executes the RHS once, and the variables from the LHS 
are only used for the join condition after evaluation of the left and right 
sub-patterns.
   
   Another way to think of a lateral join is as a `flatmap`.
   
   Example:
   
   ```
   ## Get exactly one label for each subject in a row.
   SELECT * {
      ?s ?p ?o
      LATERAL {
        SELECT * { ?s rdfs:label ?label } LIMIT 1
      }
   }
   ```
   
   ```
   ## Get zero or one labels for each subject.
   SELECT * {
      ?s ?p ?o
      LATERAL { OPTIONAL { SELECT * ?s rdfs:label ?label } LIMIT 1}
      }
   }
   ```
   
   ### Syntax
   
   The `LATERAL` keyword which has the graph pattern so far (from the `{` 
starting the current block) and a `{ }` block afterwards. 
   
   Possible addition: `LATERAL ( ?var1 ?var2 ...)` to specify certain variables 
to expose to the RHS. Other variables would be (inner)joined as usual. This may 
be an unnecessary feature.
   
   ### Scope
   
   A sub-select may have variables of the same name that are not lateral-joined 
to a variable of the same name from the RHS.
   
   ```
   SELECT * {
      ?s ?p ?o
      LATERAL {
        SELECT ?label { ?s rdfs:label ?label } LIMIT 1
      }
   }
   ```
   The inner `?s` in the `SELECT ?label` is not the outer `?s` because the 
`SELECT ?label` does not pass out `?s`. As a sub-query the `?s` could be any 
name except `?label` for the same results.
   
   This is the same situation as a sub-query in other situations.
   
   There needs to be a new syntax restriction: there can no variable introduced 
by `AS` (`BIND`, or sub-query) or `VALUES` in-scope at the top level of the 
`LATERAL` RHS, that is the same name as  any 
[in-scope](https://www.w3.org/TR/sparql11-query/#variableScope) variable from 
the LHS.
   
   ```
   ## ** Illegal **
   SELECT * {
      ?s ?p ?o
      LATERAL { BIND( 123 AS ?o) }
   }
   ```
   
   See [SPARQL Grammar note 
12](https://www.w3.org/TR/sparql11-query/#sparqlGrammar).
   
   In ARQ, [LET](https://www.w3.org/TR/sparql11-query/#variableScope) would 
work.
   `LET` for a variable that is bound acts like a filter.
   
   ### Evaluation
   
   [Substituting variables](https://afs.github.io/substitute.html) from the LHS 
into the RHS (with the same restrictions), then executing the pattern, gives 
the evaluation of `LATERAL`
   
   ### Notes
   
   There is a similarity to filter `NOT EXISTS`/`EXISTS` expressed as the 
not-legal `FILTER ( ASK { pattern } )` where the variables of the row being 
filtered are available to "pattern". This is similar to ab SQL [correlated 
subquery](https://en.wikipedia.org/wiki/Correlated_subquery).
   
   ### Elsewhere
   
   * https://github.com/w3c/sparql-12/issues/100
   * Jena's [SERVICE 
<loop:>](https://jena.apache.org/documentation/query/service_enhancer.html)
   
   * Oxigraph: 
[oxigraph/issues/267](https://github.com/oxigraph/oxigraph/issues/267), 
[oxigraph/pull/274](https://github.com/oxigraph/oxigraph/pull/274)
   * 
https://docs.stardog.com/query-stardog/stored-query-service#correlated-subqueries
   
   * 
https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-LATERAL
   * https://dev.mysql.com/doc/refman/8.0/en/lateral-derived-tables.html
   * https://en.wikipedia.org/wiki/Correlated_subquery
   
   
   ### Are you interested in contributing a solution yourself?
   
   Yes


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to