Skip to content

Commit c498202

Browse files
authored
Edits to super-sql/declarations docs (#6455)
1 parent e651c33 commit c498202

File tree

8 files changed

+49
-39
lines changed

8 files changed

+49
-39
lines changed

book/src/super-sql/declarations/constants.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const <id> = <expr>
77
where `<id>` is an [identifier](../queries.md#identifiers)
88
and `<expr>` is a constant [expression](../expressions/intro.md)
99
that must evaluate at compile time without referencing any
10-
runtime state such as `this` or a field of `this`.
10+
runtime state such as [this](../intro.md#pipe-scoping) or a field of `this`.
1111

1212
Constant declarations must appear in the declaration section of a
1313
[scope](../queries.md#scope).

book/src/super-sql/declarations/functions.md

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ New functions are declared with the syntax
55
fn <id> ( [<param> [, <param> ...]] ) : <expr>
66
```
77
where
8-
* `<id>` is an identifier representing the name of the function,
8+
* `<id>` is an [identifier](../queries.md#identifiers) representing the name of the function,
99
* each `<param>` is an identifier representing a positional argument to the function, and
1010
* `<expr>` is any [expression](../expressions/intro.md) that implements the function.
1111

@@ -14,9 +14,9 @@ Function declarations must appear in the declaration section of a [scope](../que
1414
The function body `<expr>` may refer to the passed-in arguments by name.
1515

1616
Specifically, the references to the named parameters are
17-
field references of the special value `this`, as in any expression.
17+
field references of the [special value](../intro.md#pipe-scoping) `this`, as in any expression.
1818
In particular, the value of `this` referenced in a function body
19-
is formed as record from the actual values passed to the function
19+
is formed as a [record](../types/record.md) from the actual values passed to the function
2020
where the field names correspond to the parameters of the function.
2121

2222
For example, the function `add` as defined by
@@ -25,21 +25,21 @@ fn add(a,b): a+b
2525
```
2626
when invoked as
2727
```
28-
values {x:1} | values add(x,1)
28+
values {x:1} | values add(x,2)
2929
```
3030
is passed the record the `{a:x,b:1}`, which after resolving `x` to `1`,
31-
is `{a:1,b:1}` and thus evaluates the expression
31+
is `{a:1,b:2}` and thus evaluates the expression
3232
```
3333
this.a + this.b
3434
```
35-
which results in `2`.
35+
which results in `3`.
3636

37-
Any function-as-value arguments passed to a function do not appear in the `this`
37+
Any [function references](../expressions/functions.md#function-references) passed to a function do not appear in the `this`
3838
record formed from the parameters. Instead, function values are expanded at their
39-
call sites in a macro-like fashion.
39+
call sites in a [macro](https://en.wikipedia.org/wiki/Macro_(computer_science))-like fashion.
4040

4141
Functions may be recursive. If the maximum call stack depth is exceeded,
42-
the function returns an error value indicating so. Recursive functions that
42+
the function returns an [error](../types/error.md) value indicating so. Recursive functions that
4343
run for an extended period of time without exceeding the stack depth will simply
4444
be allowed to run indefinitely and stall the query result.
4545

@@ -56,7 +56,7 @@ fn <id> ( [<param> [, <param> ...]] ) : (
5656
where `<query>` is any [query](../queries.md) and is simply wrapped in parentheses
5757
to form the subquery.
5858

59-
As with any subquery, when multiple results are expected, an array subquery
59+
As with any subquery, when multiple results are expected, an [array subquery](../expressions/subqueries.md#array-subqueries)
6060
may be used by wrapping `<query>` in square brackets instead of parentheses:
6161
```
6262
fn <id> ( [<param> [, <param> ...]] ) : [
@@ -67,24 +67,35 @@ fn <id> ( [<param> [, <param> ...]] ) : [
6767
Note when using a subquery expression in this fashion,
6868
the function's parameters do not appear in the scope of the expressions
6969
embedded in the query. For example, this function results in a type error:
70-
```
70+
```mdtest-spq fails {data-layout='no-labels'} {style='margin:auto;width:85%'}
71+
# spq
7172
fn apply(a,val): (
7273
unnest a
73-
| collect(this+val))
74+
| collect(this+val)
7475
)
7576
values apply([1,2,3], 1)
77+
# input
78+
79+
# expected output
80+
"val" no such field at line 3, column 18:
81+
| collect(this+val)
82+
~~~
7683
```
7784
because the field reference to `val` within the subquery does not exist.
78-
Instead the parameter `val` can be carried into the subquery using the
79-
alternative form of [unnest](../operators/unnest.md):
80-
```
85+
Instead the parameter `val` can be carried into the subquery using
86+
the compound form of [unnest](../operators/unnest.md):
87+
```mdtest-spq {data-layout='no-labels'} {style='margin:auto;width:85%'}
88+
# spq
8189
fn apply(a,val): (
8290
unnest {outer:val,item:a}
8391
| collect(outer+item)
8492
)
8593
values apply([1,2,3], 1)
94+
# input
95+
96+
# expected output
97+
[2,3,4]
8698
```
87-
See the example below.
8899

89100
### Examples
90101

@@ -139,7 +150,7 @@ values stats(a)
139150
{avg:4.,min:4,max:4,mode:4}
140151
```
141152
---
142-
_Function arguments are actually fields in the "this" record_
153+
_Function arguments are actually fields in the `this` record_
143154

144155
```mdtest-spq
145156
# spq
@@ -151,7 +162,7 @@ values that(x,y,3)
151162
{a:1,b:2,c:3}
152163
```
153164
---
154-
_Functions passed as values do not appear in the "this" record_
165+
_Functions passed as values do not appear in the `this` record_
155166

156167
```mdtest-spq
157168
# spq

book/src/super-sql/declarations/intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Declarations may be created for
1212
* [operators](operators.md), or
1313
* [pragmas](pragmas.md).
1414

15-
All of the names defined in a given scope are available to other declarations defined
15+
With the exception of types that reference other types, all of the names defined in a given scope are available to other declarations defined
1616
in the same scope (as well as containing scopes) independent of the order of declaration,
1717
i.e., a declaration may forward-reference another declaration that is defined in the
1818
same scope.

book/src/super-sql/declarations/operators.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ op <name> [<param> [, <param> ...]] : (
99
where
1010
* `<name>` is an [identifier](../queries.md#identifiers)
1111
representing the name of the new operator,
12-
* each `<param>` is an [identifier](../queries.md#identifiers)
12+
* each `<param>` is an identifier
1313
representing a positional parameter to the operator, and
1414
* `<query>` is any [query](../queries.md).
1515

@@ -20,7 +20,7 @@ Operator declarations must appear in the declaration section of a [scope](../que
2020
A declared operator is invoked by its name
2121
using the [call](../operators/intro.md#call) keyword.
2222
Operators can be invoked without the `call` keyword as a shortcut when such use
23-
is unambiguous with the built-in operators.
23+
is unambiguous with the [built-in operators](../operators/intro.md).
2424

2525
A called instance of a declared operator consumes input, operates on that input,
2626
and produces output. The body of the
@@ -43,7 +43,7 @@ In contrast to function calls, where the arguments are evaluated at the call sit
4343
and values are passed to the function, operator arguments are instead passed to the
4444
operator body as an expression _template_ and the expression is evaluated in the
4545
context of the operator body. That said, any other declared identifiers referenced
46-
by these expressions (e.g., constants, functions, named queries, etc.) are bound to
46+
by these expressions (e.g., [constants](constants.md), [functions](functions.md), [named queries](queries.md), etc.) are bound to
4747
those entities using the lexical scope where they are defined rather than the
4848
scope of their expanded use in the operator's definition.
4949

@@ -58,14 +58,14 @@ the operator expressions here are not generators and do not implement backtracki
5858

5959
---
6060

61-
_Trivial operator that echoes its input_
61+
_Trivial operator that echoes its input, invoked with explicit `call`_
6262

6363
```mdtest-spq
6464
# spq
6565
op echo: (
6666
values this
6767
)
68-
echo
68+
call echo
6969
# input
7070
{x:1}
7171
# expected output
@@ -74,7 +74,7 @@ echo
7474

7575
---
7676

77-
_Simple example that adds a new field to inputs records_
77+
_Simple example that adds a new field to input records_
7878

7979
```mdtest-spq
8080
# spq

book/src/super-sql/declarations/pragmas.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pragma <id> = <expr>
88
where `<id>` is an [identifier](../queries.md#identifiers)
99
and `<expr>` is a constant [expression](../expressions/intro.md)
1010
that must evaluate at compile time without referencing any
11-
runtime state such as `this` or a field of `this`.
11+
runtime state such as [this](../intro.md#pipe-scoping) or a field of `this`.
1212

1313
Pragmas must appear in the declaration section of a [scope](../queries.md#scope).
1414

book/src/super-sql/declarations/queries.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let <name> = ( <query> )
77
where `<name>` is an [identifier](../queries.md#identifiers)
88
and `<query>` is any standalone [query](../queries.md) that sources its own input.
99

10-
Named queries are similar to [common-table expressions (CTE)](../sql/with.md)
10+
Named queries are similar to SQL [common-table expressions (CTE)](../sql/with.md)
1111
and may be likewise invoked in a [from](../operators/from.md) operator by referencing
1212
the query's name, as in
1313
```
@@ -25,12 +25,11 @@ As with any expression subquery, multiple values result in an error, so when
2525
this is expected, the query reference may be enclosed in brackets to form
2626
an array subquery.
2727

28-
To create a query that can be used as an operator and thus can operate on its input,
29-
declare an [operator](operators.md).
28+
To create a query that can operate on its input, declare an [operator](operators.md).
3029

3130
A common use case for a named query is to compute a complex query that returns a scalar,
32-
then embedding that scalar result in an expression. Even though the named query
33-
appears syntactically as a sub-query in this case, the result is efficient
31+
then embedding that scalar result in an [expression](../expressions/intro.md). Even though the named query
32+
appears syntactically as a subquery in this case, the result is efficient
3433
because the compiler will materialize the result and reuse it on each invocation.
3534

3635
### Examples

book/src/super-sql/declarations/types.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ Named types are declared with the syntax
44
```
55
type <id> = <type>
66
```
7-
where `<id>` is an identifier and `<type>` is a [type](../types/intro.md).
7+
where `<id>` is an [identifier](../queries.md#identifiers) and `<type>` is a [type](../types/intro.md).
88
This creates a new type with the given name in the type system.
99

1010
Type declarations must appear in the declaration section of a [scope](../queries.md#scope).
1111

1212
Any named type that appears in the body of a type declaration must be previously
13-
declared in the same scope or in an ancestor scope, i.e., types cannot contained
13+
declared in the same scope or in an ancestor scope, i.e., types cannot contain
1414
forward references to other named types. In particular, named types cannot be recursive.
1515

1616
>[!NOTE]
1717
> A future version of SuperSQL may include recursive types. This is a research topic
1818
> for the SuperDB project.
1919
20-
Input data may create named types that conflict with type declarations. In this case,
20+
Input data may create [named types](../../formats/model.md#3-named-type) that conflict with type declarations. In this case,
2121
a reference to a declared type in the query text uses the type definition of the nearest
2222
containing scope that binds the type name independent of types in the input.
2323

24-
When a named type is referenced as a string argument to cast, then any type definition
25-
with that name is ignored and the named type is bound to the type of the argument of cast.
24+
When a named type is referenced as a string argument to [cast](../functions/types/cast.md), then any type definition
25+
with that name is ignored and the named type is bound to the type of the first argument of `cast`.
2626
This does not affect the binding of the type used in other expressions in the query text.
2727

2828
Types can also be bound to identifiers without creating a named type using a
@@ -78,7 +78,7 @@ true
7878

7979
---
8080

81-
_A type name argument to cast in the form of a string is independent type declarations_
81+
_A type name argument to `cast` in the form of a string is independent of type declarations_
8282

8383
```mdtest-spq
8484
# spq

book/src/super-sql/operators/unnest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ If `<expr>` is a record, it must have two fields of the form:
4747
{<first>: <any>, <second>:<array>}
4848
```
4949
where `<first>` and `<second>` are arbitrary field names, `<any>` is any
50-
SuperSQL value, and `<array>` is an array value. In this case, the derived
50+
SuperSQL value, and `<array>` is an array value. In this compound form of `unnest`, the derived
5151
sequence has the form:
5252
```
5353
{<first>: <any>, <second>:<elem0>}

0 commit comments

Comments
 (0)