---
metadata:
  - name: generator
    content: Diplodoc Platform v5.70.2
alternate:
  - https://ytsaurus.tech/docs/en/flow/go/examples/static_table_join.md
  - https://ytsaurus.tech/docs/ru/flow/go/examples/static_table_join.md
  - href: https://ytsaurus.tech/docs/en/flow/go/examples/static_table_join.md
    type: text/markdown
    title: Markdown version
  - href: https://ytsaurus.tech/docs/en/llms.txt?revision=3d4023814c654f56301c5b12ef057a0c6e4e82d1
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://ytsaurus.tech/docs/en/llms.txt?revision=3d4023814c654f56301c5b12ef057a0c6e4e82d1

<!-- source: en/_includes/flow/go/examples/static_table_join.md -->
# Static Table Join in YTsaurus Flow (Go)

An example of a [pipeline](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#pipeline) that loads a static reference table into an external [state](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#state) and then enriches events from a queue by the shared key.

[Source code](https://github.com/ytsaurus/ytsaurus/tree/main/yt/yt/flow/examples/go/static_table_join)

## Structure {#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` {#main-go}

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

[Source code: `main.go`](https://github.com/ytsaurus/ytsaurus/tree/main/yt/yt/flow/examples/go/static_table_join/main.go)
```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` {#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`](https://github.com/ytsaurus/ytsaurus/tree/main/yt/yt/flow/examples/go/static_table_join/reference_loader.go)
```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` {#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`](https://github.com/ytsaurus/ytsaurus/tree/main/yt/yt/flow/examples/go/static_table_join/enricher.go)
```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 {#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`.
<!-- endsource: en/_includes/flow/go/examples/static_table_join.md -->

<!-- source: en/_includes/flow/go/examples/static_table_join_also.md -->
## See also

- [External State (Go)](https://ytsaurus.tech/docs/en/flow/go/external-state.md)
- [State Accessor (Go)](https://ytsaurus.tech/docs/en/flow/go/state-accessor.md)
- [Static table](https://ytsaurus.tech/docs/en/flow/connectors/static-table.md)
<!-- endsource: en/_includes/flow/go/examples/static_table_join_also.md -->
