This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 6e4bf05b3b Unparse Sort with pushdown limit to SQL string (#12873)
6e4bf05b3b is described below

commit 6e4bf05b3bd7ee5b81ab8fb24eb98f236856ab3c
Author: Jax Liu <[email protected]>
AuthorDate: Mon Oct 14 19:46:13 2024 +0800

    Unparse Sort with pushdown limit to SQL string (#12873)
    
    * unparse Sort with push down limit
    
    * cargo fmt
    
    * set query limit directly
---
 datafusion/sql/src/unparser/plan.rs       |  6 ++++++
 datafusion/sql/tests/cases/plan_to_sql.rs | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/datafusion/sql/src/unparser/plan.rs 
b/datafusion/sql/src/unparser/plan.rs
index c4fcbb2d64..d150f0e532 100644
--- a/datafusion/sql/src/unparser/plan.rs
+++ b/datafusion/sql/src/unparser/plan.rs
@@ -357,6 +357,12 @@ impl Unparser<'_> {
                     return self.derive(plan, relation);
                 }
                 if let Some(query_ref) = query {
+                    if let Some(fetch) = sort.fetch {
+                        
query_ref.limit(Some(ast::Expr::Value(ast::Value::Number(
+                            fetch.to_string(),
+                            false,
+                        ))));
+                    }
                     query_ref.order_by(self.sorts_to_sql(sort.expr.clone())?);
                 } else {
                     return internal_err!(
diff --git a/datafusion/sql/tests/cases/plan_to_sql.rs 
b/datafusion/sql/tests/cases/plan_to_sql.rs
index 903d4e2852..aff9f99c8c 100644
--- a/datafusion/sql/tests/cases/plan_to_sql.rs
+++ b/datafusion/sql/tests/cases/plan_to_sql.rs
@@ -841,6 +841,26 @@ fn test_table_scan_pushdown() -> Result<()> {
     Ok(())
 }
 
+#[test]
+fn test_sort_with_push_down_fetch() -> Result<()> {
+    let schema = Schema::new(vec![
+        Field::new("id", DataType::Utf8, false),
+        Field::new("age", DataType::Utf8, false),
+    ]);
+
+    let plan = table_scan(Some("t1"), &schema, None)?
+        .project(vec![col("id"), col("age")])?
+        .sort_with_limit(vec![col("age").sort(true, true)], Some(10))?
+        .build()?;
+
+    let sql = plan_to_sql(&plan)?;
+    assert_eq!(
+        format!("{}", sql),
+        "SELECT t1.id, t1.age FROM t1 ORDER BY t1.age ASC NULLS FIRST LIMIT 10"
+    );
+    Ok(())
+}
+
 #[test]
 fn test_interval_lhs_eq() {
     sql_round_trip(


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to