Skip to content

Commit

Permalink
Test lots of array SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
emk committed Oct 16, 2023
1 parent cfb5803 commit e6c4512
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 12 deletions.
20 changes: 11 additions & 9 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,22 +1033,24 @@ pub struct ArrayExpression {
impl Emit for ArrayExpression {
fn emit(&self, t: Target, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match t {
Target::Snowflake => {
self.delim1.with_token_str("[").emit(t, f)?;
self.definition.emit(t, f)?;
self.delim2.with_token_str("]").emit(t, f)?;
}
Target::SQLite3 => {
if let Some(array_token) = &self.array_token {
write!(f, "{}", t.f(array_token))?;
array_token.emit(t, f)?;
} else {
write!(f, "ARRAY")?;
}
write!(
f,
"{}{}{}",
t.f(&self.delim1.with_token_str("(")),
t.f(&self.definition),
t.f(&self.delim2.with_token_str(")"))
)
self.delim1.with_token_str("(").emit(t, f)?;
self.definition.emit(t, f)?;
self.delim2.with_token_str(")").emit(t, f)?;
}
_ => self.emit_default(t, f),
_ => self.emit_default(t, f)?,
}
Ok(())
}
}

Expand Down
1 change: 1 addition & 0 deletions src/drivers/snowflake/rename_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::ast::{FunctionName, Identifier};
// A `phf_map!` of BigQuery function names to Snowflake function names. Use
// this for simple renaming.
static FUNCTION_NAMES: phf::Map<&'static str, &'static str> = phf::phf_map! {
"ARRAY_LENGTH" => "ARRAY_SIZE",
"GENERATE_UUID" => "UUID_STRING",
"REGEXP_EXTRACT" => "REGEXP_SUBSTR",
"SHA256" => "SHA2_BINARY", // Second argument defaults to SHA256.
Expand Down
24 changes: 24 additions & 0 deletions tests/sql/data_types/arrays.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- pending: sqlite3 No array support.

CREATE OR REPLACE TABLE __result1 AS
SELECT
[] AS empty_array,
[1, 2, 3] AS int64_array,
['a', 'b', 'c'] AS string_array,
ARRAY<INT64>[] AS empty_array_typed,
ARRAY<INT64>[1, 2, 3] AS int64_array_typed,
ARRAY(1, 2, 3) AS int64_array_typed2,
ARRAY_LENGTH([1,2]) AS array_len;

CREATE OR REPLACE TABLE __expected1 (
empty_array ARRAY<INT64>,
int64_array ARRAY<INT64>,
string_array ARRAY<STRING>,
empty_array_typed ARRAY<INT64>,
int64_array_typed ARRAY<INT64>,
int64_array_typed2 ARRAY<INT64>,
array_len INT64,
);
-- Snowflake does not allow array constants in VALUES.
INSERT INTO __expected1
SELECT [], [1, 2, 3], ['a', 'b', 'c'], [], [1, 2, 3], [1, 2, 3], 2;
6 changes: 3 additions & 3 deletions tests/sql/functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ generate your own version of this list by running `joinery parse

Arrays:

- [ ] ARRAY_TO_STRING(_,_)
- [ ] ARRAY_AGG(_)
- [ ] ARRAY_LENGTH(_)
- [x] ARRAY_TO_STRING(_,_)
- [x] ARRAY_AGG(_)
- [x] ARRAY_LENGTH(_)

Special date functions:

Expand Down
22 changes: 22 additions & 0 deletions tests/sql/functions/aggregate/array_agg.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- pending: sqlite3 Arrays are not available.

CREATE TEMP TABLE array_agg_data (grp STRING, idx INT64, x INT64);
INSERT INTO array_agg_data
VALUES ('a', 1, 1), ('a', 2, 1), ('a', 3, 2), ('a', 4, 0);

CREATE OR REPLACE TABLE __result1 AS
SELECT grp, ARRAY_AGG(x) AS arr
FROM (
SELECT grp, x
FROM array_agg_data
ORDER BY idx
)
GROUP BY grp;

CREATE OR REPLACE TABLE __expected1 (
grp STRING,
arr ARRAY<INT64>,
);
-- Snowflake does not allow array constants in VALUES.
INSERT INTO __expected1
SELECT 'a', [1, 1, 2, 0];
15 changes: 15 additions & 0 deletions tests/sql/functions/simple/array_to_string.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- pending: sqlite3 Array functions are not available.
--
-- ARRAY_TO_STRING

CREATE OR REPLACE TABLE __result1 AS
SELECT
ARRAY_TO_STRING([1, 2, 3], ',') AS array_int_to_string,
ARRAY_TO_STRING(['a', 'b', 'c'], ',') AS array_string_to_string;

CREATE OR REPLACE TABLE __expected1 (
array_int_to_string STRING,
array_string_to_string STRING,
);
INSERT INTO __expected1 VALUES
('1,2,3', 'a,b,c');
12 changes: 12 additions & 0 deletions tests/sql/operators/array_index.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- pending: sqlite3 No array support.

-- We pre-declare this table to avoid SELECT creating a VARIANT column, which
-- breaks our Snowflake test driver (though it appears to store correctly).
CREATE OR REPLACE TABLE __result1 (v INT64);
INSERT INTO __result1
SELECT
[1,2][1] AS array_index;

CREATE OR REPLACE TABLE __expected1 (v INT64);
INSERT INTO __expected1 VALUES
(2);

0 comments on commit e6c4512

Please sign in to comment.