-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d5581c
commit 3dca5b0
Showing
24 changed files
with
9,559 additions
and
5,087 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1 @@ | ||
export const version = '0.0.31' | ||
export const version = '0.0.32' |
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,49 @@ | ||
module DataSources.DbMiner.QuerySnowflake exposing (addLimit, exploreColumn, exploreTable) | ||
|
||
import Libs.Regex as Regex | ||
import Models.Project.ColumnPath exposing (ColumnPath) | ||
import Models.Project.TableId exposing (TableId) | ||
import Models.SqlQuery exposing (SqlQuery) | ||
|
||
|
||
exploreTable : TableId -> SqlQuery | ||
exploreTable table = | ||
"SELECT *\nFROM " ++ formatTable table ++ ";\n" | ||
|
||
|
||
exploreColumn : TableId -> ColumnPath -> SqlQuery | ||
exploreColumn table column = | ||
formatColumn column | ||
|> (\col -> "SELECT\n " ++ col ++ ",\n count(*)\nFROM " ++ formatTable table ++ "\nGROUP BY " ++ col ++ "\nORDER BY count(*) DESC, " ++ col ++ ";\n") | ||
|
||
|
||
addLimit : SqlQuery -> SqlQuery | ||
addLimit query = | ||
case query |> String.trim |> Regex.matches "^([\\s\\S]+?)(\\slimit \\d+)?(\\soffset \\d+)?;$" of | ||
(Just q) :: Nothing :: Nothing :: [] -> | ||
q ++ "\nLIMIT 100;\n" | ||
|
||
(Just q) :: Nothing :: (Just offset) :: [] -> | ||
q ++ "\nLIMIT 100" ++ offset ++ ";\n" | ||
|
||
_ -> | ||
query | ||
|
||
|
||
|
||
-- generic helpers | ||
|
||
|
||
formatTable : TableId -> String | ||
formatTable ( schema, table ) = | ||
if schema == "" then | ||
"\"" ++ table ++ "\"" | ||
|
||
else | ||
"\"" ++ schema ++ "\"" ++ "." ++ "\"" ++ table ++ "\"" | ||
|
||
|
||
formatColumn : ColumnPath -> String | ||
formatColumn column = | ||
-- FIXME: don't handle https://docs.snowflake.com/en/sql-reference/data-types-semistructured | ||
"\"" ++ column.head ++ "\"" |
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,68 @@ | ||
module DataSources.DbMiner.QuerySnowflakeTest exposing (..) | ||
|
||
import DataSources.DbMiner.QuerySnowflake exposing (addLimit, exploreColumn, exploreTable) | ||
import Expect | ||
import Models.Project.ColumnPath as ColumnPath | ||
import Test exposing (Test, describe, test) | ||
|
||
|
||
suite : Test | ||
suite = | ||
describe "QuerySnowflake" | ||
[ describe "exploreTable" exploreTableSuite | ||
, describe "exploreColumn" exploreColumnSuite | ||
, describe "addLimit" addLimitSuite | ||
] | ||
|
||
|
||
exploreTableSuite : List Test | ||
exploreTableSuite = | ||
[ test "with schema" (\_ -> exploreTable ( "schema", "table" ) |> Expect.equal """SELECT * | ||
FROM "schema"."table"; | ||
""") | ||
, test "with empty schema" (\_ -> exploreTable ( "", "table" ) |> Expect.equal """SELECT * | ||
FROM "table"; | ||
""") | ||
] | ||
|
||
|
||
exploreColumnSuite : List Test | ||
exploreColumnSuite = | ||
[ test "with schema" (\_ -> exploreColumn ( "schema", "table" ) (ColumnPath.fromString "column") |> Expect.equal """SELECT | ||
"column", | ||
count(*) | ||
FROM "schema"."table" | ||
GROUP BY "column" | ||
ORDER BY count(*) DESC, "column"; | ||
""") | ||
, test "with empty schema" (\_ -> exploreColumn ( "", "table" ) (ColumnPath.fromString "column") |> Expect.equal """SELECT | ||
"column", | ||
count(*) | ||
FROM "table" | ||
GROUP BY "column" | ||
ORDER BY count(*) DESC, "column"; | ||
""") | ||
, test "with json column" (\_ -> exploreColumn ( "", "table" ) (ColumnPath.fromString "data:email") |> Expect.equal """SELECT | ||
"data", | ||
count(*) | ||
FROM "table" | ||
GROUP BY "data" | ||
ORDER BY count(*) DESC, "data"; | ||
""") | ||
] | ||
|
||
|
||
addLimitSuite : List Test | ||
addLimitSuite = | ||
[ test "without limit" (\_ -> addLimit "SELECT * FROM users;" |> Expect.equal "SELECT * FROM users\nLIMIT 100;\n") | ||
, test "with limit" (\_ -> addLimit "SELECT * FROM users LIMIT 10;" |> Expect.equal "SELECT * FROM users LIMIT 10;") | ||
, test "with offset" (\_ -> addLimit "SELECT * FROM users OFFSET 10;" |> Expect.equal "SELECT * FROM users\nLIMIT 100 OFFSET 10;\n") | ||
, test "with limit & offset" (\_ -> addLimit "SELECT * FROM users LIMIT 10 OFFSET 10;" |> Expect.equal "SELECT * FROM users LIMIT 10 OFFSET 10;") | ||
, test "multiline" (\_ -> addLimit """SELECT e.id, e.name | ||
FROM events e | ||
WHERE e.name='project_loaded'; """ |> Expect.equal """SELECT e.id, e.name | ||
FROM events e | ||
WHERE e.name='project_loaded' | ||
LIMIT 100; | ||
""") | ||
] |
Oops, something went wrong.