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
15 changes: 15 additions & 0 deletions datafusion/sql/src/unparser/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,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 @@ -622,8 +622,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 @@ -788,6 +791,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 @@ -796,7 +800,23 @@ 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')",
);
}

#[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");
}