From d978cbc8967b8ac6ff1af6e2c62b899fc9547006 Mon Sep 17 00:00:00 2001 From: Bo Lu Date: Fri, 3 Mar 2023 23:20:37 +1100 Subject: [PATCH] fix multiple sorts deparse --- supabase-wrappers/src/interface.rs | 8 +++++++- wrappers/src/fdw/bigquery_fdw/bigquery_fdw.rs | 9 +++++++-- wrappers/src/fdw/bigquery_fdw/tests.rs | 6 +++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/supabase-wrappers/src/interface.rs b/supabase-wrappers/src/interface.rs index 4dd821f3..efa3f704 100644 --- a/supabase-wrappers/src/interface.rs +++ b/supabase-wrappers/src/interface.rs @@ -320,7 +320,7 @@ pub struct Sort { impl Sort { pub fn deparse(&self) -> String { - let mut sql = format!("order by {}", self.field); + let mut sql = self.field.to_string(); if self.reversed { sql.push_str(" desc"); @@ -334,6 +334,12 @@ impl Sort { sql.push_str(" nulls last") } + sql + } + + pub fn deparse_with_collate(&self) -> String { + let mut sql = self.deparse(); + if let Some(collate) = &self.collate { sql.push_str(&format!(" collate {}", collate)); } diff --git a/wrappers/src/fdw/bigquery_fdw/bigquery_fdw.rs b/wrappers/src/fdw/bigquery_fdw/bigquery_fdw.rs index 0322454b..9e916742 100644 --- a/wrappers/src/fdw/bigquery_fdw/bigquery_fdw.rs +++ b/wrappers/src/fdw/bigquery_fdw/bigquery_fdw.rs @@ -126,8 +126,13 @@ impl BigQueryFdw { }; // push down sorts - for sort in sorts { - sql.push_str(&format!(" {}", sort.deparse())); + if !sorts.is_empty() { + let order_by = sorts + .iter() + .map(|sort| sort.deparse()) + .collect::>() + .join(", "); + sql.push_str(&format!(" order by {}", order_by)); } // push down limits diff --git a/wrappers/src/fdw/bigquery_fdw/tests.rs b/wrappers/src/fdw/bigquery_fdw/tests.rs index a2a1515c..73b227c7 100644 --- a/wrappers/src/fdw/bigquery_fdw/tests.rs +++ b/wrappers/src/fdw/bigquery_fdw/tests.rs @@ -58,7 +58,11 @@ mod tests { assert_eq!(results, vec!["foo", "bar"]); let results = c - .select("SELECT name FROM test_table ORDER BY id DESC LIMIT 1", None, None) + .select( + "SELECT name FROM test_table ORDER BY id DESC, name LIMIT 1", + None, + None, + ) .filter_map(|r| r.by_name("name").ok().and_then(|v| v.value::<&str>())) .collect::>();