Reshapes
← Relifold documentation

Apply

Perform a custom deterministic rows-to-rows reshape.

Apply is the escape hatch for a deterministic whole-Space reshape that does not fit a named operation.

Worked example

Apply(
    name="latest-behavior-per-user",
    input_spaces=("behaviors",),
    output_space="latest-behaviors",
    fn=PythonFunction(
        name="latest_row_per_user",
        source="""def latest_row_per_user(rows):
    latest = None
    for row in rows:
        if latest is None or row.get("time", "") > latest.get("time", ""):
            latest = row
    return [] if latest is None else [latest]
""",
    ),
    group_by="user_id",
    emits=("user_id", "history"),
    output_fields=("user_id", "history"),
)
ParameterDefaultMeaning
input_spacesrequiredThe Space or Spaces read by the reshape.
namerequiredA stable node name shown in the pipeline and session.
output_spacerequiredThe Space receiving returned rows.
fnrequiredA callable receiving one flat list containing the ordinary mappings gathered from every input Space, and returning an iterable of ordinary mappings.
emitsnoneDescriptive names of fields produced by the callable.
output_fieldsnoneComplete output schema when the callable builds new rows instead of retaining all fields from its input rows.
group_bynoneOptional column or composite key. Relifold invokes the callable once per exact key group and records only that group's rows as support for its outputs.
max_output_rowsnoneOptional positive global hard ceiling enforced across all returned rows, not once per group. Use it after deterministic whole-hand ranking to bound downstream work and its forecast cost.

Output

Exactly the ordinary row mappings returned by the callable.

Design guidance

Prefer a named reshape when one expresses the operation. Apply is appropriate for explicit aggregations and projections, not semantic decisions. Set group_by for per-entity reductions; reserve whole-hand Apply for genuinely global operations.