Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ All notable changes to this project are documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

## [Unreleased]

### Fixed

- Oracle: `LIMIT` is now rendered as `[OFFSET n ROWS] FETCH FIRST m ROWS ONLY`
by the generator itself, driven by `GeneratorConfig::limit_fetch_style`
(Oracle now defaults to `LimitFetchStyle::FetchFirst`). This covers ASTs built
with the builder or generated without `transpile`, as well as subqueries, CTEs,
`INSERT ... SELECT`, and set operations that previously kept `LIMIT`.
`LIMIT ALL` / `LIMIT NULL` are dropped instead of becoming `FETCH FIRST ALL`,
and T-SQL `TOP n [PERCENT] [WITH TIES]` maps to the equivalent `FETCH FIRST`.
T-SQL's `limit_fetch_style` is now `Top`, matching its existing output.
An explicit `FetchFirst` config takes precedence over dialect-specific
LIMIT/OFFSET rendering (e.g. Presto/Trino `OFFSET n LIMIT m`), except on
T-SQL/Fabric, which keep their TOP / `OFFSET ... FETCH NEXT` output.
([#480](https://github.com/tobilg/polyglot/issues/480))
- Set operations: a branch-local row limit that renders as a trailing clause
(e.g. T-SQL `SELECT TOP 5 ...` in a `UNION` branch transpiled to `LIMIT` or
`FETCH FIRST`) is now parenthesized, so it no longer limits the whole set
operation. SQLite, which rejects parenthesized operands, gets
`SELECT * FROM (...)` instead. Operands wrapped in comments (e.g. a comment
before `UNION ALL`) are grouped the same way, with the comment kept outside
the parentheses.

## [0.13.0] - 2026-09-24

### Added
Expand Down
26 changes: 2 additions & 24 deletions crates/polyglot-sql/src/dialects/normalization/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,8 @@ pub(super) fn normalize_root(expression: Expression, context: &NormalizationCont
expr
};

// Oracle: LIMIT -> FETCH FIRST, OFFSET -> OFFSET ROWS
let expr = if matches!(target, DialectType::Oracle) {
if let Expression::Select(mut select) = expr {
if let Some(limit) = select.limit.take() {
// Convert LIMIT to FETCH FIRST n ROWS ONLY
select.fetch = Some(crate::expressions::Fetch {
direction: "FIRST".to_string(),
count: Some(limit.this),
percent: false,
rows: true,
with_ties: false,
});
}
// Add ROWS to OFFSET if present
if let Some(ref mut offset) = select.offset {
offset.rows = Some(true);
}
Expression::Select(select)
} else {
expr
}
} else {
expr
};
// Oracle LIMIT/OFFSET -> OFFSET n ROWS FETCH FIRST m ROWS ONLY is rendered by the
// generator (`LimitFetchStyle::FetchFirst`), so it also applies to nested queries.

// Handle CreateTable WITH properties transformation before recursive transforms
let expr = if let Expression::CreateTable(mut ct) = expr {
Expand Down
2 changes: 2 additions & 0 deletions crates/polyglot-sql/src/dialects/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ impl DialectImpl for OracleDialect {
tz_to_with_time_zone: true,
// Oracle UNPIVOT aliases retain literal aliases as literals.
unpivot_aliases_are_identifiers: false,
// Oracle has no LIMIT: use OFFSET n ROWS FETCH FIRST m ROWS ONLY
limit_fetch_style: crate::generator::LimitFetchStyle::FetchFirst,
..Default::default()
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/polyglot-sql/src/dialects/tsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ impl DialectImpl for TSQLDialect {
identifier_quote_style: IdentifierQuoteStyle::BRACKET,
dialect: Some(DialectType::TSQL),
// T-SQL specific settings from Python sqlglot
// SQL Server uses TOP/FETCH instead of LIMIT
limit_fetch_style: crate::generator::LimitFetchStyle::FetchFirst,
// SQL Server uses TOP (or OFFSET/FETCH, handled by the generator) instead of LIMIT
limit_fetch_style: crate::generator::LimitFetchStyle::Top,
// NULLS FIRST/LAST not supported in SQL Server
null_ordering_supported: false,
// SQL Server does not support SQL:2003 aggregate FILTER clauses.
Expand Down
Loading
Loading