You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spec/02-syntax.md
+6-1Lines changed: 6 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,13 @@
1
1
# Syntax
2
2
3
-
A GROQ query is a string consisting of Unicode characters. The encoding of the query string is implementation-defined, but UTF-8 is the preferred choice. A query consist of a single {Expression}, with {WhiteSpace} and {Comment} allowed anywhere with no effect on the interpretation.
3
+
A GROQ query is a string consisting of Unicode characters. The encoding of the query string is implementation-defined, but UTF-8 is the preferred choice.
4
4
5
5
SourceCharacter : "any Unicode character"
6
6
7
+
A query consist of a single {Expression} optionally preceded by a list of {FuncDecl}, with {WhiteSpace} and {Comment} allowed anywhere with no effect on the interpretation.
8
+
9
+
Query : FuncDecl\* Expression
10
+
7
11
## JSON Superset
8
12
9
13
GROQ's syntax is a superset of JSON, so any valid JSON value is a valid GROQ expression (that simply returns the given value). Below are a few examples of JSON values:
Copy file name to clipboardExpand all lines: spec/03-execution.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,6 +42,7 @@ A query context consists of:
42
42
- the dataset
43
43
- parameter values (map from {string} to {value})
44
44
- the mode: either "normal" or "delta"
45
+
- custom function definitions (map from {string} to {function})
45
46
46
47
If the mode is "delta" then the query context also has:
47
48
@@ -55,6 +56,7 @@ A scope consists of:
55
56
- a this value
56
57
- an optional parent scope
57
58
- a query context
59
+
- a map of parameters
58
60
59
61
A root scope can be constructed from a query context, and a nested scope can be constructed from an existing scope.
60
62
@@ -64,6 +66,7 @@ NewNestedScope(value, scope):
64
66
- Set the this value of {newScope} to {value}.
65
67
- Set the parent scope of {newScope} to {scope}.
66
68
- Set the query context of {newScope} to the query context of {scope}.
69
+
- Set the parameters of {newScope} to the parameter of {scope}.
67
70
- Return {newScope}.
68
71
69
72
NewRootScope(context):
@@ -72,6 +75,7 @@ NewRootScope(context):
72
75
- Set the this value of {newScope} to {null}.
73
76
- Set the parent scope of {newScope} to {null}.
74
77
- Set the query context of {newScope} to {context}.
78
+
- Set the parameters to the parameters given to the query request.
75
79
- Return {newScope}.
76
80
77
81
## Expression validation
@@ -83,6 +87,11 @@ Validate(expr):
83
87
- Let {validator} be the validator of {expr}.
84
88
- Execute the {validator}.
85
89
90
+
## Query validation
91
+
92
+
A query is validated by first validating the custom functions and then [validating the expression](#sec-Expression-validation).
93
+
Custom functions are validated by validating the function bodies.
94
+
86
95
## Expression evaluation
87
96
88
97
An expression is evaluated in a scope. You must successfully validate an expression before you attempt to evaluate it. Every expression type has their own evaluator function in their respective section in this specification (e.g. the evaluator of {ParenthesisExpression} is {EvaluateParenthesis()}).
Copy file name to clipboardExpand all lines: spec/06-simple-expressions.md
+42-4Lines changed: 42 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -75,10 +75,34 @@ EvaluateParent(scope):
75
75
- Decrease {level} by one.
76
76
- Return the this value of {currentScope}.
77
77
78
+
## Parameter expression
79
+
80
+
A parameter expression returns the value of a parameter
81
+
82
+
```example
83
+
*[_type == $type]
84
+
~~~~~
85
+
```
86
+
87
+
Parameter : `$` Identifier
88
+
89
+
EvaluateParameter(scope):
90
+
91
+
- Let {name} be the string value of the {Identifier}.
92
+
- Return the value of the parameter in {scope}.
93
+
94
+
ValidateParameter():
95
+
96
+
- Let {name} be the string value of the {Identifier}.
97
+
- If the parameter doesn't exist in the current validation context:
98
+
- Report an error.
99
+
78
100
## Function call expression
79
101
80
102
GROQ comes with a set of built-in functions which provides additional features. See the ["Functions"](#sec-Functions) for available functions and their namespaces.
81
103
104
+
Custom GROQ functions can be defined to extend or override the built-in function set. See [Custom functions](12-custom-functions.md) for details.
105
+
82
106
```example
83
107
*{"score": round(score, 2)}
84
108
~~~~~~~~~~~~~~~
@@ -106,17 +130,31 @@ EvaluateFuncCall(scope):
106
130
- For each {Expression} in {FuncCallArgs}:
107
131
- Let {argumentNode} be the {Expression}.
108
132
- Append {argumentNode} to {args}.
109
-
- Let {func} be the function defined under the name {name} in either {namespace} namespace if provided, or the `global` namespace.
110
-
- Return the result of {func(args, scope)}.
133
+
- If the query context of {scope} has a function defined with the given {name} and {namespace}:
134
+
- Let {funcBody} be body of the custom function.
135
+
- Let {context} be the query context of {scope}.
136
+
- Let {newScope} be the result of {NewRootScope(context)}.
137
+
- For each {param} in the parameter list of the custom function:
138
+
- Let {argNode} be next {Expression} in {FuncCallArgs}.
139
+
- Let {arg} be the result of {Evaluate(argNode, scope)}.
140
+
- Set the parameter named {param} in {newScope} to {arg}.
141
+
- Return the result of {Evaluate(funcBody, newScope)}
142
+
- Otherwise:
143
+
- Let {func} be the function defined under the name {name} in either {namespace} namespace if provided, or the `global` namespace.
144
+
- Return the result of {func(args, scope)}.
111
145
112
146
ValidateFuncCall():
113
147
148
+
- Let {name} be the string value of the {FuncIdentifier}.
114
149
- Let {namespace} be the string value of the {FuncNamespace}.
150
+
- Let {args} be an array of the {Expression}s in {FuncCallArgs}.
151
+
- If there's a custom function defined with the given {name} and {namespace}:
152
+
- For each {arg} in {args}:
153
+
- Execute {Validate(arg)}.
154
+
- Stop.
115
155
- If there is no namespace named {namespace}:
116
156
- Stop and report an error.
117
-
- Let {name} be the string value of the {FuncIdentifier}.
118
157
- If there is no function named {name} defined in either {namespace} namespace if provided, or the `global` namespace:
119
158
- Stop and report an error.
120
-
- Let {args} be an array of the {Expression}s in {FuncCallArgs}.
121
159
- Let {validator} be the validator for the function under the name {name}.
Copy file name to clipboardExpand all lines: spec/GROQ.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ _Current Working Draft_
5
5
This is the specification for GROQ (**G**raph-**R**elational **O**bject **Q**ueries), a query language and execution engine made at Sanity, Inc, for filtering and projecting JSON documents. The work started in 2015. The development of this open standard started in 2019.
6
6
7
7
GROQ is authored by [Alexander Staubo](https://twitter.com/purefiction) and [Simen Svale Skogsrud](https://twitter.com/svale).
8
-
Additional follow up work by [Erik Grinaker](https://twitter.com/erikgrinaker), [Magnus Holm](https://twitter.com/judofyr), [Radhe](https://github.com/j33ty), [Israel Roldan](https://github.com/israelroldan), [Sindre Gulseth](https://github.com/sgulseth), [Matt Craig](https://github.com/codebymatt), [Espen Hovlandsdal](https://github.com/rexxars).
8
+
Additional follow up work by [Erik Grinaker](https://twitter.com/erikgrinaker), [Magnus Holm](https://twitter.com/judofyr), [Radhe](https://github.com/j33ty), [Israel Roldan](https://github.com/israelroldan), [Sindre Gulseth](https://github.com/sgulseth), [Matt Craig](https://github.com/codebymatt), [Espen Hovlandsdal](https://github.com/rexxars), [Tonina Zhelyazkova](https://github.com/tzhelyazkova).
9
9
10
10
This specification should be considered _work in progress_ until the first release.
11
11
@@ -62,3 +62,5 @@ Conformance requirements expressed as algorithms can be fulfilled by an implemen
0 commit comments