Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/ol_dbt/macros/cross_db_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,34 @@
{# StarRocks: array subscript with 1-based indexing #}
{{ array_expr }}[{{ index }}]
{%- endmacro %}

{% macro is_courserun_current(start_on_timestamp_str, end_on_timestamp_str) -%}
{{ adapter.dispatch('is_courserun_current', 'open_learning')(start_on_timestamp_str, end_on_timestamp_str) }}
{%- endmacro %}

{% macro default__is_courserun_current(start_on_timestamp_str, end_on_timestamp_str) -%}
{# Trino: native support #}
case
when
cast(from_iso8601_timestamp({{ start_on_timestamp_str }}) as date) <= current_date
and (
{{ end_on_timestamp_str }} is null
or cast(from_iso8601_timestamp({{ end_on_timestamp_str }}) as date) >= current_date
)
then true
else false
end
{%- endmacro %}

{% macro duckdb__is_courserun_current(start_on_timestamp_str, end_on_timestamp_str) -%}
case
when
cast({{ start_on_timestamp_str }} as date) <= current_date
and (
{{ end_on_timestamp_str }} is null
or cast({{ end_on_timestamp_str }} as date) >= current_date
)
then true
else false
end
{%- endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,6 @@ with mitx_courses as (
, mitxonline_runs.courserun_upgrade_deadline
, mitxonline_runs.courserun_is_live
, mitxonline_runs.course_number
, case
when
mitxonline_runs.courserun_end_on is null
and {{ from_iso8601_timestamp('mitxonline_runs.courserun_start_on') }} <= current_date
then true
when
{{ from_iso8601_timestamp('mitxonline_runs.courserun_start_on') }} <= current_date
and {{ from_iso8601_timestamp('mitxonline_runs.courserun_end_on') }} > current_date
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how important this detail is but should the new macro's logic match the older logic exactly? I'm wondering about how this code has ">" but the new code has ">="

Copy link
Copy Markdown
Contributor Author

@rachellougee rachellougee Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is doing a timestamp boundary comparison:
from_iso8601_timestamp(mitxonline_runs.courserun_end_on) > current_date

vs

the new code performs a date-based comparison:
cast(from_iso8601_timestamp(end_on_timestamp_str) as date) >= current_date

The old logic is time-precision based, while the new logic is date-based, so they behave slightly differently.

If we change from cast(from_iso8601_timestamp(end_on_timestamp_str) as date) >= current_date to cast(from_iso8601_timestamp(end_on_timestamp_str) as date) > current_date will produce false if end_date is today. Also, this field is not highly time-sensitive, given how frequently the dbt models are built, so a date-based comparison should be enough unless the requirements change in the future

then true
else false
end as courserun_is_current
from mitxonline_runs
left join mitx_courses on mitxonline_runs.course_id = mitx_courses.mitxonline_course_id
where mitxonline_runs.courserun_platform = '{{ var("mitxonline") }}'
Expand All @@ -113,17 +102,6 @@ with mitx_courses as (
, micromasters_runs.courserun_upgrade_deadline
, null as courserun_is_live
, edxorg_runs.course_number
, case
when
edxorg_runs.courserun_end_date is null
and {{ from_iso8601_timestamp('edxorg_runs.courserun_start_date') }} <= current_date
then true
when
{{ from_iso8601_timestamp('edxorg_runs.courserun_start_date') }} <= current_date
and {{ from_iso8601_timestamp('edxorg_runs.courserun_end_date') }} > current_date
then true
else false
end as courserun_is_current
from edxorg_runs
left join mitx_courses on edxorg_runs.course_number = mitx_courses.course_number
left join micromasters_runs on edxorg_runs.courserun_readable_id = micromasters_runs.courserun_edxorg_readable_id
Expand All @@ -148,17 +126,6 @@ with mitx_courses as (
when cardinality(split(mitxpro_runs.courserun_readable_id, '+')) >= 2
then split(mitxpro_runs.courserun_readable_id, '+')[2]
end as course_number
, case
when
mitxpro_runs.courserun_end_on is null
and {{ from_iso8601_timestamp('mitxpro_runs.courserun_start_on') }} <= current_date
then true
when
{{ from_iso8601_timestamp('mitxpro_runs.courserun_start_on') }} <= current_date
and {{ from_iso8601_timestamp('mitxpro_runs.courserun_end_on') }} > current_date
then true
else false
end as courserun_is_current
from mitxpro_runs
left join mitxpro_courses on mitxpro_runs.course_id = mitxpro_courses.course_id

Expand All @@ -176,17 +143,6 @@ with mitx_courses as (
, null as courserun_upgrade_deadline
, null as courserun_is_live
, emeritus_runs.course_number
, case
when
emeritus_runs.courserun_end_on is null
and {{ from_iso8601_timestamp('mitxpro_runs.courserun_start_on') }} <= current_date
then true
when
{{ from_iso8601_timestamp('emeritus_runs.courserun_start_on') }} <= current_date
and {{ from_iso8601_timestamp('emeritus_runs.courserun_end_on') }} > current_date
then true
else false
end as courserun_is_current
from emeritus_runs
left join mitxpro_runs
on emeritus_runs.courserun_external_readable_id = mitxpro_runs.courserun_external_readable_id
Expand All @@ -206,17 +162,6 @@ with mitx_courses as (
, null as courserun_upgrade_deadline
, null as courserun_is_live
, global_alumni_runs.course_number
, case
when
global_alumni_runs.courserun_end_on is null
and {{ from_iso8601_timestamp('global_alumni_runs.courserun_start_on') }} <= current_date
then true
when
{{ from_iso8601_timestamp('global_alumni_runs.courserun_start_on') }} <= current_date
and {{ from_iso8601_timestamp('global_alumni_runs.courserun_end_on') }} > current_date
then true
else false
end as courserun_is_current
from global_alumni_runs
left join mitxpro_runs
on global_alumni_runs.courserun_external_readable_id = mitxpro_runs.courserun_external_readable_id
Expand All @@ -239,17 +184,6 @@ with mitx_courses as (
when cardinality(split(bootcamps_runs.courserun_readable_id, '+')) >= 2
then split(bootcamps_runs.courserun_readable_id, '+')[2]
end as course_number
, case
when
bootcamps_runs.courserun_end_on is null
and {{ from_iso8601_timestamp('bootcamps_runs.courserun_start_on') }} <= current_date
then true
when
{{ from_iso8601_timestamp('bootcamps_runs.courserun_start_on') }} <= current_date
and {{ from_iso8601_timestamp('bootcamps_runs.courserun_end_on') }} > current_date
then true
else false
end as courserun_is_current
from bootcamps_runs
left join bootcamps_courses on bootcamps_runs.course_id = bootcamps_courses.course_id

Expand All @@ -270,18 +204,20 @@ with mitx_courses as (
when cardinality(split(courserun_readable_id, '+')) >= 2
then split(courserun_readable_id, '+')[2]
end as course_number
, case
when
courserun_end_on is null
and {{ from_iso8601_timestamp('courserun_start_on') }} <= current_date
then true
when
{{ from_iso8601_timestamp('courserun_start_on') }} <= current_date
and {{ from_iso8601_timestamp('courserun_end_on') }} > current_date
then true
else false
end as courserun_is_current
from residential_runs
)

select * from combined_runs
select
platform
, course_title
, course_readable_id
, courserun_title
, courserun_readable_id
, courserun_url
, courserun_start_on
, courserun_end_on
, courserun_upgrade_deadline
, courserun_is_live
, course_number
, {{ is_courserun_current('courserun_start_on', 'courserun_end_on') }} as courserun_is_current
from combined_runs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{{ config(full_refresh = true) }}

with mitxonline_video_engagements as (
select * from {{ ref('marts__mitxonline_video_engagements') }}
select
user_email
, section_title
, courserun_readable_id
, video_title
, video_event_type
, coursestructure_block_index
, {{ is_courserun_current('courserun_start_on', 'courserun_end_on') }} as courserun_is_current
from {{ ref('marts__mitxonline_video_engagements') }}
)

, mitx__courses as (
Expand All @@ -25,15 +33,7 @@ select
, mitxonline_video_engagements.courserun_readable_id
, subq_video_engagements.total_video_count
, 'MITx Online' as platform
, case
when
cast(substring(mitxonline_video_engagements.courserun_start_on, 1, 10) as date) <= current_date
and
(mitxonline_video_engagements.courserun_end_on is null
or cast(substring(mitxonline_video_engagements.courserun_end_on, 1, 10) as date) >= current_date)
then true
else false end
as courserun_is_current
, mitxonline_video_engagements.courserun_is_current
, count(distinct mitxonline_video_engagements.video_title) as user_watched_video_count
, max(mitxonline_video_engagements.coursestructure_block_index) as max_coursestructure_block_index
from mitxonline_video_engagements
Expand All @@ -48,11 +48,4 @@ group by
, mitxonline_video_engagements.courserun_readable_id
, subq_video_engagements.total_video_count
, 'MITx Online'
, case
when
cast(substring(mitxonline_video_engagements.courserun_start_on, 1, 10) as date) <= current_date
and
(mitxonline_video_engagements.courserun_end_on is null
or cast(substring(mitxonline_video_engagements.courserun_end_on, 1, 10) as date) >= current_date)
then true
else false end
, mitxonline_video_engagements.courserun_is_current