@@ -600,11 +600,14 @@ export async function getChartSql({
600600 return buildInlineCohortJoin ( cohortId , projectId , 'e' , cohortMeta ) ;
601601 } ) . join ( ' ' ) ;
602602
603+ // Determine data source: use custom event CTE if present, otherwise events table
604+ const dataSource = customEvent ? 'custom_event_data' : TABLE_NAMES . events ;
605+
603606 // Add top_breakdowns CTE using the builder
604607 addCte (
605608 'top_breakdowns' ,
606609 `SELECT ${ breakdownSelects }
607- FROM ${ TABLE_NAMES . events } e
610+ FROM ${ dataSource } AS e
608611 ${ profilesJoinRef ? `${ profilesJoinRef } ` : '' } ${ cohortJoinsForTop ? `${ cohortJoinsForTop } ` : '' } ${ getWhereWithoutBar ( ) }
609612 GROUP BY ${ breakdownSelects }
610613 ORDER BY count(*) DESC
@@ -685,19 +688,22 @@ export async function getChartSql({
685688
686689 const totalCountWhere = getWhereWithoutBar ( ) ;
687690
691+ // Determine data source: use custom event CTE if present, otherwise events table
692+ const dataSourceForBreakdown = customEvent ? 'custom_event_data' : TABLE_NAMES . events ;
693+
688694 // Build cohort JOINs for breakdown_totals CTE
689695 // NOTE: ClickHouse CTEs cannot reference other CTEs in JOINs, so we inline the subquery
690696 const cohortJoinsForBreakdown = cohortIds . map ( ( cohortId ) => {
691697 const cohortMeta = cohortMetadata . get ( cohortId ) ;
692- return buildInlineCohortJoin ( cohortId , projectId , TABLE_NAMES . events , cohortMeta ) ;
698+ return buildInlineCohortJoin ( cohortId , projectId , dataSourceForBreakdown , cohortMeta ) ;
693699 } ) . join ( ' ' ) ;
694700
695701 addCte (
696702 'breakdown_totals' ,
697703 `SELECT
698704 ${ breakdownSelects } ,
699- uniq(${ TABLE_NAMES . events } .profile_id) as total_count
700- FROM ${ TABLE_NAMES . events }
705+ uniq(${ dataSourceForBreakdown } .profile_id) as total_count
706+ FROM ${ dataSourceForBreakdown }
701707 ${ profilesJoinRefForCTE ? `${ profilesJoinRefForCTE } ` : '' } ${ cohortJoinsForBreakdown ? `${ cohortJoinsForBreakdown } ` : '' } ${ totalCountWhere }
702708 GROUP BY ${ breakdownGroupBy } ` ,
703709 ) ;
@@ -714,17 +720,20 @@ export async function getChartSql({
714720 } else {
715721 const totalCountWhere = getWhereWithoutBar ( ) ;
716722
723+ // Determine data source: use custom event CTE if present, otherwise events table
724+ const dataSourceForTotal = customEvent ? 'custom_event_data' : TABLE_NAMES . events ;
725+
717726 // Build cohort JOINs for total_unique CTE
718727 // NOTE: ClickHouse CTEs cannot reference other CTEs in JOINs, so we inline the subquery
719728 const cohortJoinsForTotal = cohortIds . map ( ( cohortId ) => {
720729 const cohortMeta = cohortMetadata . get ( cohortId ) ;
721- return buildInlineCohortJoin ( cohortId , projectId , TABLE_NAMES . events , cohortMeta ) ;
730+ return buildInlineCohortJoin ( cohortId , projectId , dataSourceForTotal , cohortMeta ) ;
722731 } ) . join ( ' ' ) ;
723732
724733 addCte (
725734 'total_unique' ,
726- `SELECT uniq(${ TABLE_NAMES . events } .profile_id) as total_count
727- FROM ${ TABLE_NAMES . events }
735+ `SELECT uniq(${ dataSourceForTotal } .profile_id) as total_count
736+ FROM ${ dataSourceForTotal }
728737 ${ profilesJoinRefForCTE ? `${ profilesJoinRefForCTE } ` : '' } ${ cohortJoinsForTotal ? `${ cohortJoinsForTotal } ` : '' } ${ totalCountWhere } ` ,
729738 ) ;
730739
0 commit comments