@@ -3,7 +3,7 @@ import { UserError } from '../../../src/compiler/UserError';
3
3
import { PostgresQuery } from '../../../src/adapter/PostgresQuery' ;
4
4
import { BigqueryQuery } from '../../../src/adapter/BigqueryQuery' ;
5
5
import { PrestodbQuery } from '../../../src/adapter/PrestodbQuery' ;
6
- import { prepareJsCompiler } from '../../unit/PrepareCompiler' ;
6
+ import { prepareJsCompiler , prepareYamlCompiler } from '../../unit/PrepareCompiler' ;
7
7
import { dbRunner } from './PostgresDBRunner' ;
8
8
import { createJoinedCubesSchema } from '../../unit/utils' ;
9
9
import { testWithPreAggregation } from './pre-aggregation-utils' ;
@@ -4051,4 +4051,144 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
4051
4051
} ]
4052
4052
) ;
4053
4053
} ) ;
4054
+
4055
+ describe ( 'Transitive join paths' , ( ) => {
4056
+ const { compiler, joinGraph, cubeEvaluator } = prepareYamlCompiler ( `
4057
+ cubes:
4058
+ - name: merchant_dims
4059
+ sql: |
4060
+ (
4061
+ SELECT 101 AS merchant_sk, 'M1' AS merchant_id
4062
+ UNION ALL
4063
+ SELECT 102 AS merchant_sk, 'M2' AS merchant_id
4064
+ )
4065
+ dimensions:
4066
+ - name: merchant_sk
4067
+ sql: merchant_sk
4068
+ type: number
4069
+ primary_key: true
4070
+ - name: merchant_id
4071
+ sql: merchant_id
4072
+ type: string
4073
+
4074
+ - name: product_dims
4075
+ sql: |
4076
+ (
4077
+ SELECT 201 AS product_sk, 'P1' AS product_id
4078
+ UNION ALL
4079
+ SELECT 202 AS product_sk, 'P2' AS product_id
4080
+ )
4081
+ dimensions:
4082
+ - name: product_sk
4083
+ sql: product_sk
4084
+ type: number
4085
+ primary_key: true
4086
+ - name: product_id
4087
+ sql: product_id
4088
+ type: string
4089
+
4090
+ - name: merchant_and_product_dims
4091
+ sql: |
4092
+ (
4093
+ SELECT 'M1' AS merchant_id, 'P1' AS product_id, 'Organic' AS acquisition_channel
4094
+ UNION ALL
4095
+ SELECT 'M1' AS merchant_id, 'P2' AS product_id, 'Paid' AS acquisition_channel
4096
+ UNION ALL
4097
+ SELECT 'M2' AS merchant_id, 'P1' AS product_id, 'Referral' AS acquisition_channel
4098
+ )
4099
+ dimensions:
4100
+ - name: product_id
4101
+ sql: product_id
4102
+ type: string
4103
+ primary_key: true
4104
+ - name: merchant_id
4105
+ sql: merchant_id
4106
+ type: string
4107
+ primary_key: true
4108
+ - name: acquisition_channel
4109
+ sql: acquisition_channel
4110
+ type: string
4111
+
4112
+ - name: test_facts
4113
+ sql: |
4114
+ (
4115
+ SELECT DATE '2023-01-01' AS reporting_date, 101 AS merchant_sk, 201 AS product_sk, 100 AS amount
4116
+ UNION ALL
4117
+ SELECT DATE '2023-01-01' AS reporting_date, 101 AS merchant_sk, 202 AS product_sk, 150 AS amount
4118
+ UNION ALL
4119
+ SELECT DATE '2023-01-02' AS reporting_date, 102 AS merchant_sk, 201 AS product_sk, 200 AS amount
4120
+ )
4121
+ joins:
4122
+ - name: merchant_dims
4123
+ relationship: many_to_one
4124
+ sql: "{CUBE}.merchant_sk = {merchant_dims.merchant_sk}"
4125
+ - name: product_dims
4126
+ relationship: many_to_one
4127
+ sql: "{CUBE}.product_sk = {product_dims.product_sk}"
4128
+ - name: merchant_and_product_dims # This join depends on merchant_dims and product_dims
4129
+ relationship: many_to_one
4130
+ sql: "{merchant_dims.merchant_id} = {merchant_and_product_dims.merchant_id} AND {product_dims.product_id} = {merchant_and_product_dims.product_id}"
4131
+ dimensions:
4132
+ - name: reporting_date
4133
+ sql: reporting_date
4134
+ type: time
4135
+ primary_key: true
4136
+ - name: merchant_sk
4137
+ sql: merchant_sk
4138
+ type: number
4139
+ primary_key: true
4140
+ - name: product_sk
4141
+ sql: product_sk
4142
+ type: number
4143
+ primary_key: true
4144
+ - name: acquisition_channel # This dimension triggers the join to merchant_and_product_dims
4145
+ sql: "{merchant_and_product_dims.acquisition_channel}"
4146
+ type: string
4147
+ measures:
4148
+ - name: amount_sum
4149
+ sql: amount
4150
+ type: sum
4151
+ ` ) ;
4152
+
4153
+ it ( 'querying cube dimension that require transitive joins' , async ( ) => {
4154
+ await compiler . compile ( ) ;
4155
+ const query = new PostgresQuery ( { joinGraph, cubeEvaluator, compiler } , {
4156
+ measures : [ ] ,
4157
+ dimensions : [
4158
+ 'test_facts.reporting_date' ,
4159
+ 'test_facts.merchant_sk' ,
4160
+ 'test_facts.product_sk' ,
4161
+ 'test_facts.acquisition_channel'
4162
+ ] ,
4163
+ order : [ {
4164
+ id : 'test_facts.acquisition_channel'
4165
+ } ] ,
4166
+ timezone : 'America/Los_Angeles'
4167
+ } ) ;
4168
+
4169
+ const res = await dbRunner . testQuery ( query . buildSqlAndParams ( ) ) ;
4170
+ console . log ( JSON . stringify ( res ) ) ;
4171
+
4172
+ expect ( res ) . toEqual ( [
4173
+ {
4174
+ test_facts__acquisition_channel : 'Organic' ,
4175
+ test_facts__merchant_sk : 101 ,
4176
+ test_facts__product_sk : 201 ,
4177
+ test_facts__reporting_date : '2023-01-01T00:00:00.000Z' ,
4178
+ } ,
4179
+ {
4180
+ test_facts__acquisition_channel : 'Paid' ,
4181
+ test_facts__merchant_sk : 101 ,
4182
+ test_facts__product_sk : 202 ,
4183
+ test_facts__reporting_date : '2023-01-01T00:00:00.000Z' ,
4184
+ } ,
4185
+ {
4186
+ test_facts__acquisition_channel : 'Referral' ,
4187
+ test_facts__merchant_sk : 102 ,
4188
+ test_facts__product_sk : 201 ,
4189
+ test_facts__reporting_date : '2023-01-02T00:00:00.000Z' ,
4190
+ } ,
4191
+ ] ) ;
4192
+ } ) ;
4193
+ } ) ;
4054
4194
} ) ;
0 commit comments