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 query | Free-form serving query | Prepared statement | |
|---|---|---|---|
| Table mapping | EXTERNAL_COLUMNS(... FROM TABLE(...)) on the warehouse | The same columns FROM TABLE(src[table]) of a SOURCE_DB() | Same as free-form |
| Outer keys | FOR CUSTOMER_ID := BIND_COLUMNS(...) or BIND_VALUES(...) | FOR CUSTOMER_ID := BIND_VALUES(...) | USING CUSTOMER_ID: the caller sends them |
Rows a RELATED() aggregates | NESTED 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 request | A column of a label table, or a constant | An input bound in FOR | An 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_OFas an input is replayed in batch by binding it, and served by binding it to the call. Inside aTRANSFORM(), an input is brought onto the rows withCARRY(IMPORTtakes pure formulas only: importing an input is refused withUE/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:
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
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.PORTING.CUSTOMERS | CREATED | Feature created as not exists |
| FM.TUTORIALS.PORTING.CUSTOMER_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.PORTING.N_ORDERS | CREATED | Feature created as not exists |
| FM.TUTORIALS.PORTING.ORDERS | CREATED | Feature created as not exists |
| FM.TUTORIALS.PORTING.ORDERS_BATCH | CREATED | Feature created as not exists |
| FM.TUTORIALS.PORTING.ORDERS_SERVING | CREATED | Feature created as not exists |
| FM.TUTORIALS.PORTING.ORDER_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.PORTING.REPEAT_CUSTOMER | CREATED | Feature 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:
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
);| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.PORTING.REPEAT_CUSTOMER_SERVING | CREATED | Feature 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:
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
;| CUSTOMER_ID BIGINT | N_ORDERS BIGINT | REPEAT_CUSTOMER BOOLEAN | REPEAT_CUSTOMER_SERVING BOOLEAN |
|---|---|---|---|
| 1 | 2 | TRUE | TRUE |
| 2 | 1 | FALSE | TRUE |
| 3 | 0 | FALSE | FALSE |
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:
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
);| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.PORTING.REPEAT_CUSTOMER_PS | CREATED | Feature 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:
FM.TUTORIALS.PORTING.REPEAT_CUSTOMER_PS{"input_1": [[1], [2], [3]]} | CUSTOMER_ID BIGINT | REPEAT_CUSTOMER_PS BOOLEAN |
|---|---|
| 1 | TRUE |
| 2 | TRUE |
| 3 | FALSE |
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 theONcondition, or precompute the value in batch and serve it from Redis. What a prepared statement may read shows one. - A
FORclause, or aNESTEDbinding written in the statement, is refused. Prepared statements lists the errors.
Related
- Reading the children of a key : the same
ONaggregation over PostgreSQL. - Prepared statements :
USING, recompile and calls. - Values of the call :
REQUEST_TIME()andREQUEST_SEED(). - Train on what serving saw : log what the served variant returned, and train on it.