Skip to content
Snippets Groups Projects
Unverified Commit 1fd2c151 authored by Howon Lee's avatar Howon Lee Committed by GitHub
Browse files

22831 fix (group-by time bucketing on replacements for json aliases) (#24545)

* rewrite here...

* no bq

* thinking

* conjunction of alias thing...

* make sure we dont wanna see it

* nit

* multi-arity func

* fiddly bit on test

* add bit to see order by works right

* no default breakout true
parent 7518844e
No related branches found
No related tags found
No related merge requests found
...@@ -360,7 +360,7 @@ ...@@ -360,7 +360,7 @@
qualified (parent-method query) qualified (parent-method query)
unqualified (parent-method (update query unqualified (parent-method (update query
:breakout :breakout
sql.qp/rewrite-fields-to-force-using-column-aliases))] #(sql.qp/rewrite-fields-to-force-using-column-aliases % {:is-breakout true})))]
(if (some field/json-field? stored-fields) (if (some field/json-field? stored-fields)
(merge qualified (merge qualified
(select-keys unqualified #{:group-by})) (select-keys unqualified #{:group-by}))
......
...@@ -621,19 +621,24 @@ ...@@ -621,19 +621,24 @@
(defn rewrite-fields-to-force-using-column-aliases (defn rewrite-fields-to-force-using-column-aliases
"Rewrite `:field` clauses to force them to use the column alias regardless of where they appear." "Rewrite `:field` clauses to force them to use the column alias regardless of where they appear."
[form] ([form]
(mbql.u/replace form (rewrite-fields-to-force-using-column-aliases form {:is-breakout false}))
[:field id-or-name opts] ([form {is-breakout :is-breakout}]
[:field id-or-name (-> opts (mbql.u/replace form
(assoc ::add/source-alias (::add/desired-alias opts) [:field id-or-name opts]
::add/source-table ::add/none [:field id-or-name (cond-> opts
;; sort of a HACK but this key will tell the SQL QP not to apply casting here either. true
::nest-query/outer-select true (assoc ::add/source-alias (::add/desired-alias opts)
;; used to indicate that this is a forced alias ::add/source-table ::add/none
::forced-alias true) ;; sort of a HACK but this key will tell the SQL QP not to apply casting here either.
;; don't want to do temporal bucketing or binning inside the order by or breakout either. ::nest-query/outer-select true
;; That happens inside the `SELECT` ;; used to indicate that this is a forced alias
(dissoc :temporal-unit :binning))])) ::forced-alias true)
;; don't want to do temporal bucketing or binning inside the order by only.
;; That happens inside the `SELECT`
;; (#22831) however, we do want it in breakout
(not is-breakout)
(dissoc :temporal-unit :binning))])))
;;; +----------------------------------------------------------------------------------------------------------------+ ;;; +----------------------------------------------------------------------------------------------------------------+
;;; | Clause Handlers | ;;; | Clause Handlers |
......
...@@ -348,17 +348,33 @@ ...@@ -348,17 +348,33 @@
"injection' OR 1=1--' AND released = 1" "injection' OR 1=1--' AND released = 1"
(keyword "injection' OR 1=1--' AND released = 1")], (keyword "injection' OR 1=1--' AND released = 1")],
:name "json_alias_test"}]] :name "json_alias_test"}]]
(let [compile-res (qp/compile (let [field-bucketed [:field (u/the-id field)
{:temporal-unit :month,
:metabase.query-processor.util.add-alias-info/source-table (u/the-id table),
:metabase.query-processor.util.add-alias-info/source-alias "dontwannaseethis",
:metabase.query-processor.util.add-alias-info/desired-alias "dontwannaseethis",
:metabase.query-processor.util.add-alias-info/position 1}]
field-ordinary [:field (u/the-id field) nil]
compile-res (qp/compile
{:database (u/the-id database) {:database (u/the-id database)
:type :query :type :query
:query {:source-table (u/the-id table) :query {:source-table (u/the-id table)
:aggregation [[:count]] :aggregation [[:count]]
:breakout [[:field (u/the-id field) nil]]}})] :breakout [field-bucketed]
(is (= (str "SELECT (\"json_alias_test\".\"bob\"#>> ?::text[])::VARCHAR " :order-by [[:asc field-bucketed]]}})
only-order (qp/compile
{:database (u/the-id database)
:type :query
:query {:source-table (u/the-id table)
:order-by [[:asc field-ordinary]]}})]
(is (= (str "SELECT date_trunc('month', CAST((\"json_alias_test\".\"bob\"#>> ?::text[])::VARCHAR AS timestamp)) "
"AS \"json_alias_test\", count(*) AS \"count\" FROM \"json_alias_test\" " "AS \"json_alias_test\", count(*) AS \"count\" FROM \"json_alias_test\" "
"GROUP BY \"json_alias_test\" ORDER BY \"json_alias_test\" ASC") "GROUP BY \"json_alias_test\" ORDER BY \"json_alias_test\" ASC")
(:query compile-res))) (:query compile-res)))
(is (= '("{injection' OR 1=1--' AND released = 1,injection' OR 1=1--' AND released = 1}") (:params compile-res))))))))) (is (= '("{injection' OR 1=1--' AND released = 1,injection' OR 1=1--' AND released = 1}") (:params compile-res)))
(is (= (str "SELECT (\"json_alias_test\".\"bob\"#>> ?::text[])::VARCHAR AS \"json_alias_test\" FROM \"json_alias_test\" "
"ORDER BY \"json_alias_test\" ASC LIMIT 1048575")
(:query only-order)))))))))
(deftest describe-nested-field-columns-test (deftest describe-nested-field-columns-test
(mt/test-driver :postgres (mt/test-driver :postgres
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment