Porting a batch feature to serving

This guide serves a feature written for batch without rewriting it: VARIANT() points it at a serving source, and a prepared statement serves the variant.

Before you start, have the feature registered and running in batch, and the serving source it will read registered with SOURCE_DB() or SOURCE_KV() . The prepared statements reference covers USING, recompile and calls.

1. Check what changes

The formulas stay. Two things change: where the rows come from (the mappings), and who binds the inputs (the query, or the caller of a prepared statement).

Batch queryFree-form serving queryPrepared statement
Table mappingEXTERNAL_COLUMNS(... FROM TABLE(...)) on the warehouseThe same columns FROM TABLE(src[table]) of a SOURCE_DB() Same as free-form
Outer keysFOR CUSTOMER_ID := BIND_COLUMNS(...) or BIND_VALUES(...)FOR CUSTOMER_ID := BIND_VALUES(...)USING CUSTOMER_ID: the caller sends them
Rows a RELATED() aggregatesNESTED ORDER_ID := BIND_COLUMNS(...)NESTED ORDER_ID := @BIND_KEYSET(src_mapping)Bound by the statement: the whole table, read by the key in ON. Writing the binding is refused
Time of the requestA column of a label table, or a constantAn input bound in FORAn input bound in USING, often REQ_TS := REQUEST_TIME()

Three habits make the port a change of mappings only. Apply them to the batch feature first, if it lacks them:

  • Aggregate child rows with ON. RELATED(COUNT() ON ORDERS[customer_id] = CUSTOMER_ID) runs the same in batch and on serving. The equality on the customer column is what a prepared statement reads the orders by. See RELATED on serving .
  • Make the time of the request an input. A feature that reads AS_OF as an input is replayed in batch by binding it, and served by binding it to the call. Inside a TRANSFORM(), an input is brought onto the rows with CARRY (IMPORT takes pure formulas only: importing an input is refused with UE/TRANSFORM-IMPORT-USE-CARRY). See TRANSFORM .
  • Keep the mappings apart from the logic. A feature that reads a mapping only through other features is ported by replacing that one mapping, below.

2. Replace the batch mapping with VARIANT()

ORDERS_BATCH stands for the warehouse table, ORDERS_SERVING for the same table read from an operational database. Both are inline here so the page runs anywhere; the serving one has a fourth order, for customer 2, that the warehouse has not loaded yet. REPEAT_CUSTOMER is written once, over the batch mapping:

FeatureQL
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.PORTING AS
PERSIST
    customers := ENTITY(),
    customer_id := INPUT(BIGINT#customers),
    orders := ENTITY(),
    order_id := INPUT(BIGINT#orders),
    orders_batch := INLINE_COLUMNS(
        order_id BIGINT#orders BIND TO order_id,
        customer_id BIGINT#customers,
        amount DOUBLE
        FROM CSV(
            order_id,customer_id,amount
            1,1,10
            2,1,30
            3,2,50
        )
    ),
    orders_serving := INLINE_COLUMNS(
        order_id BIGINT#orders BIND TO order_id,
        customer_id BIGINT#customers,
        amount DOUBLE
        FROM CSV(
            order_id,customer_id,amount
            1,1,10
            2,1,30
            3,2,50
            4,2,70
        )
    ),
    n_orders := RELATED(COUNT() ON orders_batch[customer_id] = customer_id),
    repeat_customer := n_orders >= 2
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.PORTING.CUSTOMERSCREATEDFeature created as not exists
FM.TUTORIALS.PORTING.CUSTOMER_IDCREATEDFeature created as not exists
FM.TUTORIALS.PORTING.N_ORDERSCREATEDFeature created as not exists
FM.TUTORIALS.PORTING.ORDERSCREATEDFeature created as not exists
FM.TUTORIALS.PORTING.ORDERS_BATCHCREATEDFeature created as not exists
FM.TUTORIALS.PORTING.ORDERS_SERVINGCREATEDFeature created as not exists
FM.TUTORIALS.PORTING.ORDER_IDCREATEDFeature created as not exists
FM.TUTORIALS.PORTING.REPEAT_CUSTOMERCREATEDFeature created as not exists

VARIANT() makes the serving version: the same graph, with every read of ORDERS_BATCH replaced by ORDERS_SERVING. N_ORDERS, which sits between the two, is rebuilt in the variant's graph and left unchanged for REPEAT_CUSTOMER:

FeatureQL
CREATE OR REPLACE FEATURE FM.TUTORIALS.PORTING.REPEAT_CUSTOMER_SERVING AS
VARIANT(
    FM.TUTORIALS.PORTING.REPEAT_CUSTOMER
    REPLACING FM.TUTORIALS.PORTING.ORDERS_BATCH
    WITH FM.TUTORIALS.PORTING.ORDERS_SERVING
);
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.PORTING.REPEAT_CUSTOMER_SERVINGCREATEDFeature created as not exists

Check the variant against the batch version in one query. Customer 2 has one order in the batch mapping and two in the serving one, so only its answer differs:

FeatureQL
RETURN
    fm.tutorials.porting.customer_id,
    fm.tutorials.porting.n_orders,
    fm.tutorials.porting.repeat_customer,
    fm.tutorials.porting.repeat_customer_serving
FOR
    fm.tutorials.porting.customer_id := BIND_VALUES(ARRAY(1, 2, 3)),
    NESTED fm.tutorials.porting.order_id := BIND_VALUES(ARRAY(1, 2, 3, 4))
ORDER BY fm.tutorials.porting.customer_id
;
Result
CUSTOMER_ID BIGINTN_ORDERS BIGINTREPEAT_CUSTOMER BOOLEANREPEAT_CUSTOMER_SERVING BOOLEAN
12TRUETRUE
21FALSETRUE
30FALSEFALSE

The variant can replace a mapping, as here, or any intermediate feature (an array of the customer's orders read from the database, for example). The variants page gives the rules.

3. Serve the variant

A prepared statement wraps the variant like any other feature. It takes no binding for the orders:

FeatureQL
CREATE OR REPLACE FEATURE FM.TUTORIALS.PORTING.REPEAT_CUSTOMER_PS AS
PREPARED_STATEMENT(
    FM.TUTORIALS.PORTING.REPEAT_CUSTOMER_SERVING
    USING FM.TUTORIALS.PORTING.CUSTOMER_ID
);
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.PORTING.REPEAT_CUSTOMER_PSCREATEDFeature created as not exists

After RECOMPILE SERVING, a call sends customer IDs only. The answers match the variant's column above, which is how you check the port:

Prepared
FM.TUTORIALS.PORTING.REPEAT_CUSTOMER_PS
Call 1
{"input_1": [[1], [2], [3]]}
CUSTOMER_ID BIGINTREPEAT_CUSTOMER_PS BOOLEAN
1TRUE
2TRUE
3FALSE

4. Roll out a changed rule later

When a served rule changes, test the new definition as a VARIANT() in batch first. Roll it out to a fraction of live traffic with GRADUAL_ROLLOUT() and HASH01() , which send the same key to the same side on every call. For 10% of customers on the new rule:

ON_NEW_RULE := HASH01(KEY_TO_SCALAR(CUSTOMER_ID)::VARCHAR || '/decision-v2') <.2 0.10e0,
DECISION := IF(ON_NEW_RULE, DECISION_V2, DECISION_V1)

<.2 compares the DOUBLE at 2 decimals ( Decision tables ). A fixed salt, here '/decision-v2', keeps each customer on its side. A salt made with REQUEST_SEED() draws again on every call ( One draw per call, or per key ). Point the prepared statement at DECISION and run RECOMPILE SERVING. The application keeps calling the same statement id.

When the statement is refused

A prepared statement reads every source by the call's keys. A port that compiles in batch can be refused when the statement is created:

  • A read with no key equality, such as a count over the whole orders table, is UE/SERVING-NOT-A-KEY-LOOKUP. Add the key to the ON condition, or precompute the value in batch and serve it from Redis. What a prepared statement may read shows one.
  • A FOR clause, or a NESTED binding written in the statement, is refused. Prepared statements lists the errors.

Related