Package dev.frostlake.functions.table
Class Flatten
java.lang.Object
dev.frostlake.functions.TableFunction
dev.frostlake.functions.table.Flatten
FLATTEN — expands semi-structured data into one row per element.
Returns SEQ, KEY, PATH, INDEX, VALUE and THIS. INPUT must be VARIANT, OBJECT or ARRAY:
Snowflake will not read JSON out of a VARCHAR, and answers
invalid type [VARCHAR(7)] for parameter 'INPUT' for FLATTEN(INPUT => '[1,2,3]') —
PARSE_JSON is the supported spelling. That refusal, the argument names and the boolean-ness
of OUTER / RECURSIVE are all settled while the statement is compiled (see
TableFunctionArguments); what is left here is what a value decides.
The measured row rules:
- SEQ numbers the INPUT RECORD, not the element: every row of one call carries the same SEQ, starting at 1.
- Only a container expands. A VARIANT holding a scalar — a string, a number, a JSON null —
contributes NO rows, so
FLATTEN(PARSE_JSON('"abc"'))is empty rather than one row, and a string that merely LOOKS like JSON is not re-read as JSON. - RECURSIVE descends only through what the MODE emitted. Under
MODE => 'ARRAY'an object's members are neither emitted nor followed, which is whyFLATTEN(PARSE_JSON('{"arr":[1,2,3]}'), MODE => 'ARRAY', RECURSIVE => TRUE)is empty. - PATH prefixes the reported path rather than replacing it:
PATH => 'a.b'reportsa.b[0]. A path that matches nothing, or matches a scalar, yields no rows. - OUTER's stand-in row is all NULL. For an empty container it still reports the container as THIS and an empty PATH; for a scalar or NULL input both are NULL.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionThe positional form fills the same five parameters in order —FLATTEN(input, path, outer, recursive, mode)— and each one it is given counts, so a positional PATH selects a sub-element exactly as the named one does.Execute the table function with named argumentsvoidvalidateArgs(Map<String, Object> namedArgs) What is left once the statement has compiled: the argument NAMES and the missing-INPUT refusal are raised from the parse tree, where they carry a position, so only the MODE value is judged here — live spells itBad flattening mode 'NOPE' (not 'BOTH', 'ARRAY', or 'OBJECT').Methods inherited from class dev.frostlake.functions.TableFunction
getName
-
Constructor Details
-
Flatten
public Flatten()
-
-
Method Details
-
execute
The positional form fills the same five parameters in order —FLATTEN(input, path, outer, recursive, mode)— and each one it is given counts, so a positional PATH selects a sub-element exactly as the named one does.- Overrides:
executein classTableFunction
-
execute
Description copied from class:TableFunctionExecute the table function with named arguments- Specified by:
executein classTableFunction
-
validateArgs
What is left once the statement has compiled: the argument NAMES and the missing-INPUT refusal are raised from the parse tree, where they carry a position, so only the MODE value is judged here — live spells itBad flattening mode 'NOPE' (not 'BOTH', 'ARRAY', or 'OBJECT').- Specified by:
validateArgsin classTableFunction
-