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
Offset window function support for clickhouse (#79541)
Riley Thompson0218df7cd74f
diff --git a/docs/questions/query-builder/expressions/offset.md b/docs/questions/query-builder/expressions/offset.mdindex bc45d3de28c..a4dc12877e4 100644--- a/docs/questions/query-builder/expressions/offset.md+++ b/docs/questions/query-builder/expressions/offset.md@@ -4,7 +4,7 @@ title: Offset# Offset-> ⚠️ The `Offset` function is currently unavailable for MySQL/MariaDB, ClickHouse, MongoDB, and Druid.+> ⚠️ The `Offset` function is currently unavailable for MySQL/MariaDB, MongoDB, and Druid.The `Offset` function returns the value of an expression in a different row. `Offset` can only be used in the query builder's Summarize step (you cannot use `Offset` to create a custom column).diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.cljindex f82bd4735b2..db3d7639496 100644--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj@@ -83,7 +83,7 @@:transforms/table true:upload-with-auto-pk false:window-functions/cumulative (not driver-api/is-test?)- :window-functions/offset false}]+ :window-functions/offset true}](defmethod driver/database-supports? [:clickhouse feature] [_driver _feature _db] supported?))(defmethod driver/qualified-name-components :clickhousediff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse_qp.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse_qp.cljindex 12abe8553c6..a98c3462202 100644--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse_qp.clj+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse_qp.clj@@ -3,6 +3,7 @@(:refer-clojure :exclude [some])(:require[clojure.string :as str]+ [honey.sql :as sql][java-time.api :as t][metabase.driver-api.core :as driver-api][metabase.driver.clickhouse-nippy]@@ -431,6 +432,24 @@[:sum [:case (sql.qp/->honeysql driver pred) (sql.qp/->honeysql driver field):else 0]])+(defn- format-rows-between-unbounded+ [_clause _args]+ ["ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING"])++(sql/register-clause! ::rows-between-unbounded #'format-rows-between-unbounded nil)++(defmethod sql.qp/->honeysql [:clickhouse :offset]+ [driver [_offset _opts expr n]]+ (sql.qp/window-aggregation-over-rows+ driver+ (let [[f n] (if (pos? n)+ [:'leadInFrame n]+ [:'lagInFrame (- n)])+ expr-hsql (sql.qp/->honeysql driver expr)]+ (-> [f [:'toNullable expr-hsql] [:inline n]]+ (h2x/with-database-type-info (h2x/database-type expr-hsql))))+ {::rows-between-unbounded []}))+(defmethod sql.qp/add-interval-honeysql-form :clickhouse[_ dt amount unit](let [type-info (h2x/type-info dt)]diff --git a/src/metabase/driver/sql/query_processor.clj b/src/metabase/driver/sql/query_processor.cljindex b4338aa4c22..0c6c3494b26 100644--- a/src/metabase/driver/sql/query_processor.clj+++ b/src/metabase/driver/sql/query_processor.clj@@ -1127,7 +1127,7 @@(when-let [order-bys (not-empty (:order-by (apply-top-level-clause driver :order-by {} inner-query)))]{:order-by (vec order-bys)}))-(defn- window-aggregation-over-rows+(defn window-aggregation-over-rows"Generate an OVER (...) window function expression for stuff like `:offset` (`lag` and `lead`)."([driver expr](window-aggregation-over-rows driver expr nil))