Skip to content
Snippets Groups Projects
Unverified Commit 2a68dfab authored by Chris Truter's avatar Chris Truter Committed by GitHub
Browse files

Fix parsing integers after floats in CSV upload (#40552)

parent b206d85b
No related branches found
No related tags found
No related merge requests found
......@@ -175,7 +175,9 @@
number-regex
"\\s*" upload-parsing/currency-regex "?")))
(defn- int-regex [number-separators]
(defn- int-regex
"Matches numbers which do not have a decimal separator."
[number-separators]
(with-parens
(with-currency
(case number-separators
......@@ -184,7 +186,9 @@
", " #"\d[\d \u00A0]*"
".’" #"\d[\d’]*"))))
(defn- float-or-int-regex [number-separators]
(defn- float-or-int-regex
"Matches integral numbers, even if they have a decimal separator - e.g. 2 or 2.0"
[number-separators]
(with-parens
(with-currency
(case number-separators
......@@ -193,14 +197,16 @@
", " #"\d[\d \u00A0]*(\,[0.]+)?"
".’" #"\d[\d’]*(\.[0.]+)?"))))
(defn- float-regex [number-separators]
(defn- float-regex
"Matches numbers, regardless of whether they have a decimal separator - e.g. 2, 2.0, or 2.2"
[number-separators]
(with-parens
(with-currency
(case number-separators
("." ".,") #"\d[\d,]*\.\d+"
",." #"\d[\d.]*\,[\d]+"
", " #"\d[\d \u00A0]*\,[\d.]+"
".’" #"\d[\d’]*\.[\d.]+"))))
("." ".,") #"\d[\d,]*(\.\d+)?"
",." #"\d[\d.]*(\,[\d]+)?"
", " #"\d[\d \u00A0]*(\,[\d.]+)?"
".’" #"\d[\d’]*(\.[\d.]+)?"))))
(defmacro does-not-throw?
"Returns true if the given body does not throw an exception."
......@@ -246,17 +252,17 @@
(mu/defn ^:private settings->type->check :- type->check-schema
[{:keys [number-separators] :as _settings}]
(let [int? (regex-matcher (int-regex number-separators))
(let [int-string? (regex-matcher (int-regex number-separators))
float-or-int? (regex-matcher (float-or-int-regex number-separators))
float? (regex-matcher (float-regex number-separators))]
float-string? (regex-matcher (float-regex number-separators))]
{::*boolean-int* boolean-int-string?
::boolean boolean-string?
::offset-datetime offset-datetime-string?
::date date-string?
::datetime datetime-string?
::int int?
::int int-string?
::*float-or-int* float-or-int?
::float float?
::float float-string?
::varchar-255 varchar-255?
::text (constantly true)}))
......
......@@ -1926,6 +1926,25 @@
[2 1.0 1.0]]
(rows-for-table table)))))))))
(deftest create-from-csv-int-and-non-integral-float-test
(testing "Creation should handle a mix of int and float values in any order"
(mt/test-drivers (mt/normal-drivers-with-feature :uploads)
(with-mysql-local-infile-on-and-off
(with-upload-table!
[table (let [table-name (mt/random-name)]
(@#'upload/create-from-csv!
driver/*driver*
(mt/id)
table-name
(csv-file-with ["float-1,float-2"
"1, 1.1"
"1.1, 1"]))
(sync-upload-test-table! :database (mt/db) :table-name table-name))]
(testing "Check the data was uploaded into the table correctly"
(is (= [[1 1.0 1.1]
[2 1.1 1.0]]
(rows-for-table table)))))))))
(deftest append-from-csv-int-and-float-test
(mt/test-drivers (mt/normal-drivers-with-feature :uploads)
(testing "Append should handle a mix of int and float-or-int values being appended to an int column"
......
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