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