Skip to content

Sources

File formats and transforms

CSV quoting and encodings, Excel, XML and ZIP files, headerless columns, and per-file transforms.

Format-specific options and per-file Python transforms for files sources.

A field starting with quote (") may hold the delimiter and line breaks, with a quote inside doubled ("say ""hi"""). A quote inside an unquoted field (12" pipe) is plain text.

Setting Effect
csv = { escape = "\\" } A backslash in a quoted field escapes the next character: "said \"hi\"" reads as said "hi". None by default, so "C:\exports\" reads as written.
csv = { quote = false } Quotes are plain characters; fields split on the delimiter only.

Text after a closing quote, or a file ending inside a quoted field, fails the file: a.csv: line 2: text after a closing quote or unterminated quoted field. The error suggests these settings, which SQLLoad never guesses.

csv.encoding is utf-8 (default), cp1252, latin-1, ascii, utf-16le, utf-16be, or any Encoding Standard label, such as iso-8859-15 or shift_jis; an unknown name fails plan. csv.skip_rows skips lines before the header, such as a title:

csv = { encoding = "cp1252", skip_rows = 2 }

A byte the encoding doesn’t define fails the table with the file, line and byte: a.csv: line 3, byte 15: 0x81 is not valid windows-1252. Invalid UTF-8 suggests setting csv.encoding. Error line numbers count skipped lines; a file whose lines end in a bare \r fails.

A file read as latin-1, cp1252 or iso-8859-15 also fails when its bytes say otherwise, naming the encoding to set: 0x80 to 0x9F in latin-1 or iso-8859-15 suggest cp1252, and mostly valid UTF-8 (Société) suggests utf-8. encoding_check = false turns this off.

A file labelled ISO-8859-1 holding € or Œ is usually iso-8859-15 or cp1252. For Œ in BŒUF, grep -a 'B.UF' a.csv | xxd shows BC for iso-8859-15, 8C for cp1252 and C5 92 for UTF-8.

Snowflake reading CSV itself supports utf-8, latin-1, windows-1250 to -1256, iso-8859-2, -5, -6, -7 and -15, koi8-r, utf-16le and utf-16be; any other fails plan for that route. It loads undefined bytes as control characters. With encoding_check on, SQLLoad reads latin-1, cp1252 and iso-8859-15 files itself.

SQLLoad reads these itself, each workbook, XML document or ZIP member whole, up to 1 GiB.

Excel (format = "xlsx") loads one sheet: xlsx.sheet, or the first. .xls isn’t read.

  • skip_rows skips rows at the top; the next names the columns unless header = false, when they are column_1, column_2, … or column_names. Empty rows are left out.
  • Cells keep their type: whole numbers int, others float, TRUE/FALSE bool, date-formatted cells date, timestamp_ntz or time. Empty cells and errors such as #N/A are NULL; formulas load cached values. A value that doesn’t fit its sampled type fails with its row unless columns pins one.
  • A sheet over 20 million values fails.

xlsx.range reads an A1 range, as Google Sheets does: 'Q4 Report'!B3:H, or A3:C to the last row of the first sheet. It replaces sheet and skip_rows; setting either with it fails plan. Its first row with a value names the columns, or header_row, counted from the range’s first row:

# sheet, first row and columns in one place
xlsx = { range = "Prices!A3:C" }
# first sheet, columns A to C, no header row
xlsx = { range = "A:C", header = false }
column_names = ["metric", "category", "value"]

XML (format = "xml") loads each element at xml.record_path as a row, its attributes and children as columns, without namespace prefixes:

[sources.customers]
kind = "files"
connection = "lake"
path = "drop/"
format = "xml"
xml = { record_path = "customers/customer", lists = ["phone"] }
description = "Customers from the nightly XML export."
write_strategy = "replace"
  • A repeated child, or one in lists, loads as a JSON array; a child with its own attributes or children as a JSON object.
  • Text is trimmed and typed as CSV fields are; xsi:nil="true" is NULL.
  • Malformed XML, two attributes (or an attribute and a child) of one name, a UTF-16 document, or a record_path the document doesn’t have fails, with the line.

ZIP archives (compression = "zip"): each member matching the format’s suffix and members loads as a file, recorded as <archive>.zip/<member>. Define one source per format:

[sources.orders]
kind = "files"
connection = "lake"
path = "drop/"
format = "csv"
compression = "zip"
members = "*.csv"
description = "Orders from the nightly ZIP export."
write_strategy = "replace"
[sources.lines]
kind = "files"
connection = "lake"
path = "drop/"
format = "xlsx"
compression = "zip"
xlsx = { sheet = "Lines" }
description = "Order lines from the workbook in the nightly ZIP export."
write_strategy = "replace"

A member’s version is its archive’s, so under append a rewritten archive fails the table. An archive naming two members alike fails.

column_names names the columns of CSV or Excel files with header = false, in order:

[sources.metrics]
kind = "files"
connection = "inbox"
format = "xlsx"
xlsx = { range = "A:C", header = false }
column_names = ["metric", "category", "value"]
write_strategy = "merge"
unique_key = ["metric", "category"]
[sources.metrics.tables.metrics]
columns = { value = "decimal(12,2)" }

The count must match each file’s columns, or the range’s width, exactly; otherwise plan or the load fails, naming both counts. With a header row, use rename.

A transform is a Python function that makes each file loadable: fixing headers, dropping totals rows or unpivoting a sheet; keep business logic in SQL. Register it with @sl.transform (or @sl.transform("name")) in a module under python_paths, and name it in transform. It needs the sqlload Python package and pyarrow, not the standalone sqlload-cli. The same functions reshape Google Sheets ranges and sl.http pages.

python_paths = ["transforms"]
[sources.metrics]
kind = "files"
connection = "lake"
path = "drop/metrics/"
format = "csv"
description = "Monthly metrics, one file a month."
write_strategy = "append"
transform = "metric_rows"
transforms/metrics.py
import pyarrow as pa
import sqlload as sl
@sl.transform
def metric_rows(rows: pa.Table, ctx: sl.TransformContext):
for row in rows.to_pylist():
if row["metric"] in (None, "Total"):
continue
for month, value in row.items():
if month != "metric" and value is not None:
yield {"metric": row["metric"], "month": month,
"value": float(value)}

It’s called once per file (and ZIP member) as f(rows, ctx): rows is a pyarrow.Table, and ctx an sl.TransformContext with unit ("file"), path (as in _sqlload_source_file), modified_at, sheet, range, skip_rows and table. It returns or yields what a Python table may: dicts, a pyarrow.Table, or a polars or pandas DataFrame. For large files, return Arrow or a DataFrame:

transforms/metrics.py
import polars as pl
import pyarrow as pa
import sqlload as sl
@sl.transform
def metric_rows(rows: pa.Table, ctx: sl.TransformContext):
metric = pl.col("metric")
frame = pl.from_arrow(rows).filter(metric.is_not_null()
& (metric != "Total"))
tidy = frame.unpivot(index="metric", variable_name="month",
value_name="value")
return tidy.drop_nulls("value")
  • Values arrive so none fails the read: CSV, XML and JSON as text (nested JSON as JSON), Parquet and Excel as stored. Dicts are typed as JSON values are; pin exact types in columns.

  • exclude_columns and allow_columns apply before the function; columns and mask after.

  • plan runs it on the newest file by name and takes the table’s columns from the result, so keep it free of side effects. Later files may return fewer columns (loaded as NULL); a new column fails, naming it, until columns pins it.

  • A name no function is registered under fails plan, listing the registered ones, as does a name registered twice, naming both files:

    $ sqlload plan
    Errors (1)
    └── raw.metrics
    └── reason transform metrics_rows: no function is registered as metrics_rows with @sl.transform; the registered ones are metric_rows
    ✗ Plan failed 1 error
  • A transform that raises, or returns something else, fails that file and stops the table; earlier commits stay.

  • SQLLoad always reads the files itself. _sqlload_source_row numbers the rows returned.

  • Each file is held in memory while the function runs. After filtering a pandas DataFrame, call reset_index(drop=True).

Each commit records the transform’s version, a hash of the function’s file, in the table’s run log _sqlload_runs. apply or the first load creates it; then a load only inserts, so a role without CREATE can load. After a change, plan warns while the table holds files an earlier version loaded:

$ sqlload plan -v
…
└── transform changed: 2 files loaded under an earlier version (--reload transforms every file again)
…

sqlload load <table> --reload transforms every file again. Changes in imported modules aren’t noticed. A function without a source file, such as one in a notebook, is versioned by its bytecode, so upgrading Python reports it changed.