Skip to content

Commit

Permalink
Fix unparsing OFFSET (apache#12539)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stazer authored and sgrebnov committed Oct 23, 2024
1 parent c7f1147 commit ee9043f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
15 changes: 15 additions & 0 deletions datafusion/sql/src/unparser/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,21 @@ impl Unparser<'_> {
))));
}

if limit.skip > 0 {
let Some(query) = query.as_mut() else {
return internal_err!(
"Offset operator only valid in a statement context."
);
};
query.offset(Some(ast::Offset {
rows: ast::OffsetRows::None,
value: ast::Expr::Value(ast::Value::Number(
limit.skip.to_string(),
false,
)),
}));
}

self.select_to_sql_recursively(
limit.input.as_ref(),
query,
Expand Down
24 changes: 22 additions & 2 deletions datafusion/sql/tests/cases/plan_to_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,11 @@ fn test_pretty_roundtrip() -> Result<()> {
Ok(())
}

fn sql_round_trip(query: &str, expect: &str) {
let statement = Parser::new(&GenericDialect {})
fn sql_round_trip<D>(dialect: D, query: &str, expect: &str)
where
D: Dialect,
{
let statement = Parser::new(&dialect)
.try_with_sql(query)
.unwrap()
.parse_statement()
Expand Down Expand Up @@ -852,6 +855,7 @@ fn test_table_scan_pushdown() -> Result<()> {
#[test]
fn test_interval_lhs_eq() {
sql_round_trip(
GenericDialect {},
"select interval '2 seconds' = interval '2 seconds'",
"SELECT (INTERVAL '2.000000000 SECS' = INTERVAL '2.000000000 SECS')",
);
Expand All @@ -860,6 +864,7 @@ fn test_interval_lhs_eq() {
#[test]
fn test_interval_lhs_lt() {
sql_round_trip(
GenericDialect {},
"select interval '2 seconds' < interval '2 seconds'",
"SELECT (INTERVAL '2.000000000 SECS' < INTERVAL '2.000000000 SECS')",
);
Expand Down Expand Up @@ -913,4 +918,19 @@ fn test_unnest_to_sql() {
r#"SELECT unnest(array_col) as u1, struct_col, array_col FROM unnest_table WHERE array_col != NULL ORDER BY struct_col, array_col"#,
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"#,
);
}

#[test]
fn test_without_offset() {
sql_round_trip(MySqlDialect {}, "select 1", "SELECT 1");
}

#[test]
fn test_with_offset0() {
sql_round_trip(MySqlDialect {}, "select 1 offset 0", "SELECT 1");
}

#[test]
fn test_with_offset95() {
sql_round_trip(MySqlDialect {}, "select 1 offset 95", "SELECT 1 OFFSET 95");
}

0 comments on commit ee9043f

Please sign in to comment.