-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
snowflake_test.clj
523 lines (495 loc) · 30.9 KB
/
snowflake_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
(ns metabase.driver.snowflake-test
(:require
[clojure.data.json :as json]
[clojure.java.jdbc :as jdbc]
[clojure.set :as set]
[clojure.string :as str]
[clojure.test :refer :all]
[metabase.driver :as driver]
[metabase.driver.snowflake :as driver.snowflake]
[metabase.driver.sql :as driver.sql]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
[metabase.driver.sql-jdbc.sync :as sql-jdbc.sync]
[metabase.driver.sql-jdbc.sync.describe-database :as sql-jdbc.describe-database]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.jvm :as lib.metadata.jvm]
[metabase.lib.test-util :as lib.tu]
[metabase.models :refer [Table]]
[metabase.models.database :refer [Database]]
[metabase.public-settings :as public-settings]
[metabase.query-processor :as qp]
[metabase.query-processor.store :as qp.store]
[metabase.sync :as sync]
[metabase.sync.util :as sync-util]
[metabase.test :as mt]
[metabase.test.data.dataset-definitions :as defs]
[metabase.test.data.interface :as tx]
[metabase.test.data.snowflake :as test.data.snowflake]
[metabase.test.data.sql :as sql.tx]
[metabase.test.data.sql.ddl :as ddl]
[metabase.util :as u]
[toucan2.core :as t2]
[toucan2.tools.with-temp :as t2.with-temp]))
(set! *warn-on-reflection* true)
(defn- query->native [query]
(let [check-sql-fn (fn [_ _ sql & _]
(throw (ex-info "done" {::native-query sql})))]
(with-redefs [sql-jdbc.execute/prepared-statement check-sql-fn
sql-jdbc.execute/execute-statement! check-sql-fn]
(try
(qp/process-query query)
(is false "no statement created")
(catch Exception e
(-> e u/all-ex-data ::native-query))))))
(use-fixtures :each (fn [thunk]
;; 1. If sync fails when loading a test dataset, don't swallow the error; throw an Exception so we
;; can debug it. This is much less confusing when trying to fix broken tests.
;;
;; 2. Make sure we're in Honey SQL 2 mode for all the little SQL snippets we're compiling in these
;; tests.
(binding [sync-util/*log-exceptions-and-continue?* false]
(thunk))))
(deftest sanity-check-test
(mt/test-driver :snowflake
(is (= [100]
(mt/first-row
(mt/run-mbql-query venues
{:aggregation [[:count]]}))))))
(deftest ^:parallel quote-name-test
(is (nil? (#'driver.snowflake/quote-name nil)))
(is (= "\"alma\"" (#'driver.snowflake/quote-name "alma")))
(is (= "\"Al\"\"aba\"\"Ma\"" (#'driver.snowflake/quote-name "Al\"aba\"Ma"))))
(deftest ^:parallel connection-details->spec-test
(let [details {:role nil
:warehouse "COMPUTE_WH"
:additional-options nil
:db "v3_sample-dataset"
:password "passwd"
:quote-db-name false
:let-user-control-scheduling false
:private-key-options "uploaded"
:private-key-source nil
:port nil
:advanced-options true
:private-key-id 1
:schema-filters-type "all"
:account "ls10467.us-east-2.aws"
:private-key-value nil
:tunnel-enabled false
:engine :snowflake
:private-key-creator-id 3
:user "SNOWFLAKE_DEVELOPER"
:private-key-created-at "2024-01-05T19:10:30.861839Z"
:host ""}]
(testing "Database name is quoted if quoting is requested (#27856)"
(are [quote? result] (=? {:db result}
(let [details (assoc details :quote-db-name quote?)]
(sql-jdbc.conn/connection-details->spec :snowflake details)))
true "\"v3_sample-dataset\""
false "v3_sample-dataset"))
(testing "Subname is replaced if hostname is provided (#22133)"
(are [use-hostname alternative-host expected-subname] (=? expected-subname
(:subname (let [details (-> details
(assoc :host alternative-host)
(assoc :use-hostname use-hostname))]
(sql-jdbc.conn/connection-details->spec :snowflake details))))
true nil "//ls10467.us-east-2.aws.snowflakecomputing.com/"
true "" "//ls10467.us-east-2.aws.snowflakecomputing.com/"
true " " "//ls10467.us-east-2.aws.snowflakecomputing.com/"
true "snowflake.example.com/" "//snowflake.example.com/"
true "snowflake.example.com" "//snowflake.example.com/"
false nil "//ls10467.us-east-2.aws.snowflakecomputing.com/"
false "" "//ls10467.us-east-2.aws.snowflakecomputing.com/"
false "snowflake.example.com/" "//ls10467.us-east-2.aws.snowflakecomputing.com/"
false "snowflake.example.com" "//ls10467.us-east-2.aws.snowflakecomputing.com/"))))
(deftest ^:parallel ddl-statements-test
(testing "make sure we didn't break the code that is used to generate DDL statements when we add new test datasets"
(binding [test.data.snowflake/*database-prefix-fn* (constantly "v4_")]
(testing "Create DB DDL statements"
(is (= "DROP DATABASE IF EXISTS \"v4_test-data\"; CREATE DATABASE \"v4_test-data\";"
(sql.tx/create-db-sql :snowflake (mt/get-dataset-definition defs/test-data)))))
(testing "Create Table DDL statements"
(is (= (map
#(str/replace % #"\s+" " ")
["DROP TABLE IF EXISTS \"v4_test-data\".\"PUBLIC\".\"users\";"
"CREATE TABLE \"v4_test-data\".\"PUBLIC\".\"users\" (\"id\" INTEGER AUTOINCREMENT, \"name\" TEXT,
\"last_login\" TIMESTAMP_NTZ, \"password\" TEXT, PRIMARY KEY (\"id\")) ;"
"DROP TABLE IF EXISTS \"v4_test-data\".\"PUBLIC\".\"categories\";"
"CREATE TABLE \"v4_test-data\".\"PUBLIC\".\"categories\" (\"id\" INTEGER AUTOINCREMENT, \"name\" TEXT NOT NULL,
PRIMARY KEY (\"id\")) ;"
"DROP TABLE IF EXISTS \"v4_test-data\".\"PUBLIC\".\"venues\";"
"CREATE TABLE \"v4_test-data\".\"PUBLIC\".\"venues\" (\"id\" INTEGER AUTOINCREMENT, \"name\" TEXT,
\"category_id\" INTEGER, \"latitude\" FLOAT, \"longitude\" FLOAT, \"price\" INTEGER, PRIMARY KEY (\"id\")) ;"
"DROP TABLE IF EXISTS \"v4_test-data\".\"PUBLIC\".\"checkins\";"
"CREATE TABLE \"v4_test-data\".\"PUBLIC\".\"checkins\" (\"id\" INTEGER AUTOINCREMENT, \"date\" DATE,
\"user_id\" INTEGER, \"venue_id\" INTEGER, PRIMARY KEY (\"id\")) ;"
"DROP TABLE IF EXISTS \"v4_test-data\".\"PUBLIC\".\"products\";"
"CREATE TABLE \"v4_test-data\".\"PUBLIC\".\"products\" (\"id\" INTEGER AUTOINCREMENT, \"ean\" TEXT,
\"title\" TEXT, \"category\" TEXT, \"vendor\" TEXT, \"price\" FLOAT, \"rating\" FLOAT, \"created_at\"
TIMESTAMP_TZ, PRIMARY KEY (\"id\")) ;"
"DROP TABLE IF EXISTS \"v4_test-data\".\"PUBLIC\".\"people\";"
"CREATE TABLE \"v4_test-data\".\"PUBLIC\".\"people\" (\"id\" INTEGER AUTOINCREMENT, \"address\" TEXT,
\"email\" TEXT, \"password\" TEXT, \"name\" TEXT, \"city\" TEXT, \"longitude\" FLOAT, \"state\" TEXT,
\"source\" TEXT, \"birth_date\" DATE, \"zip\" TEXT, \"latitude\" FLOAT, \"created_at\" TIMESTAMP_TZ,
PRIMARY KEY (\"id\")) ;"
"DROP TABLE IF EXISTS \"v4_test-data\".\"PUBLIC\".\"reviews\";"
"CREATE TABLE \"v4_test-data\".\"PUBLIC\".\"reviews\" (\"id\" INTEGER AUTOINCREMENT, \"product_id\" INTEGER,
\"reviewer\" TEXT, \"rating\" INTEGER, \"body\" TEXT, \"created_at\" TIMESTAMP_TZ, PRIMARY KEY (\"id\")) ;"
"DROP TABLE IF EXISTS \"v4_test-data\".\"PUBLIC\".\"orders\";"
"CREATE TABLE \"v4_test-data\".\"PUBLIC\".\"orders\" (\"id\" INTEGER AUTOINCREMENT, \"user_id\" INTEGER,
\"product_id\" INTEGER, \"subtotal\" FLOAT, \"tax\" FLOAT, \"total\" FLOAT, \"discount\" FLOAT, \"created_at\"
TIMESTAMP_TZ, \"quantity\" INTEGER, PRIMARY KEY (\"id\")) ;"
"ALTER TABLE \"v4_test-data\".\"PUBLIC\".\"venues\" ADD CONSTRAINT \"gory_id_categories_-1429799958\"
FOREIGN KEY (\"category_id\") REFERENCES \"v4_test-data\".\"PUBLIC\".\"categories\" (\"id\");"
"ALTER TABLE \"v4_test-data\".\"PUBLIC\".\"checkins\" ADD CONSTRAINT \"kins_user_id_users_-1503129306\"
FOREIGN KEY (\"user_id\") REFERENCES \"v4_test-data\".\"PUBLIC\".\"users\" (\"id\");"
"ALTER TABLE \"v4_test-data\".\"PUBLIC\".\"checkins\" ADD CONSTRAINT \"ckins_venue_id_venues_55711779\"
FOREIGN KEY (\"venue_id\") REFERENCES \"v4_test-data\".\"PUBLIC\".\"venues\" (\"id\");"
"ALTER TABLE \"v4_test-data\".\"PUBLIC\".\"reviews\" ADD CONSTRAINT \"roduct_id_products_-1093665274\"
FOREIGN KEY (\"product_id\") REFERENCES \"v4_test-data\".\"PUBLIC\".\"products\" (\"id\");"
"ALTER TABLE \"v4_test-data\".\"PUBLIC\".\"orders\" ADD CONSTRAINT \"ders_user_id_people_1646240302\"
FOREIGN KEY (\"user_id\") REFERENCES \"v4_test-data\".\"PUBLIC\".\"people\" (\"id\");"
"ALTER TABLE \"v4_test-data\".\"PUBLIC\".\"orders\" ADD CONSTRAINT \"roduct_id_products_-1151848842\"
FOREIGN KEY (\"product_id\") REFERENCES \"v4_test-data\".\"PUBLIC\".\"products\" (\"id\");"])
(ddl/create-db-tables-ddl-statements :snowflake (-> (mt/get-dataset-definition defs/test-data)
(update :database-name #(str "v4_" %))))))))))
(deftest ^:parallel simple-select-probe-query-test
(testing "the simple-select-probe-query used by have-select-privilege? should be qualified with the Database name. Ignore blank keys."
(mt/test-driver :snowflake
(qp.store/with-metadata-provider (lib.tu/mock-metadata-provider
{:database (assoc (mt/db)
:details {:db " "
:dbname "dbname"})})
(is (= ["SELECT TRUE AS \"_\" FROM \"dbname\".\"PUBLIC\".\"table\" WHERE 1 <> 1 LIMIT 0"]
(sql-jdbc.describe-database/simple-select-probe-query :snowflake "PUBLIC" "table")))))))
(deftest ^:parallel have-select-privilege?-test
(mt/test-driver :snowflake
(qp.store/with-metadata-provider (mt/id)
(sql-jdbc.execute/do-with-connection-with-options
:snowflake
(mt/db)
nil
(fn [^java.sql.Connection conn]
(is (sql-jdbc.sync/have-select-privilege? :snowflake conn "PUBLIC" "venues")))))))
(deftest describe-database-test
(mt/test-driver :snowflake
(testing "describe-database"
(let [expected {:tables
#{{:name "users", :schema "PUBLIC", :description nil}
{:name "venues", :schema "PUBLIC", :description nil}
{:name "checkins", :schema "PUBLIC", :description nil}
{:name "categories", :schema "PUBLIC", :description nil}
{:name "orders", :schema "PUBLIC", :description nil}
{:name "people", :schema "PUBLIC", :description nil}
{:name "products", :schema "PUBLIC", :description nil}
{:name "reviews", :schema "PUBLIC", :description nil}}}]
(testing "should work with normal details"
(is (= expected
(driver/describe-database :snowflake (mt/db)))))
(testing "should accept either `:db` or `:dbname` in the details, working around a bug with the original impl"
(is (= expected
(driver/describe-database :snowflake (update (mt/db) :details set/rename-keys {:db :dbname})))))
(testing "should throw an Exception if details have neither `:db` nor `:dbname`"
(is (thrown? Exception
(driver/describe-database :snowflake (update (mt/db) :details set/rename-keys {:db :xyz})))))
(testing "should use the NAME FROM DETAILS instead of the DB DISPLAY NAME to fetch metadata (#8864)"
(is (= expected
(driver/describe-database :snowflake (assoc (mt/db) :name "ABC")))))))))
(deftest describe-database-default-schema-test
(testing "describe-database should include Tables from all schemas even if the DB has a default schema (#38135)"
(mt/test-driver :snowflake
(let [details (assoc (mt/dbdef->connection-details :snowflake :db {:database-name "Default-Schema-Test"})
;; simulate a DB default schema or session schema by including it in the connection
;; details... see
;; https://metaboat.slack.com/archives/C04DN5VRQM6/p1706219065462619?thread_ts=1706156558.940489&cid=C04DN5VRQM6
:schema "PUBLIC"
:schema-filters-type "inclusion"
:schema-filters-patterns "Test-Schema")
db-name (:db details)
schema-name "Test-Schema"
table-name "Test-Table"
field-name "Test-ID"
spec (sql-jdbc.conn/connection-details->spec :snowflake details)]
;; create the snowflake DB
(sql-jdbc.execute/do-with-connection-with-options
:snowflake spec nil
(fn [^java.sql.Connection conn]
(doseq [stmt (letfn [(identifier [& args]
(str/join \. (map #(str \" % \") args)))]
[(format "DROP DATABASE IF EXISTS %s;" (identifier db-name))
(format "CREATE DATABASE %s;" (identifier db-name))
(format "CREATE SCHEMA %s;" (identifier db-name schema-name))
(format "CREATE TABLE %s (%s INTEGER AUTOINCREMENT);" (identifier db-name schema-name table-name) (identifier field-name))
(format "GRANT SELECT ON %s TO PUBLIC;" (identifier db-name schema-name table-name))])]
(jdbc/execute! {:connection conn} [stmt] {:transaction? false}))))
;; fetch metadata
(t2.with-temp/with-temp [Database database {:engine :snowflake, :details details}]
(is (=? {:tables #{{:name table-name, :schema schema-name, :description nil}}}
(driver/describe-database :snowflake database))))))))
(deftest describe-database-views-test
(mt/test-driver :snowflake
(testing "describe-database views"
(let [details (mt/dbdef->connection-details :snowflake :db {:database-name "views_test"})
db-name (:db details)
spec (sql-jdbc.conn/connection-details->spec :snowflake details)]
;; create the snowflake DB
(doseq [stmt [(format "DROP DATABASE IF EXISTS \"%s\";" db-name)
(format "CREATE DATABASE \"%s\";" db-name)]]
(jdbc/execute! spec [stmt] {:transaction? false}))
;; create the DB object
(t2.with-temp/with-temp [Database database {:engine :snowflake, :details details}]
(let [sync! #(sync/sync-database! database)]
;; create a view
(doseq [statement [(format "CREATE VIEW \"%s\".\"PUBLIC\".\"example_view\" AS SELECT 'hello world' AS \"name\";" db-name)
(format "GRANT SELECT ON \"%s\".\"PUBLIC\".\"example_view\" TO PUBLIC;" db-name)]]
(jdbc/execute! spec [statement]))
;; now sync the DB
(sync!)
;; now take a look at the Tables in the database, there should be an entry for the view
(is (= [{:name "example_view"}]
(map (partial into {})
(t2/select [Table :name] :db_id (u/the-id database)))))))))))
(deftest sync-dynamic-tables-test
(testing "Should be able to sync dynamic tables"
(mt/test-driver :snowflake
(mt/dataset (mt/dataset-definition "dynamic-table"
["metabase_users"
[{:field-name "name" :base-type :type/Text}]
[["mb_qnkhuat"]]])
(let [db-id (:id (mt/db))
details (:details (mt/db))
spec (sql-jdbc.conn/connection-details->spec driver/*driver* details)]
(jdbc/execute! spec [(format "CREATE OR REPLACE DYNAMIC TABLE \"%s\".\"PUBLIC\".\"metabase_fan\" target_lag = '1 minute' warehouse = 'COMPUTE_WH' AS
SELECT * FROM \"%s\".\"PUBLIC\".\"metabase_users\" WHERE \"%s\".\"PUBLIC\".\"metabase_users\".\"name\" LIKE 'MB_%%';"
(:db details) (:db details) (:db details))])
(sync/sync-database! (t2/select-one :model/Database db-id))
(testing "both base tables and dynamic tables should be synced"
(is (= #{"metabase_fan" "metabase_users"}
(t2/select-fn-set :name :model/Table :db_id db-id)))
(testing "the fields for dynamic tables are synced correctly"
(is (= #{{:name "name" :base_type :type/Text}
{:name "id" :base_type :type/Number}}
(set (t2/select [:model/Field :name :base_type]
:table_id (t2/select-one-pk :model/Table :name "metabase_fan" :db_id db-id))))))))))))
(deftest ^:parallel describe-table-test
(mt/test-driver :snowflake
(testing "make sure describe-table uses the NAME FROM DETAILS too"
(is (= {:name "categories"
:schema "PUBLIC"
:fields #{{:name "id"
:database-type "NUMBER"
:base-type :type/Number
:pk? true
:database-position 0
:database-is-auto-increment true
:database-required false
:json-unfolding false}
{:name "name"
:database-type "VARCHAR"
:base-type :type/Text
:database-position 1
:database-is-auto-increment false
:database-required true
:json-unfolding false}}}
(driver/describe-table :snowflake (assoc (mt/db) :name "ABC") (t2/select-one Table :id (mt/id :categories))))))))
(deftest ^:parallel describe-table-fks-test
(mt/test-driver :snowflake
(testing "make sure describe-table-fks uses the NAME FROM DETAILS too"
(is (= #{{:fk-column-name "category_id"
:dest-table {:name "categories", :schema "PUBLIC"}
:dest-column-name "id"}}
#_{:clj-kondo/ignore [:deprecated-var]}
(driver/describe-table-fks :snowflake (assoc (mt/db) :name "ABC") (t2/select-one Table :id (mt/id :venues))))))))
(defn- format-env-key ^String [env-key]
(let [[_ header body footer]
(re-find #"(-----BEGIN (?:\p{Alnum}+ )?PRIVATE KEY-----)(.*)(-----END (?:\p{Alnum}+ )?PRIVATE KEY-----)" env-key)]
(str header (str/replace body #"\s+|\\n" "\n") footer)))
(deftest can-connect-test
(let [pk-key (format-env-key (tx/db-test-env-var-or-throw :snowflake :pk-private-key))
pk-user (tx/db-test-env-var :snowflake :pk-user)
pk-db (tx/db-test-env-var :snowflake :pk-db "SNOWFLAKE_SAMPLE_DATA")]
(mt/test-driver :snowflake
(let [can-connect? (partial driver/can-connect? :snowflake)]
(is (can-connect? (:details (mt/db)))
"can-connect? should return true for normal Snowflake DB details")
(let [original-query jdbc/query]
;; make jdbc/query return a falsey value, but should still be able to connect
(with-redefs [jdbc/query (fn fake-jdbc-query
([db sql-params] (fake-jdbc-query db sql-params {}))
([db sql-params opts] (if (str/starts-with? sql-params "SHOW OBJECTS IN DATABASE")
nil
(original-query db sql-params (or opts {})))))]
(is (can-connect? (:details (mt/db))))))
(is (thrown?
net.snowflake.client.jdbc.SnowflakeSQLException
(can-connect? (assoc (:details (mt/db)) :db (mt/random-name))))
"can-connect? should throw for Snowflake databases that don't exist (#9511)")
(when (and pk-key pk-user)
(mt/with-temp-file [pk-path]
(testing "private key authentication"
(spit pk-path pk-key)
(doseq [to-merge [{:private-key-value pk-key} ;; uploaded string
{:private-key-value (.getBytes pk-key "UTF-8")} ;; uploaded byte array
{:private-key-path pk-path}]] ;; local file path
(let [details (-> (:details (mt/db))
(dissoc :password)
(merge {:db pk-db :user pk-user} to-merge))]
(is (can-connect? details)))))))))))
(deftest report-timezone-test
(mt/test-driver :snowflake
(testing "Make sure temporal parameters are set and returned correctly when report-timezone is set (#11036)"
(letfn [(run-query []
(mt/rows
(qp/process-query
{:database (mt/id)
:type :native
:native {:query (str "SELECT {{filter_date}}")
:template-tags {:filter_date {:name "filter_date"
:display_name "Just A Date"
:type "date"}}}
:parameters [{:type "date/single"
:target ["variable" ["template-tag" "filter_date"]]
:value "2014-08-02"}]})))]
(testing "baseline"
(is (= [["2014-08-02T00:00:00Z"]]
(run-query))))
(testing "with report-timezone"
(mt/with-temporary-setting-values [report-timezone "US/Pacific"]
(is (= [["2014-08-02T00:00:00-07:00"]]
(run-query)))))))
(testing "Make sure temporal values are returned correctly when report-timezone is set (#11036)"
(letfn [(run-query []
(mt/rows
(qp/process-query
{:database (mt/id)
:type :native
:native {:query (str "SELECT {{filter_date}}, \"last_login\" "
(format "FROM \"%stest-data\".\"PUBLIC\".\"users\" "
(test.data.snowflake/*database-prefix-fn*))
"WHERE date_trunc('day', CAST(\"last_login\" AS timestamp))"
" = date_trunc('day', CAST({{filter_date}} AS timestamp))")
:template-tags {:filter_date {:name "filter_date"
:display_name "Just A Date"
:type "date"}}}
:parameters [{:type "date/single"
:target ["variable" ["template-tag" "filter_date"]]
:value "2014-08-02"}]})))]
(testing "baseline (no report-timezone set)"
(is (= [["2014-08-02T00:00:00Z" "2014-08-02T12:30:00Z"]
["2014-08-02T00:00:00Z" "2014-08-02T09:30:00Z"]]
(run-query))))
(testing "with report timezone set"
(is (= [["2014-08-02T00:00:00-07:00" "2014-08-02T12:30:00-07:00"]
["2014-08-02T00:00:00-07:00" "2014-08-02T09:30:00-07:00"]]
(mt/with-temporary-setting-values [report-timezone "US/Pacific"]
(run-query)))))))))
(deftest week-start-test
(mt/test-driver :snowflake
(testing "The WEEK_START session setting is correctly incorporated"
(letfn [(run-dayofweek-query [date-str]
(-> (mt/rows
(qp/process-query {:database (mt/id)
:type :native
:native {:query (str "SELECT DAYOFWEEK({{filter_date}})")
:template-tags {:filter_date {:name "filter_date"
:display_name "Just A Date"
:type "date"}}}
:parameters [{:type "date/single"
:target ["variable" ["template-tag" "filter_date"]]
:value date-str}]}))
ffirst))]
(testing "under the default value of 7 (Sunday)"
(mt/with-temporary-setting-values [start-of-week :sunday]
(is (= 1 (run-dayofweek-query "2021-01-10")) "Sunday (first day of the week)")
(is (= 2 (run-dayofweek-query "2021-01-11")) "Monday (second day of the week)")))
(testing "when we control it via the Metabase setting value"
(mt/with-temporary-setting-values [start-of-week :monday]
(is (= 7 (run-dayofweek-query "2021-01-10")) "Sunday (last day of week now)")
(is (= 1 (run-dayofweek-query "2021-01-11")) "Monday (first day of week now)")))))))
(deftest first-day-of-week-test
(mt/test-driver :snowflake
(testing "Day-of-week should work correctly regardless of what the `start-of-week` Setting is set to (#20999)"
(mt/dataset test-data
(doseq [[start-of-week friday-int] [[:friday 1]
[:monday 5]
[:sunday 6]]]
(mt/with-temporary-setting-values [start-of-week start-of-week]
(let [query (mt/mbql-query people
{:breakout [!day-of-week.birth_date]
:aggregation [[:count]]
:filter [:= $birth_date "1986-12-12"]})]
(mt/with-native-query-testing-context query
(is (= [[friday-int 1]]
(mt/rows (qp/process-query query))))))))))))
(deftest ^:parallel normalize-test
(mt/test-driver :snowflake
(testing "details should be normalized coming out of the DB"
(t2.with-temp/with-temp [Database db {:name "Legacy Snowflake DB"
:engine :snowflake,
:details {:account "my-instance"
:regionid "us-west-1"}}]
(is (= {:account "my-instance.us-west-1"}
(:details db)))))))
(deftest ^:parallel set-role-statement-test
(testing "set-role-statement should return a USE ROLE command, with the role quoted if it contains special characters"
;; No special characters
(is (= "USE ROLE MY_ROLE;" (driver.sql/set-role-statement :snowflake "MY_ROLE")))
(is (= "USE ROLE ROLE123;" (driver.sql/set-role-statement :snowflake "ROLE123")))
(is (= "USE ROLE lowercase_role;" (driver.sql/set-role-statement :snowflake "lowercase_role")))
;; Special characters
(is (= "USE ROLE \"Role.123\";" (driver.sql/set-role-statement :snowflake "Role.123")))
(is (= "USE ROLE \"$role\";" (driver.sql/set-role-statement :snowflake "$role")))))
(deftest remark-test
(testing "Queries should have a remark formatted as JSON appended to them with additional metadata"
(mt/test-driver :snowflake
(let [expected-map {"pulseId" nil
"serverId" (public-settings/site-uuid)
"client" "Metabase"
"queryHash" "cb83d4f6eedc250edb0f2c16f8d9a21e5d42f322ccece1494c8ef3d634581fe2"
"queryType" "query"
"cardId" 1234
"dashboardId" 5678
"context" "ad-hoc"
"userId" 1000
"databaseId" (mt/id)}
result-query (driver/prettify-native-form :snowflake
(query->native
(assoc
(mt/mbql-query users {:limit 2000})
:parameters [{:type "id"
:target [:dimension [:field (mt/id :users :id) nil]]
:value ["1" "2" "3"]}]
:info {:executed-by 1000
:card-id 1234
:dashboard-id 5678
:context :ad-hoc
:query-hash (byte-array [-53 -125 -44 -10 -18 -36 37 14 -37 15 44 22 -8 -39 -94 30
93 66 -13 34 -52 -20 -31 73 76 -114 -13 -42 52 88 31 -30])})))
result-comment (second (re-find #"-- (\{.*\})" result-query))
result-map (json/read-str result-comment)]
(is (= expected-map result-map))))))
(mt/defdataset dst-change
[["dst_tz_test" [{:field-name "dtz"
:base-type :type/DateTimeWithTZ}]
[["2023-09-30 23:59:59 +1000"] ; September
["2023-10-01 00:00:00 +1000"] ; October before DST starts
["2023-10-01 05:00:00 +1100"] ; October after DST starts
["2023-11-01 05:00:00 +1100"]]]]) ; November
(deftest date-bucketing-test
(testing "#37065"
(mt/test-driver :snowflake
(mt/dataset dst-change
(let [metadata-provider (lib.metadata.jvm/application-database-metadata-provider (mt/id))
ds-tz-test (lib.metadata/table metadata-provider (mt/id :dst_tz_test))
dtz (lib.metadata/field metadata-provider (mt/id :dst_tz_test :dtz))
query (-> (lib/query metadata-provider ds-tz-test)
(lib/breakout dtz)
(lib/aggregate (lib/count)))]
(mt/with-native-query-testing-context query
(is (= [["2023-09-30T00:00:00+10:00" 1]
["2023-10-01T00:00:00+10:00" 2]
["2023-11-01T00:00:00+11:00" 1]]
(mt/with-temporary-setting-values [report-timezone "Australia/Sydney"]
(mt/rows (qp/process-query query)))))))))))