Functions for structures
- TryMember
- ExpandStruct
- AddMember
- RemoveMember
- ForceRemoveMember
- ChooseMembers
- RemoveMembers
- ForceRemoveMembers
- CombineMembers
- FlattenMembers
- StructMembers
- RenameMembers
- ForceRenameMembers
- GatherMembers
- SpreadMembers
- ForceSpreadMembers
TryMember
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:
- Structure.
- Field name.
- Default value.
$struct = <|a:1|>;
SELECT
TryMember(
$struct,
"a",
123
) AS a, -- 1
TryMember(
$struct,
"b",
123
) AS b; -- 123
ExpandStruct
Adding one or more new fields to the structure.
If the field set contains duplicate values, an error is returned.
Arguments:
- The first argument passes the source structure to be expanded.
- All the other arguments must be named, each argument adds a new field and the argument’s name is used as the field’s name (as in AsStruct).
Examples
$struct = <|a:1|>;
SELECT
ExpandStruct(
$struct,
2 AS b,
"3" AS c
) AS abc;
AddMember
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:
- Source structure.
- Name of the new field.
- Value of the new field.
Examples
$struct = <|a:1|>;
SELECT
AddMember(
$struct,
"b",
2
) AS ab;
RemoveMember
Removing a field from the structure.
If the entered field hasn’t existed, an error is returned.
Arguments:
- Source structure.
- Field name.
Examples
$struct = <|a:1, b:2|>;
SELECT
RemoveMember(
$struct,
"b"
) AS a;
ForceRemoveMember
Removing a field from the structure.
If the entered field hasn’t existed, unlike RemoveMember, the error is not returned.
Arguments:
- Source structure.
- Field name.
Examples
$struct = <|a:1, b:2|>;
SELECT
ForceRemoveMember(
$struct,
"c"
) AS ab;
ChooseMembers
Selecting fields with specified names from the structure.
If any of the fields haven’t existed, an error is returned.
Arguments:
- Source structure.
- List of field names.
Examples
$struct = <|a:1, b:2, c:3|>;
SELECT
ChooseMembers(
$struct,
["a", "b"]
) AS ab;
RemoveMembers
Excluding fields with specified names from the structure.
If any of the fields haven’t existed, an error is returned.
Arguments:
- Source structure.
- List of field names.
Examples
$struct = <|a:1, b:2, c:3|>;
SELECT
RemoveMembers(
$struct,
["a", "b"]
) AS c;
ForceRemoveMembers
Excluding fields with specified names from the structure.
If any of the fields haven’t existed, it is ignored.
Arguments:
- Source structure.
- List of field names.
Examples
$struct = <|a:1, b:2, c:3|>;
SELECT
ForceRemoveMembers(
$struct,
["a", "b", "z"]
) AS c;
CombineMembers
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.
Examples
$struct1 = <|a:1, b:2|>;
$struct2 = <|c:3|>;
SELECT
CombineMembers(
$struct1,
$struct2
) AS abc;
FlattenMembers
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.
Examples
$struct1 = <|a:1, b:2|>;
$struct2 = <|c:3|>;
SELECT
FlattenMembers(
AsTuple("foo", $struct1), -- fooa, foob
AsTuple("bar", $struct2) -- barc
) AS abc;
StructMembers
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
Examples
$struct = <|a:1, b:2|>;
SELECT
StructMembers($struct); -- ['a', 'b']
RenameMembers
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:
- Source structure.
- A tuple of field names: the original name, the new name.
Examples
$struct = <|a:1, b:2|>;
SELECT
RenameMembers($struct, [('a', 'c'), ('a', 'e')]); -- (b:2, c:1, e:1)
ForceRenameMembers
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:
- Source structure.
- A tuple of field names: the original name, the new name.
Examples
$struct = <|a:1, b:2|>;
SELECT
ForceRenameMembers($struct, [('a', 'c'), ('d', 'e')]); -- (b:2, c:1)
GatherMembers
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
Examples
$struct = <|a:1, b:2|>;
SELECT
GatherMembers($struct); -- [('a', 1), ('b', 2)]
SpreadMembers
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:
- List of tuples: field name, field value.
- A list of all possible field names in the structure.
Examples
SELECT
SpreadMembers([('a',1),('a',2)],['a','b']); -- (a: 2, b: null)
ForceSpreadMembers
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:
- List of tuples: field name, field value.
- A list of all possible field names in the structure.
Examples
SELECT
ForceSpreadMembers([('a',1),('a',2),('c',100)],['a','b']); -- (a: 2, b: null)