gin

package module
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 34 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GIN Index

CI Go Reference

See CONTRIBUTING.md for local contributor workflows and SECURITY.md for disclosure guidance.

A Generalized Inverted Index (GIN) for JSON data, designed for row-group pruning in columnar storage formats like Parquet.

Features

  • String indexing - Exact match and IN queries on string fields
  • Numeric indexing - Range queries (GT, GTE, LT, LTE) with per-row-group min/max stats
  • Field transformers - Convert values (e.g., date strings to epoch) for efficient range queries
  • Trigram indexing - Full-text CONTAINS queries using n-gram matching
  • Regex support - Pattern matching with trigram-based candidate selection
  • Null tracking - IS NULL / IS NOT NULL predicates
  • Bloom filter - Fast-path rejection for non-existent values
  • HyperLogLog - Efficient cardinality estimation
  • Compression - zstd-compressed binary serialization
  • Parquet integration - Build from Parquet, embed in metadata, sidecar files, S3 support
  • CLI tool - Command-line interface for build, query, info, and extract operations

Why GIN Index?

A serverless pruning index for data lakes - the GIN index is a compact, immutable index designed to answer one question: "Which row groups might contain my data?"

The Problem

Querying large data lakes is expensive. When you search for trace_id=abc123 across millions of Parquet files, traditional approaches either:

  • Full scan - Read every row group (~TB of data, high latency, high cost)
  • Database approach - Run PostgreSQL/Elasticsearch cluster (~ms latency, operational burden)
  • Parquet stats - Use built-in min/max (useless for high-cardinality strings)
The Solution
┌─────────────────────────────────────────────────────────────────────┐
│                    Serverless Row-Group Pruning                      │
├─────────────────────────────────────────────────────────────────────┤
│                                                                      │
│   1. Cache index anywhere         2. Prune locally     3. Read only  │
│      (<1MB for millions of files)    (~1µs)              matching RGs│
│                                                                      │
│   ┌──────────────┐               ┌──────────────┐    ┌────────────┐ │
│   │  memcached   │  ─────────▶   │  GIN Index   │ ─▶ │ S3/GCS     │ │
│   │  nginx       │    decode     │  Evaluate()  │    │ [RG 5, 23] │ │
│   │  CDN edge    │               │              │    │            │ │
│   │  localStorage│               │ Result: 3    │    │ Skip 99%   │ │
│   └──────────────┘               │ row groups   │    │ of data    │ │
│                                  └──────────────┘    └────────────┘ │
│                                                                      │
└─────────────────────────────────────────────────────────────────────┘
Key Advantages
Challenge PostgreSQL GIN Elasticsearch This GIN Index
Deployment Database cluster Search cluster Just bytes - cache anywhere
Query latency ~1ms ~5-10ms ~1µs - client-side
High cardinality Index bloat Shard overhead adaptive-hybrid hot-value pruning
Index size MB-GB GB ~30KB per 1K row groups
Arbitrary JSON Schema required Mapping required Auto-discovered paths
Designed For
  • Log/observability platforms - Query by trace_id, request_id, arbitrary labels
  • Vector databases - Pre-filter segments before expensive ANN search
  • Data lake query engines - Pruning index for DuckDB, Trino, Spark
  • Edge/serverless - Cache index at CDN edge, query without backend

The index decouples pruning (which row groups to read) from execution (DuckDB, Trino, Spark). Your query engine handles the actual data reading - this index just tells it where to look.

Installation

go get github.com/amikos-tech/ami-gin
Optional SIMD parser

Ordinary builds and the default NewBuilder remain stdlib-only. To opt in, compile with -tags simdjson, construct the same-package adapter backed by github.com/amikos-tech/pure-simdjson (version pinned in go.mod), and select it explicitly:

go build -tags simdjson ./...
package app

import (
	"errors"

	gin "github.com/amikos-tech/ami-gin"
)

func newSIMDBuilder(
	config gin.GINConfig,
	numRGs int,
) (*gin.GINBuilder, gin.CloseableParser, error) {
	p, err := gin.NewSIMDParser()
	if err != nil {
		return nil, nil, err
	}

	builder, err := gin.NewBuilder(config, numRGs, gin.WithParser(p))
	if err != nil {
		return nil, nil, errors.Join(err, p.Close())
	}
	return builder, p, nil
}

The caller must retain the returned parser and close it after the builder has stopped parsing. See the SIMD deployment guide for native bootstrap, ownership, corporate mirrors, air-gapped loading, artifact integrity, explicit stdlib fallback, and numeric limits.

Quick Start

package main

import (
    "fmt"
    gin "github.com/amikos-tech/ami-gin"
)

func main() {
    // Create builder for 3 row groups
    builder, err := gin.NewBuilder(gin.DefaultConfig(), 3)
    if err != nil {
        panic(err)
    }

    // Add documents to row groups
    builder.AddDocument(0, []byte(`{"name": "alice", "age": 30}`))
    builder.AddDocument(1, []byte(`{"name": "bob", "age": 25}`))
    builder.AddDocument(2, []byte(`{"name": "alice", "age": 40}`))

    // Build index
    idx := builder.Finalize()

    // Query: find row groups where name = "alice"
    result := idx.Evaluate([]gin.Predicate{
        gin.EQ("$.name", "alice"),
    })
    fmt.Println(result.ToSlice()) // [0, 2]
}

Known limitations

GIN Index v0.2.0 expands the original predicate surface with adaptive high-cardinality pruning and derived representations, but it still intentionally excludes a few deferred capabilities.

  • OR/AND composites are not part of the v0.2.0 query API yet.
  • Index merge across multiple index files is intentionally deferred beyond v0.2.0.
  • Query-time transformers are not supported in v0.2.0; transformations must happen at index-build time.

Serialized index compatibility remains strict: Decode() rejects older payload versions. The current wire format is v11, written by v1.2.0, v1.3.0, v1.4.0, v1.5.0 and v1.6.0; indexes written by earlier releases (v0.1.0 wrote v3, v0.2.0 wrote v9, v1.1.0 and v1.1.1 wrote v10) must be rebuilt.

Query Types

Equality
gin.EQ("$.status", "active")
gin.NE("$.status", "deleted")
gin.IN("$.status", "active", "pending", "review")
gin.NIN("$.status", "deleted", "archived")  // NOT IN

NE and NIN select a row group when the path is present in it and some document holds a value other than the given ones. When several documents share one DocID (see DocID Codec), the index records which row groups hold more than one distinct value and how many, so a row group containing both deleted and active is kept for NE("$.status", "deleted") and dropped for NIN("$.status", "deleted", "active"). Numeric values are counted only when a row group holds a single one; a row group whose numbers span a range is always kept. In modes without an exact term index (bloom-only paths, unpromoted adaptive terms) the negation cannot be proven and falls back to every row group where the path is present.

Numeric Range
gin.GT("$.price", 100.0)    // price > 100
gin.GTE("$.price", 100.0)   // price >= 100
gin.LT("$.price", 500.0)    // price < 500
gin.LTE("$.price", 500.0)   // price <= 500

// Combined range
idx.Evaluate([]gin.Predicate{
    gin.GTE("$.price", 100.0),
    gin.LTE("$.price", 500.0),
})
Derived Representation Queries

Derived representations add companion indexes without dropping the raw source value. Raw-path queries stay raw by default; query a companion explicitly with gin.As(alias, value). Hidden internal target paths are not part of the public query contract.

config, _ := gin.NewConfig(
    gin.WithISODateTransformer("$.created_at", "epoch_ms"),
    gin.WithToLowerTransformer("$.email", "lower"),
    gin.WithEmailDomainTransformer("$.email", "domain"),
    gin.WithRegexExtractTransformer("$.message", "error_code", `ERROR\[(\w+)\]:`, 1),
)
builder, _ := gin.NewBuilder(config, numRGs)

builder.AddDocument(0, []byte(`{
    "created_at": "2024-07-10T09:00:00Z",
    "email": "Alice@Example.COM",
    "message": "ERROR[E1001]: Connection timeout"
}`))

idx := builder.Finalize()

// Raw source-path queries still use the original value.
raw := idx.Evaluate([]gin.Predicate{
    gin.EQ("$.created_at", "2024-07-10T09:00:00Z"),
})

// Alias queries opt into the derived companion explicitly.
july2024 := float64(time.Date(2024, 7, 1, 0, 0, 0, 0, time.UTC).UnixMilli())
dateResult := idx.Evaluate([]gin.Predicate{
    gin.GTE("$.created_at", gin.As("epoch_ms", july2024)),
})
lowerResult := idx.Evaluate([]gin.Predicate{
    gin.EQ("$.email", gin.As("lower", "alice@example.com")),
})
domainResult := idx.Evaluate([]gin.Predicate{
    gin.EQ("$.email", gin.As("domain", "example.com")),
})
errorResult := idx.Evaluate([]gin.Predicate{
    gin.EQ("$.message", gin.As("error_code", "E1001")),
})

Built-in additive helpers:

  • Date/time: WithISODateTransformer(path, alias), WithDateTransformer(path, alias), WithCustomDateTransformer(path, alias, layout)
  • String normalization: WithToLowerTransformer(path, alias), WithEmailDomainTransformer(path, alias), WithURLHostTransformer(path, alias)
  • Extracted subfields: WithRegexExtractTransformer(path, alias, pattern, group), WithRegexExtractIntTransformer(path, alias, pattern, group)
  • Numeric companions: WithIPv4Transformer(path, alias), WithSemVerTransformer(path, alias), WithDurationTransformer(path, alias), WithNumericBucketTransformer(path, alias, size), WithBoolNormalizeTransformer(path, alias)

Custom companions:

myTransformer := func(v any) (any, bool) {
    s, ok := v.(string)
    if !ok {
        return nil, false
    }
    return strings.ToUpper(s), true
}

config, _ := gin.NewConfig(
    gin.WithCustomTransformer("$.my_field", "upper", myTransformer),
)

WithCustomTransformer(...) works for in-memory indexes, but opaque custom companions are not serializable. Encode() rejects them because the function cannot be reconstructed on Decode().

Example: IP subnet queries

config, _ := gin.NewConfig(
    gin.WithIPv4Transformer("$.client_ip", "ipv4_int"),
)

start, end, _ := gin.CIDRToRange("192.168.1.0/24")
result := idx.Evaluate([]gin.Predicate{
    gin.GTE("$.client_ip", gin.As("ipv4_int", start)),
    gin.LTE("$.client_ip", gin.As("ipv4_int", end)),
})

Example: Version range queries

config, _ := gin.NewConfig(
    gin.WithSemVerTransformer("$.version", "semver_int"),
)

result := idx.Evaluate([]gin.Predicate{
    gin.GTE("$.version", gin.As("semver_int", float64(2000000))),
})

Example: Case-insensitive email queries

config, _ := gin.NewConfig(
    gin.WithToLowerTransformer("$.email", "lower"),
)

result := idx.Evaluate([]gin.Predicate{
    gin.EQ("$.email", gin.As("lower", "alice@example.com")),
})

Example: Extract error codes from log messages

config, _ := gin.NewConfig(
    gin.WithRegexExtractTransformer("$.message", "error_code", `ERROR\[(\w+)\]:`, 1),
)

result := idx.Evaluate([]gin.Predicate{
    gin.EQ("$.message", gin.As("error_code", "E1234")),
})
Full-Text Search (CONTAINS)
// Uses trigram index for substring matching
gin.Contains("$.description", "hello")
gin.Contains("$.title", "database")  // matches "database", "databases", etc.

Grams are lowercased and filtered with tables frozen at Unicode 15.0.0, so the index and the query derive the same grams on every Go toolchain (#99). The index config can record the table version as trigram_unicode_version; no key means 15.0.0. A reader that does not know the recorded version skips trigram pruning for that index (fail-open), and the other operators still prune. If a later release adds a new table version, upgrade all readers before any writer records it: ami-gin v1.5.x and older readers ignore the field. Rebuild indexes that ami-gin v1.5.x or older built with Go 1.27 or newer over data that can hold Unicode 16 or 17 characters.

Regex Matching
// Uses trigram index for regex candidate selection
gin.Regex("$.message", "ERROR|WARNING")        // Alternation
gin.Regex("$.brand", "Toyota|Tesla|Ford")      // Multiple literals
gin.Regex("$.log", "error.*timeout")           // Prefix + wildcard + suffix
gin.Regex("$.code", "[A-Z]{3}_[0-9]+")         // Pattern with literals

The Regex operator extracts literal strings from regex patterns and uses the trigram index for candidate row-group selection. This enables efficient pruning before actual regex matching.

How it works:

  1. Parse regex pattern and extract literal substrings
  2. For alternations like (error|warn)_message, extracts combined literals: ["error_message", "warn_message"]
  3. Query trigram index for each literal
  4. Union results (OR semantics for alternation)
  5. Row groups not containing any literal are pruned

Limitations:

  • Requires trigram index enabled (EnableTrigrams: true)
  • Literals shorter than trigram length (default: 3) cannot prune
  • Pure wildcard patterns (.*) return all row groups
  • This is candidate selection, not regex execution - actual matching happens at query time
Null Handling
gin.IsNull("$.optional_field")
gin.IsNotNull("$.required_field")

IsNull selects every row group holding at least one document with an explicit JSON null for the path, or without the path. An absent key counts as null, whatever the row group's document count: in a one-document row group and in a 1:1 index too. To cover a document that lacks the path, union the results of separate NE and IsNull evaluations. Evaluate ANDs its predicates, so one call with both would return their intersection.

Nested Fields and Arrays
// Nested objects
gin.EQ("$.user.address.city", "New York")

// Array elements (wildcard)
gin.EQ("$.tags[*]", "important")
gin.IN("$.roles[*]", "admin", "editor")

JSONPath Support

Supported path syntax:

  • $ - root
  • $.field - dot notation
  • $['field'] - bracket notation
  • $.items[*] - array wildcard

Not supported (will error):

  • $.items[0] - array indices
  • $..field - recursive descent
  • $.items[0:5] - slices
  • $[?(@.price > 10)] - filters

Validate paths before use:

if err := gin.ValidateJSONPath("$.user.name"); err != nil {
    log.Fatal(err)
}

Serialization

// Encode to bytes (zstd compressed)
data, err := gin.Encode(idx)

// Save to file
os.WriteFile("index.gin", data, 0644)

// Load and decode
data, _ := os.ReadFile("index.gin")
idx, err := gin.Decode(data)
Encoder memory profile

Encode reuses one zstd encoder per zstd compression mode and encoder profile for the life of the process (adjacent levels within a mode, e.g. 10-19, share one cached SpeedBestCompression encoder). By default that encoder keeps one worker per GOMAXPROCS so parallel encodes at the same level do not queue. Each additional level-15 worker holds about 34 MB of match tables, so a 16-CPU host retains roughly 560 MB after its first level-15 encode even if it only ever encodes one index at a time.

EncoderProfileBoundedMemory keeps a single worker with zstd's lower-memory buffers. Output bytes are identical to the default profile at every level; only retained memory and same-level parallelism change.

// Per call
data, err := gin.EncodeContext(ctx, idx, gin.WithEncodeProfile(gin.EncoderProfileBoundedMemory))

// Or on the config, so WriteSidecar, EncodeToMetadata and S3 sidecars use it too
cfg, err := gin.NewConfig(gin.WithEncoderProfile(gin.EncoderProfileBoundedMemory))

// The helpers also take the per-call option, e.g. for a decoded index,
// which always carries the default profile
err = gin.WriteSidecar("data.parquet", idx, gin.WithEncodeProfile(gin.EncoderProfileBoundedMemory))

// CLI: build, extract and experiment -o all accept -low-memory
gin-index build -c attributes -low-memory data.parquet
gin-index extract -low-memory -o data.parquet.gin data.parquet

To keep nothing live after the call, use EncoderProfileBoundedMemoryUncached:

data, err := gin.EncodeContext(ctx, idx, gin.WithEncodeProfile(gin.EncoderProfileBoundedMemoryUncached))

cfg, err := gin.NewConfig(gin.WithEncoderProfile(gin.EncoderProfileBoundedMemoryUncached))

The library builds the encoder for the call and drops it, so the heap retained after a level-15 encode is near zero. Do not use it in a service that encodes often. Each call pays encoder construction, about 2 ms and 44 MB allocated at level 15 on a small index. N concurrent calls build N encoders, about N x 44 MB at level 15, and the library sets no limit. Output is byte-identical and the wire format does not change. A decoded index carries the default profile, so pass WithEncodeProfile on each call when you re-encode a loaded index. See docs/encoder-profile-benchmarks.md.

A per-call WithEncodeProfile overrides the config profile, including an explicit EncoderProfileDefault. The profile is runtime-only and never serialized; a decoded index always reads EncoderProfileDefault.

Retained heap after the first level-15 encode (make bench-encoder-profile, klauspost/compress v1.19.2):

GOMAXPROCS Default profile Bounded-memory profile
1 51 MB 42 MB
4 153 MB 42 MB
16 561 MB 42 MB

Choose the bounded profile when the process runs under a memory limit or encodes one index at a time; keep the default when several goroutines encode at the same level concurrently and memory is plentiful. Level 3 retains much less under either profile (10 to 37 MB on the high-cardinality fixture, 1 to 2 MB on the small one); its output size is workload-dependent, about 8% larger than level 15 on the high-cardinality fixture but smaller than level 15 on the small fixture, so lowering the level is a different trade-off, not a strict size win or loss. Full numbers, including allocations, elapsed time and compressed size for small and high-cardinality indexes, are in docs/encoder-profile-benchmarks.md.

Parquet Integration

The GIN index integrates directly with Parquet files, supporting three storage strategies:

  1. Sidecar file - Index stored as data.parquet.gin alongside the Parquet file
  2. Embedded metadata - Index stored in Parquet file's key-value metadata
  3. Build-time embedding - Index built and embedded during Parquet file creation
Build Index from Parquet
// Build index from a Parquet file's JSON column
idx, err := gin.BuildFromParquet("data.parquet", "attributes", gin.DefaultConfig())
Sidecar Workflow
// Write index as sidecar file (data.parquet.gin)
err := gin.WriteSidecar("data.parquet", idx)

// Read sidecar
idx, err := gin.ReadSidecar("data.parquet")

// Check if sidecar exists
if gin.HasSidecar("data.parquet") {
    // ...
}
Embedded Metadata Workflow
cfg := gin.DefaultParquetConfig() // MetadataKey: "gin.index"

// Rebuild existing Parquet file with embedded index
err := gin.RebuildWithIndex("data.parquet", idx, cfg)

// Check if Parquet has embedded index
hasIdx, err := gin.HasGINIndex("data.parquet", cfg)

// Read embedded index
idx, err := gin.ReadFromParquetMetadata("data.parquet", cfg)
Auto-Loading (Embedded First, Then Sidecar)
// Tries embedded metadata first, falls back to sidecar
idx, err := gin.LoadIndex("data.parquet", gin.DefaultParquetConfig())
Encode for Parquet Metadata (Build-Time Embedding)

When creating a new Parquet file, you can embed the index during creation:

// Get key-value pair for Parquet metadata
key, value, err := gin.EncodeToMetadata(idx, gin.DefaultParquetConfig())
// key = "gin.index", value = base64-encoded compressed index

// Use with parquet-go writer
writer := parquet.NewGenericWriter[Record](f,
    parquet.KeyValueMetadata(key, value),
)
Batch Processing (Programmatic)

Helper functions for working with multiple files:

// Local filesystem
if gin.IsDirectory("./data") {
    // List all .parquet files in directory
    parquetFiles, err := gin.ListParquetFiles("./data")

    // List all .gin files in directory
    ginFiles, err := gin.ListGINFiles("./data")

    // Process each file
    for _, f := range parquetFiles {
        idx, _ := gin.BuildFromParquet(f, "attributes", gin.DefaultConfig())
        gin.WriteSidecar(f, idx)
    }
}

// S3
s3Client, _ := gin.NewS3ClientFromEnv()

// List all .parquet files under prefix
parquetKeys, err := s3Client.ListParquetFiles("bucket", "data/")

// List all .gin files under prefix
ginKeys, err := s3Client.ListGINFiles("bucket", "data/")
S3 Support

All operations support S3 paths via AWS SDK v2:

// Configure from environment variables:
// AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
// AWS_ENDPOINT_URL (for MinIO, LocalStack), AWS_S3_PATH_STYLE=true
s3Client, err := gin.NewS3ClientFromEnv()

// Build from S3
idx, err := s3Client.BuildFromParquet("bucket", "path/to/data.parquet", "attributes", gin.DefaultConfig())

// Write sidecar to S3
err := s3Client.WriteSidecar("bucket", "path/to/data.parquet", idx)

// Read sidecar from S3
idx, err := s3Client.ReadSidecar("bucket", "path/to/data.parquet")

// Load index (tries embedded, then sidecar)
idx, err := s3Client.LoadIndex("bucket", "path/to/data.parquet", gin.DefaultParquetConfig())
CLI Tool

A command-line tool is provided for common operations:

# Install
go install github.com/amikos-tech/ami-gin/cmd/gin-index@latest

# Build sidecar index
gin-index build -c attributes data.parquet
gin-index build -c attributes -o custom.gin data.parquet

# Build and embed into Parquet file
gin-index build -c attributes -embed data.parquet

# Query index
gin-index query data.parquet.gin '$.status = "error"'
gin-index query data.parquet.gin '$.count > 100'
gin-index query data.parquet.gin '$.name IN ("alice", "bob")'

# Show index info
gin-index info data.parquet.gin

# Extract embedded index to sidecar
gin-index extract -o data.parquet.gin data.parquet

# S3 paths (uses AWS env vars)
gin-index build -c attributes s3://bucket/data.parquet
gin-index query s3://bucket/data.parquet.gin '$.status = "ok"'

Batch Processing (Directory/S3 Prefix):

Process multiple files at once by passing a directory or S3 prefix:

# Build index for all .parquet files in a directory
gin-index build -c attributes ./data/
gin-index build -c attributes -embed ./data/

# Query all .gin files in a directory
gin-index query ./data/ '$.status = "error"'

# Show info for all .gin files
gin-index info ./data/

# S3 prefix - processes all .parquet files under the prefix
gin-index build -c attributes s3://bucket/data/
gin-index query s3://bucket/data/ '$.status = "error"'
gin-index info s3://bucket/data/

# Glob patterns work too
gin-index build -c attributes './data/*.parquet'
gin-index query './data/*.gin' '$.level = "error"'

CLI Query Syntax:

  • Equality: $.field = "value", $.field != "value"
  • Numeric: $.field > 100, $.field >= 100, $.field < 100, $.field <= 100
  • IN/NOT IN: $.field IN ("a", "b"), $.field NOT IN (1, 2, 3)
  • Null: $.field IS NULL, $.field IS NOT NULL
  • Contains: $.field CONTAINS "substring"
  • Regex: $.field REGEX "pattern" (e.g., $.brand REGEX "Toyota|Tesla")

Configuration

config := gin.GINConfig{
    CardinalityThreshold:    10000, // Exact below threshold, adaptive above it
    BloomFilterSize:         65536,
    BloomFilterHashes:       5,
    EnableTrigrams:          true,  // Enable CONTAINS queries
    TrigramMinLength:        3,
    HLLPrecision:            12,    // HyperLogLog precision (4-16)
    PrefixBlockSize:         16,
    AdaptiveMinRGCoverage:   2,     // Promote values seen in at least 2 row groups
    AdaptivePromotedTermCap: 64,    // Keep at most 64 exact hot terms per path
    AdaptiveCoverageCeiling: 0.80,  // Skip terms that cover more than 80% of row groups
    AdaptiveBucketCount:     128,   // Fixed bucket count for long-tail fallback
}

builder, err := gin.NewBuilder(config, numRowGroups)
if err != nil {
    panic(err)
}
High-Cardinality String Modes

GIN Index uses three string-path modes:

  • exact - path cardinality stays under CardinalityThreshold, so every observed value keeps an exact row-group bitmap.
  • adaptive-hybrid - path exceeds CardinalityThreshold, but hot values still retain exact row-group pruning while the long tail falls back to fixed hash buckets.
  • bloom-only - adaptive promotion is disabled, so high-cardinality paths keep only the bloom filter fast-path.

The additive adaptive knobs above control when a hot value is promoted (AdaptiveMinRGCoverage), how many promoted values are kept (AdaptivePromotedTermCap), how broad a promoted value is allowed to be (AdaptiveCoverageCeiling), and how much compact fallback space is reserved for the long tail (AdaptiveBucketCount). This means hot values on a high-cardinality path can still retain exact row-group pruning instead of degrading immediately to bloom-only behavior.

Examples

See the examples directory:

go run ./examples/basic/main.go        # Equality queries
go run ./examples/range/main.go        # Numeric ranges
go run ./examples/transformers/main.go # Date field transformers
go run ./examples/transformers-advanced/main.go # IP, SemVer, email, regex transformers
go run ./examples/fulltext/main.go     # CONTAINS queries
go run ./examples/regex/main.go        # Regex pattern matching
go run ./examples/null/main.go         # NULL handling
go run ./examples/nested/main.go       # Nested JSON and arrays
go run ./examples/serialize/main.go    # Persistence
go run ./examples/full/main.go         # All types and operators
go run ./examples/parquet/main.go      # Parquet integration (sidecar, embedded, queries)

Benchmarks

Run benchmarks with:

go test -bench=. -benchmem -benchtime=1s
Phase 11 Real-Corpus Workflow

The default Phase 11 path stays smoke-only and uses the checked-in fixture at testdata/phase11/github_archive_smoke.jsonl:

go test ./... -run '^$' -bench 'BenchmarkPhase11RealCorpus/tier=smoke' -benchtime=1x -count=1 -benchmem

-benchtime=1x -count=1 is load-bearing here because Phase 11 reports metrics for a finite pre-materialized corpus; rerunning calibration loops would only repeat the same fixture work and distort the intended one-pass numbers.

Opt-in external tiers use the pinned common-pile/github_archive snapshot root and the exact env vars GIN_PHASE11_GITHUB_ARCHIVE_ROOT, GIN_PHASE11_ENABLE_SUBSET, and GIN_PHASE11_ENABLE_LARGE.

Supported acquisition example:

from huggingface_hub import snapshot_download

snapshot_download(
    repo_id="common-pile/github_archive",
    repo_type="dataset",
    revision="93d90fbdbc8f06c1fab72e74d5270dc897e1a090",
    allow_patterns="gharchive/v0/documents/*.jsonl.gz",
)

With the snapshot root exported once:

export GIN_PHASE11_GITHUB_ARCHIVE_ROOT=/path/to/common-pile-github-archive
GIN_PHASE11_ENABLE_SUBSET=1 go test ./... -run '^$' -bench 'BenchmarkPhase11RealCorpus/tier=subset' -benchtime=1x -count=1 -benchmem
GIN_PHASE11_ENABLE_LARGE=1 go test ./... -run '^$' -bench 'BenchmarkPhase11RealCorpus/tier=large' -benchtime=1x -count=1 -benchmem

Tier notes:

  • Smoke: checked-in 640-row synthesized fixture; default path for lightweight local verification.
  • Subset: opt-in only; first 4 shards from gharchive/v0/documents/*.jsonl.gz; moderate local disk, RAM, and runtime cost.
  • Large: opt-in only; first 32 shards from gharchive/v0/documents/*.jsonl.gz; materially higher local disk, RAM, and runtime cost.

Validation contract:

  • Smoke fixture loading validates the full checked-in record shape (id, source, created, text, and the four metadata fields) so fixture drift fails fast.
  • Real-corpus benchmarks validate only the fields each projection indexes. projection=structured requires source, created, metadata.repo, metadata.license, and metadata.license_type; projection=text-heavy requires source, created, text, and metadata.url.

Pinned provenance and interpretation live in docs/prefix-compression-benchmark-results.md and docs/prefix-compression-real-corpus-report.md.

Phase 20 Realistic JSON Workflow

The default Phase 20 corpus is the four checked-in synthesized fixtures in testdata/phase20/*.jsonl: nested high-cardinality, mixed-type arrays, number-heavy, and their combined stream. It runs offline and does not download data.

go test ./... -run '^$' -bench '^BenchmarkPhase20RealisticJSON/tier=smoke' -benchtime=1x -count=1 -benchmem

-benchtime=1x -count=1 provides one reproducible pass over the finite prepared fixture work.

For a deliberate local run against top-level simdjson examples, provide both variables explicitly. The enable flag must be exactly 1; leaving it unset or empty disables the tier, and any other nonempty value is a configuration error.

GIN_PHASE20_ENABLE_SIMDJSON_EXTERNAL=1 GIN_PHASE20_SIMDJSON_DIR=/path/to/simdjson/jsonexamples go test ./... -run '^$' -bench 'BenchmarkPhase20RealisticJSON/tier=external/fixture=local-example' -benchtime=1x -count=1 -benchmem

That selector runs the registered Build, Encode, and Query leaves. Local input may use regular top-level .ndjson, .jsonl, or single-document .json files; symlinks are not followed. Loading is limited to 64 files, 8 MiB total, 2 MiB per document, and JSON depth 64. The Query action derives and resolves an indexed scalar-path presence query from the local input, so it does not expect a particular simdjson example schema.

This repository neither downloads simdjson data nor vendors copied upstream example rows. Any future direct redistribution requires a pinned-revision LICENSE/NOTICE review.

Performance Summary (Apple M3 Max)
Operation Latency Notes
EQ query ~1µs Bloom filter + sorted term lookup
Range query (GT/LT) 4-24µs Min/max stats scan
IN query (10 values) ~8µs Union of EQ results
CONTAINS query 2-17µs Trigram intersection
IsNull/IsNotNull 2-4µs Bitmap lookup
Bloom lookup ~100ns Fast path rejection
AddDocument ~43µs JSON parsing + indexing
Encode (1K RGs) ~4ms zstd compression
Decode (1K RGs) ~2ms zstd decompression
Index Size
Row Groups Encoded Size Per RG
100 6.7 KB 67 bytes
500 18 KB 36 bytes
1,000 30 KB 30 bytes
2,000 51 KB 26 bytes
Scaling Characteristics

Query time scales well with index size:

  • 10 RGs: ~340ns
  • 100 RGs: ~530ns
  • 1,000 RGs: ~680ns
  • 5,000 RGs: ~800ns

Build time is linear with document count and complexity:

  • 100 docs (7 fields): ~1.2ms
  • 1,000 docs: ~6.7ms
  • High cardinality (10K unique values): ~3.3ms per 1K docs
Component Performance
Component Operation Latency
Bloom Filter Add ~100ns
Bloom Filter Lookup ~100ns
RGSet (10K) Intersect ~12µs
RGSet (10K) Union ~10µs
Trigram Add (50 chars) ~16µs
Trigram Search 1-6µs
HyperLogLog Add ~70ns
HyperLogLog Estimate 7-410µs (precision dependent)
Prefix Compress 1K terms ~60µs
Real-World Scenario: 1M Docs / 50K Row Groups

Simulating a log storage scenario:

  • 1M documents across 50K row groups (~20 docs/RG)
  • 10 labels: 2 integers (status_code, duration_ms) + 8 strings
  • Mix of cardinalities: trace_id (high), service (low), host (medium)
  • Trigrams disabled (no FTS)
Metric Value
Index Size 289 KB (0.28 MB)
Bytes per RG 5.9 bytes
Bytes per doc 0.3 bytes
Build time 464ms
Encode 41ms
Decode 41ms

Query Performance:

Query Latency Notes
trace_id=X (high cardinality) 950ns adaptive-hybrid hot-term prune or compact tail fallback
service=api (low cardinality) 6.5µs ~10K RGs match
trace_id=X AND level=error 6µs High card + low card
duration_ms > 5000 244µs Range scan over 50K RGs
service=api AND env=prod AND status>=400 285µs 3 predicates combined

Key takeaway: High-cardinality lookups (trace ID, request ID) are sub-microsecond. The entire index for 1M documents fits in 289 KB - easily cacheable in memory, localStorage, or CDN edge.

Benchmark Categories

The benchmark suite (benchmark_test.go) covers:

  1. Builder Performance - Document ingestion, batch loading, finalization
  2. Query Performance - All operators, parallel queries, multiple predicates
  3. Serialization - Encode/decode latency, compression ratios
  4. Components - Bloom filter, RGSet, trigram, HLL, prefix compression
  5. Scaling - Row group count, document size, cardinality, nesting depth

Comparison with Other Solutions

vs PostgreSQL GIN/JSONB
Aspect This GIN Index PostgreSQL GIN
Query Latency ~1µs (EQ) ~0.7-1.2ms per predicate
Deployment Embedded bytes, no server Requires PostgreSQL server
Cacheability Cache anywhere (nginx, memcached, CDN) Tied to database buffer cache
Index Size 26-67 bytes/row-group Larger, includes posting lists
Range Queries Native min/max stats Poor (GIN doesn't support ranges)
Full-Text Trigram-based CONTAINS Full-featured tsvector/tsquery
ACID No (read-only after build) Full transaction support

PostgreSQL GIN uses Bitmap Index Scans which cost ~0.7-1.2ms each when cached. This index achieves ~1µs queries by being purpose-built for row-group pruning with simpler data structures.

vs Parquet Built-in Statistics
Aspect This GIN Index Parquet Min/Max Stats Parquet Bloom Filters
String Equality Exact term → RG bitmap Only min/max (poor for strings) Yes, but per-column only
CONTAINS/FTS Trigram index No No
Multi-path Queries Single index file Scattered in column chunks Scattered in column chunks
Cardinality HyperLogLog estimates No No
Null Tracking Explicit null/present bitmaps Null count only No
Index Location Footer or sidecar file Column chunk metadata Column chunk metadata

Parquet's built-in bloom filters are effective for single-column equality but require reading multiple column chunks for multi-field queries. This GIN index consolidates all paths into one structure.

vs Delta Lake / Iceberg Data Skipping
Aspect This GIN Index Delta Lake Apache Iceberg
Statistics Per-path term index + min/max First 32 columns min/max Partition-level + column stats
High Cardinality adaptive-hybrid + bloom tail fallback Requires Z-ordering Requires sorting
JSON Support Native path extraction Requires schema Requires schema
Query Planning Client-side, cacheable Spark/engine dependent Engine dependent
Deployment Standalone bytes Delta transaction log Metadata tables

Delta Lake's data skipping relies on Z-ordering for effectiveness with high-cardinality columns. This GIN index handles high-cardinality paths natively with adaptive-hybrid hot-value recovery plus compact fallback for the tail.

vs Elasticsearch
Aspect This GIN Index Elasticsearch
Query Latency ~1µs ~1-10ms (network + processing)
Deployment Embedded, no server Cluster required
Index Size ~30KB for 1K row-groups GB+ for equivalent data
Use Case Row-group pruning Full search engine
Updates Rebuild required Near real-time

Elasticsearch provides millisecond-level latency for searches but requires cluster infrastructure. This index is designed for embedding in data lake metadata.

Key Advantage: High-Cardinality Arbitrary JSON

This index was born from log storage needs - indexing arbitrary attributes/labels where:

  • High cardinality is the norm - trace IDs, request IDs, user IDs, session tokens
  • Schema is unknown - arbitrary key-value labels attached at runtime
  • Queries are selective - "find logs where trace_id=abc123" should be instant

Traditional solutions struggle here:

Challenge PostgreSQL GIN Parquet Stats This GIN Index
trace_id (millions unique) Index bloat, slow writes Min/max useless adaptive-hybrid exact hot values + compact tail fallback
user.email (arbitrary path) Requires schema Column must exist Auto-discovered paths
labels["env"] (dynamic keys) JSONB @> operator (~1ms) Not supported Native path indexing (~1µs)
Mixed types per path Type coercion issues Single type per column Tracks observed types

Log/observability example:

{
  "timestamp": "2024-01-15T10:30:00Z",
  "level": "error",
  "trace_id": "abc123def456",
  "user": {"id": "user_98765", "email": "alice@example.com"},
  "labels": {"env": "prod", "region": "us-east-1", "version": "2.1.0"},
  "message": "Connection timeout to downstream service"
}

Query: trace_id=abc123def456 AND labels.env=prod AND level=error

  • Bloom filter rejects non-matching row groups instantly
  • High-cardinality trace_id can retain exact row-group pruning for hot values via adaptive-hybrid
  • Arbitrary labels.* paths indexed automatically

Vector database metadata filtering:

Vector databases need efficient pre-filtering before similarity search. Without good metadata indexing, you either:

  1. Scan all vectors then filter (slow)
  2. Filter first with poor index (still slow)
  3. Build separate metadata infrastructure (complex)
{
  "id": "doc_12345",
  "embedding": [0.1, 0.2, ...],
  "metadata": {
    "source": "arxiv",
    "year": 2024,
    "authors": ["Alice", "Bob"],
    "topics": ["machine-learning", "transformers"],
    "cited_by": 142,
    "full_text": "We present a novel approach to..."
  }
}

Query: Find similar vectors WHERE metadata.source=arxiv AND metadata.year>=2023 AND metadata.topics[*]=transformers

┌─────────────────────────────────────────────────────────────────┐
│                   Vector DB Hybrid Search                       │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   1. Metadata Filter (GIN Index)         2. Vector Search       │
│   ┌─────────────────────────────┐       ┌──────────────────┐   │
│   │ source=arxiv                │       │                  │   │
│   │ year>=2023        ──────────┼──────▶│  ANN Search      │   │
│   │ topics[*]=transformers      │       │  (only on        │   │
│   │                             │       │   segments 2,5)  │   │
│   │ Result: segments [2, 5]     │       │                  │   │
│   └─────────────────────────────┘       └──────────────────┘   │
│           ~1µs                              search scope        │
│                                             reduced 80%         │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

This enables:

  • Pre-filtering - Prune segments before expensive ANN search
  • Flexible schemas - Each document can have different metadata fields
  • High cardinality - Filter by doc_id, user_id, session_id
  • Range + equality - year>=2023 AND source=arxiv
  • Array membership - topics[*]=machine-learning
  • Full-text on metadata - CONTAINS(full_text, "transformer")
Cacheable Pruning Index

The second differentiator is deployment flexibility:

┌─────────────────────────────────────────────────────────────────┐
│                     Data Lake Query Flow                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Client                                                        │
│     │                                                           │
│     │ 1. Fetch GIN index (cached)                              │
│     ▼                                                           │
│   ┌─────────────────────────────────────────────────────────┐  │
│   │  nginx/memcached/CDN/local cache                        │  │
│   │  ┌─────────────┐                                        │  │
│   │  │ index.gin   │  ← 30KB, serves in <1ms               │  │
│   │  │ (cached)    │                                        │  │
│   │  └─────────────┘                                        │  │
│   └─────────────────────────────────────────────────────────┘  │
│     │                                                           │
│     │ 2. Evaluate predicates locally (~1µs)                    │
│     │    Result: [RG 5, RG 23, RG 47]                          │
│     │                                                           │
│     │ 3. Read only matching row groups from object storage     │
│     ▼                                                           │
│   ┌─────────────────────────────────────────────────────────┐  │
│   │  S3 / GCS / Azure Blob                                  │  │
│   │  ┌─────────────────────────────────────────────────┐    │  │
│   │  │ data.parquet                                    │    │  │
│   │  │  RG 0 ──────── skipped                         │    │  │
│   │  │  RG 5 ◀─────── read                            │    │  │
│   │  │  RG 10 ─────── skipped                         │    │  │
│   │  │  RG 23 ◀─────── read                           │    │  │
│   │  │  RG 47 ◀─────── read                           │    │  │
│   │  │  ...                                            │    │  │
│   │  └─────────────────────────────────────────────────┘    │  │
│   └─────────────────────────────────────────────────────────┘  │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Benefits:

  • No database server - Index is just bytes, evaluate anywhere
  • Cache at edge - nginx, memcached, CDN, browser localStorage
  • Cross-language - Binary format works in any language
  • Offline capable - Cache index locally for disconnected queries
  • Cost efficient - Avoid scanning TB of Parquet data

This architecture is ideal for:

  • Log/observability platforms - Index arbitrary labels, query by trace ID
  • Vector databases - Pre-filter segments before ANN search
  • Serverless query engines - No database to manage
  • Browser-based data explorers - Cache index in localStorage
  • Edge computing / IoT analytics - Offline-capable querying
  • Cost-sensitive data lake queries - Minimize S3/GCS egress

Architecture

Index Structure
┌─────────────────────────────────────────────────────────────────────────────┐
│                              GINIndex                                       │
├─────────────────────────────────────────────────────────────────────────────┤
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │ Header                                                               │   │
│  │  • Version, NumRowGroups, NumDocs, NumPaths                         │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │ Path Directory                                                       │   │
│  │  pathID → { PathName, ObservedTypes, Cardinality, Flags }           │   │
│  │                                                                      │   │
│  │  Example:                                                            │   │
│  │    0 → { "$.name",   String,  150,   0x00 }                         │   │
│  │    1 → { "$.age",    Int,     80,    0x00 }                         │   │
│  │    2 → { "$.tags[*]", String, 50000, FlagBloomOnly }                │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │ Global Bloom Filter                                                  │   │
│  │  Fast rejection for path=value pairs                                 │   │
│  │  Contains: "$.name=alice", "$.name=bob", "$.age=30", ...            │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                                                             │
│  ┌───────────────────────┐  ┌───────────────────────┐                      │
│  │ StringIndex           │  │ NumericIndex          │                      │
│  │ (per pathID)          │  │ (per pathID)          │                      │
│  │                       │  │                       │                      │
│  │ pathID: 0 ($.name)    │  │ pathID: 1 ($.age)     │                      │
│  │ ┌─────────┬────────┐  │  │ ┌─────┬──────┬──────┐│                      │
│  │ │  Term   │ RGSet  │  │  │ │ RG  │ Min  │ Max  ││                      │
│  │ ├─────────┼────────┤  │  │ ├─────┼──────┼──────┤│                      │
│  │ │ "alice" │ {0,2}  │  │  │ │  0  │  25  │  35  ││                      │
│  │ │ "bob"   │ {1}    │  │  │ │  1  │  20  │  45  ││                      │
│  │ │ "carol" │ {2,3}  │  │  │ │  2  │  30  │  30  ││                      │
│  │ └─────────┴────────┘  │  │ └─────┴──────┴──────┘│                      │
│  └───────────────────────┘  └───────────────────────┘                      │
│                                                                             │
│  ┌───────────────────────┐  ┌───────────────────────┐                      │
│  │ NullIndex             │  │ TrigramIndex          │                      │
│  │ (per pathID)          │  │ (per pathID)          │                      │
│  │                       │  │                       │                      │
│  │ pathID: 1 ($.age)     │  │ pathID: 3 ($.desc)    │                      │
│  │ ┌──────────┬────────┐ │  │ ┌─────────┬────────┐ │                      │
│  │ │ NullRGs  │ {4,7}  │ │  │ │ Trigram │ RGSet  │ │                      │
│  │ │ Present  │ {0-9}  │ │  │ ├─────────┼────────┤ │                      │
│  │ └──────────┴────────┘ │  │ │ "hel"   │ {0,2}  │ │                      │
│  └───────────────────────┘  │ │ "ell"   │ {0,2}  │ │                      │
│                             │ │ "llo"   │ {0,2,5}│ │                      │
│  ┌───────────────────────┐  │ │ "wor"   │ {1,3}  │ │                      │
│  │ AggregateIndex        │  │ └─────────┴────────┘ │                      │
│  │ (per pathID, only     │  └───────────────────────┘                      │
│  │  when DocIDs share    │                                                 │
│  │  several documents)   │  ┌───────────────────────┐                     │
│  │ ┌────────────┬──────┐ │  │ PathCardinality (HLL) │                     │
│  │ │ MultiValue │ {2}  │ │  │ (per pathID)          │                     │
│  │ │ Absent     │ {5}  │ │  │                       │                     │
│  │ └────────────┴──────┘ │  │ Estimates unique vals │                     │
│  └───────────────────────┘  └───────────────────────┘                     │
│                                                                             │
│  ┌────────────────────────┐                                                 │
│  │ DocID Mapping          │                                                 │
│  │ (optional)             │                                                 │
│  │ pos → DocID            │                                                 │
│  │  0  → 1000             │                                                 │
│  │  1  → 1001             │                                                 │
│  └────────────────────────┘                                                 │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

Note: All RGSet bitmaps use Roaring Bitmaps for efficient compression
Data Flow
                                BUILD PHASE
┌─────────────────────────────────────────────────────────────────────────────┐
│                                                                             │
│   JSON Documents                                                            │
│        │                                                                    │
│        ▼                                                                    │
│   ┌─────────┐     ┌──────────────────────────────────────────────────┐     │
│   │ DocID 0 │────▶│                  GINBuilder                       │     │
│   │ RG: 0   │     │                                                   │     │
│   └─────────┘     │  AddDocument(docID, json)                        │     │
│   ┌─────────┐     │       │                                          │     │
│   │ DocID 1 │────▶│       ▼                                          │     │
│   │ RG: 0   │     │  ┌─────────────┐                                 │     │
│   └─────────┘     │  │ Walk JSON   │                                 │     │
│   ┌─────────┐     │  │ Extract:    │                                 │     │
│   │ DocID 2 │────▶│  │  • paths    │                                 │     │
│   │ RG: 1   │     │  │  • values   │                                 │     │
│   └─────────┘     │  │  • types    │                                 │     │
│        ⋮          │  └──────┬──────┘                                 │     │
│                   │         │                                         │     │
│                   │         ▼                                         │     │
│                   │  ┌─────────────────────────────────────────┐     │     │
│                   │  │ Update per-path structures:             │     │     │
│                   │  │  • stringTerms[term] → RGSet.Set(pos)   │     │     │
│                   │  │  • numericStats[pos].Min/Max            │     │     │
│                   │  │  • nullRGs.Set(pos) if null             │     │     │
│                   │  │  • trigrams.Add(term, pos)              │     │     │
│                   │  │  • bloom.Add(path=value)                │     │     │
│                   │  │  • hll.Add(value)                       │     │     │
│                   │  └─────────────────────────────────────────┘     │     │
│                   │                                                   │     │
│                   └───────────────────────┬──────────────────────────┘     │
│                                           │                                 │
│                                           ▼                                 │
│                                    Finalize()                               │
│                                           │                                 │
│                                           ▼                                 │
│                                    ┌─────────────┐                          │
│                                    │  GINIndex   │                          │
│                                    └─────────────┘                          │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘

                                QUERY PHASE
┌─────────────────────────────────────────────────────────────────────────────┐
│                                                                             │
│   Predicates: [EQ("$.name", "alice"), GT("$.age", 25)]                     │
│        │                                                                    │
│        ▼                                                                    │
│   ┌─────────────────────────────────────────────────────────────────┐      │
│   │                     idx.Evaluate(predicates)                     │      │
│   └─────────────────────────────────────────────────────────────────┘      │
│        │                                                                    │
│        ├──────────────────────┬──────────────────────┐                     │
│        ▼                      ▼                      ▼                     │
│   ┌──────────┐          ┌──────────┐          ┌──────────┐                 │
│   │ Predicate│          │ Predicate│          │   ...    │                 │
│   │    1     │          │    2     │          │          │                 │
│   └────┬─────┘          └────┬─────┘          └──────────┘                 │
│        │                     │                                              │
│        ▼                     ▼                                              │
│   ┌──────────────┐     ┌──────────────┐                                    │
│   │ Bloom Check  │     │ Bloom Check  │   ◀── Fast rejection path          │
│   │ path=value?  │     │   (skip for  │                                    │
│   └──────┬───────┘     │    ranges)   │                                    │
│          │             └──────┬───────┘                                    │
│          ▼                    ▼                                             │
│   ┌──────────────┐     ┌──────────────┐                                    │
│   │ StringIndex  │     │ NumericIndex │                                    │
│   │ lookup term  │     │ scan min/max │                                    │
│   │ → RGSet      │     │ → RGSet      │                                    │
│   └──────┬───────┘     └──────┬───────┘                                    │
│          │                    │                                             │
│          │    RGSet{0,2}      │    RGSet{0,1,2}                            │
│          │                    │                                             │
│          └─────────┬──────────┘                                             │
│                    │                                                        │
│                    ▼                                                        │
│             ┌─────────────┐                                                 │
│             │  Intersect  │                                                 │
│             │  (AND all)  │                                                 │
│             └──────┬──────┘                                                 │
│                    │                                                        │
│                    ▼                                                        │
│             ┌─────────────┐                                                 │
│             │ RGSet{0,2}  │  ◀── Matching row groups                       │
│             └──────┬──────┘                                                 │
│                    │                                                        │
│                    ▼                                                        │
│             ┌─────────────┐                                                 │
│             │ ToSlice()   │  → [0, 2]                                      │
│             │     or      │                                                 │
│             │ MatchingDocIDs() → [DocID...]                                │
│             └─────────────┘                                                 │
│                                                                             │
└─────────────────────────────────────────────────────────────────────────────┘
DocID Codec (Optional)

For composite document identifiers (e.g., file + row group):

// Encode file index and row group into single DocID
codec := gin.NewRowGroupCodec(20)  // 20 RGs per file
builder, err := gin.NewBuilder(config, totalRGs, gin.WithCodec(codec))
if err != nil {
    panic(err)
}

docID := codec.Encode(fileIndex, rgIndex)  // e.g., file=3, rg=15 → DocID=75
builder.AddDocument(docID, jsonDoc)
// Adding several documents under one DocID is supported. The builder then
// records per-path aggregate evidence (AggregateIndex) so NE, NIN and IsNull
// stay sound: a row group holding a value outside the query, or a document
// without the path, is never pruned by those operators.

// Query and decode results
result := idx.Evaluate(predicates)
for _, docID := range idx.MatchingDocIDs(result) {
    decoded := codec.Decode(docID)  // [3, 15]
    fileIdx, rgIdx := decoded[0], decoded[1]
}

How It Works

The GIN index maintains several data structures:

  1. Path Directory - Maps JSON paths to their metadata (types, cardinality)
  2. String Index - For each path, maps terms to row-group bitmaps (Roaring)
  3. Numeric Index - Per-row-group min/max values for range pruning
  4. Null Index - Bitmaps tracking which row groups have null/present values
  5. Trigram Index - Maps 3-character sequences to row-group bitmaps
  6. Global Bloom Filter - Fast rejection of non-existent path=value pairs
  7. DocID Mapping - Optional external DocID to internal position mapping

Query evaluation intersects the matching row-group bitmaps from each predicate.

Design Notes

Why numRGs Must Be Known Upfront

The NewBuilder(config, numRGs) requires the total number of row groups at construction time. This is intentional:

  1. Complement operations require universe size - Operations like AllRGs() and Invert() need to know the total number of row groups to compute complements. When a query cannot prune (e.g., unknown path, graceful degradation), the index returns "all row groups" - which requires knowing what "all" means.

  2. Parquet metadata provides this - The index is designed for Parquet row-group pruning. In this context, the number of row groups is always available from Parquet file metadata before indexing begins.

  3. Bounds checking - The builder validates that document positions don't exceed the declared row group count, catching configuration errors early.

License

MIT

Documentation

Index

Constants

View Source
const (
	MagicBytes = "GIN\x01"
	// Version is the binary format version. Decode rejects mismatches with
	// ErrVersionMismatch; the only migration path is to rebuild the index
	// with the target binary. Version history:
	//   v11: per-row-group distinct value counts in the AggregateIndex
	//        section so NIN drops a multi-value row group whose values all
	//        fall inside the query list (#63)
	//   v10: per-path AggregateIndex section so NE, NIN and IsNull stay
	//        sound when several documents share one DocID (#60)
	//   v9: compaction for ordered-string sections, including path
	//       directory names and string/adaptive term payloads
	//   v8: explicit companion transformer failure modes in serialized config
	//       and representation metadata (strict by default, soft-fail opt-in)
	//   v7: explicit representation metadata for derived alias routing
	//   v6: PathEntry.Mode byte + FlagTrigramIndex bit reassignment for
	//       adaptive high-cardinality indexing
	//   v5: never released; payloads are always rejected. Was an in-tree
	//       iteration of the adaptive string index section before the wire
	//       format was finalised in v6.
	//   v4: earlier pre-OSS format
	Version = 11
)
View Source
const (
	TypeString uint8 = 1 << iota
	TypeInt
	TypeFloat
	TypeBool
	TypeNull
)
View Source
const (
	// Deprecated: use IngestFailureHard.
	TransformerFailureStrict = IngestFailureHard
	// Deprecated: use IngestFailureSoft.
	TransformerFailureSoft = IngestFailureSoft
)
View Source
const DefaultMetadataKey = "gin.index"
View Source
const (
	FlagHasDocIDMap uint16 = 1 << iota
)
View Source
const (
	FlagTrigramIndex uint8 = 1 << iota // path has trigram index for CONTAINS queries
)

Variables

View Source
var (
	// ErrVersionMismatch is returned by Decode when the binary format version
	// does not match the expected version (Version constant).
	ErrVersionMismatch = errors.New("version mismatch")

	// ErrInvalidFormat is returned by Decode when the binary data is structurally
	// invalid: unrecognized magic bytes, oversized allocations, or corrupt fields.
	ErrInvalidFormat = errors.New("invalid format")

	// ErrDecodedSizeExceedsLimit is returned by Decode when compressed input
	// expands beyond maxDecodedIndexSize during zstd decompression.
	ErrDecodedSizeExceedsLimit = errors.New("decoded size exceeds configured limit")

	// ErrNilIndex is returned when a serialization or persistence boundary is
	// asked to operate on a nil index.
	ErrNilIndex = errors.New("nil index")
)

Functions

func BoolNormalize

func BoolNormalize(v any) (any, bool)

BoolNormalize normalizes various boolean-like values to actual booleans. Handles: bool, "true"/"false"/"yes"/"no"/"1"/"0"/"on"/"off", float64 (0 = false).

func CIDRToRange

func CIDRToRange(cidr string) (start, end float64, err error)

CIDRToRange parses a CIDR notation string and returns the start and end IP addresses as float64 values suitable for use with GTE/LTE predicates on IPv4ToInt-transformed fields. Example: CIDRToRange("192.168.1.0/24") returns (3232235776, 3232236031, nil)

func CompressionStats

func CompressionStats(terms []string) (compressed, original int, ratio float64)

CompressionRatio returns the compression ratio for a set of terms. Returns (compressed size, original size, ratio).

func DateToEpochMs

func DateToEpochMs(v any) (any, bool)

DateToEpochMs parses "2006-01-02" format to Unix milliseconds (midnight UTC).

func DurationToMs

func DurationToMs(v any) (any, bool)

DurationToMs parses Go duration strings (e.g., "1h30m", "500ms") to milliseconds.

func EmailDomain

func EmailDomain(v any) (any, bool)

EmailDomain extracts and lowercases the domain from an email address.

func Encode

func Encode(idx *GINIndex) ([]byte, error)

Encode serializes the index using zstd-15 compression (recommended default). It delegates to EncodeContext with context.Background().

func EncodeContext added in v1.1.0

func EncodeContext(ctx context.Context, idx *GINIndex, opts ...EncodeOption) ([]byte, error)

EncodeContext is the context-aware sibling of Encode. It wraps EncodeWithLevelContext at CompressionBest and emits a coarse boundary span.

func EncodeToMetadata

func EncodeToMetadata(idx *GINIndex, cfg ParquetConfig, opts ...EncodeOption) (key string, value string, err error)

EncodeToMetadata encodes idx and returns the Parquet key-value metadata pair to embed it. opts (e.g. WithEncodeProfile) override the profile carried by idx.Config, since a decoded index always carries EncoderProfileDefault.

func EncodeWithLevel

func EncodeWithLevel(idx *GINIndex, level CompressionLevel) ([]byte, error)

EncodeWithLevel serializes the index with the specified compression level. It delegates to EncodeWithLevelContext with context.Background(). Use CompressionNone (0) for no compression, or 1-19 for zstd compression levels.

func EncodeWithLevelContext added in v1.1.0

func EncodeWithLevelContext(ctx context.Context, idx *GINIndex, level CompressionLevel, opts ...EncodeOption) ([]byte, error)

EncodeWithLevelContext is the context-aware, configurable-compression sibling of EncodeWithLevel. Observability and the encoder profile are seeded from idx.Config when present; caller EncodeOptions (WithEncodeSignals, WithEncodeProfile) override them. Cancellation is checked before the blocking encode body, but the zstd codec itself cannot be preempted once it starts.

func ExtractLiterals

func ExtractLiterals(pattern string) ([]string, error)

ExtractLiterals extracts literal strings from a regex pattern for trigram-based candidate selection. Every match of the pattern contains at least one returned literal as a substring, so callers must treat the list as OR. A case-folded literal run ((?i)) is dropped entirely — contributing no literal — when any rune in it has a unicode.SimpleFold orbit member whose frozen trigram lowercase (trigramLowerRune, Unicode 15.0.0) differs from that rune's own; the drop applies to the whole merged literal node, not just the offending rune (see foldsUnderToLower). Returns nil when no literal is guaranteed or the expansion exceeds maxLiteralExpansion.

"foo|bar"          -> ["foo", "bar"]
"(error|warn)_msg" -> ["error_msg", "warn_msg"]
"foo.*bar"         -> ["foo", "bar"]

func ExtractTrigrams

func ExtractTrigrams(s string) []string

func GenerateBigrams

func GenerateBigrams(text string) []string

func GenerateNGrams

func GenerateNGrams(text string, n int, opts ...NGramOption) ([]string, error)

func GenerateTrigrams

func GenerateTrigrams(text string) []string

func HasGINIndex

func HasGINIndex(parquetFile string, cfg ParquetConfig) (bool, error)

func HasGINIndexReader

func HasGINIndexReader(parquetFile string, cfg ParquetConfig, reader io.ReaderAt, size int64) (bool, error)

func HasSidecar

func HasSidecar(parquetFile string) bool

func IPv4ToInt

func IPv4ToInt(v any) (any, bool)

IPv4ToInt converts IPv4 address strings to uint32 (as float64) for range queries.

func ISODateToEpochMs

func ISODateToEpochMs(v any) (any, bool)

ISODateToEpochMs parses RFC3339/ISO8601 strings to Unix milliseconds.

func IsDirectory

func IsDirectory(path string) bool

func IsS3Path

func IsS3Path(path string) bool

func IsValidJSONPath

func IsValidJSONPath(path string) bool

func ListGINFiles

func ListGINFiles(dir string) ([]string, error)

func ListParquetFiles

func ListParquetFiles(dir string) ([]string, error)

func MustValidateJSONPath

func MustValidateJSONPath(path string) string

func NormalizePath

func NormalizePath(path string) string

NormalizePath converts a JSONPath to a canonical dot-notation form without validating that the path uses only GIN-supported JSONPath features. Callers handling untrusted input should use ValidateJSONPath or canonicalizeSupportedPath first.

func ParseJSONPath

func ParseJSONPath(path string) (jp.Expr, error)

ParseJSONPath parses and validates a JSONPath, returning the parsed expression.

func ParseS3Path

func ParseS3Path(path string) (bucket, key string, err error)

func RebuildWithIndex

func RebuildWithIndex(parquetFile string, idx *GINIndex, cfg ParquetConfig, opts ...EncodeOption) error

RebuildWithIndex rewrites parquetFile with idx embedded as metadata. opts (e.g. WithEncodeProfile) override the profile carried by idx.Config, since a decoded index always carries EncoderProfileDefault.

func SemVerToInt

func SemVerToInt(v any) (any, bool)

SemVerToInt encodes semantic versions as integers: major*1000000 + minor*1000 + patch. Supports formats: "1.2.3", "v1.2.3", "1.2", "v1.2", "1.2.3-beta" (pre-release suffix ignored).

func SidecarPath

func SidecarPath(parquetFile string) string

func ToLower

func ToLower(v any) (any, bool)

ToLower normalizes strings to lowercase for case-insensitive queries.

func URLHost

func URLHost(v any) (any, bool)

URLHost extracts and lowercases the host from a URL.

func ValidateJSONPath

func ValidateJSONPath(path string) error

ValidateJSONPath validates a JSONPath expression and ensures it only uses features supported by the GIN index (dot notation, wildcards). Unsupported: array indices [0], filters [?()], recursive descent .., scripts

func WriteSidecar

func WriteSidecar(parquetFile string, idx *GINIndex, opts ...EncodeOption) error

WriteSidecar encodes idx and writes it to the sidecar path derived from parquetFile. opts (e.g. WithEncodeProfile) override the profile carried by idx.Config, since a decoded index always carries EncoderProfileDefault.

Types

type AdaptiveStringIndex added in v0.2.0

type AdaptiveStringIndex struct {
	// Terms holds the promoted exact-match values in sorted order.
	Terms []string
	// RGBitmaps[i] lists the row groups that contain Terms[i].
	RGBitmaps []*RGSet
	// BucketRGBitmaps partitions the long-tail terms by xxhash; len must be a
	// non-zero power of two. A bucket hit is a superset match (may include
	// row groups that do not actually contain the queried term).
	BucketRGBitmaps []*RGSet
}

AdaptiveStringIndex stores promoted exact terms plus lossy tail buckets. Terms must be sorted lexically; RGBitmaps is parallel to Terms. Values that are not promoted fall into one of len(BucketRGBitmaps) hash buckets, which may return false-positive row groups.

func NewAdaptiveStringIndex added in v0.2.0

func NewAdaptiveStringIndex(terms []string, rgBitmaps []*RGSet, bucketBitmaps []*RGSet) (*AdaptiveStringIndex, error)

NewAdaptiveStringIndex validates and constructs an adaptive string index.

type AggregateIndex added in v1.1.0

type AggregateIndex struct {
	// MultiValueRGs marks row groups where at least two documents carry the
	// path and the path holds at least two distinct values. Such a row group
	// always satisfies NE and NIN for any single value, because some
	// document holds a different one.
	MultiValueRGs *RGSet
	// AbsentRGs marks row groups holding at least two documents where at
	// least one document does not carry the path. Such a row group always
	// satisfies IsNull. One-document row groups are not recorded here:
	// IsNull derives them at query time from the root path's
	// NullIndex.PresentRGBitmap minus this path's NullIndex.PresentRGBitmap,
	// so no extra data is stored.
	AbsentRGs *RGSet
	// DistinctCounts holds one entry per set bit of MultiValueRGs, in bit
	// order: the number of distinct values the row group holds. Strings,
	// booleans, an explicit null and a container value (object or array)
	// each count once; a single numeric value adds one. A known count is
	// always at least 2. An entry of 0 means the count is unknown because
	// the row group's numeric values span a range. NIN drops a multi-value
	// row group when the count does not exceed the number of query terms it
	// matches, because then no value falls outside the list.
	DistinctCounts []uint32
}

AggregateIndex records, per path, evidence that only exists when several documents share one DocID (for example through RowGroupCodec). The negation operators NE, NIN and IsNull consult it; every other operator ignores it. A path without an entry has no row group in MultiValueRGs or AbsentRGs.

type BloomFilter

type BloomFilter struct {
	// contains filtered or unexported fields
}

func BloomFilterFromBits

func BloomFilterFromBits(bits []uint64, numBits uint32, numHashes uint8) *BloomFilter

func MustNewBloomFilter

func MustNewBloomFilter(numBits uint32, numHashes uint8, opts ...BloomFilterOption) *BloomFilter

func NewBloomFilter

func NewBloomFilter(numBits uint32, numHashes uint8, opts ...BloomFilterOption) (*BloomFilter, error)

func (*BloomFilter) Add

func (bf *BloomFilter) Add(data []byte)

func (*BloomFilter) AddString

func (bf *BloomFilter) AddString(s string)

func (*BloomFilter) Bits

func (bf *BloomFilter) Bits() []uint64

func (*BloomFilter) MayContain

func (bf *BloomFilter) MayContain(data []byte) bool

func (*BloomFilter) MayContainString

func (bf *BloomFilter) MayContainString(s string) bool

func (*BloomFilter) NumBits

func (bf *BloomFilter) NumBits() uint32

func (*BloomFilter) NumHashes

func (bf *BloomFilter) NumHashes() uint8

type BloomFilterOption

type BloomFilterOption func(*BloomFilter) error

type BuilderOption

type BuilderOption func(*GINBuilder) error

func WithCodec

func WithCodec(codec DocIDCodec) BuilderOption

func WithParser added in v1.1.0

func WithParser(p Parser) BuilderOption

WithParser installs a custom JSON parser. The default is stdlibParser (encoding/json.Decoder with UseNumber). Supplying nil returns an error. If supplied multiple times, the last WithParser wins (BuilderOption convention).

NOTE: external (out-of-package) implementations of Parser are not currently possible because parserSink is package-private. WithParser exists today as a forward-compat entry point and a seam for testing and internal telemetry.

If p also implements CloseableParser, the caller retains the obligation to call Close: GINBuilder never closes a supplied parser.

type CloseableParser added in v1.1.0

type CloseableParser interface {
	Parser
	Close() error
}

CloseableParser is a Parser that owns resources requiring deterministic release. The caller owns a CloseableParser and must call Close after every builder using it has stopped parsing. GINBuilder never closes a supplied parser because parser ownership remains with the caller.

type CompressedTermBlock

type CompressedTermBlock struct {
	FirstTerm string
	Entries   []PrefixEntry
}

type CompressionLevel

type CompressionLevel int

CompressionLevel specifies the compression level for index serialization.

const (
	CompressionNone     CompressionLevel = 0  // No compression
	CompressionFastest  CompressionLevel = 1  // zstd level 1
	CompressionBalanced CompressionLevel = 3  // zstd level 3
	CompressionBetter   CompressionLevel = 9  // zstd level 9
	CompressionBest     CompressionLevel = 15 // zstd level 15 (recommended)
	CompressionMax      CompressionLevel = 19 // zstd level 19 (slow)
)

type ConfigOption

type ConfigOption func(*GINConfig) error

func WithAdaptiveBucketCount added in v0.2.0

func WithAdaptiveBucketCount(bucketCount int) ConfigOption

WithAdaptiveBucketCount sets the fan-out of the long-tail bucket layer. Must be a positive power of two. To disable adaptive mode, omit this option (and WithAdaptivePromotedTermCap) or build a GINConfig literal with AdaptiveBucketCount/AdaptivePromotedTermCap set to 0; this option rejects 0 to keep the builder path explicit.

func WithAdaptiveCoverageCeiling added in v0.2.0

func WithAdaptiveCoverageCeiling(ceiling float64) ConfigOption

WithAdaptiveCoverageCeiling sets the maximum fraction of row groups a term may cover and still be eligible for promotion. Terms above the ceiling are treated as too-ubiquitous and fall through to the bucket layer. Must be in the open interval (0, 1).

func WithAdaptiveMinRGCoverage added in v0.2.0

func WithAdaptiveMinRGCoverage(minCoverage int) ConfigOption

WithAdaptiveMinRGCoverage sets the minimum number of row groups a term must cover to be eligible for promotion to the exact adaptive index. Terms below this threshold fall into the bucket layer.

func WithAdaptivePromotedTermCap added in v0.2.0

func WithAdaptivePromotedTermCap(cap int) ConfigOption

WithAdaptivePromotedTermCap caps the number of terms promoted to the exact adaptive index per high-cardinality path. Zero disables adaptive mode.

func WithBoolNormalizeTransformer

func WithBoolNormalizeTransformer(path, alias string, opts ...TransformerOption) ConfigOption

func WithCustomDateTransformer

func WithCustomDateTransformer(path, alias, layout string, opts ...TransformerOption) ConfigOption

func WithCustomTransformer added in v0.2.0

func WithCustomTransformer(path, alias string, fn FieldTransformer, opts ...TransformerOption) ConfigOption

func WithDateTransformer

func WithDateTransformer(path, alias string, opts ...TransformerOption) ConfigOption

func WithDurationTransformer

func WithDurationTransformer(path, alias string, opts ...TransformerOption) ConfigOption

func WithEmailDomainTransformer

func WithEmailDomainTransformer(path, alias string, opts ...TransformerOption) ConfigOption

func WithEncoderProfile added in v1.3.0

func WithEncoderProfile(profile EncoderProfile) ConfigOption

WithEncoderProfile sets the zstd encoder memory profile on the config so that every encode path reading the index config uses it. See EncoderProfile for the memory trade-off. Unknown values are rejected.

func WithFTSPaths

func WithFTSPaths(paths ...string) ConfigOption

func WithFieldTransformer

func WithFieldTransformer(path string, fn FieldTransformer) ConfigOption

func WithIPv4Transformer

func WithIPv4Transformer(path, alias string, opts ...TransformerOption) ConfigOption

func WithISODateTransformer

func WithISODateTransformer(path, alias string, opts ...TransformerOption) ConfigOption

func WithLogger added in v1.1.0

func WithLogger(logger logging.Logger) ConfigOption

WithLogger sets a custom logger on the config. Nil is rejected; omitting the option leaves the noop default in place.

func WithMaxStagedPaths added in v1.1.0

func WithMaxStagedPaths(limit int) ConfigOption

WithMaxStagedPaths limits all distinct canonical JSON paths staged for one document, including internal companion representation paths. The root and object or array container paths count. Zero leaves staging unlimited.

If the limit is exceeded, AddDocument returns a hard *IngestError with Layer() == IngestLayerResource. Path() is the path whose staging was rejected; when the rejection occurs inside a transformer's companion representation, Path() reports the source path that owns the companion, never the internal derived path. Object keys are visited in lexical order, so the rejected path is deterministic but need not be the first key in the input document. Value() is empty; Cause() reports the configured limit and a lower-bound count of the total paths the document requires. This builder resource failure is never softened by ParserFailureMode and is reported by AddDocument, not Finalize.

func WithNumericBucketTransformer

func WithNumericBucketTransformer(path, alias string, size float64, opts ...TransformerOption) ConfigOption

func WithNumericFailureMode added in v1.1.0

func WithNumericFailureMode(mode IngestFailureMode) ConfigOption

func WithParserFailureMode added in v1.1.0

func WithParserFailureMode(mode IngestFailureMode) ConfigOption

func WithPrefixBlockSize added in v1.1.0

func WithPrefixBlockSize(blockSize int) ConfigOption

WithPrefixBlockSize configures the block size for front-coded prefix compression used in ordered string sections. Zero keeps the use-default sentinel (the library falls back to defaultPrefixBlockSize). Values above math.MaxUint16 are rejected because the on-wire entry count is encoded as uint16 (see prefix.go:writeCompressedTerms).

func WithRegexExtractIntTransformer

func WithRegexExtractIntTransformer(path, alias, pattern string, group int, opts ...TransformerOption) ConfigOption

func WithRegexExtractTransformer

func WithRegexExtractTransformer(path, alias, pattern string, group int, opts ...TransformerOption) ConfigOption

func WithRegisteredTransformer

func WithRegisteredTransformer(path, alias string, id TransformerID, params []byte, opts ...TransformerOption) ConfigOption

func WithSemVerTransformer

func WithSemVerTransformer(path, alias string, opts ...TransformerOption) ConfigOption

func WithSignals added in v1.1.0

func WithSignals(signals telemetry.Signals) ConfigOption

WithSignals sets a Signals container on the config. Passing telemetry.Disabled() is valid and keeps the silent default behavior.

func WithToLowerTransformer

func WithToLowerTransformer(path, alias string, opts ...TransformerOption) ConfigOption

func WithURLHostTransformer

func WithURLHostTransformer(path, alias string, opts ...TransformerOption) ConfigOption

type CustomDateParams

type CustomDateParams struct {
	Layout string `json:"layout"`
}

type DecodeOption added in v1.1.0

type DecodeOption func(*decodeRuntime)

DecodeOption configures runtime observability for DecodeContext. Construct options with the exported WithDecodeSignals helper; the underlying runtime struct is intentionally unexported.

func WithDecodeSignals added in v1.1.0

func WithDecodeSignals(signals telemetry.Signals) DecodeOption

WithDecodeSignals sets the telemetry signals used by DecodeContext. Decode has no index receiver, so signals default to telemetry.Disabled(); callers use this option to route spans and metrics into an existing provider.

type DocID

type DocID uint64

DocID represents an external document identifier.

type DocIDCodec

type DocIDCodec interface {
	Encode(indices ...int) DocID
	Decode(docID DocID) []int
	Name() string
}

DocIDCodec encodes/decodes composite information into a single DocID.

type EncodeOption added in v1.1.0

type EncodeOption func(*encodeRuntime)

EncodeOption configures per-call runtime behavior for EncodeContext or EncodeWithLevelContext: observability (WithEncodeSignals) and the zstd encoder profile (WithEncodeProfile). The underlying runtime struct is intentionally unexported.

func WithEncodeProfile added in v1.3.0

func WithEncodeProfile(profile EncoderProfile) EncodeOption

WithEncodeProfile selects the zstd encoder profile for one encode call. It overrides the profile carried by idx.Config (see WithEncoderProfile), even when the value is EncoderProfileDefault. An unknown value fails the encode.

func WithEncodeSignals added in v1.1.0

func WithEncodeSignals(signals telemetry.Signals) EncodeOption

WithEncodeSignals overrides the telemetry signals used by EncodeContext and EncodeWithLevelContext. By default the encoder seeds signals from idx.Config; this option lets external callers provide explicit signals when the index config is unavailable or when they want to override it per call.

type EncoderProfile added in v1.3.0

type EncoderProfile uint8

EncoderProfile selects how the zstd encoder for an encode call is built and whether it stays in the shared cache, which sets how much memory the process retains after the call. The profile never changes the compressed bytes: for a given index and compression level every profile produces identical output. It is a runtime-only setting and is not written into the serialized index.

const (
	// EncoderProfileDefault keeps one zstd worker per GOMAXPROCS so concurrent
	// encodes at the same level run in parallel. Each additional level-15
	// worker retains roughly 34 MB for the life of the process, so a 16-CPU
	// host holds about 560 MB after the first level-15 encode. See
	// docs/encoder-profile-benchmarks.md for the measured tables. The worker
	// pool size is read from GOMAXPROCS once, when the shared encoder for a
	// given (mode, profile) key is first constructed, and is not resized
	// later even if GOMAXPROCS changes.
	EncoderProfileDefault EncoderProfile = iota
	// EncoderProfileBoundedMemory keeps a single zstd worker with zstd's
	// lower-memory buffers. Level 15 retains about 42 MB regardless of
	// GOMAXPROCS. Concurrent encodes at the same level and profile queue on
	// that one worker instead of running in parallel. See
	// docs/encoder-profile-benchmarks.md for the measured tables.
	EncoderProfileBoundedMemory
	// EncoderProfileBoundedMemoryUncached builds a one-worker, low-memory zstd
	// encoder for each call and drops it. Nothing stays in the shared encoder
	// cache, so the heap that remains after the call is near zero, even at
	// level 15. The output is byte-identical to the other profiles.
	//
	// Do not use it in a process that encodes often. Each call pays encoder
	// construction: about 2 ms and 44 MB allocated at level 15 on a small index. Use
	// EncoderProfileBoundedMemory there. N concurrent calls build N encoders,
	// which allocate about N x 44 MB at level 15 (this is allocation, not a
	// measured peak). The library sets no limit. The encoder is dropped for the
	// garbage collector to reclaim. The call does not close it. A decoded index
	// carries the default profile, so pass WithEncodeProfile on each call when
	// you re-encode a loaded index. See docs/encoder-profile-benchmarks.md for
	// numbers.
	EncoderProfileBoundedMemoryUncached
)

func (EncoderProfile) String added in v1.3.0

func (p EncoderProfile) String() string

String returns the profile name; unknown values render as their number.

type FieldTransformer

type FieldTransformer func(value any) (any, bool)

FieldTransformer transforms a value before indexing. Returns (transformedValue, ok). If ok=false, the companion representation follows the registration's configured failure mode. Hard rejects the document; soft skips only the derived representation and keeps the source document indexed.

func CustomDateToEpochMs

func CustomDateToEpochMs(layout string) FieldTransformer

CustomDateToEpochMs returns a transformer for custom date formats.

func NumericBucket

func NumericBucket(size float64) FieldTransformer

NumericBucket returns a transformer that buckets numeric values by size. Example: NumericBucket(100) transforms 150 -> 100, 250 -> 200.

func ReconstructTransformer

func ReconstructTransformer(id TransformerID, params json.RawMessage) (FieldTransformer, error)

func RegexExtract

func RegexExtract(pattern string, group int) FieldTransformer

RegexExtract returns a transformer that extracts a substring via regex capture group. Pattern is compiled once at config time. Group 0 = full match, group 1+ = capture groups.

func RegexExtractInt

func RegexExtractInt(pattern string, group int) FieldTransformer

RegexExtractInt extracts a substring via regex and converts it to float64.

type GINBuilder

type GINBuilder struct {
	// contains filtered or unexported fields
}

func NewBuilder

func NewBuilder(config GINConfig, numRGs int, opts ...BuilderOption) (*GINBuilder, error)

func (*GINBuilder) AddDocument

func (b *GINBuilder) AddDocument(docID DocID, jsonDoc []byte) error

func (*GINBuilder) BeginDocument added in v1.1.0

func (b *GINBuilder) BeginDocument(rgID int) *documentBuildState

func (*GINBuilder) Err added in v1.1.0

func (b *GINBuilder) Err() error

Err returns the tragic builder error that closes the builder, if any. User-input document failures are reported from AddDocument and do not appear here.

func (*GINBuilder) Finalize

func (b *GINBuilder) Finalize() *GINIndex

Finalize returns an immutable index for all successfully committed documents. If the builder previously encountered a tragic internal failure, Finalize returns nil and logs the refusal; callers that continue after AddDocument errors must nil-check the result before using it.

func (*GINBuilder) Logger added in v1.1.0

func (b *GINBuilder) Logger() logging.Logger

func (*GINBuilder) MarkPresent added in v1.1.0

func (b *GINBuilder) MarkPresent(state *documentBuildState, canonicalPath string) error

func (*GINBuilder) NumSoftSkippedDocuments added in v1.1.0

func (b *GINBuilder) NumSoftSkippedDocuments() uint64

NumSoftSkippedDocuments reports how many documents were dropped because a builder-level soft ingest mode chose skip-over-error semantics.

func (*GINBuilder) NumSoftSkippedRepresentations added in v1.1.0

func (b *GINBuilder) NumSoftSkippedRepresentations() uint64

NumSoftSkippedRepresentations reports how many companion representations were omitted because a per-transformer soft failure kept the source document and skipped only the failed derived value.

func (*GINBuilder) ShouldBufferForTransform added in v1.1.0

func (b *GINBuilder) ShouldBufferForTransform(canonicalPath string) bool

func (*GINBuilder) SoftSkippedDocuments deprecated added in v1.1.0

func (b *GINBuilder) SoftSkippedDocuments() uint64

SoftSkippedDocuments reports how many documents were dropped because a builder-level soft ingest mode chose skip-over-error semantics.

Deprecated: use NumSoftSkippedDocuments.

func (*GINBuilder) StageFloat64 added in v1.1.0

func (b *GINBuilder) StageFloat64(state *documentBuildState, canonicalPath string, v float64) error

StageFloat64 preserves the source's float classification. Unlike StageNativeNumeric, it never folds a whole float64 value to an integer.

func (*GINBuilder) StageInt64 added in v1.1.0

func (b *GINBuilder) StageInt64(state *documentBuildState, canonicalPath string, v int64) error

func (*GINBuilder) StageJSONNumber added in v1.1.0

func (b *GINBuilder) StageJSONNumber(state *documentBuildState, canonicalPath, raw string) error

func (*GINBuilder) StageMaterialized added in v1.1.0

func (b *GINBuilder) StageMaterialized(state *documentBuildState, path string, value any, allowTransform bool) error

func (*GINBuilder) StageNativeNumeric added in v1.1.0

func (b *GINBuilder) StageNativeNumeric(state *documentBuildState, canonicalPath string, v any) error

StageNativeNumeric stages an already-materialized Go number. Unlike StageFloat64, it may fold a whole float64 value to the integer domain.

func (*GINBuilder) StageScalar added in v1.1.0

func (b *GINBuilder) StageScalar(state *documentBuildState, canonicalPath string, token any) error

func (*GINBuilder) StageUint64 added in v1.1.0

func (b *GINBuilder) StageUint64(state *documentBuildState, canonicalPath string, v uint64) error

StageUint64 routes values above math.MaxInt64 to the numeric failure policy; see StageFloat64 and StageNativeNumeric for related staging contracts.

type GINConfig

type GINConfig struct {
	CardinalityThreshold    uint32
	BloomFilterSize         uint32
	BloomFilterHashes       uint8
	EnableTrigrams          bool
	TrigramMinLength        int
	HLLPrecision            uint8
	PrefixBlockSize         int
	AdaptiveMinRGCoverage   int
	AdaptivePromotedTermCap int
	AdaptiveCoverageCeiling float64
	AdaptiveBucketCount     int
	// ParserFailureMode and NumericFailureMode are builder-time ingest routing
	// knobs. They are never serialized into finalized indexes; see
	// SerializedConfig and writeConfig/readConfig for persisted config state.
	ParserFailureMode  IngestFailureMode
	NumericFailureMode IngestFailureMode
	// MaxStagedPaths caps staged JSON paths per document; see WithMaxStagedPaths for the full contract.
	MaxStagedPaths int

	// Runtime-only observability fields. These are never serialized into the
	// on-wire config payload (see SerializedConfig and writeConfig/readConfig).
	Logger  logging.Logger    // noop by default; set via WithLogger
	Signals telemetry.Signals // disabled by default; set via WithSignals

	// EncoderProfile selects the zstd encoder memory profile used by every
	// encode path that reads this config (Encode, WriteSidecar,
	// EncodeToMetadata, S3 sidecars). Runtime-only, never serialized; a decoded
	// index always reads EncoderProfileDefault. Set via WithEncoderProfile;
	// WithEncodeProfile overrides it per call.
	EncoderProfile EncoderProfile
	// contains filtered or unexported fields
}

func DefaultConfig

func DefaultConfig() GINConfig

func NewConfig

func NewConfig(opts ...ConfigOption) (GINConfig, error)

func (GINConfig) AdaptiveEnabled added in v0.2.0

func (c GINConfig) AdaptiveEnabled() bool

AdaptiveEnabled reports whether adaptive high-cardinality indexing is enabled.

type GINIndex

type GINIndex struct {
	// GINIndex is immutable after `Finalize()` or `Decode()`; pathLookup is
	// derived, non-serialized state rebuilt once and then treated as read-only.
	Header                Header
	PathDirectory         []PathEntry
	GlobalBloom           *BloomFilter
	StringIndexes         map[uint16]*StringIndex
	AdaptiveStringIndexes map[uint16]*AdaptiveStringIndex
	NumericIndexes        map[uint16]*NumericIndex
	NullIndexes           map[uint16]*NullIndex
	AggregateIndexes      map[uint16]*AggregateIndex
	TrigramIndexes        map[uint16]*TrigramIndex
	StringLengthIndexes   map[uint16]*StringLengthIndex
	PathCardinality       map[uint16]*HyperLogLog
	DocIDMapping          []DocID
	Config                *GINConfig
	// contains filtered or unexported fields
}

func BuildFromParquet

func BuildFromParquet(parquetFile string, jsonColumn string, config GINConfig) (*GINIndex, error)

BuildFromParquet builds a GIN index from a local Parquet file. It delegates to BuildFromParquetContext with context.Background().

func BuildFromParquetContext added in v1.1.0

func BuildFromParquetContext(ctx context.Context, parquetFile string, jsonColumn string, config GINConfig) (*GINIndex, error)

BuildFromParquetContext is the context-aware sibling of BuildFromParquet. It starts a coarse build boundary span, then delegates to BuildFromParquetReaderContext for the actual index construction.

func BuildFromParquetReader

func BuildFromParquetReader(parquetFile string, jsonColumn string, config GINConfig, reader io.ReaderAt, size int64) (*GINIndex, error)

BuildFromParquetReader builds a GIN index from a Parquet file using an explicit reader. It delegates to BuildFromParquetReaderContext with context.Background().

func BuildFromParquetReaderContext added in v1.1.0

func BuildFromParquetReaderContext(ctx context.Context, parquetFile string, jsonColumn string, config GINConfig, reader io.ReaderAt, size int64) (*GINIndex, error)

BuildFromParquetReaderContext is the context-aware sibling of BuildFromParquetReader. Observability comes from config. One coarse boundary span wraps the entire build; no spans appear inside page/value/document loops.

func Decode

func Decode(data []byte) (*GINIndex, error)

Decode deserializes an index, validates cross-structure path references, and canonicalizes supported JSONPath spellings in PathDirectory while rebuilding derived lookup state. It delegates to DecodeContext with context.Background().

func DecodeContext added in v1.1.0

func DecodeContext(ctx context.Context, data []byte, opts ...DecodeOption) (*GINIndex, error)

DecodeContext is the context-aware sibling of Decode. It wraps the deserialization work in a coarse boundary span. Caller DecodeOptions can supply explicit observability signals since Decode has no config receiver. Cancellation is checked before the blocking decode body, but zstd decompression itself cannot be preempted once it starts.

func DecodeFromMetadata

func DecodeFromMetadata(value string) (*GINIndex, error)

func LoadIndex

func LoadIndex(parquetFile string, cfg ParquetConfig) (*GINIndex, error)

func LoadIndexReader

func LoadIndexReader(parquetFile string, cfg ParquetConfig, reader io.ReaderAt, size int64) (*GINIndex, error)

func NewGINIndex

func NewGINIndex() *GINIndex

func ReadFromParquetMetadata

func ReadFromParquetMetadata(parquetFile string, cfg ParquetConfig) (*GINIndex, error)

func ReadFromParquetMetadataReader

func ReadFromParquetMetadataReader(parquetFile string, cfg ParquetConfig, reader io.ReaderAt, size int64) (*GINIndex, error)

func ReadSidecar

func ReadSidecar(parquetFile string) (*GINIndex, error)

func (*GINIndex) Evaluate

func (idx *GINIndex) Evaluate(predicates []Predicate) *RGSet

Evaluate evaluates a set of predicates against the index and returns the set of row groups that may contain matching documents. It delegates to EvaluateContext with context.Background().

func (*GINIndex) EvaluateContext added in v1.1.0

func (idx *GINIndex) EvaluateContext(ctx context.Context, predicates []Predicate) *RGSet

EvaluateContext evaluates predicates against the index and returns the set of row groups that may contain matching documents. The context is propagated into the coarse query boundary span and checked before each predicate. If evaluation cannot complete, the method conservatively returns all row groups because this API does not return an error. If ctx is nil, context.Background() is used. Observability is read from idx.Config; a nil config falls back to silent/noop behavior without panicking.

func (*GINIndex) MatchingDocIDs

func (idx *GINIndex) MatchingDocIDs(rgSet *RGSet) []DocID

func (*GINIndex) Representations added in v0.2.0

func (idx *GINIndex) Representations(path string) []RepresentationInfo

func (*GINIndex) TrigramUnicodeVersion added in v1.6.0

func (idx *GINIndex) TrigramUnicodeVersion() (version string, supported bool)

TrigramUnicodeVersion returns the Unicode version of the trigram tables the index was built with and whether this reader supports it. An unsupported version is returned verbatim; trigram pruning is then disabled (#99).

type Header struct {
	Magic             [4]byte
	Version           uint16
	Flags             uint16
	NumRowGroups      uint32
	NumDocs           uint64
	NumPaths          uint32
	CardinalityThresh uint32
}

type HyperLogLog

type HyperLogLog struct {
	// contains filtered or unexported fields
}

HyperLogLog implements the HyperLogLog algorithm for cardinality estimation. It uses 2^precision registers to estimate the number of distinct elements.

func HyperLogLogFromRegisters

func HyperLogLogFromRegisters(registers []uint8, precision uint8) *HyperLogLog

func MustNewHyperLogLog

func MustNewHyperLogLog(precision uint8, opts ...HyperLogLogOption) *HyperLogLog

func NewHyperLogLog

func NewHyperLogLog(precision uint8, opts ...HyperLogLogOption) (*HyperLogLog, error)

NewHyperLogLog creates a new HyperLogLog with the given precision. Precision must be between 4 and 16. Higher precision = more accuracy but more memory. Memory usage: 2^precision bytes. Standard error: 1.04 / sqrt(m) where m = 2^precision

func (*HyperLogLog) Add

func (hll *HyperLogLog) Add(data []byte)

func (*HyperLogLog) AddString

func (hll *HyperLogLog) AddString(s string)

func (*HyperLogLog) Clear

func (hll *HyperLogLog) Clear()

func (*HyperLogLog) Clone

func (hll *HyperLogLog) Clone() *HyperLogLog

func (*HyperLogLog) Estimate

func (hll *HyperLogLog) Estimate() uint64

func (*HyperLogLog) Merge

func (hll *HyperLogLog) Merge(other *HyperLogLog)

func (*HyperLogLog) Precision

func (hll *HyperLogLog) Precision() uint8

func (*HyperLogLog) Registers

func (hll *HyperLogLog) Registers() []uint8

type HyperLogLogOption

type HyperLogLogOption func(*HyperLogLog) error

type IdentityCodec

type IdentityCodec struct{}

IdentityCodec treats the position as the DocID (1:1 mapping).

func NewIdentityCodec

func NewIdentityCodec() *IdentityCodec

func (*IdentityCodec) Decode

func (c *IdentityCodec) Decode(docID DocID) []int

func (*IdentityCodec) Encode

func (c *IdentityCodec) Encode(indices ...int) DocID

func (*IdentityCodec) Name

func (c *IdentityCodec) Name() string

type IngestError added in v1.1.0

type IngestError struct {
	// contains filtered or unexported fields
}

IngestError reports a hard per-document ingest failure.

Path() returns the source JSONPath that rejected the document. Parser-level failures that are not attributable to a path report the empty string.

Layer() identifies the ingest stage that rejected the document. Callers must tolerate future layer strings in addition to the built-in parser, transformer, numeric, schema, and resource values.

Value() returns a verbatim string representation of the offending input or value for document-data failures. It is empty for resource failures, which have no offending document value; their diagnostics are available from Cause(). The library does not redact or truncate document-data values, so callers that log untrusted documents own their redaction and output-size policy.

func (*IngestError) Cause added in v1.1.0

func (e *IngestError) Cause() error

Cause returns the underlying cause for github.com/pkg/errors compatibility.

func (*IngestError) Error added in v1.1.0

func (e *IngestError) Error() string

Error returns a stable human-readable message for the hard ingest failure. The "ingest <layer> failure [at <path>]: <cause>" format is API-stable.

func (*IngestError) Layer added in v1.1.0

func (e *IngestError) Layer() IngestLayer

Layer returns the ingest stage that rejected the document.

func (*IngestError) Path added in v1.1.0

func (e *IngestError) Path() string

Path returns the source JSONPath that rejected the document.

func (*IngestError) Unwrap added in v1.1.0

func (e *IngestError) Unwrap() error

Unwrap returns the underlying cause for stdlib error unwrapping.

func (*IngestError) Value added in v1.1.0

func (e *IngestError) Value() string

Value returns the verbatim offending input or transformed value. It is empty for resource failures, which do not have an offending document value.

type IngestFailureMode added in v1.1.0

type IngestFailureMode string
const (
	IngestFailureHard IngestFailureMode = "hard"
	IngestFailureSoft IngestFailureMode = "soft"
)

type IngestLayer added in v1.1.0

type IngestLayer string

IngestLayer identifies the ingest layer that rejected a document.

const (
	// IngestLayerParser identifies JSON parser failures for a document.
	IngestLayerParser IngestLayer = "parser"

	// IngestLayerTransformer identifies field transformer failures for a document.
	IngestLayerTransformer IngestLayer = "transformer"

	// IngestLayerNumeric identifies numeric coercion or promotion failures for a document.
	IngestLayerNumeric IngestLayer = "numeric"

	// IngestLayerSchema identifies unsupported value-shape failures for a document.
	IngestLayerSchema IngestLayer = "schema"

	// IngestLayerResource identifies a builder resource limit that rejected an
	// otherwise valid document. Callers can rebuild with a higher limit.
	IngestLayerResource IngestLayer = "resource"
)

type JSONPathError

type JSONPathError struct {
	Path    string
	Message string
}

func (*JSONPathError) Error

func (e *JSONPathError) Error() string

type NGramConfig

type NGramConfig struct {
	N       int
	Padding string
}

type NGramOption

type NGramOption func(*NGramConfig) error

func WithN

func WithN(n int) NGramOption

func WithPadding

func WithPadding(pad string) NGramOption

type NullIndex

type NullIndex struct {
	NullRGBitmap    *RGSet
	PresentRGBitmap *RGSet
}

type NumericBucketParams

type NumericBucketParams struct {
	Size float64 `json:"size"`
}

type NumericIndex

type NumericIndex struct {
	// ValueType is the numeric storage mode: int-only or float/mixed.
	ValueType    NumericValueType
	IntGlobalMin int64
	IntGlobalMax int64
	GlobalMin    float64
	GlobalMax    float64
	RGStats      []RGNumericStat
}

type NumericValueType added in v0.2.0

type NumericValueType uint8
const (
	NumericValueTypeIntOnly NumericValueType = iota
	NumericValueTypeFloatMixed
)

type Operator

type Operator uint8
const (
	OpEQ Operator = iota
	OpNE
	OpGT
	OpLT
	OpGTE
	OpLTE
	OpIN
	OpNIN
	OpIsNull
	OpIsNotNull
	OpContains
	OpRegex
)

func (Operator) String

func (o Operator) String() string

type ParquetConfig

type ParquetConfig struct {
	MetadataKey string
}

func DefaultParquetConfig

func DefaultParquetConfig() ParquetConfig

type ParquetIndexWriter

type ParquetIndexWriter struct {
	// contains filtered or unexported fields
}

func NewParquetIndexWriter

func NewParquetIndexWriter(w io.Writer, schema *parquet.Schema, jsonColumn string, numRowGroups int, ginConfig GINConfig, pqConfig ParquetConfig) (*ParquetIndexWriter, error)

type Parser added in v1.1.0

type Parser interface {
	// Name returns a stable identifier for telemetry (e.g. "stdlib").
	// MUST NOT return the empty string; NewBuilder rejects an empty name.
	Name() string

	// Parse walks jsonDoc and stages observations for rgID via sink. The
	// parser's first sink call MUST be sink.BeginDocument(rgID), and Parse
	// MUST call BeginDocument exactly once. After Parse returns successfully,
	// AddDocument verifies the call count and rgID; call ordering and error
	// paths remain the parser implementation's responsibility.
	//
	// Present-marking contract: for object and array roots, Parse MUST
	// call sink.MarkPresent for the container's canonicalPath before
	// staging children; otherwise IsNull / IsNotNull queries will return
	// wrong results for that path. All Stage* sink methods (StageScalar,
	// StageInt64, StageUint64, StageFloat64, StageJSONNumber,
	// StageNativeNumeric, and StageMaterialized) implicitly mark their path
	// present.
	Parse(jsonDoc []byte, rgID int, sink parserSink) error
}

Parser translates one JSON document into staged per-path observations, writing them through the supplied sink. Implementations MUST preserve the source number's integer or float classification without coercing exact integers through float64. Raw-text parsers use sink.StageJSONNumber so the builder classifies the source lexeme. Exact typed parsers may use sink.StageInt64 for signed integers, sink.StageUint64 for unsigned integers, and sink.StageFloat64 for lexeme-classified floats. Unsigned integer values above math.MaxInt64 fail at the numeric layer.

Parse may add parser-local context to errors it creates. Errors returned by sink Stage callbacks must retain their layer. AddDocument preserves staged error provenance and applies parser hard/soft classification only to errors that did not originate from a Stage callback. A parser that cannot safely parse another document must return a parserLifecycleError; AddDocument then terminates the builder regardless of ParserFailureMode.

External implementability: the sink type referenced by Parse (parserSink) is package-private; third-party Parser implementations outside package gin cannot satisfy this interface. The Parser name is exported today so WithParser remains a stable entry point; exporting the sink and enabling external parsers is deferred.

type PathEntry

type PathEntry struct {
	PathID        uint16
	PathName      string
	ObservedTypes uint8
	Cardinality   uint32
	// Mode is the exclusive string-evaluation mode for this path.
	Mode  PathMode
	Flags uint8
	// AdaptivePromotedTerms and AdaptiveBucketCount are derived metadata
	// populated from the adaptive section at decode time. They are not
	// persisted in the path directory; encoders must not rely on them.
	AdaptivePromotedTerms uint16
	AdaptiveBucketCount   uint16
}

type PathMode added in v0.2.0

type PathMode uint8

PathMode is the exclusive storage mode for a path entry. The zero value is the classic exact mode.

const (
	// PathModeClassic keeps the full exact string index for a path.
	// Its user-facing string label remains "exact" because that describes the
	// query semantics more clearly than the internal mode name.
	PathModeClassic PathMode = iota
	// PathModeBloomOnly stores no exact term index and answers via bloom-filter fallback.
	PathModeBloomOnly
	// PathModeAdaptiveHybrid stores promoted exact terms plus lossy tail buckets.
	PathModeAdaptiveHybrid
)

func (PathMode) IsValid added in v0.2.0

func (m PathMode) IsValid() bool

IsValid reports whether m is one of the declared PathMode constants. Decoders should call this on every byte read from disk before trusting the value; values outside the declared range indicate a corrupt payload.

func (PathMode) String added in v0.2.0

func (m PathMode) String() string

String returns the user-facing label used in CLI output and diagnostics.

type Predicate

type Predicate struct {
	Path     string
	Operator Operator
	Value    any
}

func Contains

func Contains(path string, pattern string) Predicate

func EQ

func EQ(path string, value any) Predicate

func GT

func GT(path string, value any) Predicate

func GTE

func GTE(path string, value any) Predicate

func IN

func IN(path string, values ...any) Predicate

func InSubnet

func InSubnet(path, cidr string) []Predicate

InSubnet creates predicates using the conventional IPv4 companion alias "ipv4_int". Use InSubnetAs when a path is configured with a different alias.

func InSubnetAs added in v0.2.0

func InSubnetAs(path, alias, cidr string) []Predicate

InSubnetAs creates predicates to check if an IP field (transformed with IPv4ToInt under the provided alias) falls within a CIDR subnet range. Example: InSubnetAs("$.client_ip", "ipv4_int", "192.168.1.0/24") returns predicates for 192.168.1.0-255. Panics if CIDR is invalid - use CIDRToRange for error handling.

func IsNotNull

func IsNotNull(path string) Predicate

func IsNull

func IsNull(path string) Predicate

IsNull selects every row group holding at least one document whose value for path is an explicit JSON null, or that does not carry the path, whatever the row group's document count. Union its result with a separate NE or NIN evaluation to keep row groups whose documents lack the path; Evaluate ANDs its predicates.

func LT

func LT(path string, value any) Predicate

func LTE

func LTE(path string, value any) Predicate

func NE

func NE(path string, value any) Predicate

func NIN

func NIN(path string, values ...any) Predicate

func Regex

func Regex(path string, pattern string) Predicate

func (Predicate) String

func (p Predicate) String() string

type PrefixCompressor

type PrefixCompressor struct {
	// contains filtered or unexported fields
}

PrefixCompressor implements front-coding compression for sorted string lists. Each string is stored as: shared prefix length + suffix. This works well for sorted terms that share common prefixes.

func MustNewPrefixCompressor

func MustNewPrefixCompressor(blockSize int, opts ...PrefixCompressorOption) *PrefixCompressor

func NewPrefixCompressor

func NewPrefixCompressor(blockSize int, opts ...PrefixCompressorOption) (*PrefixCompressor, error)

func (*PrefixCompressor) BlockSize

func (pc *PrefixCompressor) BlockSize() int

func (*PrefixCompressor) Compress

func (pc *PrefixCompressor) Compress(terms []string) []CompressedTermBlock

func (*PrefixCompressor) CompressInOrder added in v1.1.0

func (pc *PrefixCompressor) CompressInOrder(terms []string) []CompressedTermBlock

CompressInOrder applies front coding without reordering the caller-provided terms. Use this for serialized sections whose bitmap/layout metadata must stay aligned with the original slice position.

func (*PrefixCompressor) Decompress

func (pc *PrefixCompressor) Decompress(blocks []CompressedTermBlock) []string

type PrefixCompressorOption

type PrefixCompressorOption func(*PrefixCompressor) error

type PrefixEntry

type PrefixEntry struct {
	PrefixLen uint16
	Suffix    string
}

type RGNumericStat

type RGNumericStat struct {
	IntMin   int64
	IntMax   int64
	Min      float64
	Max      float64
	HasValue bool
}

type RGSet

type RGSet struct {
	NumRGs int
	// contains filtered or unexported fields
}

func AllRGs

func AllRGs(numRGs int) *RGSet

func MustNewRGSet

func MustNewRGSet(numRGs int, opts ...RGSetOption) *RGSet

func NewRGSet

func NewRGSet(numRGs int, opts ...RGSetOption) (*RGSet, error)

func NoRGs

func NoRGs(numRGs int) *RGSet

func RGSetFromRoaring

func RGSetFromRoaring(bitmap *roaring.Bitmap, numRGs int) *RGSet

func (*RGSet) All

func (rs *RGSet) All() *RGSet

func (*RGSet) Clear

func (rs *RGSet) Clear(rgID int)

func (*RGSet) Clone

func (rs *RGSet) Clone() *RGSet

func (*RGSet) Count

func (rs *RGSet) Count() int

func (*RGSet) Intersect

func (rs *RGSet) Intersect(other *RGSet) *RGSet

func (*RGSet) Invert

func (rs *RGSet) Invert() *RGSet

func (*RGSet) IsEmpty

func (rs *RGSet) IsEmpty() bool

func (*RGSet) IsSet

func (rs *RGSet) IsSet(rgID int) bool

func (*RGSet) Roaring

func (rs *RGSet) Roaring() *roaring.Bitmap

func (*RGSet) Set

func (rs *RGSet) Set(rgID int)

func (*RGSet) ToSlice

func (rs *RGSet) ToSlice() []int

func (*RGSet) Union

func (rs *RGSet) Union(other *RGSet) *RGSet

func (*RGSet) UnionWith added in v0.2.0

func (rs *RGSet) UnionWith(other *RGSet)

UnionWith merges other into rs in place, avoiding the per-call clone of Union. Use this when the receiver is exclusively owned and the result does not need to preserve the prior state.

type RGSetOption

type RGSetOption func(*RGSet) error

type RGStringLengthStat

type RGStringLengthStat struct {
	Min      uint32
	Max      uint32
	HasValue bool
}

type RegexLiteralInfo

type RegexLiteralInfo struct {
	Literals    []string // Extracted literal strings
	HasWildcard bool     // Pattern contains unbounded wildcards
	MinLength   int      // Minimum length of any literal
}

func AnalyzeRegex

func AnalyzeRegex(pattern string) (*RegexLiteralInfo, error)

AnalyzeRegex extracts literals and metadata from a regex pattern

type RegexParams

type RegexParams struct {
	Pattern string `json:"pattern"`
	Group   int    `json:"group"`
}

type RepresentationInfo added in v0.2.0

type RepresentationInfo struct {
	SourcePath  string
	Alias       string
	Transformer string
}

type RepresentationSpec added in v0.2.0

type RepresentationSpec struct {
	SourcePath   string          `json:"source_path"`
	Alias        string          `json:"alias"`
	TargetPath   string          `json:"target_path"`
	Transformer  TransformerSpec `json:"transformer"`
	Serializable bool            `json:"serializable"`
}

type RepresentationValue added in v0.2.0

type RepresentationValue struct {
	Alias string
	Value any
}

func As added in v0.2.0

func As(alias string, value any) RepresentationValue

type RowGroupCodec

type RowGroupCodec struct {
	// contains filtered or unexported fields
}

RowGroupCodec encodes file index and row group index into a DocID. Layout: DocID = fileIndex * rowGroupsPerFile + rgIndex

func NewRowGroupCodec

func NewRowGroupCodec(rowGroupsPerFile int) *RowGroupCodec

func (*RowGroupCodec) Decode

func (c *RowGroupCodec) Decode(docID DocID) []int

func (*RowGroupCodec) Encode

func (c *RowGroupCodec) Encode(indices ...int) DocID

func (*RowGroupCodec) Name

func (c *RowGroupCodec) Name() string

func (*RowGroupCodec) RowGroupsPerFile

func (c *RowGroupCodec) RowGroupsPerFile() int

type S3Client

type S3Client struct {
	// contains filtered or unexported fields
}

func NewS3Client

func NewS3Client(cfg S3Config) (*S3Client, error)

func NewS3ClientFromEnv

func NewS3ClientFromEnv() (*S3Client, error)

func (*S3Client) BuildFromParquet

func (c *S3Client) BuildFromParquet(bucket, key, jsonColumn string, ginCfg GINConfig) (*GINIndex, error)

BuildFromParquet builds a GIN index from a remote Parquet file. It delegates to BuildFromParquetContext with context.Background().

func (*S3Client) BuildFromParquetContext added in v1.1.0

func (c *S3Client) BuildFromParquetContext(ctx context.Context, bucket, key, jsonColumn string, ginCfg GINConfig) (*GINIndex, error)

BuildFromParquetContext is the context-aware sibling of BuildFromParquet. Caller context propagates through the S3 range reads performed during Parquet page access, so cancellation stops the build without waiting for individual range-read timeouts to expire.

func (*S3Client) Exists

func (c *S3Client) Exists(bucket, key string) (bool, error)

func (*S3Client) ExistsContext added in v1.1.0

func (c *S3Client) ExistsContext(ctx context.Context, bucket, key string) (bool, error)

func (*S3Client) GetObjectSize

func (c *S3Client) GetObjectSize(bucket, key string) (int64, error)

func (*S3Client) GetObjectSizeContext added in v1.1.0

func (c *S3Client) GetObjectSizeContext(ctx context.Context, bucket, key string) (int64, error)

GetObjectSizeContext is the context-aware sibling of GetObjectSize. Caller cancellation propagates into the HeadObject call. The request is capped at 10s; caller cancellation and a shorter caller deadline both take precedence.

func (*S3Client) HasGINIndex

func (c *S3Client) HasGINIndex(bucket, key string, cfg ParquetConfig) (bool, error)

func (*S3Client) HasGINIndexContext added in v1.1.0

func (c *S3Client) HasGINIndexContext(ctx context.Context, bucket, key string, cfg ParquetConfig) (bool, error)

func (*S3Client) HasSidecar

func (c *S3Client) HasSidecar(bucket, parquetKey string) (bool, error)

func (*S3Client) HasSidecarContext added in v1.1.0

func (c *S3Client) HasSidecarContext(ctx context.Context, bucket, parquetKey string) (bool, error)

func (*S3Client) ListGINFiles

func (c *S3Client) ListGINFiles(bucket, prefix string) ([]string, error)

func (*S3Client) ListParquetFiles

func (c *S3Client) ListParquetFiles(bucket, prefix string) ([]string, error)

func (*S3Client) LoadIndex

func (c *S3Client) LoadIndex(bucket, parquetKey string, cfg ParquetConfig) (*GINIndex, error)

func (*S3Client) LoadIndexContext added in v1.1.0

func (c *S3Client) LoadIndexContext(ctx context.Context, bucket, parquetKey string, cfg ParquetConfig) (*GINIndex, error)

func (*S3Client) OpenParquet

func (c *S3Client) OpenParquet(bucket, key string) (*parquet.File, io.ReaderAt, int64, error)

func (*S3Client) OpenParquetContext added in v1.1.0

func (c *S3Client) OpenParquetContext(ctx context.Context, bucket, key string) (*parquet.File, io.ReaderAt, int64, error)

OpenParquetContext opens a remote Parquet file and returns a reader that derives each range-read timeout from ctx, so that caller cancellation propagates into S3 GetObject calls made during Parquet page reads.

func (*S3Client) ReadFile

func (c *S3Client) ReadFile(bucket, key string) ([]byte, error)

func (*S3Client) ReadFileContext added in v1.1.0

func (c *S3Client) ReadFileContext(ctx context.Context, bucket, key string) ([]byte, error)

func (*S3Client) ReadFromParquetMetadata

func (c *S3Client) ReadFromParquetMetadata(bucket, key string, cfg ParquetConfig) (*GINIndex, error)

func (*S3Client) ReadFromParquetMetadataContext added in v1.1.0

func (c *S3Client) ReadFromParquetMetadataContext(ctx context.Context, bucket, key string, cfg ParquetConfig) (*GINIndex, error)

func (*S3Client) ReadSidecar

func (c *S3Client) ReadSidecar(bucket, parquetKey string) (*GINIndex, error)

func (*S3Client) ReadSidecarContext added in v1.1.0

func (c *S3Client) ReadSidecarContext(ctx context.Context, bucket, parquetKey string) (*GINIndex, error)

func (*S3Client) WriteFile

func (c *S3Client) WriteFile(bucket, key string, data []byte) error

func (*S3Client) WriteFileContext added in v1.1.0

func (c *S3Client) WriteFileContext(ctx context.Context, bucket, key string, data []byte) error

func (*S3Client) WriteSidecar

func (c *S3Client) WriteSidecar(bucket, parquetKey string, idx *GINIndex, opts ...EncodeOption) error

WriteSidecar encodes idx and writes it to the S3 sidecar key derived from parquetKey. opts (e.g. WithEncodeProfile) override the profile carried by idx.Config, since a decoded index always carries EncoderProfileDefault.

func (*S3Client) WriteSidecarContext added in v1.1.0

func (c *S3Client) WriteSidecarContext(ctx context.Context, bucket, parquetKey string, idx *GINIndex, opts ...EncodeOption) error

WriteSidecarContext is the context-aware sibling of WriteSidecar. opts (e.g. WithEncodeProfile) override the profile carried by idx.Config, since a decoded index always carries EncoderProfileDefault.

type S3Config

type S3Config struct {
	Endpoint  string
	Region    string
	AccessKey string
	SecretKey string
	PathStyle bool
}

func S3ConfigFromEnv

func S3ConfigFromEnv() S3Config

type SerializedConfig

type SerializedConfig struct {
	BloomFilterSize         uint32            `json:"bloom_filter_size"`
	BloomFilterHashes       uint8             `json:"bloom_filter_hashes"`
	EnableTrigrams          bool              `json:"enable_trigrams"`
	TrigramMinLength        int               `json:"trigram_min_length"`
	HLLPrecision            uint8             `json:"hll_precision"`
	PrefixBlockSize         int               `json:"prefix_block_size"`
	AdaptiveMinRGCoverage   int               `json:"adaptive_min_rg_coverage"`
	AdaptivePromotedTermCap int               `json:"adaptive_promoted_term_cap"`
	AdaptiveCoverageCeiling float64           `json:"adaptive_coverage_ceiling"`
	AdaptiveBucketCount     int               `json:"adaptive_bucket_count"`
	FTSPaths                []string          `json:"fts_paths,omitempty"`
	Transformers            []TransformerSpec `json:"transformers,omitempty"`
	// TrigramUnicodeVersion is the Unicode version of the trigram tables.
	// Absent means trigramUnicodeVersion; see trigram_unicode.go (#99).
	TrigramUnicodeVersion string `json:"trigram_unicode_version,omitempty"`
}

type StringIndex

type StringIndex struct {
	Terms     []string
	RGBitmaps []*RGSet
}

type StringLengthIndex

type StringLengthIndex struct {
	GlobalMin uint32
	GlobalMax uint32
	RGStats   []RGStringLengthStat
}

type TransformerFailureMode deprecated added in v0.2.0

type TransformerFailureMode = IngestFailureMode

TransformerFailureMode is kept as a deprecated source-compatible alias for pre-phase-17 callers.

Deprecated: use IngestFailureMode.

type TransformerID

type TransformerID uint8
const (
	TransformerUnknown TransformerID = iota
	TransformerISODateToEpochMs
	TransformerDateToEpochMs
	TransformerCustomDateToEpochMs
	TransformerToLower
	TransformerIPv4ToInt
	TransformerSemVerToInt
	TransformerRegexExtract
	TransformerRegexExtractInt
	TransformerDurationToMs
	TransformerEmailDomain
	TransformerURLHost
	TransformerNumericBucket
	TransformerBoolNormalize
)

type TransformerOption added in v0.2.0

type TransformerOption func(*transformerRegistrationOptions) error

func WithTransformerFailureMode added in v0.2.0

func WithTransformerFailureMode(mode IngestFailureMode) TransformerOption

type TransformerSpec

type TransformerSpec struct {
	Path        string            `json:"path"`
	Alias       string            `json:"alias,omitempty"`
	TargetPath  string            `json:"target_path,omitempty"`
	FailureMode IngestFailureMode `json:"failure_mode,omitempty"`
	ID          TransformerID     `json:"id"`
	Name        string            `json:"name"`
	Params      json.RawMessage   `json:"params,omitempty"`
}

func NewTransformerSpec

func NewTransformerSpec(path string, id TransformerID, params json.RawMessage) TransformerSpec

type TrigramIndex

type TrigramIndex struct {
	Trigrams  map[string]*RGSet
	NumRGs    int
	N         int
	Padding   string
	MinLength int
	// contains filtered or unexported fields
}

func MustNewTrigramIndex added in v0.2.0

func MustNewTrigramIndex(numRGs int, opts ...NGramOption) *TrigramIndex

func NewTrigramIndex

func NewTrigramIndex(numRGs int, opts ...NGramOption) (*TrigramIndex, error)

func (*TrigramIndex) Add

func (ti *TrigramIndex) Add(value string, rgID int)

func (*TrigramIndex) Search

func (ti *TrigramIndex) Search(pattern string) *RGSet

func (*TrigramIndex) TrigramCount

func (ti *TrigramIndex) TrigramCount() int

Directories

Path Synopsis
cmd
gin-index command
examples
basic command
Example: Basic GIN index usage with equality queries
Example: Basic GIN index usage with equality queries
failure-modes command
Example: hard and soft ingest failure modes
Example: hard and soft ingest failure modes
full command
Example: Comprehensive GIN index usage demonstrating all index types and query operators
Example: Comprehensive GIN index usage demonstrating all index types and query operators
fulltext command
Example: Full-text search with trigram index (CONTAINS queries)
Example: Full-text search with trigram index (CONTAINS queries)
nested command
Example: Nested JSON objects and arrays
Example: Nested JSON objects and arrays
null command
Example: NULL handling queries
Example: NULL handling queries
parquet command
range command
Example: Numeric range queries with GIN index
Example: Numeric range queries with GIN index
regex command
Example: Regex pattern matching with trigram-based candidate selection
Example: Regex pattern matching with trigram-based candidate selection
serialize command
Example: Serializing and deserializing GIN index
Example: Serializing and deserializing GIN index
transformers command
Example: Field transformers for date indexing
Example: Field transformers for date indexing
transformers-advanced command
Example: Advanced field transformers for IP ranges, semantic versions, emails, and regex extraction
Example: Advanced field transformers for IP ranges, semantic versions, emails, and regex extraction
Package logging defines the repo-owned, backend-neutral logging contract used throughout github.com/amikos-tech/ami-gin.
Package logging defines the repo-owned, backend-neutral logging contract used throughout github.com/amikos-tech/ami-gin.
slogadapter
Package slogadapter wires log/slog into the repo-owned logging contract.
Package slogadapter wires log/slog into the repo-owned logging contract.
stdadapter
Package stdadapter wires the stdlib *log.Logger into the repo-owned logging contract.
Package stdadapter wires the stdlib *log.Logger into the repo-owned logging contract.
Package telemetry provides the local Signals container and coarse boundary helper for github.com/amikos-tech/ami-gin.
Package telemetry provides the local Signals container and coarse boundary helper for github.com/amikos-tech/ami-gin.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL