Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix multiple sorts deparse #66

Merged
merged 1 commit into from
Mar 8, 2023
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
8 changes: 7 additions & 1 deletion supabase-wrappers/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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));
}
Expand Down
9 changes: 7 additions & 2 deletions wrappers/src/fdw/bigquery_fdw/bigquery_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<String>>()
.join(", ");
sql.push_str(&format!(" order by {}", order_by));
}

// push down limits
Expand Down
6 changes: 5 additions & 1 deletion wrappers/src/fdw/bigquery_fdw/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();

Expand Down