Static Table Join in YTsaurus Flow (Go)

An example of a pipeline that loads a static reference table into an external state and then enriches events from a queue by the shared key.

Source code

Structure

  • reference_reader — a native static-table source that publishes the reference rows to the reference stream.
  • reference_loader (referenceLoader) — a Go transform computation that normalizes the value and writes it to the external state /reference_state.
  • event_reader — a native queue source that publishes the event stream.
  • enricher (enricher) — a Go transform computation that reads /reference_state through external_state_joiners and publishes the result to the enriched stream.

The manager and the joiner refer to the same state table: the first one fills it from the static table, the second one performs a read-only lookup for the incoming events.

main.go

The entry point registers three typed streams and both Go computations.

Source code: main.go

const referenceStateName = "/reference_state"

type referenceMessage struct {
	flow.YSONMessage
	Key  uint64 `yson:"key"`
	Name string `yson:"name"`
}

type eventMessage struct {
	flow.YSONMessage
	Key uint64 `yson:"key"`
}

type enrichedMessage struct {
	flow.YSONMessage
	Key  uint64 `yson:"key"`
	Name string `yson:"name"`
}

func main() {
	pipeline := flow.NewPipeline()
	pipeline.AddStreams(
		flow.NewYSONStream[referenceMessage]("reference"),
		flow.NewYSONStream[eventMessage]("event"),
		flow.NewYSONStream[enrichedMessage](enrichedStreamID),
	)
	pipeline.Add(
		flow.NewRowComputation("reference_loader", &referenceLoader{}),
		flow.NewRowComputation("enricher", &enricher{}),
	)

	if err := pipeline.Run(); err != nil {
		fmt.Fprintf(os.Stderr, "static_table_join: %v\n", err)
		os.Exit(1)
	}
}

reference_loader.go

The loader decodes a reference row, opens the external state for its key, and saves the normalized value.

Source code: reference_loader.go

type referenceState struct {
	NormalizedName *string `yson:"normalized_name"`
}

type referenceLoader struct{}

var _ flow.RowFunction = (*referenceLoader)(nil)

func (*referenceLoader) OnMessage(
	ctx context.Context,
	rt flow.Runtime,
	msg flow.ExtendedMessage,
	out flow.OutputCollector,
) error {
	var reference referenceMessage
	if err := msg.ConvertTo(&reference); err != nil {
		return err
	}

	state, err := flow.OpenExternalState(rt, referenceStateName, msg)
	if err != nil {
		return err
	}

	normalized := normalize(reference.Name)
	return state.ConvertFrom(&referenceState{NormalizedName: &normalized})
}

func normalize(name string) string {
	return strings.ToLower(strings.TrimSpace(name))
}

enricher.go

The enricher reads the joined state row and creates an output message only for a key that was found.

Source code: enricher.go


type enricher struct{}

var _ flow.RowFunction = (*enricher)(nil)

func (*enricher) OnMessage(
	ctx context.Context,
	rt flow.Runtime,
	msg flow.ExtendedMessage,
	out flow.OutputCollector,
) error {
	var event eventMessage
	if err := msg.ConvertTo(&event); err != nil {
		return err
	}

	name, joined, err := joinedName(rt, msg)
	if err != nil || !joined {
		return err
	}

	enriched := flow.NewYSONMessage[enrichedMessage](enrichedStreamID)
	enriched.Key = event.Key
	enriched.Name = name
	encoded, err := flow.ConvertFrom(rt, enriched)
	if err != nil {
		return err
	}
	out.AddMessage(encoded)
	return nil
}
func joinedName(rt flow.Runtime, msg flow.ExtendedMessage) (string, bool, error) {
	state, err := flow.OpenJoinedExternalState(rt, referenceStateName, msg)
	if errors.Is(err, flow.ErrStateNotRead) {
		return "", false, nil
	}
	if err != nil {
		return "", false, err
	}

	var reference referenceState
	exists, err := state.ConvertTo(&reference)
	if err != nil || !exists || reference.NormalizedName == nil {
		return "", false, err
	}
	return *reference.NormalizedName, true, nil
}

Key patterns

  • flow.OpenExternalState and ConvertFrom are used at the loading stage to write the reference data.
  • flow.OpenJoinedExternalState and ConvertTo are used at the enrichment stage for the read-only lookup.
  • Both computations work with typed structures; the payload is converted only at the input and the output of the handler.
  • The stream schemas are derived from the yson tags and registered through flow.NewYSONStream.

See also