nullnullnulls
nullnullnulls

Reputation: 41

How can I build a preaggregation that is able to query a cube with a filter on its joins cube?

For simplicity:

Goal: Query for Orders with a dynamic filter on OrderItem Status.

IE: count orders that have an orderItem of "Approved"

cube(`orders`, {
  sql_table: `public.orders`,

  data_source: `default`,

  joins: {
    order_items: {
      sql: `${CUBE}.id = ${order_items}.order_id`,
      relationship: `one_to_many`
    },
  },

  dimensions: {
    id: {
      sql: `id`,
      type: `number`,
      primary_key: true
    },

    type: {
      sql: `type`,
      type: `string`,
    },
  },

  measures: {
    count: {
      type: `count`
    },
  }
});
cube(`order_items`, {
  sql_table: `public.order_items`,

  data_source: `default`,

  dimensions: {
    id: {
      sql: `id`,
      type: `number`,
      primary_key: true
    },

    status: {
      sql: `status`,
      type: `string`
    },
  },

  measures: {
    count: {
      type: `count`
    },

    unique_order_count: {
      sql: `order_id`,
      type: `count_distinct`,
    }
  }
});

If I try to preaggregate on Order, Cube is unable to build the preaggregation and returns this error Error in processing loop: Internal: Can't connect to cubestore-router.cubejs_prod:9999: No route to host (os error 113)

Preaggregation in Order

  pre_aggregations: {
    main: {
      measures: [
        orders.count,
      ],
      dimensions: [
        orders.type,
        order_items.status,
      ],
      time_dimension: orders.updated_at,
      granularity: `hour`,
      partition_granularity: `month`,
      refresh_key: {
        every: `1 day`,
        sql: `SELECT MAX(updated_at) FROM orders`
      },
    }
  }

If I try to preaggregate on OrderItem, Cube does not use the preaggreation when trying to query with the unique_order_count measure. Querying for everything except the distinct count measure does trigger the preaggregation.

Preaggregation in OrderItem

  pre_aggregations: {
    uniq_by_order: {
      measures: [
        order_items.unique_order_count,
      ],
      dimensions: [
        order_items.status,
      ],
      time_dimension: order_items.updated_at,
      granularity: `hour`,
      partition_granularity: `month`,
      refresh_key: {
        every: `1 day`,
        sql: `SELECT MAX(updated_at) FROM order_items`
      },
    }
  }

Upvotes: 0

Views: 20

Answers (0)

Related Questions