Skip to content
Snippets Groups Projects
Unverified Commit 06996aff authored by Robert Roland's avatar Robert Roland Committed by GitHub
Browse files

Handling of queries with custom exprs (#13038)

This will better handle queries with expressions like [Discount] *
[Quantity]
parent 65ebcd6f
No related branches found
No related tags found
No related merge requests found
(ns metabase.api.query-description
"Functions for generating human friendly query descriptions"
(:require [clojure.string :as str]
[metabase.mbql.util :as mbql.u]
[metabase.mbql
[predicates :as mbql.preds]
[util :as mbql.u]]
[metabase.models
[field :refer [Field]]
[metric :refer [Metric]]
......@@ -12,11 +14,11 @@
[metadata query]
{:table (:display_name metadata)})
(defn- get-aggregation-description
(defn- get-aggregation-details
[metadata query]
(let [field-name (fn [match] (map
(fn [m] (:display_name (Field (mbql.u/field-clause->id-or-literal m))))
(mbql.u/match match :field-id)))]
(let [field-name (fn [match] (or (when (mbql.preds/Field? match)
(:display_name (Field (mbql.u/field-clause->id-or-literal match))))
(flatten (get-aggregation-details metadata match))))]
(when-let [agg-matches (mbql.u/match query
[:aggregation-options _ (options :guard :display-name)]
{:type :aggregation :arg (:display-name options)}
......@@ -24,6 +26,9 @@
[:aggregation-options ag _]
(recur ag)
[(operator :guard #{:+ :- :/ :*}) & args]
(interpose (name operator) (map field-name args))
[:metric arg] {:type :metric
:arg (let [metric (Metric arg)]
(if (not (str/blank? (:name metric)))
......@@ -39,7 +44,12 @@
[:cum-sum arg] {:type :cum-sum :arg (field-name arg)}
[:max arg] {:type :max :arg (field-name arg)}
[:min arg] {:type :min :arg (field-name arg)})]
{:aggregation agg-matches})))
agg-matches)))
(defn- get-aggregation-description
[metadata query]
(when-let [details (get-aggregation-details metadata query)]
{:aggregation details}))
(defn- get-breakout-description
[metadata query]
......
......@@ -31,7 +31,7 @@
(testing "with cumulative sum of price"
(is (= {:table "Venues"
:aggregation [{:type :cum-sum
:arg ["Price"]}]}
:arg "Price"}]}
(sut/generate-query-description (Table (mt/id :venues))
(:query (mt/mbql-query :venues
{:aggregation [[:cum-sum $price]]}))))))
......@@ -88,4 +88,21 @@
[:sum [:*
[:field-id (mt/id :venues :latitude)]
[:field-id (mt/id :venues :longitude)]]]
{:display-name "Nonsensical named metric"}]]})))))))
{:display-name "Nonsensical named metric"}]]}))))
(testing "with unnamed complex aggregation"
(is (= {:table "Venues"
:aggregation [{:type :sum :arg ["Latitude" "*" "Longitude"]}]}
(sut/generate-query-description (Table (mt/id :venues))
{:aggregation [[:sum [:*
[:field-id (mt/id :venues :latitude)]
[:field-id (mt/id :venues :longitude)]]]]}))))
(testing "with unnamed complex aggregation with multiple arguments"
(is (= {:table "Venues"
:aggregation [{:type :sum :arg ["Latitude" "+" "Longitude" "+" "ID"]}]}
(sut/generate-query-description (Table (mt/id :venues))
{:aggregation [[:sum [:+
[:field-id (mt/id :venues :latitude)]
[:field-id (mt/id :venues :longitude)]
[:field-id (mt/id :venues :id)]]]]})))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment