Trying to get a field from the structure. If it's not found among the fields or null in the structure value, use the default value.
Arguments:
Default value.
$struct = <|a:1|>;
SELECT
TryMember(
$struct,
"a",
123
) AS a, -- 1
TryMember(
$struct,
"b",
123
) AS b; -- 123
Adding one or more new fields to the structure.
If the field set contains duplicate values, an error is returned.
Arguments:
$struct = <|a:1|>;
SELECT
ExpandStruct(
$struct,
2 AS b,
"3" AS c
) AS abc;
Adding one new field to the structure. If you need to add multiple fields, better use ExpandStruct.
If the field set contains duplicate values, an error is returned.
Arguments:
$struct = <|a:1|>;
SELECT
AddMember(
$struct,
"b",
2
) AS ab;
Removing a field from the structure.
If the entered field hasn't existed, an error is returned.
Arguments:
$struct = <|a:1, b:2|>;
SELECT
RemoveMember(
$struct,
"b"
) AS a;
Removing a field from the structure.
If the entered field hasn't existed, unlike RemoveMember, the error is not returned.
Arguments:
$struct = <|a:1, b:2|>;
SELECT
ForceRemoveMember(
$struct,
"c"
) AS ab;
Selecting fields with specified names from the structure.
If any of the fields haven't existed, an error is returned.
Arguments:
$struct = <|a:1, b:2, c:3|>;
SELECT
ChooseMembers(
$struct,
["a", "b"]
) AS ab;
Excluding fields with specified names from the structure.
If any of the fields haven't existed, an error is returned.
Arguments:
$struct = <|a:1, b:2, c:3|>;
SELECT
RemoveMembers(
$struct,
["a", "b"]
) AS c;
Excluding fields with specified names from the structure.
If any of the fields haven't existed, it is ignored.
Arguments:
$struct = <|a:1, b:2, c:3|>;
SELECT
ForceRemoveMembers(
$struct,
["a", "b", "z"]
) AS c;
Combining the fields from multiple structures into a new structure.
If the resulting field set contains duplicate values, an error is returned.
Arguments: two or more structures.
$struct1 = <|a:1, b:2|>;
$struct2 = <|c:3|>;
SELECT
CombineMembers(
$struct1,
$struct2
) AS abc;
Combining the fields from multiple new structures into another new structure with prefix support.
If the resulting field set contains duplicate values, an error is returned.
Arguments: two or more tuples of two items: prefix and structure.
$struct1 = <|a:1, b:2|>;
$struct2 = <|c:3|>;
SELECT
FlattenMembers(
AsTuple("foo", $struct1), -- fooa, foob
AsTuple("bar", $struct2) -- barc
) AS abc;
Returns an unordered list of field names (possibly removing one Optional level) for a single argument that is a structure. For the NULL
argument, an empty list of strings is returned.
Argument: structure
$struct = <|a:1, b:2|>;
SELECT
StructMembers($struct); -- ['a', 'b']
Renames the fields in the structure passed. In this case, you can rename a source field into multiple target fields. All fields not mentioned in the renaming as source names are moved to the result structure. If some source field is omitted in the rename list, an error is returned. For an Optional structure or NULL
, the result has the same type.
Arguments:
$struct = <|a:1, b:2|>;
SELECT
RenameMembers($struct, [('a', 'c'), ('a', 'e')]); -- (b:2, c:1, e:1)
Renames the fields in the structure passed. In this case, you can rename a source field into multiple target fields. All fields not mentioned in the renaming as source names are moved to the result structure. If some source field is omitted in the rename list, the name is ignored. For an Optional structure or NULL
, the result has the same type.
Arguments:
$struct = <|a:1, b:2|>;
SELECT
ForceRenameMembers($struct, [('a', 'c'), ('d', 'e')]); -- (b:2, c:1)
Returns an unordered list of tuples including the field name and value. For the NULL
argument, EmptyList
is returned. It can be used only in the cases when the types of items in the structure are the same or compatible. Returns an optional list for an optional structure.
Argument: structure
$struct = <|a:1, b:2|>;
SELECT
GatherMembers($struct); -- [('a', 1), ('b', 2)]
Creates a structure with a specified list of fields and applies a specified list of edits to it in the format (field name, field value). All types of fields in the resulting structure are the same and equal to the type of values in the update list with added Optional (unless they are optional already). If the field wasn't mentioned among the list of updated fields, it's returned as NULL
. Among all updates for a field, the latest one is written. If the update list is Optional or NULL
, the result has the same type. If the list of edits includes a field that is not in the list of expected fields, an error is returned.
Arguments:
SELECT
SpreadMembers([('a',1),('a',2)],['a','b']); -- (a: 2, b: null)
Creates a structure with a specified list of fields and applies to it the specified list of updates in the format (field name, field value). All types of fields in the resulting structure are the same and equal to the type of values in the update list with added Optional (unless they are optional already). If the field wasn't mentioned among the list of updated fields, it's returned as NULL
. Among all updates for a field, the latest one is written. If the update list is optional or equal to NULL
, the result has the same type. If the list of updates includes a field that is not in the list of expected fields, this edit is ignored.
Arguments:
SELECT
ForceSpreadMembers([('a',1),('a',2),('c',100)],['a','b']); -- (a: 2, b: null)
Combine two structures using one of the four methods (using the provided lambda to merge fields with the same name):
StructUnion
adds all fields of both of the structures to the result.StructIntersection
adds only the fields which are present in both of the structures.StructDifference
adds only the fields of left
, which are absent in right
.StructSymmetricDifference
adds all fields that are present in exactly one of the structures.StructUnion(left:Struct<...>, right:Struct<...>[, mergeLambda:(name:String, l:T1?, r:T2?)->T])->Struct<...>
StructIntersection(left:Struct<...>, right:Struct<...>[, mergeLambda:(name:String, l:T1?, r:T2?)->T])->Struct<...>
StructDifference(left:Struct<...>, right:Struct<...>)->Struct<...>
StructSymmetricDifference(left:Struct<...>, right:Struct<...>)->Struct<...>
Arguments:
left
- first structure.right
- second structure.mergeLambda
- (optional) function to merge fields with the same name (arguments: field name, Optional
field value of the first struct, Optional
field value of the second struct - arguments are Nothing<T?>
in case of absence of the corresponding struct field). By default, if present, the first structure's field value is used; otherwise, the second one's value is used.$merge = ($name, $l, $r) -> {
return ($l ?? 0) + ($r ?? 0);
};
$left = <|a: 1, b: 2, c: 3|>;
$right = <|c: 1, d: 2, e: 3|>;
SELECT
StructUnion($left, $right), -- <|a: 1, b: 2, c: 3, d: 2, e: 3|>
StructUnion($left, $right, $merge), -- <|a: 1, b: 2, c: 4, d: 2, e: 3|>
StructIntersection($left, $right, $merge), -- <|c: 4|>
StructDifference($left, $right), -- <|a: 1, b: 1|>
StructSymmetricDifference($left, $right) -- <|a: 1, b: 2, d: 2, e: 3|>
;