Skip to main content

Crate sqlglot_rust

Crate sqlglot_rust 

Source
Expand description

§sqlglot-rust

A SQL parser, optimizer, and transpiler library written in Rust, inspired by Python’s sqlglot.

§Features

  • Parse SQL strings into a structured AST
  • Generate SQL from AST nodes
  • Transpile between SQL dialects (30 dialects including MySQL, PostgreSQL, BigQuery, Snowflake, DuckDB, Hive, Spark, Presto, Trino, T-SQL, Oracle, ClickHouse, Redshift, and more)
  • Optimize SQL queries
  • CTEs, subqueries, UNION/INTERSECT/EXCEPT
  • Window functions, CAST, EXTRACT, EXISTS
  • Pretty-print SQL output
  • AST traversal (walk, find, transform)

§Quick Start

use sqlglot_rust::{parse, generate, transpile, Dialect};

// Parse a SQL query
let ast = parse("SELECT a, b FROM t WHERE a > 1", Dialect::Ansi).unwrap();

// Generate SQL for a specific dialect
let sql = generate(&ast, Dialect::Postgres);
assert_eq!(sql, "SELECT a, b FROM t WHERE a > 1");

// One-step transpile between dialects
let result = transpile("SELECT a, b FROM t", Dialect::Ansi, Dialect::Postgres).unwrap();

Re-exports§

pub use ast::Expr;
pub use ast::MergeClauseKind;
pub use ast::QuoteStyle;
pub use ast::Statement;
pub use dialects::Dialect;
pub use errors::SqlglotError;
pub use generator::generate;
pub use generator::generate_pretty;
pub use optimizer::annotate_types::TypeAnnotations;
pub use optimizer::annotate_types::annotate_types;
pub use optimizer::pushdown_predicates::pushdown_predicates;
pub use optimizer::scope_analysis::Scope;
pub use optimizer::scope_analysis::ScopeType;
pub use optimizer::scope_analysis::build_scope;
pub use optimizer::scope_analysis::find_all_in_scope;
pub use parser::parse;

Modules§

ast
dialects
errors
generator
optimizer
Query optimization passes.
parser
schema
Schema management system for schema-aware analysis and optimization.
tokens

Functions§

transpile
Transpile a SQL string from one dialect to another.
transpile_statements
Transpile a SQL string, returning multiple statements if the input contains semicolons.