The distribute flag in YTsaurus Flow (Python)
The distribute flag is a per-message flag that you set when you add an output message to SourceComputation. It controls whether the message is published further along the processing graph.
The distribute flag ensures:
- Correct watermark evaluation: messages with
distribute=Falseare still accounted for by the watermark generator (unlike filtering inon_message, which can break the watermark). - Assignment of deterministic identifiers to messages.
Warning
To filter a message in SourceComputation, don’t skip it in on_message. Instead, emit it with distribute=False. This way, the message isn’t published further, but it remains accounted for in watermark evaluation.
When to use distribute=False
Use the distribute=False flag when:
- You need to filter some output messages at the source-computation stage.
- Correct watermark evaluation is important.
If you don’t set the flag, it defaults to True, and the message is published further.
Usage
You move the filtering logic to the processing function: instead of a separate filtering step, you emit the message with the required flag.
from yt.yt.flow.library.python.companion.computation import AddMessageOptions, RowFunction
class HitParsingFunction(RowFunction):
def on_message(self, message, output, ctx):
builder = ctx.message_builder("hit")
builder.set("hit_id", message.payload["hit_id"])
builder.set("hit_payload", message.payload["hit_payload"])
# Duplicates are emitted but not published further.
is_duplicate = message.payload["hit_payload"] == "duplicate_payload"
output.add_message(
builder.finish(),
AddMessageOptions(distribute=not is_duplicate),
)
Registering a source-computation
You register a source-computation via Pipeline.add() with source=True. You no longer need a separate filtering parameter — the decision about publishing is made in the processing function.
from yt.yt.flow.library.python.companion import Pipeline
pipeline = Pipeline()
pipeline.add("hit_reader", HitParsingFunction(), source=True)