Skip to content
Snippets Groups Projects
Unverified Commit b61cf6d0 authored by Noah Moss's avatar Noah Moss Committed by GitHub
Browse files

Make sure lazy seqs get realized during CSV exports (#26327)

* realize lazy seqs during exports

* add test & helper

* update based on tamas's comment

* use mapv

* remove unused import

* try to fix test

* switch to using just a LocalDate for the second test since the ZonedDateTime test was failing for certain drivers and not others, and that isnt relevant to this test
parent 8da6d99a
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,8 @@
[metabase.query-processor.store :as qp.store]
[metabase.query-processor.timezone :as qp.timezone]
[metabase.util.date-2 :as u.date])
(:import [java.time LocalDate LocalDateTime LocalTime OffsetDateTime OffsetTime ZonedDateTime]))
(:import clojure.lang.ISeq
[java.time LocalDate LocalDateTime LocalTime OffsetDateTime OffsetTime ZonedDateTime]))
(defn in-result-time-zone
"Set the time zone of a temporal value `t` to result timezone without changing the actual moment in time. e.g.
......@@ -29,6 +30,10 @@
Object
(format-value [this] this)
ISeq
(format-value [this]
(mapv format-value this))
LocalDate
(format-value [t]
(u.date/format t))
......
......@@ -2,8 +2,10 @@
(:require [cheshire.core :as json]
[clojure.data.csv :as csv]
[clojure.test :refer :all]
[metabase.query-processor.streaming.interface :as qp.si]
[metabase.test :as mt]
[metabase.test.data.dataset-definitions :as defs]))
[metabase.test.data.dataset-definitions :as defs])
(:import [java.io BufferedOutputStream ByteArrayOutputStream]))
(defn- parse-and-sort-csv [response]
(assert (some? response))
......@@ -55,3 +57,27 @@
["4" "Simcha Yan" "2014-01-01T08:30:00"]
["5" "Quentin Sören" "2014-10-03T17:30:00"]]
(parse-and-sort-csv result)))))
(defn- csv-export
"Given a seq of result rows, write it as a CSV, then read the CSV and return the resulting data."
[rows]
(with-open [bos (ByteArrayOutputStream.)
os (BufferedOutputStream. bos)]
(let [results-writer (qp.si/streaming-results-writer :csv os)]
(qp.si/begin! results-writer {:data {:ordered-cols []}} {})
(doall (map-indexed
(fn [i row] (qp.si/write-row! results-writer row i [] {}))
rows))
(qp.si/finish! results-writer {:row_count (count rows)}))
(let [bytea (.toByteArray bos)]
(rest (csv/read-csv (String. bytea))))))
(deftest lazy-seq-realized-test
(testing "Lazy seqs within rows are automatically realized during exports (#26261)"
(let [row (first (csv-export [[(lazy-seq [1 2 3])]]))]
(is (= ["[1 2 3]"] row))))
(testing "LocalDate in a lazy seq (checking that elements in a lazy seq are formatted correctly as strings)"
(mt/with-everything-store
(let [row (first (csv-export [[(lazy-seq [#t "2021-03-30T"])]]))]
(is (= ["[\"2021-03-30\"]"] row))))))
......@@ -131,7 +131,7 @@
;; see also `metabase.query-processor.streaming.xlsx-test/report-timezone-test`
;; TODO this test doesn't seem to run?
(deftest report-timezone-test
(testing "Export downloads should format stuff with the report timezone rather than UTC (#13677)\n"
(testing "Export downloads should format stuff with the report timezone rather than UTC (#13677)"
(mt/test-driver :postgres
(let [query (mt/dataset attempted-murders
(mt/mbql-query attempts
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment