Huginn.Clickhouse.Stream (Huginn v0.6.0)

Copy Markdown View Source

Builds the QueryInfo message stream for ExecuteQueryWithStreamInput.

input_stream/3 encodes rows (or passes pre-encoded binaries through), packs them into messages of roughly :chunk_size bytes and turns them into the lazy message stream that Huginn.Clickhouse.Client.insert_stream/3 sends.

Row encoding

The :format option selects how structured rows are encoded. It must match the FORMAT clause of the INSERT statement, which is what ClickHouse uses to parse the data.

  • TabSeparated/TSV (and the WithNames/WithNamesAndTypes variants) - a row is a list of fields joined by \t and ended by \n. Fields are escaped like ClickHouse's own writer: \, ', \t, \n, \r, \0, \b and \f become backslash sequences. nil becomes \N (NULL).
  • CSV (and CSVWithNames/CSVWithNamesAndTypes) - binaries are always double-quoted with " doubled, so empty strings, the literal string \N, surrounding whitespace, commas and newlines all survive. Integers and floats are written bare. nil becomes a bare \N (NULL).
  • JSONEachRow - a row is a map, encoded as one JSON object per line. List rows raise ArgumentError; use JSONCompactEachRow for them.
  • JSONCompactEachRow - a row is a list, encoded as one JSON array per line.

In the TSV and CSV formats, non-binary scalars (integers, floats, booleans, atoms, anything implementing String.Chars) are converted with to_string/1. Nested lists and maps are not supported there; encode them yourself or use a JSON format.

Elements that are binaries are passed through verbatim whatever the :format, so pre-encoded data (for example File.stream!/2 chunks) keeps the caller's own framing. Each binary element must end on a row boundary that the format understands.

Summary

Functions

Builds a lazy stream of QueryInfo messages for a streaming insert.

Functions

input_stream(sql, data_enum, opts \\ [])

@spec input_stream(String.t(), Enumerable.t(), keyword()) :: Enumerable.t(struct())

Builds a lazy stream of QueryInfo messages for a streaming insert.

The first message carries the SQL query plus the first data chunk; every later message carries only data. The source is enumerated exactly once and lazily, so non-restartable sources (File.stream!/2, a Stream.map/2 with side effects) are safe and memory stays bounded by :chunk_size. An empty source still yields one message (the query with empty data).

Options

  • :chunk_size - target number of bytes of input data per message (default: 1048576). Encoded rows are accumulated until the buffer reaches this size, then sent as one message. An element that is by itself at least :chunk_size bytes is sent alone in its own message; elements are never split.
  • :format - how structured rows are encoded (default: "TabSeparated"); see the module docs. It does not change the SQL - the FORMAT clause of the INSERT must name the same format.
  • All options from Query.build/2

next_query_info is set by Huginn.Clickhouse.Client.insert_stream/3, which knows which message is last.

Examples

# Structured rows, encoded by this module
rows = [["a", 1], ["b\tc", nil]]
input_stream("INSERT INTO t FORMAT TabSeparated", rows)

# Maps as JSON objects
input_stream("INSERT INTO t FORMAT JSONEachRow", [%{"id" => 1}], format: "JSONEachRow")

# Pre-encoded chunks, passed through untouched and packed into ~1 MiB messages
File.stream!("data.csv", 65_536)
|> then(&input_stream("INSERT INTO t FORMAT CSV", &1))