diff --git a/.linkspector.yml b/.linkspector.yml index 8c92f247e..c7283d2f3 100644 --- a/.linkspector.yml +++ b/.linkspector.yml @@ -20,6 +20,7 @@ ignorePatterns: # Not crawling the following due to persistent HTTP 503 errors - pattern: '^https://sqlite.org' + - pattern: '^https://github.com/apache/spark/blob/master/common/variant/README.md' # Not crawling the following due to a Cloudflare challenge that causes HTTP # 429/403 errors during link checking diff --git a/book/src/SUMMARY.md b/book/src/SUMMARY.md index 8cb3d7226..3be9b8ef2 100644 --- a/book/src/SUMMARY.md +++ b/book/src/SUMMARY.md @@ -62,6 +62,7 @@ - [Sets](super-sql/types/set.md) - [Maps](super-sql/types/map.md) - [Unions](super-sql/types/union.md) + - [Fusions](super-sql/types/fusion.md) - [Enums](super-sql/types/enum.md) - [Errors](super-sql/types/error.md) - [Named Types](super-sql/types/named.md) diff --git a/book/src/super-sql/types/fusion.md b/book/src/super-sql/types/fusion.md new file mode 100644 index 000000000..b3a4cabd5 --- /dev/null +++ b/book/src/super-sql/types/fusion.md @@ -0,0 +1,158 @@ +# Fusions + +A _fusion value_ is a value of one type, called the fusion's _supertype_, +together with the type of the value it represents, called its _subtype_. +Fusions let values of many different types be stored and processed as +values of a single type without losing their original types. + +Fusions conform with the definition of the +[fusion type](../../formats/model.md#28-fusion) in the super-structured data +model and follow the [syntax](../../formats/sup.md#259-fusion-type) of +fusions in the [SUP format](../../formats/sup.md), i.e., a fusion type has +the form +``` +fusion( ) +``` +where `` is the supertype, and a fusion value has the form +``` +fusion( , ) +``` +where `` is a value of the supertype and `` is its subtype, +e.g., +``` +fusion(1::(int64|string),) +``` + +## Why Fusions + +[Type fusion](../type-fusion.md) merges a set of types into one type that +can represent all of them, e.g., the records `{a:1}` and `{b:"x"}` fuse into +the type `{a:int64|missing,b:string|missing}`. A plain fused type, as computed +by the [fuse](../aggregates/fuse.md) aggregate function, is lossy: once the +records are cast to it, there is no way to tell which fields each record +originally had. + +A fusion type keeps that information. Converting the records to a fusion +type stores each one as a value of the single fused type along with its +original type, so the records can be processed together as one type and +still be recovered exactly. This is what lets SuperDB statically +[type check](../intro.md) queries over heterogeneous data and store it +efficiently in columnar form, while every value keeps its own type. + +## Creating Fusion Values + +The [superfuse](../aggregates/superfuse.md) aggregate function computes +the fusion type of its input: +```mdtest-spq {data-layout="stacked"} +# spq +aggregate superfuse(this) +# input +{a:1} +{b:"x"} +# expected output +<{a:fusion(int64|missing),b:fusion(string|missing)}> +``` +and the [superfuse](../operators/superfuse.md) operator converts all of its +input to that type. A value can also be converted to a fusion type with +[upcast](../functions/types/upcast.md), and the +[fusion_type](../functions/types/fusion_type.md) function forms a fusion type +from any type. + +Casting a value to the type `any` produces a value of type `fusion(all)`, +which holds a value of any type along with its type. In SUP, such a value is +written `::any`. + +## Displaying Fusion Values + +Fusion values are displayed as their underlying values by default, i.e., +fusion values in query results are automatically +[defused](../functions/types/superdefuse.md): +```mdtest-spq +# spq +superfuse +# input +{a:1} +{b:"x"} +# expected output +{a:1} +{b:"x"} +``` +The `-fusion` option of [super](../../command/super.md) displays the fusion +values themselves: +```mdtest-command +super -fusion -s -c "values {a:1},{b:'x'} | superfuse" +``` +```mdtest-output +{a:fusion(1::(int64|missing),),b:fusion(missing::(string|missing),)} +{a:fusion(missing::(int64|missing),),b:fusion("x"::(string|missing),)} +``` + +## Fusion Value Semantics + +A fusion value behaves like the value it represents. +[typeof](../functions/types/typeof.md), [kind](../functions/types/kind.md), +[is](../functions/types/is.md), +[type assertions](../expressions/type-assertions.md), and +[match](../expressions/match.md) all see the subtype, while +[supertypeof](../functions/types/supertypeof.md) returns the fusion type: +```mdtest-spq {data-layout="stacked"} +# spq +superfuse +| values {typeof:typeof(a), supertypeof:supertypeof(a), is_int:is(a,)} +# input +{a:1} +{a:"x"} +# expected output +{typeof:,supertypeof:,is_int:true} +{typeof:,supertypeof:,is_int:false} +``` + +Accessing a field of a fused record yields the field's original value, and +a field that was absent from the original record is +[missing](none.md#missing), so the type checker requires the query to +handle it, e.g., with `?.` and `??`: +```mdtest-spq +# spq +superfuse | values {a:this?.a ?? 0, b:this?.b ?? ""} +# input +{a:1} +{b:"x"} +# expected output +{a:1,b:""} +{a:0,b:"x"} +``` + +The type checker checks expressions against the supertype, since that is +the type the query sees for every value. For example, `a+1` is an error when +`a` has type `fusion(int64|string)` because `a` might be a string, so the query +must say what to do, e.g., with a type assertion: +```mdtest-spq +# spq +superfuse | values (a.(int64) ?? 0) + 1 +# input +{a:1} +{a:"x"} +# expected output +2 +1 +``` + +## Defusing + +The [superdefuse](../functions/types/superdefuse.md) function converts +fusion values back into their original values, and +[defuse](../functions/types/defuse.md) undoes the unions and optional +fields of a plain fused type. Since query results are automatically +defused for display, these functions are mainly useful within a query that +needs the original values themselves rather than fusion values, e.g., so that +`supertypeof` returns the original type: +```mdtest-spq +# spq +superfuse | values supertypeof(superdefuse(this)) +# input +{a:1} +{b:"x"} +# expected output +<{a:int64}> +<{b:string}> +``` diff --git a/book/src/super-sql/types/intro.md b/book/src/super-sql/types/intro.md index 3bb2a8d82..c45a22462 100644 --- a/book/src/super-sql/types/intro.md +++ b/book/src/super-sql/types/intro.md @@ -6,6 +6,7 @@ comprising [primitive types](../../formats/model.md#1-primitive-types), [complex types](../../formats/model.md#2-complex-types), [sum types](union.md), +[fusion types](fusion.md), [named types](named.md), the [null type](null.md), the [none and missing types](none.md), diff --git a/book/src/super-sql/types/union.md b/book/src/super-sql/types/union.md index a22530229..8987f4388 100644 --- a/book/src/super-sql/types/union.md +++ b/book/src/super-sql/types/union.md @@ -58,34 +58,56 @@ the result might be `null`. In other languages, the _discriminant_ is the type name, e.g., `u.(int64)`. -However, SuperSQL is polymorphic so there is no requirement to explicitly -discriminate the member type of a union value. When an expression -operator or function references a union value in computation, -then the underlying value in its member type is _automatically_ expressed -from the union value. +SuperSQL uses the type name as the discriminant: a +[type assertion](../expressions/type-assertions.md) like `u.(int64)` yields +the underlying value when `u` holds an `int64` and `none` otherwise. + +Many functions and operators accept union values directly and operate on +the underlying value, e.g., [upper](../functions/strings/upper.md) applied +to `"foo"::(int64|string)` is `"FOO"`. +[Arithmetic](../expressions/arithmetic.md#type-checking) and +[comparisons](../expressions/comparisons.md#type-checking) are type checked +against every type in the union, though. When every type works with the +other operand, the union value is used as is, e.g., this predicate is true +```mdtest-spq {data-layout='no-labels'} {style='margin:auto;width:85%'} +# spq +values 1::(int64|float64)==1 +# input -For example, this predicate is true +# expected output +true ``` +but when some type in the union doesn't work, it's a type error, because the +query hasn't said what to do with a value of that type: +```mdtest-spq fails {data-layout='no-labels'} {style='margin:auto;width:85%'} +# spq values 1::(int64|string)==1 -``` -because the union value is automatically expressed as `1::int64` -by the comparison operator. +# input -Likewise +# expected output +type mismatch: int64|string == int64 at line 1, column 8: +values 1::(int64|string)==1 + ~~~~~~~~~~~~~~~~~~~~ ``` -values 1::(int64|string)+2::(int64|string) +A type assertion narrows the union to the type that works, and the query +must then say what to do when the value has another type, e.g., with `??`: +```mdtest-spq {data-layout='no-labels'} {style='margin:auto;width:85%'} +# spq +values (this.(int64) ?? 0) + 1 +# input +1::(int64|string) +"foo"::(int64|string) +# expected output +2 +1 ``` -results in `3::int64`. Note that because of automatic expression, -the union type is not retained here. -Passing a union value to a function, however, does not involve -evaluation and thus automatic expression does not occur here, e.g., +The [typeof](../functions/types/typeof.md) function, however, returns the +union type itself rather than the type of the underlying value, e.g., ``` values typeof(1::(int64|string)) ``` -is `` because the union value is _not_ automatically -expressed as `1::int64` when it is passed to the -[typeof](../functions/types/typeof.md) function. +is ``. When desired, the [`under`](../functions/generics/under.md) function may be used to express the underlying value explicitly. For example, @@ -97,55 +119,38 @@ results in ``. ## Union Dispatch Languages with sum types often include a construct to dispatch the -union to a case for each of its possible types. - -Because SuperSQL is polymorphic, union dispatch is not generally needed. -Instead, union values are simply operated upon and the "right thing happens". -The exceptions are [arithmetic](../expressions/arithmetic.md#type-checking), -which requires a union whose types aren't all numbers to be narrowed first, -and [comparisons](../expressions/comparisons.md#type-checking), which require -each type of a union to be comparable with the other operand. - -That said, union dispatch may be accomplished with the -[`switch`](../operators/switch.md) operator or a -[`case`](../expressions/conditional.md) expression. - -For example, `switch` can be used to route union values to different -branches of a query: -``` +union to a case for each of its possible types. In SuperSQL, this is the +[match](../expressions/match.md) expression, which selects a result +according to the type of a value. Within each case, `this` is the +underlying value with the matched type, so it can be operated on without +a type error: +```mdtest-spq {data-layout="stacked"} +# spq values - {u:1::(int64|string|ip)}, - {u:"foo"::(int64|string|ip)}, - {u:192.168.1.1::(int64|string|ip)} -| switch typeof(under(u)) - case ( ... ) - case ( ... ) - case ( ... ) - default ( values error({message: "unknown type", on:this}) ) -| ... -``` ->[!NOTE] -> Note the presence of a default case above. In statically typed languages with -> sum types, the compiler can ensure that all possible cases for a union are covered -> and report an error otherwise. In this case, there would be no need for a default. -> A future version of SuperSQL will include more comprehensive compile-time type -> checking and will include a mechanism for explicit union dispatch with -> static type checking. - -A `case` expression can also be used to dispatch union values inside of -an expression as in + match u + when int64 then this+1 + when string then upper(this) + when ip then network_of(this) + end +# input +{u:1::(int64|string|ip)} +{u:"foo"::(int64|string|ip)} +{u:192.168.1.1::(int64|string|ip)} +# expected output +2 +"FOO" +192.168.1.0/24 ``` -values - {u:1::(int64|string|ip)}, - {u:"foo"::(int64|string|ip)}, - {u:192.168.1.1::(int64|string|ip)} -| values - case typeof(under(u)) - when then u+1 - when then upper(u) - when then network_of(u) - else "unknown" - end +A value that matches no case yields `none`, or the result of an +`else` clause if there is one. + +To route union values to different branches of a query instead, use the +[`switch`](../operators/switch.md) operator, e.g., +``` +switch typeof(under(u)) + case ( ... ) + case ( ... ) + case ( ... ) ``` ## Examples