Combining sources

One query can read several sources, of different families. This page shows how a query combines them and in which order the sources are read.

Features are named expressions, so one query can mix connectors. A feature can look up a customer in PostgreSQL, fetch the latest order from Redis and call a scoring API over HTTP, in one request.

This query reads customers from PostgreSQL and adds the details of their last order from Redis:

FeatureQL
DEFINE
    orders := ENTITY(),
    order_id := INPUT(BIGINT#orders),
    redis_key := 'serving_slt:order:' || KEY_TO_SCALAR(order_id)::VARCHAR,
    order_details_str := EXTERNAL_KV(
        KEY redis_key
        FROM fm.tutorials.federated.redis_src
    )[value],
    order_details := JSON_PARSE_AS(
        order_details_str AS ROW<order_id BIGINT, price DOUBLE>
    ),
    customers := ENTITY(),
    customer_id := INPUT(BIGINT#customers),
    customer_details := EXTERNAL_COLUMNS(
        customer_id BIGINT#customers BIND TO customer_id,
        last_order_id BIGINT#orders
        FROM TABLE(fm.tutorials.federated.postgres_conn[serving_slt_federated_customers])
    ),
    last_order_id := customer_details[last_order_id],
    last_order_join := EXTEND(
        ROW(last_order_id AS last_order_id)
        WITH order_details AS order_details
        VIA last_order_id BIND TO order_id
    ),
    last_order_price := last_order_join[order_details][price]
RETURN
    customer_id,
    last_order_price
FOR
    customer_id := BIND_VALUES(ARRAY(1, 2))
;
Result
CUSTOMER_ID BIGINT#CUSTOMERSLAST_ORDER_PRICE DOUBLE
119.99
212.74

The query defines two entities in two stores. CUSTOMERS is in PostgreSQL, read with EXTERNAL_COLUMNS(). ORDERS is in Redis, read with EXTERNAL_KV(). EXTEND() links them. It takes last_order_id from the PostgreSQL row and looks up that order in Redis, through the BIGINT#ORDERS entity annotation.

The order of the reads follows the dependency graph. PostgreSQL is read first, for last_order_id, then Redis, for the order. Each source is read its own way. PostgreSQL gets parameterized SQL, and Redis a lookup by key.

A query over several sources has no syntax of its own. Its features reference different connectors, and the dependency graph decides the order, as it does within one source. Any query over one connector extends to more sources. Add features that read the other connectors, and link them with entity annotations or EXTEND(). A prepared statement compiles the whole graph once, the joins across sources included.

The same goes for history: a feature can aggregate a customer's orders in PostgreSQL, read their recent purchases from a Redis sorted set, and combine both with a score from an HTTP model. See Aggregating a key's recent history .

Federated serving builds a two-store lookup step by step, from the data to a prepared statement.