The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[MBQL Lib] Define ::external-query malli schema for External MBQL 5 (#72797)
technomancyd454fbda70f8
diff --git a/src/metabase/lib/schema.cljc b/src/metabase/lib/schema.cljcindex 72edf621e15..f90389c6604 100644--- a/src/metabase/lib/schema.cljc+++ b/src/metabase/lib/schema.cljc@@ -8,6 +8,7 @@future we can deprecate that namespace and eventually do away with it entirely."(:refer-clojure :exclude [ref every? some select-keys empty? get-in])(:require+ [malli.util :as mut][medley.core :as m][metabase.lib.options :as lib.options][metabase.lib.schema.actions :as actions]@@ -546,6 +547,28 @@:source-table ":source-table is not allowed in the top level of a query, only in MBQL stages":type ":type is not allowed in MBQL 5, use :lib/type instead."})])+(mr/def ::external-query+ "Schema for \"External MBQL\" 5 query."+ [:schema {:registry {::id/database :string+ ::id/card :string+ ::id/segment :string+ ::id/measure :string+ ::id/snippet :string+ ::id/schema [:or nil? :string]+ ::id/table [:cat ::id/database ::id/schema :string]+ ::id/field [:cat ::id/database ::id/schema :string [:+ :string]]+ ;; this spec has a :multi clause that assumes field IDs+ ;; must be integers. the 3 in the update call refers to+ ;; the :multi; if that gets moved, this'll need to change+ :mbql.clause/field (update (mr/schema :mbql.clause/field) 3+ conj [:dispatch-type/sequential+ ::ref/field.id])+ ;; similarly we need to get rid of the :lib/uuid key of+ ;; a map that's nested in position 2 of an :and schema:+ ::common/options (update (mr/schema ::common/options) 2+ mut/dissoc :lib/uuid)}}+ [:ref ::query]])+(defn native-only-query?"Whether MBQL 5 `query` only has a single native stage (and is thus pure-native). This is the equivalent of the old`:type :native` queries in MBQL <= 4."diff --git a/test/metabase/lib/schema_test.cljc b/test/metabase/lib/schema_test.cljcindex e4ecbfb5a4a..b914a4f861a 100644--- a/test/metabase/lib/schema_test.cljc+++ b/test/metabase/lib/schema_test.cljc@@ -362,3 +362,59 @@(is (= {:stages [["Initial MBQL stage must have either :source-table or :source-card (but not both)"]]}(me/humanize (mr/explain ::lib.schema/query{:lib/type :mbql/query, :database 2378, :stages [{:lib/type :mbql.stage/mbql}]})))))++;; these two queries are taken from the representations repository+(def basic-external-query+ {:lib/type :mbql/query+ :database "Sample Database"+ :stages [{:lib/type :mbql.stage/mbql+ :source-table ["Sample Database" "PUBLIC" "ORDERS"]+ :aggregation [[:sum {} [:field {:base-type :type/Float}+ ["Sample Database" "PUBLIC" "ORDERS" "TOTAL"]]]]}]})++(def native-external-query+ {:lib/type :mbql/query+ :database "Sample Database"+ :stages+ [{:lib/type :mbql.stage/native+ :native "SELECT *+ FROM ORDERS+ WHERE TOTAL > {{min_total}}+ AND CREATED_AT > {{after_date}}+ AND USER_ID = {{user_id}}+ AND {{is_active}}"+ :template-tags {"min_total" {:type :number+ :name "min_total"+ :id "aa000001-0000-0000-0000-000000000001"+ :display-name "Minimum Total"+ :default 50+ :required true+ :sectionid "number"}+ "after_date" {:type :date+ :name "after_date"+ :id "aa000002-0000-0000-0000-000000000002"+ :display-name "After Date"+ :default "2024-01-01"+ :sectionid "date"}+ "user_id" {:type :text+ :name "user_id"+ :id "aa000003-0000-0000-0000-000000000003"+ :display-name "User ID"+ :default "1"}+ "is_active" {:type :boolean+ :name "is_active"+ :id "aa000004-0000-0000-0000-000000000004"+ :display-name "Is Active"+ :default true+ :sectionid "boolean"}}}]})++(deftest ^:parallel external-test+ ;; this one is not valid according to the internal schema because it+ ;; uses a string tuple for the field instead of an integer.+ (is (mr/explain ::lib.schema/query basic-external-query))+ ;; if these fail, it's likely because the :mbql.clause/field or ::common/options+ ;; schemas changed; see the comment in ::lib.schema/external-query for details.+ (is (not (me/humanize (mr/explain ::lib.schema/external-query+ basic-external-query))))+ (is (not (me/humanize (mr/explain ::lib.schema/external-query+ native-external-query)))))