RELATED()

All functions > CORE > RELATED()

Brings a value from another entity: a lookup through a foreign key, or an aggregation of the matching rows

Signatures

Returns: One value per outer entity

RELATED(
  expression
  VIA key
  [ BIND TO target ]
) → T

key.RELATED( expression [ BIND TO target ] ) → T

RELATED(
  aggregate
  ON predicate
) → T
ParameterTypeRequiredDescription
expressionTYesValue computed at the related entity's grain: a feature to look up, or an aggregate such as SUM(AMOUNT) GROUP BY ORDER_CUSTOMER_ID
keyFEATURENoVIA and chained forms: foreign key of the outer entity that points to the related entity
targetFEATURENoInput of the related entity that key binds to, when entity annotations cannot infer it
aggregateTNoON form: aggregate over the related rows that predicate matches, such as MAX_BY(LABEL, START_DATE)
predicateBOOLEANNoON form: join condition between outer and related features (ranges, as-of conditions, inequalities)

Notes

  • VIA follows a key: a foreign-key lookup, or child rows aggregated back to the outer entity. key.RELATED(expression) is the same lookup written as a chain
  • ON matches with a predicate instead of a key. It replaces VIA and BIND TO, and the expression must aggregate because several related rows can match
  • Outer rows are kept when nothing matches: a lookup returns NULL, COUNT() returns 0, other aggregates return NULL. Matches never fan out the outer rows
  • BIND TO takes one target. For several keys or field-level bindings, use EXTEND(... VIA ... BIND TO ...)

See also

Examples

Look up a value through a foreign key (VIA)

FeatureQL
DEFINE
    -- ** Features defining the semantic entities [likely already persisted in the registry] **
    orders := ENTITY(),
    order_id := INPUT(BIGINT#orders),
    stores := ENTITY(),
    store_id := INPUT(BIGINT#stores),
    -- ** Features depending on ORDER_ID [likely already persisted in the registry] **
    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
            16,99
        )
    ),
    -- ** Features depending on STORE_ID [likely already persisted in the registry] **
    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
        )
    ),
    -- ** New feature depending on ORDER_ID **
    order_category := RELATED(
        tables.stores[store_category] VIA tables.orders[order_store_id]
    )
RETURN
    order_id,
    order_category
FOR
    order_id := BIND_VALUES(SEQUENCE(10, 16))
ORDER BY order_id
;
Result
ORDER_ID BIGINT#ORDERSORDER_CATEGORY VARCHAR
10PIZZA
11SUSHI
12BURGER
13SUSHI
14BURGER
15SUSHI
16NULL

Aggregate child rows back to their parent (VIA ... GROUP BY)

FeatureQL
DEFINE
    -- ** Define features at ORDER level **
    orders := ENTITY(),
    order_id := INPUT(BIGINT#orders),
    order_source := INLINE_COLUMNS(
        order_id BIGINT#orders BIND TO order_id,
        order_store_id BIGINT#stores,
        price DECIMAL(10,2)
        FROM CSV(
            order_id,order_store_id,price
            10,20,12.51
            11,21,13.20
            12,22,15.06
            13,21,25.70
            14,22,30.26
            15,21,10.00
        )
    ),
    -- ** Define features at STORE level **
    stores := ENTITY(),
    store_id := INPUT(BIGINT#stores),
    num_orders := RELATED(
        COUNT() GROUP BY (order_source[order_store_id]) VIA store_id
    ),
    num_high_value_orders := RELATED(
        COUNT()
        FILTER (WHERE order_source[price] > 20.00)
        GROUP BY (order_source[order_store_id]) VIA store_id
    ),
    has_orders := COALESCE(num_orders, 0) > 0
RETURN
    store_id,
    num_orders,
    num_high_value_orders,
    has_orders
FOR
    store_id := BIND_VALUES(ARRAY(20, 21, 22, 23)),
    NESTED order_id := BIND_VALUES(SEQUENCE(10, 15)) -- Orders-side INPUT for RELATED(... GROUP BY ...); bind as NESTED (subquery scope)
ORDER BY store_id
;
Result
STORE_ID BIGINT#STORESNUM_ORDERS BIGINTNUM_HIGH_VALUE_ORDERS BIGINTHAS_ORDERS BOOLEAN
2010TRUE
2131TRUE
2221TRUE
2300FALSE

Match a range with a predicate (ON)

FeatureQL
DEFINE
    products := ENTITY(),
    product_id := INPUT(BIGINT#products),
    product_data := INLINE_COLUMNS(
        product_id BIGINT#products BIND TO product_id,
        price DECIMAL(10,2)
        FROM CSV(
            product_id,price
            1,52.20
            2,302.00
            3,1200.05
        )
    ),
    bands := ENTITY(),
    band_id := INPUT(BIGINT#bands),
    band_data := INLINE_COLUMNS(
        band_id BIGINT#bands BIND TO band_id,
        name VARCHAR,
        price_min DECIMAL(10,2),
        price_max DECIMAL(10,2)
        FROM CSV(
            band_id,name,price_min,price_max
            100,low,0.,100.
            101,mid,100.,1000.
            102,high,1000.,100000.
        )
    )
RETURN
    product_id,
    price := product_data[price],
    price_band := RELATED(
        MAX_BY(band_data[name], band_data[price_max]) ON band_data[price_min]
        <= product_data[price]
        AND product_data[price] < band_data[price_max]
    )
FOR
    product_id := BIND_VALUES(ARRAY(1, 2, 3)),
    NESTED band_id := BIND_VALUES(ARRAY(100, 101, 102))
ORDER BY product_id
;
Result
PRODUCT_ID BIGINT#PRODUCTSPRICE DECIMAL(10,2)PRICE_BAND VARCHAR
152.20low
2302.00mid
31200.05high

As-of lookup: latest event before a timestamp (ON)

FeatureQL
DEFINE
    accounts := ENTITY(),
    account_id := INPUT(BIGINT#accounts),
    account_data := INLINE_COLUMNS(
        account_id BIGINT#accounts BIND TO account_id,
        as_of_date DATE
        FROM CSV(
            account_id,as_of_date
            1,2024-01-10
            2,2024-01-20
        )
    ),
    events := ENTITY(),
    event_id := INPUT(BIGINT#events),
    event_data := INLINE_COLUMNS(
        event_id BIGINT#events BIND TO event_id,
        account_id BIGINT#accounts,
        event_date DATE,
        status VARCHAR
        FROM CSV(
            event_id,account_id,event_date,status
            1,1,2024-01-01,new
            2,1,2024-01-05,active
            3,1,2024-01-15,paused
            4,2,2024-01-05,new
            5,2,2024-01-25,active
        )
    )
RETURN
    account_id,
    as_of_date := account_data[as_of_date],
    last_status := RELATED(
        MAX_BY(event_data[status], event_data[event_date]) ON event_data[account_id]
        = account_id
        AND event_data[event_date] <= account_data[as_of_date]
    )
FOR
    account_id := BIND_VALUES(ARRAY(1, 2)),
    NESTED event_id := BIND_VALUES(ARRAY(1, 2, 3, 4, 5))
ORDER BY account_id
;
Result
ACCOUNT_ID BIGINT#ACCOUNTSAS_OF_DATE DATELAST_STATUS VARCHAR
12024-01-10active
22024-01-20new