Skip to content
Snippets Groups Projects
Unverified Commit 69b5b407 authored by Case Nelson's avatar Case Nelson Committed by GitHub
Browse files

[MLv2] Fix inner native query key (#30162)

* [MLv2] Fix inner native query key

* Unskip tests

* Move rename-keys because source-query uses :native and native query uses :query to point at sql, so we have to see how the stage is used

* Don't touch parameters in conversion
parent 4dfd1809
Branches
Tags
No related merge requests found
......@@ -243,14 +243,17 @@
;; In pMBQL, the :lib/stage-metadata is attached to the same stage it applies to.
;; So when chaining pMBQL stages back into legacy form, if stage n has :lib/stage-metadata, stage n+1 needs
;; :source-metadata attached.
(first (reduce (fn [[inner stage-metadata] stage]
[(cond-> (->legacy-MBQL stage)
inner (assoc :source-query inner)
stage-metadata (assoc :source-metadata (mapv ->legacy-MBQL (:columns stage-metadata))))
;; Get the :lib/stage-metadata off the original pMBQL stage, not the converted one.
(:lib/stage-metadata stage)])
nil
stages)))
(let [inner-query (first (reduce (fn [[inner stage-metadata] stage]
[(cond-> (->legacy-MBQL stage)
inner (assoc :source-query inner)
stage-metadata (assoc :source-metadata (mapv ->legacy-MBQL (:columns stage-metadata))))
;; Get the :lib/stage-metadata off the original pMBQL stage, not the converted one.
(:lib/stage-metadata stage)])
nil
stages))]
(cond-> inner-query
;; If this is a native query, inner query will be used like: `{:type :native :native #_inner-query {:query ...}}`
(:native inner-query) (set/rename-keys {:native :query}))))
(defmethod ->legacy-MBQL :dispatch-type/map [m]
(into {}
......@@ -316,12 +319,13 @@
(defmethod ->legacy-MBQL :mbql/query [query]
(let [base (disqualify query)
parameters (:parameters base)
inner-query (chain-stages base)
query-type (if (-> query :stages last :lib/type (= :mbql.stage/native))
:native
:query)]
(merge (-> base
(dissoc :stages)
(dissoc :stages :parameters)
(update-vals ->legacy-MBQL))
{:type query-type
query-type inner-query})))
(cond-> {:type query-type query-type inner-query}
(seq parameters) (assoc :parameters parameters)))))
......@@ -238,7 +238,27 @@
:condition [:= [:field 2 nil] [:field 2 nil]]
:fields [[:field 1 {:join-alias "Cat"}]]}]
:limit 1
:source-table 4}}))
:source-table 4}}
{:database 310,
:query {:middleware {:disable-remaps? true},
:source-card-id 1301,
:source-query {:native "SELECT id, name, category_id, latitude, longitude, price FROM venues ORDER BY id ASC LIMIT 2"}},
:type :query}
{:type :native,
:native
{:query
"SELECT \"PUBLIC\".\"VENUES\".\"ID\" AS \"ID\", \"PUBLIC\".\"VENUES\".\"NAME\" AS \"NAME\", \"PUBLIC\".\"VENUES\".\"CATEGORY_ID\" AS \"CATEGORY_ID\", \"PUBLIC\".\"VENUES\".\"LATITUDE\" AS \"LATITUDE\", \"PUBLIC\".\"VENUES\".\"LONGITUDE\" AS \"LONGITUDE\", \"PUBLIC\".\"VENUES\".\"PRICE\" AS \"PRICE\" FROM \"PUBLIC\".\"VENUES\" LIMIT 1048575",
:params nil}
:database 2360}
{:database 1,
:native {:query "select 111 as my_number, 'foo' as my_string"},
:parameters [{:target [:dimension [:field 16 {:source-field 5}]],
:type :category,
:value [:param-value]}],
:type :native}))
(deftest ^:parallel round-trip-preserve-metadata-test
(testing "Round-tripping should not affect embedded metadata"
......
......@@ -45,9 +45,6 @@
(mbql.u/match-one joins
{:fields (join-fields :guard (partial not= :none))}
"#29904"))
;; #29908: native queries do not round trip correctly
(when (:native legacy-query)
"#29908")
;; #29909: these clauses are not implemented yet.
(mbql.u/match-one legacy-query
#{:get-year :get-quarter :get-month :get-day :get-day-of-week :get-hour :get-minute :get-second}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment