From 2c1e0a7c01fa0d66901c5080e9b4eaf5b351f50b Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Sat, 4 Jan 2025 17:49:25 +0000 Subject: [PATCH 1/5] Initial values-insertion draft --- spec/index.html | 141 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/spec/index.html b/spec/index.html index 39d9604..a864af6 100644 --- a/spec/index.html +++ b/spec/index.html @@ -10592,7 +10592,148 @@

Evaluation Semantics

eval( |D|(|G|), Slice(|L|, |start|, |length|) ) = Slice( eval(|D|(|G|), |L|), |start|, |length| )

+ + +
+

Values Insertion and `EXISTS`

+
+

The following subsections contain draft of material for a revised + "`exists`" operation. +

+

+ Background: SPARQL CG SEP-0007 +

+
+
+

Syntax Restriction

+

Additional [[[#sparqlGrammar]]] note:

+
+ Any variable that is assigned to in the graph pattern of `EXISTS`/`NOT EXISTS` must not be in-scope. + This applies to `BIND`, variables introduced by `AS` in a `SELECT` clause, variables in a `VALUES` + clause, and variables introduced by `AS` in `GROUP BY`. +
+

Extend the "in-scope" rules to include the variables in-scope from the current row:

+ + + + + + + + + + + +
Syntax FormIn-scope variables
`EXISTS` and `NOT EXISTS` filters v is in-scope if it is in-scope for the pattern to which the `FILTER` is applied. +
+

+ This restriction means that values inserted + do not conflict with values assigned to variables within the pattern. +

+
+
+

Remapping

+

+ Remapping ensures that a variable name used inside a project expression, + but which is not part of the results of the evaluation of the project expression, + does not coincide with a variable mentioned anywhere else in the + algebra expression of a query. +

+

+ Renaming these variables does not change the results of evaluating + the project expresssion. +

+
+ Definition: Projection Expression Variable Remapping +

+ For a projection algebra operation #sparqlProjection `Project(A, PV)` acting on algreg express `A` and with set of variables `PV`, define + a partial mapping `F` from + `V`, + the set of all variables, to `V` where: +

+
F(v) = v1 if v is in PV, where v1 is a fresh variable
+F(v) = v if v is not in PV
+

+ Define the Projection Expression Variable Remapping `ProjectMap(P,PV)` +

+
ProjectMap(Project(A, PV)) = Project(A1, PV) 
+                                where A1 is the result of applying F
+                                to every variable mentioned in A.
+                
+

+ The Projection Expression Variable Remapping yields an algrebra expression that + evaluates to the same results as the Project argument. No variable of `ProjectMap(Project(A, PV))` + that is not in `PV` is mentioned anywhere else in the algebra expression for the query. +

+
+

This process is applied throughout the graph pattern of EXISTS:

+
+ Definition: Variable Remapping +

+ For any algebra expression `X`, define the Variable Remapping `PrjMap(X)` + of algebra expression `X`: +

+
PrjMap(X) = replace all project operations Project(P, PV) 
+                with ProjectMap(P, PV) for each projection in X.
+
+

+ The outcome of `PrjMap` is independent of the order of replacement + (e.g. bottom-up or top-down). + Replacements may happen several times, depending on recursive order + but each time a replacement is made, the variable not used anywhere else. +

+ +

+ A variable inside a project expression that is not in the variables projected + is not affected by the values insertion operation because it is renamed apart. +

+
+
+

Values Insertion

+
+
+ Definition: Values Insertion +

+ Define the Values Insertion function `ValuesInsert(X, μ)` +

+
Let Table(μ) = { μ } and multiplicity( μ | Table(μ) = { μ } ) = 1
+
+Replace each occurence of `Y` in X where `Y` is one of
+          Basic Graph Pattern,
+          Property Path Expression,
+          `Graph(Var, pattern)`,
+          Inline Data
+with `join(Y, Table(μ))`.
+ +
+

@@ rename as ???correllate

+
+ +
+

+ Examples +

+
+
+
+

Evaluation of EXISTS

+
+ Definition: Evaluation of Exists +

+ Let `μ` be the current solution mapping for a filter, and `X` a graph pattern, + define the Evaluation of Exists `exists(X)` +

+
exists(X) = true 
+              if eval( D(G), ValuesInsert(PrjMap(X), μ) )
+                 is a non-empty solution sequence.
+exists(X) = false otherwise
+
+
+
+ + +

Extending SPARQL Basic Graph Matching

The overall SPARQL design can be used for queries which assume a more elaborate form of From 4730d145eeae72afcfa172e4ebfc12a956e064fc Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Sun, 18 May 2025 20:08:57 +0100 Subject: [PATCH 2/5] Add 'row in scope' approach --- spec/index.html | 101 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 79 insertions(+), 22 deletions(-) diff --git a/spec/index.html b/spec/index.html index a864af6..7aa85dd 100644 --- a/spec/index.html +++ b/spec/index.html @@ -10597,7 +10597,7 @@

Evaluation Semantics

Values Insertion and `EXISTS`

-

The following subsections contain draft of material for a revised +

The following subsections contain draft material for a revised "`exists`" operation.

@@ -10612,7 +10612,10 @@

Syntax Restriction

This applies to `BIND`, variables introduced by `AS` in a `SELECT` clause, variables in a `VALUES` clause, and variables introduced by `AS` in `GROUP BY`. -

Extend the "in-scope" rules to include the variables in-scope from the current row:

+

+ Extend the "in-scope" rules to include the possible + variables that are in-scope for the current row: +

@@ -10626,10 +10629,15 @@

Syntax Restriction

-

- This restriction means that values inserted - do not conflict with values assigned to variables within the pattern. -

+
+

+ This restriction means that values inserted + do not conflict with values assigned to variables within the pattern. +

+

+ This operation is performed as part of query parsing. +

+

Remapping

@@ -10644,7 +10652,7 @@

Remapping

the project expresssion.

- Definition: Projection Expression Variable Remapping + Definition: Projection Expression Variable Remapping

For a projection algebra operation #sparqlProjection `Project(A, PV)` acting on algreg express `A` and with set of variables `PV`, define a partial mapping `F` from @@ -10654,7 +10662,7 @@

Remapping

F(v) = v1 if v is in PV, where v1 is a fresh variable
 F(v) = v if v is not in PV

- Define the Projection Expression Variable Remapping `ProjectMap(P,PV)` + Define the Projection Expression Variable Remapping `ProjectMap(P, PV)`

ProjectMap(Project(A, PV)) = Project(A1, PV) 
                                 where A1 is the result of applying F
@@ -10668,7 +10676,7 @@ 

Remapping

This process is applied throughout the graph pattern of EXISTS:

- Definition: Variable Remapping + Definition: Variable Remapping

For any algebra expression `X`, define the Variable Remapping `PrjMap(X)` of algebra expression `X`: @@ -10682,31 +10690,57 @@

Remapping

Replacements may happen several times, depending on recursive order but each time a replacement is made, the variable not used anywhere else.

- -

- A variable inside a project expression that is not in the variables projected - is not affected by the values insertion operation because it is renamed apart. -

+ +
+

+ A variable inside a project expression that is not in the variables projected + is not affected by the values insertion operation because it is renamed apart. +

+

+ This operation is as part of the translation to the SPARQL + algebra. +

+

Values Insertion

+

+ Alternative 1: rewrite the algebra during + translation to the SPARQL algebra + to include a function that evaluates to the current row. +

+
- Definition: Values Insertion + Definition: Values In Scope

- Define the Values Insertion function `ValuesInsert(X, μ)` + Define the function `BindingInScope()`. + Evaluation of `BindingInScope()` results in a table of one row, + being the current binding of the enclosing filter.

-
Let Table(μ) = { μ } and multiplicity( μ | Table(μ) = { μ } ) = 1
+                
+
+
+
+ Definition: Access to the current binding +

+ During translation to the SPARQL algebra +

+
 Replace each occurence of `Y` in X where `Y` is one of
           Basic Graph Pattern,
           Property Path Expression,
           `Graph(Var, pattern)`,
           Inline Data
-with `join(Y, Table(μ))`.
- + with `join(Y, BindingInScope())`. +
+
+ c.f. section Translate Graph Patterns + where an empty basic graph pattern start any + GroupGraphPattern. + It happens before the simplification step.
-

@@ rename as ???correllate

@@ -10714,17 +10748,40 @@

Values Insertion

Examples

+ +

+ Alternative 2: rewrite the algebra during execution. This corresponds to the + original SEP-0007 proposal. +

+
+
+ Definition: Values Insertion +

+ Define the Values Insertion function `ValuesInsert(X, μ)` +

+
Let Table(μ) = { μ } and multiplicity( μ | Table(μ) = { μ } ) = 1
+
+Replace each occurence of `Y` in X where `Y` is one of
+          Basic Graph Pattern,
+          Property Path Expression,
+          `Graph(Var, pattern)`,
+          Inline Data
+with `join(Y, Table(μ))`.
+ +
+
+

Evaluation of EXISTS

- Definition: Evaluation of Exists + Definition: Evaluation of Exists

Let `μ` be the current solution mapping for a filter, and `X` a graph pattern, define the Evaluation of Exists `exists(X)`

exists(X) = true 
-              if eval( D(G), ValuesInsert(PrjMap(X), μ) )
+              if eval( D(G), ValuesInScope(PrjMap(X)), μ)
                  is a non-empty solution sequence.
 exists(X) = false otherwise
From 853a2681ea4c0bc9370ca420c13a7305ad477cd3 Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Thu, 7 Aug 2025 11:09:07 +0100 Subject: [PATCH 3/5] Updates from reviews --- spec/index.html | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/spec/index.html b/spec/index.html index 7aa85dd..5ed50df 100644 --- a/spec/index.html +++ b/spec/index.html @@ -10631,7 +10631,7 @@

Syntax Restriction

- This restriction means that values inserted + This restriction means that inserted values do not conflict with values assigned to variables within the pattern.

@@ -10654,13 +10654,14 @@

Remapping

Definition: Projection Expression Variable Remapping

- For a projection algebra operation #sparqlProjection `Project(A, PV)` acting on algreg express `A` and with set of variables `PV`, define - a partial mapping `F` from - `V`, - the set of all variables, to `V` where: + For a projection algebra operation + `Project(A, PV)` acting on algebra expression `A` and with set of variables `PV`, + define a mapping `F` from `V`, + the set of all variables, + to `V` where:

-
F(v) = v1 if v is in PV, where v1 is a fresh variable
-F(v) = v if v is not in PV
+
F(v) = v1 if v is not in PV, where v1 is a fresh variable
+F(v) = v if v is in PV

Define the Projection Expression Variable Remapping `ProjectMap(P, PV)`

@@ -10678,15 +10679,15 @@

Remapping

Definition: Variable Remapping

- For any algebra expression `X`, define the Variable Remapping `PrjMap(X)` + For any algebra expression `X`, define the Variable Remapping `PrjMap(X)` of algebra expression `X`:

PrjMap(X) = replace all project operations Project(P, PV) 
-                with ProjectMap(P, PV) for each projection in X.
+ with ProjectMap(Project(A, PV), PV) for each projection in X.

The outcome of `PrjMap` is independent of the order of replacement - (e.g. bottom-up or top-down). + (whether "bottom-up" or "top-down") order. Replacements may happen several times, depending on recursive order but each time a replacement is made, the variable not used anywhere else.

@@ -10719,6 +10720,7 @@

Values Insertion

being the current binding of the enclosing filter.

+

Need to define `BindingInScope`

@@ -10727,13 +10729,14 @@

Values Insertion

During translation to the SPARQL algebra

+

Define putting in the `BindingInScope` operator

-Replace each occurence of `Y` in X where `Y` is one of
+Replace each occurence of Y where Y is one of
           Basic Graph Pattern,
-          Property Path Expression,
-          `Graph(Var, pattern)`,
+          Property Path Patterns,
+          Graph(Var, pattern),
           Inline Data
-          with `join(Y, BindingInScope())`.
+ with join(Y, BindingInScope()).
c.f. section Translate Graph Patterns @@ -10755,18 +10758,18 @@

Values Insertion

- Definition: Values Insertion + Definition: Insert Values

- Define the Values Insertion function `ValuesInsert(X, μ)` + Define the function `InsertValues(X, μ)`

Let Table(μ) = { μ } and multiplicity( μ | Table(μ) = { μ } ) = 1
 
-Replace each occurence of `Y` in X where `Y` is one of
+Replace each occurence of Y in X where Y is one of
           Basic Graph Pattern,
-          Property Path Expression,
-          `Graph(Var, pattern)`,
+          Property Path Patterns,
+          Graph(Var, pattern),
           Inline Data
-with `join(Y, Table(μ))`.
+ with join(Y, Table(μ)).
@@ -10780,8 +10783,11 @@

Evaluation of EXISTS

Let `μ` be the current solution mapping for a filter, and `X` a graph pattern, define the Evaluation of Exists `exists(X)`

+

+ @@ Set up the current row for `BindingdInScope`. +

exists(X) = true 
-              if eval( D(G), ValuesInScope(PrjMap(X)), μ)
+              if eval( D(G), μ)
                  is a non-empty solution sequence.
 exists(X) = false otherwise
From fd7b58fec75adebac70a4662f05c81220fe74bd5 Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Wed, 6 Aug 2025 16:00:54 +0100 Subject: [PATCH 4/5] EXISTS: scoping restriction --- spec/index.html | 121 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 34 deletions(-) diff --git a/spec/index.html b/spec/index.html index 5ed50df..15bb41a 100644 --- a/spec/index.html +++ b/spec/index.html @@ -8781,16 +8781,27 @@

Variable Scope

VALUES varlist { values } v is in-scope if v is in varlist + + `EXISTS` and `NOT EXISTS` filters + + v is in-scope if it is in-scope for the + solution mapping + where the `FILTER` containing `EXISTS` or `NOT EXISTS` is applied. + + -

The variable v must not be in-scope at the point of the (expr AS - v) form. The scoping for (expr AS v) applies immediately in - SELECT expressions.

+

The variable v must not be in-scope at the point of the + (expr AS v) form. The scoping for (expr AS v) + applies immediately in SELECT expressions. +

In BIND (expr AS v) requires that the variable v is not - in-scope from the preceeding elements in the group graph pattern in which it is used.

+ in-scope from the preceeding elements in the group graph pattern in which it is used. +

In SELECT, the variable v must not be in-scope in the graph pattern of the SELECT clause, nor used in another select expression earlier in - the clause.

+ the clause. +

Converting Graph Patterns

@@ -9078,7 +9089,6 @@
Translate Graph Patterns

If the form is GroupGraphPattern:

-Let FS := the empty set
 Let G := the empty pattern, a basic graph pattern which is the empty set.
 
 For each element E in the sequence of elements in the GroupGraphPattern
@@ -11150,52 +11160,95 @@ 

Grammar

section 6 Notation.

Notes:

    -
  1. Keywords are matched in a case-insensitive manner with the exception of +
  2. + Keywords are matched in a case-insensitive manner with the exception of the keyword 'a' which, in line with Turtle and N3, is used in place of the IRI rdf:type - (in full, http://www.w3.org/1999/02/22-rdf-syntax-ns#type).
  3. -
  4. Escape sequences are case sensitive.
  5. -
  6. When tokenizing the input and choosing grammar rules, the longest match is chosen.
  7. -
  8. The SPARQL grammar is LL(1) when the rules with uppercased names are used as terminals.
  9. -
  10. There are two entry points into the grammar: QueryUnit for the SPARQL query language - and UpdateUnit for the SPARQL update language.
  11. -
  12. In signed numbers, no white space is allowed between the sign and the number. + (in full, http://www.w3.org/1999/02/22-rdf-syntax-ns#type). +
  13. +
  14. + Escape sequences are case sensitive. +
  15. +
  16. + When tokenizing the input and choosing grammar rules, the longest match is chosen. +
  17. +
  18. + The SPARQL grammar is LL(1) when the rules with uppercased names are used as terminals. +
  19. +
  20. + There are two entry points into the grammar: QueryUnit for the SPARQL query language + and UpdateUnit for the SPARQL update language. +
  21. +
  22. + In signed numbers, no white space is allowed between the sign and the number. The AdditiveExpression grammar rule allows for this by covering the two cases of an expression followed by a signed number. These - produce an addition or subtraction of the unsigned number as appropriate.
  23. -
  24. The tokens INSERT DATA, + produce an addition or subtraction of the unsigned number as appropriate. +
  25. +
  26. + The tokens INSERT DATA, DELETE DATA and DELETE WHERE allow any amount of white space between the words. - The single space version is used in the grammar for clarity.
  27. -
  28. The QuadData and + The single space version is used in the grammar for clarity. +
  29. +
  30. + The QuadData and QuadPattern rules both use rule Quads. The rule QuadData, used in INSERTDATA and DELETE DATA, - must not allow variables in the quad patterns.
  31. -
  32. Blank node syntax is not allowed in DELETE WHERE, + must not allow variables in the quad patterns. +
  33. +
  34. + Blank node syntax is not allowed in DELETE WHERE, the DeleteClause for DELETE, - nor in DELETE DATA.
  35. -
  36. Rules for limiting the use of blank node identifiers are given in section 19.6.
  37. -
  38. The number of variables in the variable list of VALUES block - must be the same as the number of each list of associated values in the DataBlock.
  39. -
  40. Variables introduced by AS in a SELECT clause - must not already be in-scope.
  41. -
  42. The variable assigned in a BIND clause must not be already + nor in DELETE DATA. +
  43. +
  44. + Rules for limiting the use of blank node identifiers are given in + section 19.6. +
  45. +
  46. + The number of variables in the variable list of VALUES block + must be the same as the number of each list of associated values in + the DataBlock. +
  47. +
  48. + Variables introduced by AS in a SELECT clause + must not already be in-scope. +
  49. +
  50. + The variable assigned in a BIND clause must not be already in-use within the immediately preceding TriplesBlock within a - GroupGraphPattern.
  51. -
  52. Aggregate functions can be one of the + GroupGraphPattern. +
  53. +
  54. + Any variable that is assigned to in the graph pattern of `EXISTS` or `NOT EXISTS` + must not be in-scope. This applies to `BIND`, + variables introduced by `AS` in a `SELECT` clause, + variables introduced by `AS` in `GROUP BY`, + and variables in a `VALUES` clause. +
  55. +
  56. + Aggregate functions can be one of the built-in keywords for aggregates or a custom aggregate, which is syntactically a function call. Aggregate functions may only be used in SELECT, HAVING - and ORDER BY clauses.
  57. -
  58. The expression argument of an aggregate function can not contain an aggregate function.
  59. -
  60. Only custom aggregate functions use the DISTINCT keyword - in a function call.
  61. -
  62. A reifier or + and ORDER BY clauses. +
  63. +
  64. + The expression argument of an aggregate function can not contain an aggregate function. +
  65. +
  66. + Only custom aggregate functions use the DISTINCT keyword + in a function call. +
  67. +
  68. + A reifier or annotation syntax is only permitted after a triple when the property position is a simple path (an IRI, the keyword a, or a variable), From 1a0a79f708d4a1cdbdca73ea8242ec81271d80fc Mon Sep 17 00:00:00 2001 From: Andy Seaborne Date: Wed, 13 Aug 2025 17:13:36 +0100 Subject: [PATCH 5/5] WIP: Sketch correlated EXISTS --- spec/index.html | 333 ++++++++++++++++++------------------------------ 1 file changed, 125 insertions(+), 208 deletions(-) diff --git a/spec/index.html b/spec/index.html index 15bb41a..ef694e8 100644 --- a/spec/index.html +++ b/spec/index.html @@ -8871,6 +8871,7 @@
    Expand Syntax Forms

    Expand abbreviations for IRIs and triple patterns given in Section .

+
Collect FILTER Elements

FILTER expressions apply to the whole group graph pattern in which they @@ -8886,13 +8887,120 @@

Collect FILTER Elements
Let FS := empty set For each form FILTER(expr) in the group graph pattern In expr, replace NOT EXISTS{P} with fn:not(exists(translate(P))) - In expr, replace EXISTS{P} with exists(translate(P)) + In expr, replace EXISTS{P} with prepare(EXISTS{P}) FS := FS ∪ {expr} End

The set of filter expressions FS is used later.

+ +
+ Prepare EXISTS and NOT EXISTS +

prepare(EXISTS{P})

+ +
+

@@ Scoping rule already done (grammar note).
+ Prepare FILTER for exists. +

+
    +
  • A1 = translate(P) without applying simplification
  • +
  • A2 = A1 rewritten to include access current binding
  • +
  • A3 = A2 Remapped hidden-scope variables (if we do that)
  • +
+ +
+
+ Definition: Access to the current binding +

+ During translation to the SPARQL algebra +

+
+Replace each occurence of `Y` in X where `Y` is one of
+          Basic Graph Pattern,
+          Property Path Expression,
+          Graph(Var, pattern),
+          Inline Data
+          with join(Y, BindingInScope()) .
+
+
+ c.f. section Translate Graph Patterns + where an empty basic graph pattern start any + GroupGraphPattern. + It happens before the simplification step. +
+
+ +
+

+ One way to provide `BindingInScope` is to change `eval` to also have the current row + as an argument or a "null" token. . Normally this the "null" token. This would be set in + eval-filter. +

+

+ Another way is to have a global (to the execution) variable. In eval-exists, the + old value is recorded, the global set to the new value, and reset on exit - this forms a + stack for nested EXISTS. +

+

+ Modifing `eval` is better prepartion for other correlted operations.
+ A current-row stack in EXISTS isless intrusive. +

+
+ +
+ Definition: Projection Expression Variable Remapping +

+ For a projection algebra operation #sparqlProjection `Project(A, PV)` acting on algreg express `A` and with set of variables `PV`, define + a partial mapping `F` from + `V`, + the set of all variables, to `V` where: +

+
F(v) = v1 if v is in PV, where v1 is a fresh variable
+F(v) = v if v is not in PV
+

+ Define the Projection Expression Variable Remapping `ProjectMap(P, PV)` +

+
ProjectMap(Project(A, PV)) = Project(A1, PV) 
+                 where A1 is the result of applying F
+                 to every variable mentioned in A.
+

+ The Projection Expression Variable Remapping yields an algrebra expression that + evaluates to the same results as the Project argument. No variable of `ProjectMap(Project(A, PV))` + that is not in `PV` is mentioned anywhere else in the algebra expression for the query. +

+
+

This process is applied throughout the graph pattern of EXISTS:

+
+ Definition: Variable Remapping +

+ For any algebra expression `X`, define the Variable Remapping `PrjMap(X)` + of algebra expression `X`: +

+
PrjMap(X) = replace all project operations Project(P, PV) 
+      with ProjectMap(P, PV) for each projection in X.
+
+

+ The outcome of `PrjMap` is independent of the order of replacement + (e.g. bottom-up or top-down). + Replacements may happen several times, depending on recursive order + but each time a replacement is made, the variable not used anywhere else. +

+ +
+

+ A variable inside a project expression that is not in the variables projected + is not affected by the values insertion operation because it is renamed apart. +

+

+ This operation is as part of the translation to the SPARQL + algebra. +

+
+
+
+ +
Translate Property Path Expressions

The following table gives the translation @@ -9149,14 +9257,17 @@

Filters of Group
Simplification step
-

Some groups of one graph pattern become Join(|Z|, |A|), where |Z| is the empty basic graph +

+ @@ Move out of the general `translate()` process and + perform once after top level translation only +

+

Some groups of one graph pattern become + Join(|Z|, |A|), where |Z| is the empty basic graph pattern (which is the empty set). These are replaced by |A|. The empty graph pattern |Z| is the identity for join:

 Replace Join(Z, A) by A
-Replace Join(A, Z) by A
-
+Replace Join(A, Z) by A
@@ -10464,6 +10575,12 @@

Evaluation Semantics

Definition: Evaluation of Filter

+ +

@@ Make current μ available. Or use current language in Exist. + Long term: change eval() to be arity three - 3rd argument is "current row". + editors note. +

+

eval( |D|(|G|), Filter(|F|, |P|) ) = Filter( |F|, eval(|D|(|G|), |P|), |D|(|G|) )

'substitute' is a filter function in support of the evaluation of @@ -10479,6 +10596,9 @@

Evaluation Semantics

Definition: Evaluation of Exists

+ +

@@ Update

+

Let μ be the current solution mapping for a filter and |P| a graph pattern:

The value exists(|P|), given |D|(|G|) is true if and only if eval( |D|(|G|), substitute(|P|, μ)) is @@ -10602,209 +10722,6 @@

Evaluation Semantics

eval( |D|(|G|), Slice(|L|, |start|, |length|) ) = Slice( eval(|D|(|G|), |L|), |start|, |length| )

- - -
-

Values Insertion and `EXISTS`

-
-

The following subsections contain draft material for a revised - "`exists`" operation. -

-

- Background: SPARQL CG SEP-0007 -

-
-
-

Syntax Restriction

-

Additional [[[#sparqlGrammar]]] note:

-
- Any variable that is assigned to in the graph pattern of `EXISTS`/`NOT EXISTS` must not be in-scope. - This applies to `BIND`, variables introduced by `AS` in a `SELECT` clause, variables in a `VALUES` - clause, and variables introduced by `AS` in `GROUP BY`. -
-

- Extend the "in-scope" rules to include the possible - variables that are in-scope for the current row: -

- - - - - - - - - - - -
Syntax FormIn-scope variables
`EXISTS` and `NOT EXISTS` filters v is in-scope if it is in-scope for the pattern to which the `FILTER` is applied. -
-
-

- This restriction means that inserted values - do not conflict with values assigned to variables within the pattern. -

-

- This operation is performed as part of query parsing. -

-
-
-
-

Remapping

-

- Remapping ensures that a variable name used inside a project expression, - but which is not part of the results of the evaluation of the project expression, - does not coincide with a variable mentioned anywhere else in the - algebra expression of a query. -

-

- Renaming these variables does not change the results of evaluating - the project expresssion. -

-
- Definition: Projection Expression Variable Remapping -

- For a projection algebra operation - `Project(A, PV)` acting on algebra expression `A` and with set of variables `PV`, - define a mapping `F` from `V`, - the set of all variables, - to `V` where: -

-
F(v) = v1 if v is not in PV, where v1 is a fresh variable
-F(v) = v if v is in PV
-

- Define the Projection Expression Variable Remapping `ProjectMap(P, PV)` -

-
ProjectMap(Project(A, PV)) = Project(A1, PV) 
-                                where A1 is the result of applying F
-                                to every variable mentioned in A.
-                
-

- The Projection Expression Variable Remapping yields an algrebra expression that - evaluates to the same results as the Project argument. No variable of `ProjectMap(Project(A, PV))` - that is not in `PV` is mentioned anywhere else in the algebra expression for the query. -

-
-

This process is applied throughout the graph pattern of EXISTS:

-
- Definition: Variable Remapping -

- For any algebra expression `X`, define the Variable Remapping `PrjMap(X)` - of algebra expression `X`: -

-
PrjMap(X) = replace all project operations Project(P, PV) 
-                with ProjectMap(Project(A, PV), PV) for each projection in X.
-
-

- The outcome of `PrjMap` is independent of the order of replacement - (whether "bottom-up" or "top-down") order. - Replacements may happen several times, depending on recursive order - but each time a replacement is made, the variable not used anywhere else. -

- -
-

- A variable inside a project expression that is not in the variables projected - is not affected by the values insertion operation because it is renamed apart. -

-

- This operation is as part of the translation to the SPARQL - algebra. -

-
-
-
-

Values Insertion

-

- Alternative 1: rewrite the algebra during - translation to the SPARQL algebra - to include a function that evaluates to the current row. -

- -
-
- Definition: Values In Scope -

- Define the function `BindingInScope()`. - Evaluation of `BindingInScope()` results in a table of one row, - being the current binding of the enclosing filter. -

-
-

Need to define `BindingInScope`

-
- -
-
- Definition: Access to the current binding -

- During translation to the SPARQL algebra -

-

Define putting in the `BindingInScope` operator

-
-Replace each occurence of Y where Y is one of
-          Basic Graph Pattern,
-          Property Path Patterns,
-          Graph(Var, pattern),
-          Inline Data
-      with join(Y, BindingInScope()).
-
-
- c.f. section Translate Graph Patterns - where an empty basic graph pattern start any - GroupGraphPattern. - It happens before the simplification step. -
-
- -
-

- Examples -

-
- -

- Alternative 2: rewrite the algebra during execution. This corresponds to the - original SEP-0007 proposal. -

-
-
- Definition: Insert Values -

- Define the function `InsertValues(X, μ)` -

-
Let Table(μ) = { μ } and multiplicity( μ | Table(μ) = { μ } ) = 1
-
-Replace each occurence of Y in X where Y is one of
-          Basic Graph Pattern,
-          Property Path Patterns,
-          Graph(Var, pattern),
-          Inline Data
-      with join(Y, Table(μ)).
- -
-
-
- -
-

Evaluation of EXISTS

-
- Definition: Evaluation of Exists -

- Let `μ` be the current solution mapping for a filter, and `X` a graph pattern, - define the Evaluation of Exists `exists(X)` -

-

- @@ Set up the current row for `BindingdInScope`. -

-
exists(X) = true 
-              if eval( D(G), μ)
-                 is a non-empty solution sequence.
-exists(X) = false otherwise
-
-
-
- -