Shared schema discovery

Use ISchemaResult to display the same column metadata for several formats. Its Columns are ordered as the reader exposes them. Each column has a Name, RuntimeType, and IsNullable. The result also includes a SchemaDescriptor and diagnostics about the sample or tabular projection.

Sample CSV, NDJSON, LTSV, or logfmt

Existing CSV callers can use the dialect and schema from CsvInference.Infer as before. The same result also implements ISchemaResult.

var csv = new CsvInference().Infer("events.csv");
var csvReader = new CsvReaderBuilder()
    .WithDialect(csv.Dialect)
    .WithSchema(csv.Schema)
    .Build();

var ndjson = new NdjsonInference(new() { MaximumRecords = 500 }).Infer("events.ndjson");
var ndjsonReader = new NdjsonReaderBuilder().WithInferredSchema(ndjson).Build();

var ltsv = new KeyValueInference(KeyValueFormat.Ltsv).Infer("events.ltsv");
var ltsvReader = new LtsvReaderBuilder().WithInferredSchema(ltsv).Build();

var logfmt = new KeyValueInference(KeyValueFormat.Logfmt, inferTypes: true).Infer("events.logfmt");
var logfmtReader = new LogfmtReaderBuilder().WithInferredSchema(logfmt).Build();

Sampling is opt-in. MaximumRecords bounds the number of records. Optional MaximumBytes also bounds input reads, including read-ahead. If the parser needs more bytes to finish sampling, it throws InvalidDataException. NDJSON distinguishes JSON scalars from quoted strings and represents nested values as JSON text in the tabular projection. Keys become columns in order of first appearance. LTSV and logfmt preserve strings by default. When type inference is enabled, values with significant leading zeroes remain strings. Sampled results expose counts for values, nulls, empty strings, and missing keys.

WithInferredSchema configures the sampled projection. Later keys outside that projection are ignored, and missing keys are exposed as null. Incompatible values surface through the existing typed reader conversions when a value is accessed. Use WithSchema to supply explicit types for projected columns. An empty sample returns a diagnostic and has no projection to apply. NDJSON sampling and the inferred reader default to LF framing, including CRLF input. For a custom dialect, pass it to the inferer and configure it on the reader builder.

Infer(Stream) leaves the input open and restores a seekable input by default. A forward-only input advances by the amount read by the parser, including any read-ahead. For NDJSON or key-value input that must be used after inference, use InferAndReplay and open the returned stream through the reader. The replay stream buffers the sampled prefix and then reads the remainder from the original stream. Disposing it also closes the original input unless leaveOpen was specified.

var (result, input) = new NdjsonInference().InferAndReplay(forwardOnlyInput);
using var replay = input;
using var rows = new NdjsonReaderBuilder()
    .WithInferredSchema(result)
    .Build()
    .ToDataReader(replay);

Read declared layouts and file metadata

ISchemaResult common = new CommonLogReader().ReadSchema();
ISchemaResult w3c = new W3cExtendedLogReader().ReadSchema("access.log");
ISchemaResult fixedWidth = configuredFixedWidthReader.ReadSchema();
ISchemaResult parquet = new ParquetReader().ReadSchema("events.parquet");
ISchemaResult avro = new AvroReader().ReadSchema("events.avro");

Common Log Format and fixed-width layouts are available from the reader configuration. W3C Extended Log Format reads its first #Fields: directive. Parquet and Avro read the file’s metadata and expose the supported scalar columns with the reader’s .NET types and declared nullability. They report unsupported fields through diagnostics. Select the returned column names when opening a data reader on a file that also has unsupported fields. Use the Arrow APIs for the full nested schema and values. Declared schema results have no sampling counts.

Parquet ReadSchema and ToDataReader name scalar leaves inside non-repeated structs by their full path, such as BillingAddress.Zip and ShippingAddress.Zip. Pass these exact, case-sensitive paths to select and order the columns. ADO.NET represents a null leaf or null containing struct as DBNull.Value. If two columns produce the same name, ToDataReader rejects the ambiguous schema. For files with lists or maps, explicitly select the scalar columns or use the Arrow API.


Infer a schema and dialect
Select JSON nodes as rows