@@ -31,21 +31,24 @@ SQL >
3131 {% if String(countOnly) == '1' or Int8(countOnly, 0) == 1 %} {% set is_count = 1 %} {% end %}
3232 {% end %}
3333 WITH
34+ {% if defined(segments) %} arrayDistinct({{ Array(segments, 'String') }}) AS segments_arr
35+ {% else %} [] AS segments_arr
36+ {% end %},
3437 base_ar AS (
3538 SELECT
3639 ar.activityId AS id,
37- ar.channel,
38- ar.memberId,
39- ar.organizationId,
40- ar.platform,
41- ar.segmentId,
42- ar.sourceId,
43- ar.sourceParentId,
44- ar.timestamp,
45- ar.type
40+ ar.channel AS channel ,
41+ ar.memberId AS memberId ,
42+ ar.organizationId AS organizationId ,
43+ ar.platform AS platform ,
44+ ar.segmentId AS segmentId ,
45+ ar.sourceId AS sourceId ,
46+ ar.sourceParentId AS sourceParentId ,
47+ ar.timestamp AS timestamp ,
48+ ar.type AS type
4649 FROM activityRelations_deduplicated_ds AS ar
4750 WHERE
48- ar.segmentId IN {{ Array(segments, 'String') }}
51+ (length(segments_arr) = 0 OR ar.segmentId IN segments_arr)
4952 {% if defined(startDate) %}
5053 AND ar.timestamp > parseDateTimeBestEffort({{ String(startDate) }})
5154 {% end %}
@@ -373,41 +376,64 @@ SQL >
373376 {% end %}
374377 -- ================== end G1..G5 ==================
375378 )
376- SELECT
377- ar.id,
378- ar.channel,
379- ar.memberId,
380- ar.organizationId,
381- ar.platform,
382- ar.segmentId,
383- ar.sourceId,
384- ar.sourceParentId,
385- ar.timestamp,
386- ar.type
387- FROM base_ar AS ar
388- {% if defined(searchTerm) and searchTerm %}
389- SEMI
390- JOIN
391- (
392- SELECT CAST(id AS String) AS id
393- FROM activities_deduplicated_ds
394- WHERE
395- 1
396- {% if defined(startDate) %}
397- AND timestamp > parseDateTimeBestEffort({{ String(startDate) }})
398- {% end %}
399- {% if defined(endDate) %}
400- AND timestamp < parseDateTimeBestEffort({{ String(endDate) }})
401- {% end %}
402- AND (
403- positionCaseInsensitive(title, {{ String(searchTerm) }}) > 0
404- OR positionCaseInsensitive(body, {{ String(searchTerm) }}) > 0
405- )
406- ) act
407- ON act.id = CAST(ar.id AS String)
408- {% end %}
409- WHERE 1
410- {% if not is_count %}
379+ {% if is_count %}
380+ SELECT count() AS count
381+ FROM base_ar AS ar
382+ {% if defined(searchTerm) and searchTerm %}
383+ SEMI
384+ JOIN
385+ (
386+ SELECT CAST(id AS String) AS id
387+ FROM activities_deduplicated_ds
388+ WHERE
389+ 1
390+ {% if defined(startDate) %}
391+ AND timestamp > parseDateTimeBestEffort({{ String(startDate) }})
392+ {% end %}
393+ {% if defined(endDate) %}
394+ AND timestamp < parseDateTimeBestEffort({{ String(endDate) }})
395+ {% end %}
396+ AND (
397+ positionCaseInsensitive(title, {{ String(searchTerm) }}) > 0
398+ OR positionCaseInsensitive(body, {{ String(searchTerm) }}) > 0
399+ )
400+ ) act
401+ ON act.id = CAST(ar.id AS String)
402+ {% end %}
403+ {% else %}
404+ SELECT
405+ ar.id,
406+ ar.channel,
407+ ar.memberId,
408+ ar.organizationId,
409+ ar.platform,
410+ ar.segmentId,
411+ ar.sourceId,
412+ ar.sourceParentId,
413+ ar.timestamp,
414+ ar.type
415+ FROM base_ar AS ar
416+ {% if defined(searchTerm) and searchTerm %}
417+ SEMI
418+ JOIN
419+ (
420+ SELECT CAST(id AS String) AS id
421+ FROM activities_deduplicated_ds
422+ WHERE
423+ 1
424+ {% if defined(startDate) %}
425+ AND timestamp > parseDateTimeBestEffort({{ String(startDate) }})
426+ {% end %}
427+ {% if defined(endDate) %}
428+ AND timestamp < parseDateTimeBestEffort({{ String(endDate) }})
429+ {% end %}
430+ AND (
431+ positionCaseInsensitive(title, {{ String(searchTerm) }}) > 0
432+ OR positionCaseInsensitive(body, {{ String(searchTerm) }}) > 0
433+ )
434+ ) act
435+ ON act.id = CAST(ar.id AS String)
436+ {% end %}
411437 ORDER BY ar.timestamp DESC, ar.id DESC
412438 LIMIT {{ Int32(pageSize, 10) }}
413439 OFFSET {{ Int32(page, 0) * Int32(pageSize, 10) }}
@@ -416,30 +442,44 @@ SQL >
416442NODE activities_enriched_v1
417443SQL >
418444 %
419- {% if defined(countOnly) and countOnly == '1' %}
420- SELECT countDistinct(fr.id) AS count FROM filtered_relations fr
445+ {% set is_count = 0 %}
446+ {% if defined(countOnly) %}
447+ {% if String(countOnly) == '1' or Int8(countOnly, 0) == 1 %} {% set is_count = 1 %} {% end %}
448+ {% end %}
449+ WITH
450+ {% if defined(segments) %} arrayDistinct({{ Array(segments, 'String') }}) AS segments_arr
451+ {% else %} [] AS segments_arr
452+ {% end %},
453+ base_ar AS (
454+ SELECT
455+ ar.activityId AS id,
456+ ar.channel,
457+ ar.memberId,
458+ ar.organizationId,
459+ ar.platform,
460+ ar.segmentId,
461+ ar.sourceId,
462+ ar.sourceParentId,
463+ ar.timestamp,
464+ ar.type
465+ FROM activityRelations_deduplicated_ds AS ar
466+ WHERE (length(segments_arr) = 0 OR ar.segmentId IN segments_arr)
467+ )
468+ {% if is_count %} SELECT count() AS count FROM base_ar AS ar
421469 {% else %}
422470 SELECT
423- fr.id,
424- fr.channel,
425- fr.memberId,
426- fr.organizationId,
427- fr.platform,
428- fr.segmentId,
429- fr.sourceId,
430- fr.sourceParentId,
431- fr.timestamp,
432- fr.type,
433- a.attributes,
434- a.url,
435- a.body,
436- a.title
437- FROM filtered_relations AS fr ANY
438- LEFT JOIN
439- (
440- SELECT CAST(id AS String) AS activity_id, attributes, url, body, title
441- FROM activities_deduplicated_ds
442- WHERE CAST(id AS String) IN (SELECT DISTINCT CAST(id AS String) FROM filtered_relations)
443- ) AS a
444- ON CAST(fr.id AS String) = a.activity_id
471+ ar.id,
472+ ar.channel,
473+ ar.memberId,
474+ ar.organizationId,
475+ ar.platform,
476+ ar.segmentId,
477+ ar.sourceId,
478+ ar.sourceParentId,
479+ ar.timestamp,
480+ ar.type
481+ FROM base_ar AS ar
482+ ORDER BY ar.timestamp DESC, ar.id DESC
483+ LIMIT {{ Int32(pageSize, 10) }}
484+ OFFSET {{ Int32(page, 0) * Int32(pageSize, 10) }}
445485 {% end %}
0 commit comments