EXTEND(...)

All functions > ARRAY OF ROWS > EXTEND(...)

Adds computed feature fields to a base row or array of rows with field bindings

Syntax

EXTEND(
  base
  WITH feature AS field [, feature AS field ...]
  [ VIA field [, field ...] [ BIND TO feature [, feature ...] ] ]
)

Notes

  • Allows adding computed features as new fields to existing rows
  • BINDING clause maps base fields to feature inputs
  • Features are computed using the bound field values
  • Works with both single rows and arrays of rows
  • New field names must not conflict with existing fields

Related Functions

Examples

Single base row

FeatureQL
DEFINE
    -- ** Setup the relational model **
    orders := ENTITY(),
    order_id := INPUT(BIGINT#orders),
    stores := ENTITY(),
    store_id := INPUT(BIGINT#stores),
    -- ** Declare external data sources **
    tables.orders := INLINE_COLUMNS(
        order_id BIGINT#orders BIND TO order_id,
        order_store_id BIGINT#stores
        FROM CSV(
            order_id,order_store_id
            10,20
            11,21
            12,22
            13,21
            14,22
            15,21
        )
    ),
    tables.stores := INLINE_COLUMNS(
        store_id BIGINT#stores BIND TO store_id,
        store_category VARCHAR,
        store_city VARCHAR
        FROM CSV(
            store_id,store_category,store_city
            20,PIZZA,BCN
            21,SUSHI,MAD
            22,BURGER,VAL
        )
    ),
    -- ** Define new features **
    order_store_id := tables.orders[order_store_id],
    order_store_id_details := EXTEND(
        ROW(order_store_id AS order_store_id)
        WITH tables.stores[store_category] AS store_category,
        tables.stores[store_city] AS store_city
        VIA order_store_id BIND TO store_id
    )
RETURN
    order_id,
    order_store_id,
    order_store_id_details
FOR
    order_id := BIND_VALUES(ARRAY(10, 12, 15))
;
Result
ORDER_ID BIGINT#ORDERSORDER_STORE_ID BIGINT#STORESORDER_STORE_ID_DETAILS ROW<order_store_id BIGINT#STORES, store_category VARCHAR, store_city VARCHAR>
1020{order_store_id: 20, store_category: PIZZA, store_city: BCN}
1222{order_store_id: 22, store_category: BURGER, store_city: VAL}
1521{order_store_id: 21, store_category: SUSHI, store_city: MAD}

Array of rows

FeatureQL
DEFINE
    -- ** Setup the relational model **
    customers := ENTITY(),
    orders := ENTITY(),
    customer_id := INPUT(BIGINT#customers), -- We just need our primary key CUSTOMER_ID as inputs
    order_id := INPUT(BIGINT#orders),
    -- ** Simulating a customer table. Notice the list of order_ids. **
    customer_source := INLINE_COLUMNS(
        customer_id BIGINT#customers BIND TO customer_id,
        order_ids ARRAY(BIGINT#orders)
        FROM CSV(
            customer_id,order_ids
            1,"[10, 13]"
            2,"[11, 14, 15]"
            3,"[12, 99]"
        )
    ),
    order_ids := customer_source[order_ids],
    -- ** Simulating an order table. Notice there is no customer_id foreign key. **
    order_source := INLINE_COLUMNS(
        order_id BIGINT#orders BIND TO order_id,
        price DOUBLE
        FROM CSV(
            order_id,price
            10,12.51
            11,13.20
            12,15.06
            13,25.70
            14,30.26
            15,10.00
        )
    ),
    -- ** We need to add the price to each entry of the ORDER_IDS feature **
    order_ids_details := EXTEND(
        ZIP(order_ids AS order_id)
        WITH order_source[price] AS order_price
        VIA order_id BIND TO order_id
    )
RETURN
    customer_id,
    order_ids_details,
    revenue_per_customer := ARRAY_SUM(order_ids_details[order_price])
FOR
    customer_id := BIND_VALUES(ARRAY(1, 2, 3))
;
Result
CUSTOMER_ID BIGINT#CUSTOMERSORDER_IDS_DETAILS ARRAY<ROW<order_id BIGINT#ORDERS, order_price DOUBLE>>REVENUE_PER_CUSTOMER DOUBLE
1[{order_id: 10, order_price: 12.51}, {order_id: 13, order_price: 25.70}]38.21
2[{order_id: 11, order_price: 13.20}, {order_id: 14, order_price: 30.26}, {order_id: 15, order_price: 10.00}]53.46
3[{order_id: 12, order_price: 15.06}, {order_id: 99, order_price: NULL}]15.06

Chained EXTEND

FeatureQL
DEFINE
    orders := ENTITY(),
    order_id := INPUT(BIGINT#orders),
    order_data := INLINE_COLUMNS(
        order_id BIGINT#orders BIND TO order_id,
        store_id BIGINT#stores
        FROM CSV(
            order_id,store_id
            10,20
            11,21
            12,22
        )
    ),
    stores := ENTITY(),
    store_id := INPUT(BIGINT#stores),
    store_data := INLINE_COLUMNS(
        store_id BIGINT#stores BIND TO store_id,
        category VARCHAR
        FROM CSV(
            store_id,category
            20,PIZZA
            21,SUSHI
            22,BURGER
        )
    ),
    order_rows := ZIP(ARRAY(10, 11, 12) AS order_id),
    orders_with_store := EXTEND(
        order_rows
        WITH order_data[store_id] AS store_id
        VIA order_id BIND TO order_id
    ),
    enriched := EXTEND(
        orders_with_store
        WITH store_data[category] AS category
        VIA store_id BIND TO store_id
    )
RETURN
    enriched
;
Result
ENRICHED ARRAY<ROW<order_id BIGINT, store_id BIGINT#STORES, category VARCHAR>>
[{order_id: 10, store_id: 20, category: PIZZA}, {order_id: 11, store_id: 21, category: SUSHI}, {order_id: 12, store_id: 22, category: BURGER}]