cube icon indicating copy to clipboard operation
cube copied to clipboard

Support `group by` replacements for dimensions

Open cams-data opened this issue 3 months ago • 0 comments

Is your feature request related to a problem? Please describe. This feature request does relate to a problem. I am experiencing inefficient querying against my database when grouping by a text dimension that contains geojson data.

Describe the solution you'd like Dimensions should take a new field that allows users to supply sql that replaces the primary sql for that dimension when used in the group by and order by sections of a query.

Take this cube for example:

cubes:
- name: geographic_area_shapes
  sql_table: '`ch_geography_silver`.`geographic_area_shapes`'
  dimensions:
  - name: geographic_area_key
    sql: geographic_area_key
    type: string
    meta:
      cube:
        primary_key: true
  - name: geographic_area_type
    sql: geographic_area_type
    type: string
  - name: shape_clipped_json
    sql: shape_clipped_json
    type: string

When I use the dimension for grouping I get a query like this:

SELECT
  geographic_area_shapes.shape_clipped_json,
  SUM(some_col) AS ...
FROM ch_geography_silver.geographic_area_shapes
GROUP BY geographic_area_shapes.shape_clipped_json

What I would like to see is a way to force cube to generate SQL that looks like this instead:

SELECT
  ANY(geographic_area_shapes.shape_clipped_json) AS shape_clipped_json,
  SUM(some_col) AS ...
FROM ch_geography_silver.geographic_area_shapes
GROUP BY geographic_area_shapes.geographic_area_key # Using a stand-in for the grouping

This could be achieved with something like this:

cubes:
- name: geographic_area_shapes
  sql_table: '`ch_geography_silver`.`geographic_area_shapes`'
  dimensions:
  - name: geographic_area_key
    sql: geographic_area_key
    type: string
    meta:
      cube:
        primary_key: true
  - name: geographic_area_type
    sql: geographic_area_type
    type: string
  - name: shape_clipped_json
    sql: shape_clipped_json
    type: string
    group_replacement:
      sql: {CUBE}.geographic_area_key

The bottom query is far more efficient in many database technologies when the shape_clipped_json column contains a large number of bytes. This is particularly true for columnar databases, where loading the shape column can be deferred until later on. In my narrow testing I was able to reduce query memory usage from 6 Gib to 3.9Gib, just by using a stand-in for the group by. The query also ran about 25% quicker.

Describe alternatives you've considered I cannot think of a way to force the behaviour I am describing inside Cube

Additional context This feature is useful in many contexts, where one column contains a lot of data, and another column maps 1:1 cleanly with it.

cams-data avatar Aug 28 '26 03:08 cams-data