@@ -422,67 +422,31 @@ SQL >
422422NODE activities_enriched_v1
423423SQL >
424424 %
425- {% set has_start = defined(startDate) %}
426- {% set has_end = defined(endDate) %}
427-
428- {% if defined(countOnly) and countOnly == '1' %}
429- -- If IDs are guaranteed unique in filtered_relations, use COUNT().
430- -- Otherwise use countDistinct for correctness.
431- SELECT
432- {% if defined(fr_ids_are_unique) and fr_ids_are_unique == '1' %}
433- count()
434- {% else %}
435- countDistinct(fr.id)
436- {% end %} AS count
437- FROM filtered_relations AS fr
438- {% else %}
439- SELECT
440- fr.id_str AS id,
441- fr.channel,
442- fr.isContribution,
443- fr.memberId,
444- fr.organizationId,
445- fr.platform,
446- fr.segmentId,
447- fr.sourceId,
448- fr.sourceParentId,
449- fr.timestamp,
450- fr.type,
451- a.attributes,
452- a.url,
453- a.body,
454- a.title
455- FROM
456- (
457- -- Cast IDs only once here and keep alias fr
458- SELECT
459- CAST(id AS String) AS id_str,
460- channel,
461- isContribution,
462- memberId,
463- organizationId,
464- platform,
465- segmentId,
466- sourceId,
467- sourceParentId,
468- timestamp,
469- type
470- FROM filtered_relations
471- ) AS fr
472- ANY LEFT JOIN
473- (
425+ {% if defined(countOnly) and countOnly == '1' %}
426+ SELECT countDistinct(fr.id) AS count FROM filtered_relations fr
427+ {% else %}
474428 SELECT
475- CAST(id AS String) AS id_str,
476- attributes,
477- url,
478- body,
479- title
480- FROM activities_deduplicated_ds
481- WHERE 1
482- {% if has_start %} AND timestamp > parseDateTimeBestEffort({{ String(startDate) }}) {% end %}
483- {% if has_end %} AND timestamp < parseDateTimeBestEffort({{ String(endDate) }}) {% end %}
484- -- If activities_deduplicated_ds is partitioned by timestamp,
485- -- this WHERE clause allows partition pruning
486- ) AS a
487- ON a.id_str = fr.id_str
488- {% end %}
429+ fr.id,
430+ fr.channel,
431+ fr.isContribution,
432+ fr.memberId,
433+ fr.organizationId,
434+ fr.platform,
435+ fr.segmentId,
436+ fr.sourceId,
437+ fr.sourceParentId,
438+ fr.timestamp,
439+ fr.type,
440+ a.attributes,
441+ a.url,
442+ a.body,
443+ a.title
444+ FROM filtered_relations AS fr ANY
445+ LEFT JOIN
446+ (
447+ SELECT CAST(id AS String) AS activity_id, attributes, url, body, title
448+ FROM activities_deduplicated_ds
449+ WHERE CAST(id AS String) IN (SELECT DISTINCT CAST(id AS String) FROM filtered_relations)
450+ ) AS a
451+ ON CAST(fr.id AS String) = a.activity_id
452+ {% end %}
0 commit comments