Skip to content

catkins/polyglot-sql-rb

Repository files navigation

polyglot-sql

Build status Gem Version

Ruby bindings for polyglot-sql — a Rust-based SQL transpiler supporting 30+ database dialects.

Installation

Add to your Gemfile:

gem "polyglot-sql"

Requires Rust toolchain for compilation. Install via rustup.

Usage

Transpile SQL Between Dialects

require "polyglot"

# PostgreSQL to MySQL
Polyglot.transpile("SELECT NOW()", from: :postgres, to: :mysql)
# => ["SELECT CURRENT_TIMESTAMP()"]

# PostgreSQL to Snowflake
Polyglot.transpile("SELECT CAST(x AS TEXT)", from: :postgres, to: :snowflake)
# => ["SELECT CAST(x AS TEXT)"]

Parse SQL to AST

ast = Polyglot.parse("SELECT 1", dialect: :postgres)
# => [{"select" => {...}}]

ast = Polyglot.parse_one("SELECT 1", dialect: :postgres)
# => {"select" => {...}}

Generate SQL from AST

ast = Polyglot.parse_one("SELECT 1", dialect: :postgres)
Polyglot.generate(ast, dialect: :mysql)
# => "SELECT 1"

Format SQL

Polyglot.format("SELECT a, b FROM t WHERE x = 1", dialect: :postgres)

Validate SQL

result = Polyglot.validate("SELECT 1", dialect: :postgres)
result.valid?  # => true
result.errors  # => []

result = Polyglot.validate("SELEC 1")
result.valid?  # => false
result.errors.first.message  # => "..."

List Supported Dialects

Polyglot.dialects
# => ["generic", "athena", "bigquery", "clickhouse", ..., "tsql"]

Supported Dialects

  • Generic
  • Athena
  • BigQuery
  • ClickHouse
  • CockroachDB
  • Databricks
  • Doris
  • Dremio
  • Drill
  • Druid
  • DuckDB
  • Dune
  • Exasol
  • Fabric
  • Hive
  • Materialize
  • MySQL
  • Oracle
  • PostgreSQL
  • Presto
  • Redshift
  • RisingWave
  • SingleStore
  • Snowflake
  • Solr
  • Spark
  • SQLite
  • StarRocks
  • Tableau
  • Teradata
  • TiDB
  • Trino
  • T-SQL

Error Handling

Polyglot::Error            # Base error class
Polyglot::ParseError       # SQL parsing errors
Polyglot::GenerateError    # SQL generation errors
Polyglot::UnsupportedError # Unsupported dialect features

Error Semantics

  • Unknown dialects raise ArgumentError (for transpile, parse, parse_one, generate, format, and validate).
  • generate raises Polyglot::GenerateError for invalid AST payloads.
  • Invalid AST error messages are concise and include line/column context when available.

Development

bundle install
bundle exec rake          # compile + test
bundle exec rake compile  # compile only
bundle exec rake spec     # test only
bundle exec standardrb    # lint
bundle exec rake docs:dialects  # sync README dialect list

CI

Buildkite runs:

  • Ruby build + test matrix on Ruby 3.2, 3.3, 3.4, and 4.0
  • Ruby lint (standardrb)
  • Rust checks (cargo check and cargo clippy -- -D warnings)

License

MIT

About

Ruby bindings to polyglot-sql crate

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •