Skip to content

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

Handle mixed catalog table errors in Trino sync (#63356)

Timothy S. Dean80d6389e6e23
diff --git a/modules/drivers/starburst/src/metabase/driver/starburst.clj b/modules/drivers/starburst/src/metabase/driver/starburst.clj
index 2f025eef164..f7fc294b90e 100644
--- a/modules/drivers/starburst/src/metabase/driver/starburst.clj
+++ b/modules/drivers/starburst/src/metabase/driver/starburst.clj
@@ -31,6 +31,7 @@
PreparedStatement
ResultSet
ResultSetMetaData
+ SQLException
SQLType
Time
Types)
@@ -431,16 +432,30 @@
base-type))
(defmethod sql-jdbc.sync.interface/have-select-privilege? :starburst
- [_driver ^Connection conn table-schema table-name]
+ [driver ^Connection conn table-schema table-name]
(try
- (let [sql (str "SHOW TABLES FROM \"" table-schema "\" LIKE '" table-name "'")]
- ;; if the query completes without throwing an Exception, we can SELECT from this table
+ ;; Both Hive and Iceberg plugins for Trino expose one another's tables
+ ;; at the metadata level, even though they are not queryable through that catalog.
+ ;; So rather than using SHOW TABLES, we will DESCRIBE the table to check for
+ ;; queryability. If the table is not queryable for this reason, we will return
+ ;; false. It's a slight stretch of the concept of "permissions," but it is true
+ ;; that we cannot query these tables...
+ (let [catalog (some-> conn .getCatalog)
+ sql (describe-table-sql driver catalog table-schema table-name)]
(with-open [stmt (.prepareStatement conn sql)
rs (.executeQuery stmt)]
(.next rs)))
- (catch Throwable e
- (log/fatal e "ERROR WITH QUERY ")
- false)))
+ (catch SQLException e
+ ;; The actual exception thrown is TrinoException with error code UNSUPPORTED_TABLE_TYPE (133001),
+ ;; but we can't check the type directly since the relevant io.trino.spi.* classes are not
+ ;; included in trino-jdbc. We check the vendor-specific error code instead.
+ ;; See HiveMetadata.java and UnknownTableTypeException.java in trinodb/trino
+ (if (= 133001 (.getErrorCode e))
+ (do
+ (log/debugf e "Table %s.%s is not accessible through this catalog (mixed catalog table type)"
+ table-schema table-name)
+ false)
+ (throw e)))))
(defn- describe-schema
"Gets a set of maps for all tables in the given `catalog` and `schema`."
diff --git a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj b/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
index df7eb88f605..9b5e606a405 100644
--- a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
+++ b/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
@@ -8,6 +8,7 @@
[metabase.driver.common.table-rows-sample :as table-rows-sample]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
+ [metabase.driver.sql-jdbc.sync.interface :as sql-jdbc.sync.interface]
[metabase.driver.sql.query-processor :as sql.qp]
[metabase.driver.starburst :as starburst]
[metabase.query-processor :as qp]
@@ -22,7 +23,7 @@
[toucan2.core :as t2]
[toucan2.tools.with-temp :as t2.with-temp])
(:import
- (java.sql Connection)))
+ (java.sql Connection PreparedStatement ResultSet SQLException)))
(set! *warn-on-reflection* true)
@@ -33,6 +34,51 @@
[_]
(alter-var-root #'timezones-test/broken-drivers conj :starburst))
+(deftest have-select-privilege-mixed-tables-test
+ (testing "have-select-privilege? correctly handles mixed Hive/Iceberg tables (Issue #63127)"
+ (testing "Returns true when DESCRIBE succeeds (compatible table type)"
+ (let [mock-conn (reify Connection
+ (getCatalog [_] "hive")
+ (prepareStatement [_ sql]
+ (is (= "DESCRIBE \"hive\".\"sales_data\".\"hive_table\"" sql))
+ (reify PreparedStatement
+ (executeQuery [_]
+ (reify ResultSet
+ (next [_] true)
+ (close [_] nil)))
+ (close [_] nil))))]
+ (is (true? (sql-jdbc.sync.interface/have-select-privilege?
+ :starburst mock-conn "sales_data" "hive_table")))))
+
+ (testing "Returns false when DESCRIBE fails with UNSUPPORTED_TABLE_TYPE error (incompatible table type like Iceberg in Hive catalog)"
+ (let [mock-conn (reify Connection
+ (getCatalog [_] "hive")
+ (prepareStatement [_ sql]
+ (is (= "DESCRIBE \"hive\".\"sales_data\".\"iceberg_table\"" sql))
+ (reify PreparedStatement
+ (executeQuery [_]
+ ;; This simulates the actual Trino error when Hive catalog tries to describe an Iceberg table
+ ;; The error code 133001 corresponds to UNSUPPORTED_TABLE_TYPE in Trino's StandardErrorCode
+ (throw (SQLException. "Cannot query Iceberg table 'sales_data.iceberg_table'" nil 133001)))
+ (close [_] nil))))]
+ (is (false? (sql-jdbc.sync.interface/have-select-privilege?
+ :starburst mock-conn "sales_data" "iceberg_table")))))
+
+ (testing "Rethrows SQLException for non-mixed-catalog errors"
+ (let [mock-conn (reify Connection
+ (getCatalog [_] "hive")
+ (prepareStatement [_ sql]
+ (is (= "DESCRIBE \"hive\".\"sales_data\".\"restricted_table\"" sql))
+ (reify PreparedStatement
+ (executeQuery [_]
+ ;; This is a regular permission error, not a mixed catalog issue (different error code)
+ (throw (SQLException. "Access Denied: Cannot access table restricted_table" nil 42000)))
+ (close [_] nil))))]
+ ;; This should throw the exception rather than returning false
+ (is (thrown? SQLException
+ (sql-jdbc.sync.interface/have-select-privilege?
+ :starburst mock-conn "sales_data" "restricted_table")))))))
+
(deftest describe-database-test
(mt/test-driver :starburst
(is (= {:tables #{{:name "categories" :schema "default"}