Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[Athena] Correctly recognize `:base_type` when db type is uppercase (#68369)

Braden Shepherdsonfb225582a874
diff --git a/modules/drivers/athena/src/metabase/driver/athena.clj b/modules/drivers/athena/src/metabase/driver/athena.clj
index a60be290197..8bcf0e4b600 100644
--- a/modules/drivers/athena/src/metabase/driver/athena.clj
+++ b/modules/drivers/athena/src/metabase/driver/athena.clj
@@ -96,36 +96,42 @@
;;; ------------------------------------------------- sql-jdbc.sync --------------------------------------------------
+(def ^:private db-type->base-type
+ {:array :type/Array
+ :bigint :type/BigInteger
+ :binary :type/*
+ :varbinary :type/*
+ :boolean :type/Boolean
+ :char :type/Text
+ :date :type/Date
+ :decimal :type/Decimal
+ :double :type/Float
+ :float :type/Float
+ :integer :type/Integer
+ :int :type/Integer
+ :uuid :type/UUID
+ :map :type/*
+ :smallint :type/Integer
+ :string :type/Text
+ :struct :type/Dictionary
+ ;; Athena sort of has a time type, sort of does not. You can specify it in literals but I don't think you can store
+ ;; it.
+ :time :type/Time
+ :timestamp :type/DateTime
+ ;; Same for timestamp with time zone... the type sort of exists. You can't store it AFAIK but you can create one
+ ;; from a literal or by converting a `timestamp` column, e.g. with the `with_timezone` function.
+ (keyword "timestamp with time zone") :type/DateTimeWithZoneID
+ :tinyint :type/Integer
+ :varchar :type/Text})
+
;; Map of column types -> Field base types
;; https://s3.amazonaws.com/athena-downloads/drivers/JDBC/SimbaAthenaJDBC_2.0.5/docs/Simba+Athena+JDBC+Driver+Install+and+Configuration+Guide.pdf
(defmethod sql-jdbc.sync/database-type->base-type :athena
[_driver database-type]
- ({:array :type/Array
- :bigint :type/BigInteger
- :binary :type/*
- :varbinary :type/*
- :boolean :type/Boolean
- :char :type/Text
- :date :type/Date
- :decimal :type/Decimal
- :double :type/Float
- :float :type/Float
- :integer :type/Integer
- :int :type/Integer
- :uuid :type/UUID
- :map :type/*
- :smallint :type/Integer
- :string :type/Text
- :struct :type/Dictionary
- ;; Athena sort of has a time type, sort of does not. You can specify it in literals but I don't think you can store
- ;; it.
- :time :type/Time
- :timestamp :type/DateTime
- ;; Same for timestamp with time zone... the type sort of exists. You can't store it AFAIK but you can create one
- ;; from a literal or by converting a `timestamp` column, e.g. with the `with_timezone` function.
- (keyword "timestamp with time zone") :type/DateTimeWithZoneID
- :tinyint :type/Integer
- :varchar :type/Text} database-type))
+ ;; Most databases have a canonical spelling for the types returned by JDBC, and ignore the upper/lower case you type
+ ;; in eg. `CREATE TABLE` statements. However, Athena has an admin interface where the case typed by the user is what
+ ;; gets returned by JDBC calls. Therefore, lower-case the incoming `database-type` and then look up its `base-type`.
+ (-> database-type name u/lower-case-en keyword db-type->base-type))
;;; ------------------------------------------------ sql-jdbc execute ------------------------------------------------
diff --git a/modules/drivers/athena/test/metabase/driver/athena_test.clj b/modules/drivers/athena/test/metabase/driver/athena_test.clj
index 3c2b67bae9f..ee1ef4c59da 100644
--- a/modules/drivers/athena/test/metabase/driver/athena_test.clj
+++ b/modules/drivers/athena/test/metabase/driver/athena_test.clj
@@ -31,6 +31,10 @@
[{:column_name "id", :type_name "string"}
{:column_name "ts", :type_name "string"}])
+(def ^:private upper-case-schema-columns
+ [{:column_name "id", :type_name "string"}
+ {:column_name "ts", :type_name "STRING"}])
+
(deftest sync-test
(testing "sync with nested fields"
(with-redefs [athena/run-query (constantly nested-schema)]
@@ -47,7 +51,11 @@
(testing "sync without nested fields"
(is (= #{{:name "id", :base-type :type/Text, :database-type "string", :database-position 0}
{:name "ts", :base-type :type/Text, :database-type "string", :database-position 1}}
- (#'athena/describe-table-fields-without-nested-fields :athena "test" "test" flat-schema-columns)))))
+ (#'athena/describe-table-fields-without-nested-fields :athena "test" "test" flat-schema-columns))))
+ (testing "sync with upper-case letters in types (#68325)"
+ (is (= #{{:name "id", :base-type :type/Text, :database-type "string", :database-position 0}
+ {:name "ts", :base-type :type/Text, :database-type "STRING", :database-position 1}}
+ (#'athena/describe-table-fields-without-nested-fields :athena "test" "test" upper-case-schema-columns)))))
(deftest ^:parallel describe-table-fields-with-nested-fields-test
(driver/with-driver :athena