filesql

September 3, 2026 · View on GitHub

Mentioned in Awesome Go Go Reference MultiPlatformUnitTest Coverage

logo

filesql loads files into an in-memory SQLite database. Open CSV, TSV, LTSV, JSON, JSONL, Parquet, XLSX, ACH, or Fedwire inputs, then query them with normal SQLite syntax.

The same module also includes prep, which cleans and validates rows before they become tables.

sqly is the shell built on the same core.

Documentation: https://pkg.go.dev/github.com/nao1215/filesql

Try it in 30 seconds

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/nao1215/filesql"
)

func main() {
	db, err := filesql.Open(context.Background(), "users.csv")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	var n int
	if err := db.QueryRow("SELECT COUNT(*) FROM users WHERE age > 25").Scan(&n); err != nil {
		log.Fatal(err)
	}
	fmt.Println(n)
}

The table is named after the file, so users.csv is users. Join across formats by opening more of them:

db, err := filesql.Open(ctx, "users.csv", "orders.jsonl", "returns.parquet")

Why filesql?

filesql is for cases where the data is already in a file and the fastest useful tool is SQL.

  • Open files as tables without setting up a server.
  • Join across CSV, TSV, LTSV, JSON, JSONL, Parquet, XLSX, ACH, and Fedwire.
  • Keep edits in memory until you decide to save them.
  • Clean inputs with prep before loading them.

Features

  • Query file data with standard SQLite syntax, including joins, CTEs, and json_extract().
  • Optionally query with MySQL, PostgreSQL, or GoogleSQL syntax via WithDialect (translated to SQLite).
  • Read from file paths, directories, io.Reader, and embed.FS.
  • Handle compressed CSV, TSV, LTSV, JSON, JSONL, Parquet, and XLSX files transparently.
  • Write csv, tsv, and ltsv output in Shift-JIS, EUC-JP, ISO-2022-JP, or UTF-16 as well as UTF-8.
  • Load into a new in-memory database or into a *sql.DB you already manage.
  • Save changes with DumpDatabase, EnableAutoSave, or EnableAutoSaveOnCommit.
  • Stay in one module for loading (filesql) and cleanup (prep).

Installation

go get github.com/nao1215/filesql

Requirements:

  • Go 1.25.13 or later, or 1.26.6 or later on the 1.26 line
  • Linux, macOS, or Windows

The patch releases are the point: 1.25.13 and 1.26.6 are the ones carrying the standard library fixes for GO-2026-6088 (encoding/xml) and GO-2026-5972 (encoding/asn1), and filesql reaches encoding/xml on every XLSX it reads. An earlier 1.26 is not supported, even though it is a later release than 1.25.13.

Recipes

filesql.Open takes a context, so a load can be given a timeout or canceled with the request it belongs to. Pass context.Background() when it needs neither.

Query files with SQLite

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/nao1215/filesql"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	db, err := filesql.Open(ctx, "users.csv", "orders.jsonl")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	rows, err := db.QueryContext(ctx, `
		SELECT
			u.name,
			COUNT(*) AS order_count
		FROM users u
		JOIN orders o
			ON u.id = json_extract(o.data, '$.user_id')
		GROUP BY u.name
		ORDER BY order_count DESC, u.name
	`)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	for rows.Next() {
		var name string
		var orderCount int
		if err := rows.Scan(&name, &orderCount); err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s: %d\n", name, orderCount)
	}
	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}
}

Query with another SQL dialect

By default queries use SQLite syntax. WithDialect lets you write queries in MySQL, PostgreSQL, or GoogleSQL (BigQuery / Cloud Spanner) instead; filesql translates them to SQLite before running. Loading files always uses SQLite, so only the queries you write are affected.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/nao1215/filesql"
	"github.com/nao1215/filesql/dialect"
)

func main() {
	ctx := context.Background()

	db, err := filesql.NewBuilder().
		AddPath("users.csv").
		WithDialect(dialect.PostgreSQL).
		Open(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// PostgreSQL syntax: "::" cast and ILIKE.
	rows, err := db.QueryContext(ctx,
		"SELECT name, age::text FROM users WHERE name ILIKE 'a%'")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	// ...
	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}
	fmt.Println("ok")
}

Translation implements a stated subset of SQL rather than a full emulator: the query is parsed, rewritten and written back out, so common incompatibilities (identifier quoting, DATE_ADD, SPLIT_PART, SAFE_DIVIDE, EXTRACT, casts, …) are rewritten or backed by helper functions, and a construct with no SQLite equivalent (for example QUALIFY, DISTINCT ON, MySQL's XOR, or MySQL's 0x literal, which is a string in one place and a number in another) returns a clear error. Nothing is passed to SQLite untranslated: a query outside the subset is refused with the line and column of the construct. What the translation cannot reach is SQLite's type system: there is no boolean, no interval and no array, so a comparison answers 1 or 0, an INTERVAL literal works only in date arithmetic, and a construct whose result would be one of those types is refused rather than answered. A non-SQLite dialect cannot be combined with auto-save. See the dialect package for the full list of supported translations.

Load into a database you already own

package main

import (
	"context"
	"database/sql"
	"log"

	"github.com/nao1215/filesql"
	_ "modernc.org/sqlite"
)

func main() {
	db, err := sql.Open("sqlite", ":memory:")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// A plain ":memory:" database is private per connection.
	db.SetMaxOpenConns(1)

	if err := filesql.LoadInto(context.Background(), db, "users.csv", "payments.parquet"); err != nil {
		log.Fatal(err)
	}
}

Clean rows before loading with prep

Use prep when the file needs normalization before it becomes a table: trimming, case normalization, defaults, and validation errors with row numbers.

package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"strings"

	"github.com/nao1215/filesql"
	"github.com/nao1215/filesql/prep"
)

type User struct {
	Name  string `prep:"trim" validate:"required"`
	Email string `prep:"trim,lowercase" validate:"required,email"`
	Role  string `prep:"trim,uppercase" validate:"required,oneof=ADMIN USER"`
}

func main() {
	csvData := `name,email,role
  Alice  ,ALICE@EXAMPLE.COM, admin
Bob,bob@example.com,user
`

	processor := prep.NewProcessor(prep.FileTypeCSV)
	var users []User

	reader, result, err := processor.Process(strings.NewReader(csvData), &users)
	if err != nil {
		log.Fatal(err)
	}
	if result.HasErrors() {
		log.Fatal(result.ValidationErrors())
	}

	fmt.Println(users[0].Name, users[0].Email, users[0].Role)

	cleaned, err := io.ReadAll(reader)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Print(string(cleaned))

	ctx := context.Background()
	db, err := filesql.NewBuilder().
		AddReader(strings.NewReader(string(cleaned)), "users", filesql.FileTypeCSV).
		Open(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
}

Supported File Formats

ExtensionFormatNotes
.csvCSVHeader row becomes column names
.tsvTSVTab-separated text
.ltsvLTSVLabeled tab-separated text
.jsonJSONQuery nested data with json_extract()
.jsonlJSONLOne JSON value per line
.parquetParquetColumnar format
.xlsxExcel XLSXOne sheet becomes one table, named file_sheet (just file when the sheet repeats it). A workbook handed to AddReader hangs its sheets off the table name given there instead of off a file name, so a workbook added as book loads as book_Sheet1 and as plain book when the sheet is itself named book. ExcelSheetTableNames works out the same names when it is given that table name in place of a path, and sqlite_master has them after a load. Every sheet that names a column is loaded by default, a blank scratch sheet being passed over; see Excel Sheet Visibility
.achACH (NACHA)One table per record kind; see ACH and Fedwire
.fedFedwireOne message becomes one row 326 columns wide; see ACH and Fedwire

Two inputs are the same source only when they are in the same place. dir/users.csv and dir/users.csv.gz are one dataset offered twice, and the plain one is read; a/users.csv and b/users.csv are two files, and both are loaded. What happens when both then want the table users is the loading API's business: Open builds a fresh database and refuses it with ErrDuplicateTable, while LoadInto and LoadIntoTx load into a database you own and keep their last-wins rule, so the later input replaces the table. Neither one silently drops a file. Table names are compared the way SQLite compares identifiers, with ASCII case folded, so Users.csv and users.csv want the same table too.

Column names inside a file follow two separate rules, and a header that breaks either is refused with ErrDuplicateColumn before it reaches SQLite. Two names differing only in ASCII letter case are one column, because SQLite is what holds them — ID and id are a duplicate — and the folding stops at ASCII as SQLite's does, so ä and Ä stay two columns. Two names identical after their surrounding whitespace is trimmed are one column too: name and " name " are one name typed twice. The rules are applied one at a time and never combined, so " A" beside a is accepted, which is what SQLite does with it as well. LTSV carries its labels on every record rather than in a header, so the same check runs per record.

Compressed wrappers are supported for CSV, TSV, LTSV, JSON, JSONL, Parquet, and XLSX: .gz, .bz2, .xz, .zst, .z, .snappy, .s2, .lz4.

ACH and Fedwire do not use external compression wrappers.

An xz or zstd stream states in its header how much working memory its decoder must hold, and a decoder allocates it before reading any data. filesql caps that at a 256 MiB xz dictionary, four times what xz -9 declares, and a 128 MiB zstd window, which is the largest the zstd CLI reaches on its own. A damaged file therefore costs a fixed ceiling rather than whatever its header names. The zstd cap holds for every frame; the xz one is read from the first block of the first stream, so a later block or a concatenated second stream is not covered.

Format and compression are separate. FileType names the format only — FileTypeCSV is a CSV whether or not a codec wraps it — and a path says which codec that is. A reader has no path, so AddReader takes the codec as an option:

gz, err := os.Open("users.csv.gz")
if err != nil {
	return err
}
defer gz.Close()

builder.AddReader(gz, "users", filesql.FileTypeCSV, filesql.WithCompression(filesql.CompressionGZ))

Without WithCompression the reader's bytes are read as the format directly, so the ordinary three-argument call is unchanged.

Behavior and limits

The rules the loader and the writers follow are documented beside the API they belong to, so they are in reach while the call is being written: column typing and what a blank cell means, memory and chunked loading, sharing a database across goroutines, what each way of saving writes, Excel sheet visibility, and the ACH and Fedwire write-back. Read them at pkg.go.dev/github.com/nao1215/filesql, or with go doc github.com/nao1215/filesql.

Examples

API example index

The GoDoc examples are fully tested with go test. The tables below show the fastest path from a feature name in the README to the exact example function in the repo.

filesql

FeatureExample functionSource
Open files and query themExampleOpen, ExampleOpen_timeoutexample_api_test.go, example_test.go
Load files into an existing *sql.DBExampleLoadInto, ExampleDBBuilder_LoadIntoexample_api_test.go
Load into a transaction you ownExampleDBBuilder_LoadIntoTxexample_api_test.go
Load into your own database, edit, and save it backExampleLoadInto_dumpDatabaseexample_api_test.go
Build from readers, paths, or embedded FSExampleNewBuilder, ExampleDBBuilder_AddReader, ExampleDBBuilder_AddPath, ExampleDBBuilder_AddFSexample_test.go
Read a compressed readerExampleDBBuilder_AddReader_compressedexample_test.go
Tune chunked loadingExampleDBBuilder_SetDefaultChunkSizeexample_api_test.go
Handle malformed rowsExampleDBBuilder_WithMalformedRowPolicyexample_api_test.go
Count the rows a skip policy discardedExampleDBBuilder_SkippedRowsexample_api_test.go
Query with MySQL, PostgreSQL, or GoogleSQL syntaxExampleDBBuilder_WithDialectexample_api_test.go
Load only the sheets a workbook showsExampleDBBuilder_WithExcelSheetPolicyexample_api_test.go
Report a workbook's sheets and their visibilityExampleExcelSheetsInFile, ExampleExcelSheetsInReaderexample_api_test.go
Check a workbook's sheets for table names that collideExampleExcelSheetTableNamesexample_api_test.go
Attach a slog loggerExampleDBBuilder_WithLoggerexample_api_test.go
Open a database that refuses writesExampleDBBuilder_OpenReadOnlyexample_api_test.go
Save on close or commitExampleDBBuilder_EnableAutoSave, ExampleDBBuilder_EnableAutoSaveOnCommitexample_api_test.go, example_test.go
Export under a deadlineExampleDumpDatabase_deadlineexample_api_test.go
Export tables with format/compression/encoding/line-ending optionsExampleDumpDatabase, ExampleNewDumpOptions, ExampleDumpOptions_WithFormat, ExampleDumpOptions_WithCompression, ExampleDumpOptions_WithEncoding, ExampleDumpOptions_WithLineEndingexample_api_test.go, example_test.go
Compress a stream, or open a compressed fileExampleCompressionType_NewWriter, ExampleCompressionType_NewReader, ExampleOpenReaderexample_api_test.go
Strip compression suffixesExampleRemoveCompressionExtensionexample_api_test.go
Write an ACH or Fedwire file back after editing itExampleDumpACH, ExampleDumpFedWireexample_api_test.go
Write one back when the database came from an io.ReaderExampleDumpACHWithSource, ExampleDumpFedWireWithSourceexample_api_test.go
Load a compressed stream whose name is not a pathExampleWithCompressionexample_api_test.go
Tell which of several inputs failed to loadExampleParseErrorexample_api_test.go
Inspect enum namesExampleMalformedRowPolicy_String, ExampleExcelSheetPolicy_String, ExampleFileType_String, ExampleCompressionType_String, ExampleEncoding_String, ExampleLineEnding_String, ExampleOutputFormat_Stringexample_api_test.go

prep

FeatureExample functionSource
Strict tag parsing for invalid prep/validate tagsExampleWithStrictTagParsingprep/example_api_test.go
Keep only valid rows in the output streamExampleWithValidRowsOnlyprep/example_api_test.go
Clean CSV data into structs and a reusable readerExampleProcessor_Processprep/example_api_test.go
Unwrap a codec before preprocessingExampleProcessor_Process_compressedprep/example_api_test.go
Convert JSON arrays into JSONL outputExampleProcessor_Process_jsonprep/example_api_test.go
Stream cleaned output into any writerExampleProcessor_ProcessToWriterprep/example_api_test.go
Inspect validation countsExampleProcessResult_InvalidRowCount, ExampleProcessResult_HasErrorsprep/example_api_test.go
Read validation error detailsExampleProcessResult_ValidationErrorsprep/example_api_test.go
See a struct field that names no column refusedExampleProcessor_Process_unknownColumnprep/example_api_test.go
Give a field a default when the column is absentExampleProcessor_Process_defaultForAbsentColumnprep/example_api_test.go
Compare one column against anotherExampleProcessor_Process_crossFieldprep/example_api_test.go
Require a column only when other columns say soExampleProcessor_Process_conditionalRequiredprep/example_api_test.go
Forbid a column when other columns say soExampleProcessor_Process_conditionalExcludedprep/example_api_test.go
Validate IP address and port columnsExampleProcessor_Process_networkColumnsprep/example_api_test.go
Validate JSON, time zone, version and digest columnsExampleProcessor_Process_encodedColumnsprep/example_api_test.go
Verify a check digit on an ISBN or a card numberExampleProcessor_Process_checksummedIdentifiersprep/example_api_test.go
Validate country and currency code columnsExampleProcessor_Process_codeColumnsprep/example_api_test.go
Refuse a duplicate in a key columnExampleProcessor_Process_uniqueColumnprep/example_api_test.go
Validate DNS label, color and numeric currency columnsExampleProcessor_Process_labelColorAndNumericCodeprep/example_api_test.go
Read preprocessing error detailsExampleProcessResult_PrepErrorsprep/example_api_test.go
Check output and original formatsExampleProcessResult_OutputFormatprep/example_api_test.go
Load a processed reader into filesqlExampleProcessor_Process_intoFilesqlprep/example_api_test.go
Rewind and reread the processed streamExample_streamSeekprep/example_api_test.go

dialect

FeatureExample functionSource
Translate a query into SQLite SQL and recognize what has no equivalentExampleTranslatedialect/example_test.go
Run PostgreSQL constructs SQLite has no form forExampleTranslate_postgreSQLdialect/example_test.go
Run BigQuery constructs SQLite has no form forExampleTranslate_googleSQLdialect/example_test.go
Tell a construct SQLite cannot express from one outside the supported subsetExampleTranslate_unsupportedFeaturedialect/example_test.go
Turn a user-supplied dialect name into a DialectExampleParsedialect/example_test.go
List the built-in dialects and spell one for a personExampleDialects, ExampleDialect_DisplayNamedialect/example_test.go

Integration examples

The examples directory shows how to use filesql with regular Go database tooling:

ExampleDescription
basicBasic CSV queries
multi-formatJoin across CSV, TSV, and LTSV
sqlcUse filesql with sqlc
gormUse filesql with GORM
sqlxUse filesql with sqlx
bunUse filesql with Bun
squirrelUse filesql with Squirrel
entUse filesql with Ent
ProjectDescription
sqlyInteractive shell for ad-hoc SQL against files
filesql/prepRow cleanup and validation before SQL

Contributing

Contributions are welcome. See CONTRIBUTING.md before sending a PR.

Support

If filesql is useful in your work:

License

filesql is released under the MIT License.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

CHIKAMATSU Naohiro
CHIKAMATSU Naohiro

💻 📖
Sai Asish Y
Sai Asish Y

📖
Krishna lokhande
Krishna lokhande

🐛