EXTERNAL_COLUMNS()

All functions > SOURCE > EXTERNAL_COLUMNS()

Maps columns of a table, view, SQL query or inline data to a ROW feature.

Signatures

Returns: One row per input key; read its fields with [], as in ORDERS[amount]

EXTERNAL_COLUMNS(
  column [ AS field ] TYPE [ BIND TO input ],
  ...
  FROM { TABLE(table) | TABLE(source[table]) | SQL(query) }
  [ ON condition ]
) → ROW

INLINE_COLUMNS(
  column [ AS field ] TYPE [ BIND TO input ],
  ...
  FROM { CSV(text) | TSV(text) | JSON(text) }
  [ ON condition ]
) → ROW
ParameterTypeRequiredDescription
columnidentifierYesSource column to read
fieldidentifierNoField name in the result row (defaults to the column name)
TYPEtypeYesFeatureQL type of the column, such as BIGINT or VARCHAR
inputFEATURENoINPUT() feature that this key column matches, such as CUSTOMER_ID
tableidentifierNoFROM TABLE(...): table of the query backend, such as shop.orders
querySQLNoFROM SQL(...): query run by the query backend
sourceFEATURENoFROM TABLE(source[table]): a SOURCE_DB() or SOURCE_KV() feature, and one of its tables (or a key pattern of a key-value store) in brackets
textVARCHARNoFROM CSV/TSV/JSON(...), INLINE_COLUMNS only: the embedded rows
conditionBOOLEANNoRow filter over the declared columns and constants

Notes

  • Bind every key column to an INPUT() with BIND TO. The other columns become fields of the returned ROW
  • The source must return at most one row per key combination. Duplicate keys are not checked: they fan out, one result row per source row
  • INLINE_COLUMNS takes the same column list with embedded data, for prototypes and tests. EXTERNAL_COLUMNS reads the query backend (TABLE(table), SQL) or a table of a source (TABLE(source[table]), served online)
  • A column can also declare a data contract (DETERMINED BY (...), NOT ADDITIVE [ACROSS (...)]) after its type

See also

Examples

Inline CSV rows keyed by an input (INLINE_COLUMNS)

FeatureQL
DEFINE
    customer_id := INPUT(BIGINT),
    dim_customers := INLINE_COLUMNS(
        customer_id BIGINT BIND TO customer_id,
        name VARCHAR
        FROM CSV(
            customer_id,name
            101,Alice
            102,Bob
            103,Charly
            104,Daniel
            105,Elody
            106,Francis
        )
    )
RETURN
    customer_id,
    dim_customers,
    customer_name := dim_customers[name]
FOR
    customer_id := BIND_VALUES(ARRAY(101, 102, 103, 104, 105, 106))
;
Result
CUSTOMER_ID BIGINTDIM_CUSTOMERS ROW<customer_id BIGINT, name VARCHAR>CUSTOMER_NAME VARCHAR
101{customer_id: 101, name: Alice}Alice
102{customer_id: 102, name: Bob}Bob
103{customer_id: 103, name: Charly}Charly
104{customer_id: 104, name: Daniel}Daniel
105{customer_id: 105, name: Elody}Elody
106{customer_id: 106, name: Francis}Francis

Inline JSON rows (INLINE_COLUMNS)

FeatureQL
DEFINE
    customer_id := INPUT(BIGINT),
    dim_customers := INLINE_COLUMNS(
        customer_id BIGINT BIND TO customer_id,
        name VARCHAR
        FROM JSON(
            [
                {"customer_id": 101, "name": "Alice"},
                {"customer_id": 102, "name": "Bob"},
                {"customer_id": 103, "name": "Charly"},
                {"customer_id": 104, "name": "Daniel"},
                {"customer_id": 105, "name": "Elody"},
                {"customer_id": 106, "name": "Francis"}
            ]
        )
    )
RETURN
    customer_id,
    dim_customers,
    customer_name := dim_customers[name]
FOR
    customer_id := BIND_VALUES(ARRAY(101, 102, 103, 104, 105, 106))
;
Result
CUSTOMER_ID BIGINTDIM_CUSTOMERS ROW<customer_id BIGINT, name VARCHAR>CUSTOMER_NAME VARCHAR
101{customer_id: 101, name: Alice}Alice
102{customer_id: 102, name: Bob}Bob
103{customer_id: 103, name: Charly}Charly
104{customer_id: 104, name: Daniel}Daniel
105{customer_id: 105, name: Elody}Elody
106{customer_id: 106, name: Francis}Francis

Columns of a backend table (FROM TABLE)

FeatureQL
DEFINE
    customer_id := INPUT(BIGINT),
    dim_customers := EXTERNAL_COLUMNS(
        customer_id BIGINT BIND TO customer_id,
        name VARCHAR
        FROM TABLE(ext.dim_customers)
    )
RETURN
    customer_id,
    dim_customers,
    customer_name := dim_customers[name]
FOR
    customer_id := BIND_VALUES(ARRAY(101, 102, 103, 104, 105, 106))
;
Result
CUSTOMER_ID BIGINTDIM_CUSTOMERS ROW<customer_id BIGINT, name VARCHAR>CUSTOMER_NAME VARCHAR
101{customer_id: 101, name: Alice}Alice
102{customer_id: 102, name: Bob}Bob
103{customer_id: 103, name: Charly}Charly
104{customer_id: 104, name: Daniel}Daniel
105{customer_id: 105, name: Elody}Elody
106{customer_id: 106, name: Francis}Francis

Columns of a SQL query (FROM SQL)

FeatureQL
DEFINE
    customer_id := INPUT(BIGINT),
    dim_customers := EXTERNAL_COLUMNS(
        customer_id BIGINT BIND TO customer_id,
        name VARCHAR
        FROM SQL(SELECT customer_id, name FROM ext.dim_customers)
    )
RETURN
    customer_id,
    dim_customers,
    customer_name := dim_customers[name]
FOR
    customer_id := BIND_VALUES(ARRAY(101, 102, 103, 104, 105, 106))
;
Result
CUSTOMER_ID BIGINTDIM_CUSTOMERS ROW<customer_id BIGINT, name VARCHAR>CUSTOMER_NAME VARCHAR
101{customer_id: 101, name: Alice}Alice
102{customer_id: 102, name: Bob}Bob
103{customer_id: 103, name: Charly}Charly
104{customer_id: 104, name: Daniel}Daniel
105{customer_id: 105, name: Elody}Elody
106{customer_id: 106, name: Francis}Francis