diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj index 82ae1f3b7f7397d075c3eeba262fe5ec07e5cc3c..9e95437a8570ddb62a103bedcf210d9c4e972cb6 100644 --- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj +++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj @@ -20,11 +20,11 @@ [metabase.util.schema :as su] [schema.core :as s] [toucan.db :as db]) - (:import [com.google.cloud.bigquery BigQuery BigQuery$DatasetListOption BigQuery$JobOption BigQuery$TableListOption + (:import clojure.lang.PersistentList + [com.google.cloud.bigquery BigQuery BigQuery$DatasetListOption BigQuery$JobOption BigQuery$TableListOption BigQuery$TableOption BigQueryException BigQueryOptions Dataset DatasetId Field Field$Mode FieldValue FieldValueList QueryJobConfiguration Schema Table TableId - TableResult] - java.util.Collections)) + TableResult])) (driver/register! :bigquery-cloud-sdk, :parent :sql) @@ -33,17 +33,20 @@ ;;; | Client | ;;; +----------------------------------------------------------------------------------------------------------------+ -(def ^:private bigquery-scope - "The scope to use for executing BigQuery requests; see: +(def ^:private ^PersistentList bigquery-scopes + "The scopes to use for executing BigQuery requests; see: `https://cloud.google.com/bigquery/docs/samples/bigquery-auth-drive-scope`. - Unclear if this can be sourced from the `com.google.cloud.bigquery` package directly." - "https://www.googleapis.com/auth/bigquery") + Unclear if this can be sourced from the `com.google.cloud.bigquery` package directly. We use the standard bigquery + scope, as well as the drive scope (allowing for configured Drive external tables to be queried, as per + `https://cloud.google.com/bigquery/external-data-drive`)." + '("https://www.googleapis.com/auth/bigquery" + "https://www.googleapis.com/auth/drive")) (defn- ^BigQuery database->client [database] (let [creds (bigquery.common/database-details->service-account-credential (:details database)) bq-bldr (doto (BigQueryOptions/newBuilder) - (.setCredentials (.createScoped creds (Collections/singletonList bigquery-scope))))] + (.setCredentials (.createScoped creds bigquery-scopes)))] (.. bq-bldr build getService))) ;;; +----------------------------------------------------------------------------------------------------------------+ diff --git a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj index 2665ed75d22ac11dd51401f9fd146aaeaddbc606..1b3dbd8ad81a9757eca08f1eb0763566e7fbb362 100644 --- a/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj +++ b/modules/drivers/bigquery-cloud-sdk/test/metabase/driver/bigquery_cloud_sdk_test.clj @@ -477,3 +477,22 @@ (doseq [table (map Table [(u/the-id table1) (u/the-id table2)])] ;; and the existing tables should have been updated with that schema (is (= "my-dataset" (:schema table))))))))))) + +(deftest query-drive-external-tables + (mt/test-driver :bigquery-cloud-sdk + (testing "Google Sheets external tables can be queried via BigQuery (#4179)" + ;; link to the underlying Google sheet, which everyone in the Google domain should have edit permission on + ;; https://docs.google.com/spreadsheets/d/1ETIY759w8Xd8ZXcL-IullMxWjKdO-sKSIUOfG1KYh8U/edit?usp=sharing + ;; the service account to which our CI credentials are associated: + ;; metabase-ci@metabase-bigquery-ci.iam.gserviceaccount.com + ;; was given View permission to this sheet (via the Drive UI), and was ALSO given BigQuery Data Viewer + ;; permission in the BigQuery UI under the project (`metabase-bigquery-ci`), dataset (`google_drive_dataset`), + ;; and table (`metabase_ci_bigquery_sheet`) + (is (= [[1 "foo" "bar"] + [2 "alice" "bob"] + [3 "x" "y"]] + (-> {:query + "SELECT * FROM `metabase-bigquery-ci.google_drive_dataset.metabase_ci_bigquery_sheet` ORDER BY `id`"} + mt/native-query + qp/process-query + mt/rows))))))