Skip to content

parser: add csv format parser (Format=csv) - #12273

Closed
jadeKim1993 wants to merge 1 commit into
fluent:masterfrom
jadeKim1993:parser/csv
Closed

parser: add csv format parser (Format=csv)#12273
jadeKim1993 wants to merge 1 commit into
fluent:masterfrom
jadeKim1993:parser/csv

Conversation

@jadeKim1993

@jadeKim1993 jadeKim1993 commented Aug 12, 2026

Copy link
Copy Markdown

Adds a new "csv" parser format (FLB_PARSER_CSV), picking up the idea from the never-merged #5040, but built for performance: a single left-to-right scan over each record records (offset, length) pairs for every field with no intermediate copies, then a second pass writes the already-known-size msgpack map directly from those positions. Fields on the stack (up to 128) avoid heap allocation entirely for typical CSV lines and grow geometrically onto the heap only for pathological inputs; unescaping of embedded "" is only done for the (rare) fields that actually contain a quote, also via a reusable stack buffer.

By default, fields are assigned numbered string keys ("0", "1", ...). An optional comma separated csv_fields parser config key assigns named keys instead. time_key designates which field carries the record timestamp - a 0-based index when no csv_fields is set, or a field name when it is - reusing the existing time_format/time_keep machinery shared with the other parsers.

Also registers tests/internal/parser_csv.c covering basic splitting, trailing empty fields, quoted/escaped fields, named fields, time_key by index and by name, time_keep, and type casting.


Enter [N/A] in the box, if an item is not applicable to your change.

Testing
Before we can approve your change; please submit the following in a comment:

  • Example configuration file for the change
  • Debug log output from testing the change
  • Attached Valgrind output that shows no leaks or memory corruption was found

If this is a change to packaging of containers or native binaries then please confirm it works for all targets.

  • Run local packaging test showing all targets (including any new ones) build.
  • Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • Documentation required for this feature

Backporting

  • Backport to latest stable release.

Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.

Summary by CodeRabbit

  • New Features

    • Added CSV parser support for comma-separated log and data records.
    • Supports quoted values, escaped quotes, empty trailing fields, and optional field names.
    • Enables timestamp extraction by field name or index, with optional timestamp retention.
    • Supports automatic type conversion for parsed values.
  • Tests

    • Added coverage for CSV parsing, timestamps, field naming, data types, and error handling.

Adds a new "csv" parser format (FLB_PARSER_CSV), picking up the idea
from the never-merged fluent#5040, but built for
performance: a single left-to-right scan over each record records
(offset, length) pairs for every field with no intermediate copies,
then a second pass writes the already-known-size msgpack map directly
from those positions. Fields on the stack (up to 128) avoid heap
allocation entirely for typical CSV lines and grow geometrically onto
the heap only for pathological inputs; unescaping of embedded "" is
only done for the (rare) fields that actually contain a quote, also
via a reusable stack buffer.

By default, fields are assigned numbered string keys ("0", "1", ...).
An optional comma separated `csv_fields` parser config key assigns
named keys instead. `time_key` designates which field carries the
record timestamp - a 0-based index when no `csv_fields` is set, or a
field name when it is - reusing the existing time_format/time_keep
machinery shared with the other parsers.

Also registers tests/internal/parser_csv.c covering basic splitting,
trailing empty fields, quoted/escaped fields, named fields, time_key
by index and by name, time_keep, and type casting.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 645ebcf5-1d1e-4d6d-8c86-2186adfa72a1

📥 Commits

Reviewing files that changed from the base of the PR and between ae51533 and f23d47d.

📒 Files selected for processing (6)
  • include/fluent-bit/flb_parser.h
  • src/CMakeLists.txt
  • src/flb_parser.c
  • src/flb_parser_csv.c
  • tests/internal/CMakeLists.txt
  • tests/internal/parser_csv.c

📝 Walkthrough

Walkthrough

CSV parser support is added to the public parser API, configuration loader, build system, runtime dispatch, and internal tests. The parser supports quoted fields, named fields, timestamps, type casting, decoder processing, and MessagePack output.

Changes

CSV parser support

Layer / File(s) Summary
CSV contract and configuration
include/fluent-bit/flb_parser.h, src/flb_parser.c
Adds the CSV parser type, parser state, field configuration, timestamp resolution, lifecycle cleanup, and runtime dispatch.
CSV parsing and output
src/flb_parser_csv.c, src/CMakeLists.txt
Adds CSV splitting, quoted-field handling, named fields, timestamp extraction, type casting, decoder processing, MessagePack output, and error cleanup.
CSV parser validation
tests/internal/CMakeLists.txt, tests/internal/parser_csv.c
Adds internal tests for parsing, quoting, field names, timestamps, retained time fields, typed values, and MessagePack results.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ParserConfig
  participant flb_parser_csv_set_fields
  participant flb_parser_csv_resolve_time_field
  participant flb_parser_csv_do
  participant MessagePackOutput
  ParserConfig->>flb_parser_csv_set_fields: configure CSV field names
  ParserConfig->>flb_parser_csv_resolve_time_field: resolve timestamp index
  ParserConfig->>flb_parser_csv_do: parse CSV record
  flb_parser_csv_do->>MessagePackOutput: emit typed fields and timestamp
Loading

Suggested reviewers: edsiper, cosmo0920

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f23d47d91a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/flb_parser_csv.c
Comment on lines +233 to +235
if (!parser->time_key) {
return;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor the default CSV time key

When a CSV parser is configured with Time_Format and named fields such as CSV_Fields time,msg but omits Time_Key, this early return leaves csv_time_field_index at -1. Unlike the existing JSON/LTSV/logfmt/regex parsers, which default a missing time_key to "time", flb_parser_csv_do() will never parse the CSV timestamp, so inputs fall back to ingestion/current time and the time field is not removed when Time_Keep is false. Resolve "time" against csv_field_names when parser->time_key is NULL.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant