cel2sql

package module
v2.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

README

cel2sql

Convert CEL (Common Expression Language) expressions to PostgreSQL SQL

Go Version PostgreSQL License

cel2sql makes it easy to build dynamic SQL queries using CEL expressions. Write type-safe, expressive filters in CEL and automatically convert them to PostgreSQL-compatible SQL.

Quick Start

Installation
go get github.com/spandigital/cel2sql/v2
Basic Example
package main

import (
    "fmt"
    "github.com/google/cel-go/cel"
    "github.com/spandigital/cel2sql/v2"
    "github.com/spandigital/cel2sql/v2/pg"
)

func main() {
    // 1. Define your database table schema
    userSchema := pg.Schema{
        {Name: "name", Type: "text"},
        {Name: "age", Type: "integer"},
        {Name: "active", Type: "boolean"},
    }

    // 2. Create CEL environment
    env, _ := cel.NewEnv(
        cel.CustomTypeProvider(pg.NewTypeProvider(map[string]pg.Schema{
            "User": userSchema,
        })),
        cel.Variable("user", cel.ObjectType("User")),
    )

    // 3. Write your filter expression in CEL
    ast, _ := env.Compile(`user.age >= 18 && user.active`)

    // 4. Convert to SQL
    sqlWhere, _ := cel2sql.Convert(ast)

    fmt.Println(sqlWhere)
    // Output: user.age >= 18 AND user.active IS TRUE

    // 5. Use in your query
    query := "SELECT * FROM users WHERE " + sqlWhere
}

Why cel2sql?

Type-Safe: Catch errors at compile time, not runtime ✅ PostgreSQL 17: Fully compatible with the latest PostgreSQL ✅ Rich Features: JSON/JSONB, arrays, regex, timestamps, and more ✅ Well-Tested: 100+ tests including integration tests with real PostgreSQL ✅ Easy to Use: Simple API, comprehensive documentation ✅ Secure by Default: Built-in protections against SQL injection and ReDoS attacks

Security Features

cel2sql includes comprehensive security protections:

  • 🛡️ Field Name Validation - Prevents SQL injection via field names
  • 🔒 JSON Field Escaping - Automatic quote escaping in JSON paths
  • 🚫 ReDoS Protection - Validates regex patterns to prevent catastrophic backtracking
  • ⏱️ Context Timeouts - Optional timeout protection for complex expressions

All security features are enabled by default with zero configuration required.

Advanced Options

cel2sql supports optional advanced features via functional options:

import (
    "context"
    "log/slog"
    "github.com/spandigital/cel2sql/v2"
)

// Basic conversion
sql, err := cel2sql.Convert(ast)

// With schemas for JSON/JSONB support
sql, err := cel2sql.Convert(ast,
    cel2sql.WithSchemas(schemas))

// With context for timeouts
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
sql, err := cel2sql.Convert(ast,
    cel2sql.WithContext(ctx),
    cel2sql.WithSchemas(schemas))

// With logging for observability
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
sql, err := cel2sql.Convert(ast,
    cel2sql.WithContext(ctx),
    cel2sql.WithSchemas(schemas),
    cel2sql.WithLogger(logger))

Available Options:

  • WithSchemas(map[string]pg.Schema) - Provide table schemas for JSON detection
  • WithContext(context.Context) - Enable cancellation and timeouts
  • WithLogger(*slog.Logger) - Enable structured logging

Common Use Cases

1. User Filters
// CEL: Simple comparison
user.age > 21 && user.country == "USA"
// SQL: user.age > 21 AND user.country = 'USA'
// CEL: String operations
user.email.startsWith("admin") || user.name.contains("John")
// SQL: user.email LIKE 'admin%' OR POSITION('John' IN user.name) > 0
3. Date Filters
// CEL: Date comparisons
user.created_at > timestamp("2024-01-01T00:00:00Z")
// SQL: user.created_at > CAST('2024-01-01T00:00:00Z' AS TIMESTAMP WITH TIME ZONE)
4. JSON/JSONB Fields
// CEL: JSON field access
user.preferences.theme == "dark"
// SQL: user.preferences->>'theme' = 'dark'
5. Array Operations
// CEL: Check if all items match
user.scores.all(s, s >= 60)
// SQL: NOT EXISTS (SELECT 1 FROM UNNEST(user.scores) AS s WHERE NOT (s >= 60))

Documentation

Supported Features

Feature CEL Example PostgreSQL SQL
Comparisons age > 18 age > 18
Logic active && verified active IS TRUE AND verified IS TRUE
Strings name.startsWith("A") name LIKE 'A%'
Lists "admin" in roles 'admin' IN UNNEST(roles)
JSON data.key == "value" data->>'key' = 'value'
Regex email.matches(r".*@test\.com") email ~ '.*@test\.com'
Dates created_at.getFullYear() == 2024 EXTRACT(YEAR FROM created_at) = 2024
Conditionals age > 30 ? "senior" : "junior" CASE WHEN age > 30 THEN 'senior' ELSE 'junior' END

Type Mapping

CEL Type PostgreSQL Type
int bigint
double double precision
bool boolean
string text
bytes bytea
list ARRAY
timestamp timestamp with time zone
duration INTERVAL

Dynamic Schema Loading

Load table schemas directly from your PostgreSQL database:

// Connect to database and load schema
provider, _ := pg.NewTypeProviderWithConnection(ctx, "postgres://user:pass@localhost/db")
defer provider.Close()

// Load table schema dynamically
provider.LoadTableSchema(ctx, "users")

// Use with CEL
env, _ := cel.NewEnv(
    cel.CustomTypeProvider(provider),
    cel.Variable("user", cel.ObjectType("users")),
)

See Getting Started Guide for more details.

Requirements

  • Go 1.24 or higher
  • PostgreSQL 17 (also compatible with PostgreSQL 15+)

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

Apache 2.0 - See LICENSE for details.

  • CEL-Go - Common Expression Language implementation in Go
  • CEL Spec - Common Expression Language specification

Need Help?

Documentation

Overview

Package cel2sql converts CEL (Common Expression Language) expressions to PostgreSQL SQL conditions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(ast *cel.Ast, opts ...ConvertOption) (string, error)

Convert converts a CEL AST to a PostgreSQL SQL WHERE clause condition. Options can be provided to configure the conversion behavior.

Example without options:

sql, err := cel2sql.Convert(ast)

Example with schema information for JSON/JSONB support:

sql, err := cel2sql.Convert(ast, cel2sql.WithSchemas(schemas))

Types

type ComprehensionInfo

type ComprehensionInfo struct {
	Type      ComprehensionType
	IterVar   string
	IndexVar  string // for two-variable comprehensions
	AccuVar   string
	HasFilter bool
	IsTwoVar  bool
	Transform *exprpb.Expr // transform expression for map/transformList
	Predicate *exprpb.Expr // predicate expression for filtering
	Filter    *exprpb.Expr // filter expression for map with filter
}

ComprehensionInfo holds metadata about a comprehension operation

type ComprehensionType

type ComprehensionType int

ComprehensionType represents the type of comprehension operation

const (
	ComprehensionAll               ComprehensionType = iota // All elements satisfy condition
	ComprehensionExists                                     // At least one element satisfies condition
	ComprehensionExistsOne                                  // Exactly one element satisfies condition
	ComprehensionMap                                        // Transform elements using expression
	ComprehensionFilter                                     // Filter elements by predicate
	ComprehensionTransformList                              // Transform list elements
	ComprehensionTransformMap                               // Transform map entries
	ComprehensionTransformMapEntry                          // Transform map key-value pairs
	ComprehensionUnknown                                    // Unrecognized comprehension pattern
)

CEL comprehension types supported by cel2sql

func (ComprehensionType) String

func (ct ComprehensionType) String() string

String returns a string representation of the comprehension type

type ConvertOption added in v2.11.0

type ConvertOption func(*convertOptions)

ConvertOption is a functional option for configuring the Convert function.

func WithContext added in v2.11.0

func WithContext(ctx context.Context) ConvertOption

WithContext provides a context for cancellation and timeout support. If not provided, operations will run without cancellation checks. This allows long-running conversions to be cancelled and enables timeout protection.

Example with timeout:

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
sql, err := cel2sql.Convert(ast, cel2sql.WithContext(ctx))

Example with cancellation:

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sql, err := cel2sql.Convert(ast, cel2sql.WithContext(ctx), cel2sql.WithSchemas(schemas))

func WithLogger added in v2.11.0

func WithLogger(logger *slog.Logger) ConvertOption

WithLogger provides a logger for observability and debugging. If not provided, logging is disabled with zero overhead using slog.DiscardHandler.

The logger enables visibility into:

  • JSON path detection decisions (table, field, operator selection)
  • Comprehension type identification (all, exists, filter, map)
  • Schema lookups (hits/misses, field types)
  • Performance metrics (conversion duration)
  • Regex pattern transformations (RE2 to POSIX)
  • Operator mapping decisions
  • Error contexts with full details

Example with JSON output:

logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
sql, err := cel2sql.Convert(ast, cel2sql.WithLogger(logger))

Example with text output:

logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
sql, err := cel2sql.Convert(ast, cel2sql.WithSchemas(schemas), cel2sql.WithLogger(logger))

func WithSchemas added in v2.11.0

func WithSchemas(schemas map[string]pg.Schema) ConvertOption

WithSchemas provides schema information for proper JSON/JSONB field handling. This option is required for correct SQL generation when using JSON/JSONB fields.

Example:

schemas := provider.GetSchemas()
sql, err := cel2sql.Convert(ast, cel2sql.WithSchemas(schemas))

Directories

Path Synopsis
examples
basic command
Package main demonstrates basic usage of cel2sql with a predefined schema.
Package main demonstrates basic usage of cel2sql with a predefined schema.
comprehensions command
Package main demonstrates CEL comprehensions support in cel2sql with PostgreSQL integration.
Package main demonstrates CEL comprehensions support in cel2sql with PostgreSQL integration.
context command
Package main demonstrates context usage with cel2sql for cancellation and timeout support.
Package main demonstrates context usage with cel2sql for cancellation and timeout support.
load_table_schema command
Package main demonstrates loading table schema dynamically from PostgreSQL.
Package main demonstrates loading table schema dynamically from PostgreSQL.
logging command
Package main demonstrates cel2sql structured logging with log/slog.
Package main demonstrates cel2sql structured logging with log/slog.
Package pg provides PostgreSQL type provider for CEL type system integration.
Package pg provides PostgreSQL type provider for CEL type system integration.
Package sqltypes provides custom SQL type definitions for CEL (Date, Time, DateTime).
Package sqltypes provides custom SQL type definitions for CEL (Date, Time, DateTime).
Package test provides PostgreSQL schema definitions for testing.
Package test provides PostgreSQL schema definitions for testing.

Jump to

Keyboard shortcuts

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