简体   繁体   English

在多维数据集的 SQL 定义中使用上下文变量时,Cube.js 是否支持 preAggregations?

[英]Does Cube.js support preAggregations when using Context Variables inside the SQL definition of a cube?

According to the Cube.js documentation , one can define cubes using something called Context Variables, that allows the injection of filters (such as a particular user email) when querying that cube.根据Cube.js 文档,可以使用称为上下文变量的东西来定义多维数据集,这允许在查询该多维数据集时注入过滤器(例如特定用户的电子邮件)。 I haven't tried it yet, but I want to know if this feature is compatible with preaggregations, and also if it handles empty filter cases.我还没有尝试过,但我想知道此功能是否与预聚合兼容,以及它是否处理空过滤器情况。
For example, if I define this cube (same as the example from the docs):例如,如果我定义这个多维数据集(与文档中的示例相同):

    cube(`OrderFacts`, {
      sql: `SELECT * FROM orders WHERE ${FILTER_PARAMS.OrderFacts.date.filter('date')}`,

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

      dimensions: {
        date: {
          sql: `date`,
          type: `time`
        }
      }
    });

will I be able to define a preAggregation such as this?我能定义这样的 preAggregation 吗?

  preAggregations: {
    date: {
      type: `rollup`,
      measureReferences: [someMeasure],
      dimensionReferences: [someDimension],
      timeDimensionReference: date,
      granularity: `month`
    }
  }

and can I perform queries without a date dimension filter?我可以在没有date维度过滤器的情况下执行查询吗? (so the sql would end as SELECT * FROM orders WHERE; ) ? (所以sql将以SELECT * FROM orders WHERE;结尾)?

To sum up: is there a way of injecting filters to a cube's sql definition dynamically, but not at query time and instead at schema compile time ?总结一下:有没有一种方法可以动态地将过滤器注入多维数据集的sql定义,但不是在查询时而是在模式编译时
I ask this because we are currently extending some cubes with information retrieved from our API, and we were overwriting the segment field from this cubes, but due to performance issues we would prefer to overwrite the cube's sql field (to filter unnecessary data from the start).我问这个是因为我们目前正在使用从我们的 API 检索到的信息扩展一些多维数据集,并且我们正在从这个多维数据集中覆盖segment字段,但是由于性能问题,我们更愿意覆盖多维数据集的sql字段(从一开始就过滤不必要的数据) )。

NOTE: We are using asyncModule to perform the queries to our API.注意:我们使用asyncModule来执行对 API 的查询。 We also need to build different cubes (for all our clients) referencing a common table, but with a dynamic SQL that will change depending on the client.我们还需要构建不同的多维数据集(为我们所有的客户)引用一个公用表,但使用将根据客户而改变的动态 SQL。

Our desired output should be (for an Orders table and F1 , ..., Fn client filters from our API):我们想要的输出应该是(对于来自我们 API 的 Orders 表和F1 , ..., Fn客户端过滤器):
N cubes that extend a base Orders cube: OrdersC1 , OrdersC2 , ..., OrdersCn .扩展基本Orders多维数据集的 N 个多维数据集: OrdersC1OrdersC2 、 ...、 OrdersCn
Each OrdersCi cube with a modified version of the base Orders sql, containing its Fi filter.每个OrdersCi多维数据集都带有基本Orders sql 的修改版本,包含其Fi过滤器。
Each OrdersCi cube with the same dimensions , measures and preAggregations definitions, inherited from the base Orders cube.每个OrdersCi多维数据集具有相同的dimensionsmeasurespreAggregations定义,继承自基本Orders多维数据集。

We managed to implement all that I said before, but instead of modifying the sql field, we overwrote the segments field.我们设法实现了我之前所说的所有内容,但是我们没有修改sql字段,而是覆盖了segments字段。

In general only partitioned rollups pass FILTER_PARAMS for partitioned time dimensions.一般来说,只有分区汇总才能通过FILTER_PARAMS来获取分区时间维度。 All other pre-aggregations doesn't allow passing context variables.所有其他预聚合都不允许传递上下文变量。 There're two approaches you can use to leverage pre-aggregations in multi-tenant environments.您可以使用两种方法在多租户环境中利用预聚合。

  1. Override sql for each customer cube such as OrdersC1 , OrdersC2 , etc. In this case all pre-aggregations defined in base Orders cube will be inherited.为每个客户多维数据集(例如OrdersC1OrdersC2等)覆盖sql 。在这种情况下,基本Orders多维数据集中定义的所有预聚合都将被继承。 Each customer cube will have it's own set of pre-aggregations.每个客户多维数据集都有自己的一组预聚合。 It means if there're N customers and M pre-aggregations then N * M pre-aggregation tables should be built which can be costly in some scenarios.这意味着如果有N客户和M预聚合,那么应该构建N * M预聚合表,这在某些情况下可能会很昂贵。
cube(`Orders`, {
  sql: `SELECT * FROM orders`,

  preAggregations: {
    date: {
      type: `rollup`,
      measureReferences: [someMeasure],
      dimensionReferences: [someDimension],
      timeDimensionReference: date,
      granularity: `month`
    },
    // ...
  }
});

cube(`OrdersC1`, {
  extends: Orders,
  sql: `SELECT * FROM orders WHERE customer_id = 'C1'`,
});
  1. Use tenant field as a dimension of rollup.使用租户字段作为汇总维度。 Every segment can be converted to dimension which provides an opportunity to use single rollup table for all customers.每个段都可以转换为维度,这提供了为所有客户使用单个汇总表的机会。 To route requests to right tenant data queryTransformer can be used.可以使用queryTransformer将请求路由到正确的租户数据。
cube(`Orders`, {
  sql: `SELECT * FROM orders`,

  // ...

  dimensions: {
    // ...

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

  preAggregations: {
    date: {
      type: `rollup`,
      measureReferences: [someMeasure],
      dimensionReferences: [customerId, someDimension],
      timeDimensionReference: date,
      granularity: `month`
    },

    // ...
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM