cartesian-product
When applying a cartesian product to two worksheets A and B, this directive pairs every row in worksheet B with every row in worksheet A.
When applying the cartesian product directive to two worksheets A and B, this directive pairs every row in worksheet B with every row in worksheet A. The resulting row set would have as many rows as the total number of rows in worksheet A multiplied by the total number of rows in worksheet B.
Consider the following worksheets Customers and Discounts.
Here is the Customers worksheet. It has 3 rows.
Customer ID | Balance |
---|---|
c0 | 45 |
c1 | 3 |
c2 | 0 |
Here is the Discounts worksheet. It has 2 rows.
Discount ID | Percentage |
---|---|
d0 | 0.5 |
d1 | 0.1 |
Here is the comment snippet that will direct Pebble Stream to apply the cartesian product to the Customers and Discounts worksheets. Notice the use of the "from" directive to identify the first worksheet in the cartesian product as Customers.
pebblestream:from(Customers)
pebblestream:cartesian-product(Discounts)
Resulting in the final worksheet with 6 (2 x 3) rows.
Customer ID | Balance | Discount ID | Percentage |
---|---|---|---|
c0 | 45 | d0 | 0.5 |
c0 | 45 | d1 | 0.1 |
c1 | 3 | d0 | 0.5 |
c1 | 3 | d1 | 0.1 |
c2 | 0 | d0 | 0.5 |
c2 | 0 | d1 | 0.1 |
Performance Tip
Be careful with cartesian products. The number of rows it produces can grow quite quickly!
Use Case Tip
Cartesian products are great for applying multiple discounts to a customer's basket of items. Whenever you have a group of percentages that you need to apply to every item using a cartesian product is a good bet.
Updated over 2 years ago