forked from gluesql/gluesql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement COALESCE function (gluesql#1333)
Returns the first non-NULL value. Returns NULL if all arguments in the COALESCE function are NULL. e.g. SELECT COALESCE(NULL, 3, "test");
- Loading branch information
1 parent
53f35cf
commit 53b943f
Showing
13 changed files
with
348 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use { | ||
crate::*, | ||
chrono::{NaiveDate, NaiveDateTime}, | ||
gluesql_core::{ | ||
ast_builder::{self, *}, | ||
executor::Payload, | ||
prelude::Value::*, | ||
}, | ||
}; | ||
|
||
test_case!(coalesce, async move { | ||
let glue = get_glue!(); | ||
|
||
// create table - Foo | ||
let actual = table("Foo") | ||
.create_table() | ||
.add_column("id INTEGER PRIMARY KEY") | ||
.add_column("first TEXT") | ||
.add_column("second INTEGER") | ||
.add_column("third TIMESTAMP") | ||
.execute(glue) | ||
.await; | ||
let expected = Ok(Payload::Create); | ||
assert_eq!(actual, expected, "create table - Foo"); | ||
|
||
// insert into Foo | ||
let actual = table("Foo") | ||
.insert() | ||
.columns("id, first, second, third") | ||
.values(vec![ | ||
vec![num(100), text("visible"), null(), null()], | ||
vec![num(200), null(), num(42), null()], | ||
vec![num(300), null(), null(), timestamp("2023-06-01 12:00:00")], | ||
vec![num(400), null(), null(), null()], | ||
]) | ||
.execute(glue) | ||
.await; | ||
let expected = Ok(Payload::Insert(4)); | ||
assert_eq!(actual, expected, "insert into Foo"); | ||
|
||
let actual = table("Foo") | ||
.select() | ||
.project("id") | ||
.project(ast_builder::coalesce(vec![ | ||
null(), | ||
col("first"), | ||
col("second"), | ||
col("third"), | ||
])) | ||
.order_by("id") | ||
.execute(glue) | ||
.await; | ||
let expected = Ok(select_with_null!( | ||
id | r#"COALESCE(NULL, "first", "second", "third")"#; | ||
I64(100) Str("visible".to_owned()); | ||
I64(200) I64(42); | ||
I64(300) Timestamp("2023-06-01T12:00:00".parse::<NaiveDateTime>().unwrap()); | ||
I64(400) Null | ||
)); | ||
assert_eq!(actual, expected, "coalesce with table columns"); | ||
|
||
let actual = values(vec![ | ||
vec![ast_builder::coalesce(vec![text("뀨")])], | ||
vec![ast_builder::coalesce(vec![null(), num(1)])], | ||
vec![ast_builder::coalesce(vec![ | ||
null(), | ||
null(), | ||
date("2000-01-01"), | ||
])], | ||
]) | ||
.execute(glue) | ||
.await; | ||
let expected = Ok(select_with_null!( | ||
column1; | ||
Str("뀨".to_owned()); | ||
I64(1); | ||
Date("2000-01-01".parse::<NaiveDate>().unwrap()) | ||
)); | ||
assert_eq!(actual, expected, "coalesce without table"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod coalesce; | ||
pub mod ifnull; |
Oops, something went wrong.