Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: A computed dimension failing to group on MySQL

Damian Legawieca80659a2cf58
diff --git a/spree/core/lib/spree/reporting/adapters/live.rb b/spree/core/lib/spree/reporting/adapters/live.rb
index f024e22665..a0aa7902ba 100644
--- a/spree/core/lib/spree/reporting/adapters/live.rb
+++ b/spree/core/lib/spree/reporting/adapters/live.rb
@@ -55,7 +55,7 @@ module Spree
dimension_selects = query.dimensions.map do |d|
"#{dimension_expression(d)} AS #{dimension_alias(d)}"
end
- scope = scope.group(query.dimensions.map { |d| Arel.sql(dimension_expression(d)) })
+ scope = scope.group(query.dimensions.map { |d| Arel.sql(group_by_term(d)) })
scope = apply_key_filter(scope, key_filter) if key_filter
scope = apply_sql_sort(scope, metrics) if push_sort
selects = dimension_selects + selects
@@ -222,6 +222,15 @@ module Spree
"d_#{dim[:dimension].name}"
end
+ # What GROUP BY names for a dimension. A plain column repeats its
+ # expression, but an expression dimension groups by the SELECT alias:
+ # MySQL's only_full_group_by refuses to match a repeated expression
+ # containing a correlated subquery, seeing only the bare column inside
+ # it. All three databases accept grouping by the alias.
+ def group_by_term(dim)
+ dim[:dimension].expression? ? dimension_alias(dim) : dimension_expression(dim)
+ end
+
def dimension_expression(dim)
dimension = dim[:dimension]
return time_bucket_sql(qualified_column(dimension), dim[:grain]) if dimension.time?