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