Skip to content

Commit ee9043f

Browse files
Stazersgrebnov
authored andcommitted
Fix unparsing OFFSET (apache#12539)
1 parent c7f1147 commit ee9043f

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

datafusion/sql/src/unparser/plan.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,21 @@ impl Unparser<'_> {
368368
))));
369369
}
370370

371+
if limit.skip > 0 {
372+
let Some(query) = query.as_mut() else {
373+
return internal_err!(
374+
"Offset operator only valid in a statement context."
375+
);
376+
};
377+
query.offset(Some(ast::Offset {
378+
rows: ast::OffsetRows::None,
379+
value: ast::Expr::Value(ast::Value::Number(
380+
limit.skip.to_string(),
381+
false,
382+
)),
383+
}));
384+
}
385+
371386
self.select_to_sql_recursively(
372387
limit.input.as_ref(),
373388
query,

datafusion/sql/tests/cases/plan_to_sql.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,11 @@ fn test_pretty_roundtrip() -> Result<()> {
653653
Ok(())
654654
}
655655

656-
fn sql_round_trip(query: &str, expect: &str) {
657-
let statement = Parser::new(&GenericDialect {})
656+
fn sql_round_trip<D>(dialect: D, query: &str, expect: &str)
657+
where
658+
D: Dialect,
659+
{
660+
let statement = Parser::new(&dialect)
658661
.try_with_sql(query)
659662
.unwrap()
660663
.parse_statement()
@@ -852,6 +855,7 @@ fn test_table_scan_pushdown() -> Result<()> {
852855
#[test]
853856
fn test_interval_lhs_eq() {
854857
sql_round_trip(
858+
GenericDialect {},
855859
"select interval '2 seconds' = interval '2 seconds'",
856860
"SELECT (INTERVAL '2.000000000 SECS' = INTERVAL '2.000000000 SECS')",
857861
);
@@ -860,6 +864,7 @@ fn test_interval_lhs_eq() {
860864
#[test]
861865
fn test_interval_lhs_lt() {
862866
sql_round_trip(
867+
GenericDialect {},
863868
"select interval '2 seconds' < interval '2 seconds'",
864869
"SELECT (INTERVAL '2.000000000 SECS' < INTERVAL '2.000000000 SECS')",
865870
);
@@ -913,4 +918,19 @@ fn test_unnest_to_sql() {
913918
r#"SELECT unnest(array_col) as u1, struct_col, array_col FROM unnest_table WHERE array_col != NULL ORDER BY struct_col, array_col"#,
914919
r#"SELECT UNNEST(unnest_table.array_col) AS u1, unnest_table.struct_col, unnest_table.array_col FROM unnest_table WHERE (unnest_table.array_col <> NULL) ORDER BY unnest_table.struct_col ASC NULLS LAST, unnest_table.array_col ASC NULLS LAST"#,
915920
);
921+
}
922+
923+
#[test]
924+
fn test_without_offset() {
925+
sql_round_trip(MySqlDialect {}, "select 1", "SELECT 1");
926+
}
927+
928+
#[test]
929+
fn test_with_offset0() {
930+
sql_round_trip(MySqlDialect {}, "select 1 offset 0", "SELECT 1");
931+
}
932+
933+
#[test]
934+
fn test_with_offset95() {
935+
sql_round_trip(MySqlDialect {}, "select 1 offset 95", "SELECT 1 OFFSET 95");
916936
}

0 commit comments

Comments
 (0)