Skip to content

Commit 2d64ca7

Browse files
feat: DimCalendarMonth inserts
1 parent 8b525ca commit 2d64ca7

File tree

3 files changed

+214
-3
lines changed

3 files changed

+214
-3
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
from ...config import Config
2+
3+
4+
def dim_calendar_month_insert_template(config: Config) -> str:
5+
dcm_conf = config.dim_calendar_month
6+
dcm_cols = dcm_conf.columns
7+
dd_conf = config.dim_date
8+
dd_cols = dd_conf.columns
9+
h_conf = config.holidays
10+
holiday_columndef: list[str] = []
11+
holiday_colselect: list[str] = []
12+
for i, t in enumerate(h_conf.holiday_types):
13+
holiday_columndef.append(
14+
f"{t.generated_column_prefix}{t.generated_monthly_count_column_postfix} = SUM({t.generated_column_prefix}{t.generated_flag_column_postfix} * 1)"
15+
)
16+
holiday_colselect.append(
17+
f"{t.generated_column_prefix}{t.generated_monthly_count_column_postfix}"
18+
)
19+
20+
holiday_columndef_str = ",\n ".join(holiday_columndef)
21+
22+
holiday_colselect_str = ",\n ".join(holiday_colselect)
23+
24+
return f"""WITH DistinctMonths AS (
25+
SELECT
26+
{dcm_cols.month_start_key.name} = CONVERT(
27+
int,
28+
CONVERT(
29+
varchar(8),
30+
{dd_cols.current_month_start.name},
31+
112
32+
)
33+
),
34+
{dcm_cols.month_end_key.name} = CONVERT(
35+
int,
36+
CONVERT(
37+
varchar(8),
38+
{dd_cols.current_month_end.name},
39+
112
40+
)
41+
),
42+
{holiday_columndef_str}
43+
FROM
44+
{dd_conf.table_schema}.{dd_conf.table_name}
45+
GROUP BY {dd_cols.current_month_start.name}, {dd_cols.current_month_end.name}
46+
)
47+
48+
INSERT INTO {dcm_conf.table_schema}.{dcm_conf.table_name} (
49+
{dcm_cols.month_start_key.name},
50+
{dcm_cols.month_end_key.name},
51+
{dcm_cols.month_start_date.name},
52+
{dcm_cols.month_end_date.name},
53+
{dcm_cols.month_start_iso_date_name.name},
54+
{dcm_cols.month_end_iso_date_name.name},
55+
{dcm_cols.month_start_american_date_name.name},
56+
{dcm_cols.month_end_american_date_name.name},
57+
{dcm_cols.month_name.name},
58+
{dcm_cols.month_abbrev.name},
59+
{dcm_cols.month_start_year_week_name.name},
60+
{dcm_cols.month_end_year_week_name.name},
61+
{dcm_cols.year_month_name.name},
62+
{dcm_cols.month_year_name.name},
63+
{dcm_cols.year_quarter_name.name},
64+
{dcm_cols.year.name},
65+
{dcm_cols.month_start_year_week.name},
66+
{dcm_cols.month_end_year_week.name},
67+
{dcm_cols.year_month.name},
68+
{dcm_cols.year_quarter.name},
69+
{dcm_cols.month_start_day_of_quarter.name},
70+
{dcm_cols.month_end_day_of_quarter.name},
71+
{dcm_cols.month_start_day_of_year.name},
72+
{dcm_cols.month_end_day_of_year.name},
73+
{dcm_cols.month_start_week_of_quarter.name},
74+
{dcm_cols.month_end_week_of_quarter.name},
75+
{dcm_cols.month_start_week_of_year.name},
76+
{dcm_cols.month_end_week_of_year.name},
77+
{dcm_cols.month_of_quarter.name},
78+
{dcm_cols.quarter.name},
79+
{dcm_cols.days_in_month.name},
80+
{dcm_cols.days_in_quarter.name},
81+
{dcm_cols.days_in_year.name},
82+
{dcm_cols.current_month_flag.name},
83+
{dcm_cols.prior_month_flag.name},
84+
{dcm_cols.next_month_flag.name},
85+
{dcm_cols.current_quarter_flag.name},
86+
{dcm_cols.prior_quarter_flag.name},
87+
{dcm_cols.next_quarter_flag.name},
88+
{dcm_cols.current_year_flag.name},
89+
{dcm_cols.prior_year_flag.name},
90+
{dcm_cols.next_year_flag.name},
91+
{dcm_cols.first_day_of_month_flag.name},
92+
{dcm_cols.last_day_of_month_flag.name},
93+
{dcm_cols.first_day_of_quarter_flag.name},
94+
{dcm_cols.last_day_of_quarter_flag.name},
95+
{dcm_cols.first_day_of_year_flag.name},
96+
{dcm_cols.last_day_of_year_flag.name},
97+
{dcm_cols.month_start_fraction_of_quarter.name},
98+
{dcm_cols.month_end_fraction_of_quarter.name},
99+
{dcm_cols.month_start_fraction_of_year.name},
100+
{dcm_cols.month_end_fraction_of_year.name},
101+
{dcm_cols.current_quarter_start.name},
102+
{dcm_cols.current_quarter_end.name},
103+
{dcm_cols.current_year_start.name},
104+
{dcm_cols.current_year_end.name},
105+
{dcm_cols.prior_month_start.name},
106+
{dcm_cols.prior_month_end.name},
107+
{dcm_cols.prior_quarter_start.name},
108+
{dcm_cols.prior_quarter_end.name},
109+
{dcm_cols.prior_year_start.name},
110+
{dcm_cols.prior_year_end.name},
111+
{dcm_cols.next_month_start.name},
112+
{dcm_cols.next_month_end.name},
113+
{dcm_cols.next_quarter_start.name},
114+
{dcm_cols.next_quarter_end.name},
115+
{dcm_cols.next_year_start.name},
116+
{dcm_cols.next_year_end.name},
117+
{dcm_cols.month_start_quarterly_burnup.name},
118+
{dcm_cols.month_end_quarterly_burnup.name},
119+
{dcm_cols.month_start_yearly_burnup.name},
120+
{dcm_cols.month_end_yearly_burnup.name},
121+
{holiday_colselect_str}
122+
)
123+
-- Yank the day-level stuff we need for both the start and end dates from {dd_conf.table_name}
124+
SELECT
125+
base.{dcm_cols.month_start_key.name},
126+
base.{dcm_cols.month_end_key.name},
127+
{dcm_cols.month_start_date.name} = startdate.{dd_cols.date_key.name},
128+
{dcm_cols.month_end_date.name} = enddate.{dd_cols.date_key.name},
129+
{dcm_cols.month_start_iso_date_name.name} = startdate.{dd_cols.iso_date_name.name},
130+
{dcm_cols.month_end_iso_date_name.name} = enddate.{dd_cols.iso_date_name.name},
131+
{dcm_cols.month_start_american_date_name.name} = startdate.{dd_cols.american_date_name.name},
132+
{dcm_cols.month_end_american_date_name.name} = enddate.{dd_cols.american_date_name.name},
133+
{dcm_cols.month_name.name} = startdate.{dd_cols.month_name.name},
134+
{dcm_cols.month_abbrev.name} = startdate.{dd_cols.month_abbrev.name},
135+
{dcm_cols.month_start_year_week_name.name} = startdate.{dd_cols.year_week_name.name},
136+
{dcm_cols.month_end_year_week_name.name} = enddate.{dd_cols.year_week_name.name},
137+
{dcm_cols.year_month_name.name} = startdate.{dd_cols.year_month_name.name},
138+
{dcm_cols.month_year_name.name} = startdate.{dd_cols.month_year_name.name},
139+
{dcm_cols.year_quarter_name.name} = startdate.{dd_cols.year_quarter_name.name},
140+
{dcm_cols.year.name} = startdate.{dd_cols.year.name},
141+
{dcm_cols.month_start_year_week.name} = startdate.{dd_cols.year_week.name},
142+
{dcm_cols.month_end_year_week.name} = enddate.{dd_cols.year_week.name},
143+
{dcm_cols.year_month.name} = startdate.{dd_cols.year_month.name},
144+
{dcm_cols.year_quarter.name} = startdate.{dd_cols.year_quarter.name},
145+
{dcm_cols.month_start_day_of_quarter.name} = startdate.{dd_cols.day_of_quarter.name},
146+
{dcm_cols.month_end_day_of_quarter.name} = enddate.{dd_cols.day_of_quarter.name},
147+
{dcm_cols.month_start_day_of_year.name} = startdate.{dd_cols.day_of_year.name},
148+
{dcm_cols.month_end_day_of_year.name} = enddate.{dd_cols.day_of_year.name},
149+
{dcm_cols.month_start_week_of_quarter.name} = startdate.{dd_cols.week_of_quarter.name},
150+
{dcm_cols.month_end_week_of_quarter.name} = enddate.{dd_cols.week_of_quarter.name},
151+
{dcm_cols.month_start_week_of_year.name} = startdate.{dd_cols.week_of_year.name},
152+
{dcm_cols.month_end_week_of_year.name} = enddate.{dd_cols.week_of_year.name},
153+
{dcm_cols.month_of_quarter.name} = startdate.{dd_cols.month_of_quarter.name},
154+
{dcm_cols.quarter.name} = startdate.{dd_cols.quarter.name},
155+
{dcm_cols.days_in_month.name} = startdate.{dd_cols.days_in_month.name},
156+
{dcm_cols.days_in_quarter.name} = startdate.{dd_cols.days_in_quarter.name},
157+
{dcm_cols.days_in_year.name} = startdate.{dd_cols.days_in_year.name},
158+
{dcm_cols.current_month_flag.name} = startdate.{dd_cols.current_month_flag.name},
159+
{dcm_cols.prior_month_flag.name} = startdate.{dd_cols.prior_month_flag.name},
160+
{dcm_cols.next_month_flag.name} = startdate.{dd_cols.next_month_flag.name},
161+
{dcm_cols.current_quarter_flag.name} = startdate.{dd_cols.current_quarter_flag.name},
162+
{dcm_cols.prior_quarter_flag.name} = startdate.{dd_cols.prior_quarter_flag.name},
163+
{dcm_cols.next_quarter_flag.name} = startdate.{dd_cols.next_quarter_flag.name},
164+
{dcm_cols.current_year_flag.name} = startdate.{dd_cols.current_year_flag.name},
165+
{dcm_cols.prior_year_flag.name} = startdate.{dd_cols.prior_year_flag.name},
166+
{dcm_cols.next_year_flag.name} = startdate.{dd_cols.next_year_flag.name},
167+
{dcm_cols.first_day_of_month_flag.name} = startdate.{dd_cols.first_day_of_month_flag.name},
168+
{dcm_cols.last_day_of_month_flag.name} = startdate.{dd_cols.last_day_of_month_flag.name},
169+
{dcm_cols.first_day_of_quarter_flag.name} = startdate.{dd_cols.first_day_of_quarter_flag.name},
170+
{dcm_cols.last_day_of_quarter_flag.name} = startdate.{dd_cols.last_day_of_quarter_flag.name},
171+
{dcm_cols.first_day_of_year_flag.name} = startdate.{dd_cols.first_day_of_year_flag.name},
172+
{dcm_cols.last_day_of_year_flag.name} = startdate.{dd_cols.last_day_of_year_flag.name},
173+
{dcm_cols.month_start_fraction_of_quarter.name} = startdate.{dd_cols.fraction_of_quarter.name},
174+
{dcm_cols.month_end_fraction_of_quarter.name} = enddate.{dd_cols.fraction_of_quarter.name},
175+
{dcm_cols.month_start_fraction_of_year.name} = startdate.{dd_cols.fraction_of_year.name},
176+
{dcm_cols.month_end_fraction_of_year.name} = enddate.{dd_cols.fraction_of_year.name},
177+
{dcm_cols.current_quarter_start.name} = startdate.{dd_cols.current_quarter_start.name},
178+
{dcm_cols.current_quarter_end.name} = startdate.{dd_cols.current_quarter_end.name},
179+
{dcm_cols.current_year_start.name} = startdate.{dd_cols.current_year_start.name},
180+
{dcm_cols.current_year_end.name} = startdate.{dd_cols.current_year_end.name},
181+
{dcm_cols.prior_month_start.name} = startdate.{dd_cols.prior_month_start.name},
182+
{dcm_cols.prior_month_end.name} = startdate.{dd_cols.prior_month_end.name},
183+
{dcm_cols.prior_quarter_start.name} = startdate.{dd_cols.prior_quarter_start.name},
184+
{dcm_cols.prior_quarter_end.name} = startdate.{dd_cols.prior_quarter_end.name},
185+
{dcm_cols.prior_year_start.name} = startdate.{dd_cols.prior_year_start.name},
186+
{dcm_cols.prior_year_end.name} = startdate.{dd_cols.prior_year_end.name},
187+
{dcm_cols.next_month_start.name} = startdate.{dd_cols.next_month_start.name},
188+
{dcm_cols.next_month_end.name} = startdate.{dd_cols.next_month_end.name},
189+
{dcm_cols.next_quarter_start.name} = startdate.{dd_cols.next_quarter_start.name},
190+
{dcm_cols.next_quarter_end.name} = startdate.{dd_cols.next_quarter_end.name},
191+
{dcm_cols.next_year_start.name} = startdate.{dd_cols.next_year_start.name},
192+
{dcm_cols.next_year_end.name} = startdate.{dd_cols.next_year_end.name},
193+
{dcm_cols.month_start_quarterly_burnup.name} = startdate.{dd_cols.quarterly_burnup.name},
194+
{dcm_cols.month_end_quarterly_burnup.name} = enddate.{dd_cols.quarterly_burnup.name},
195+
{dcm_cols.month_start_yearly_burnup.name} = startdate.{dd_cols.yearly_burnup.name},
196+
{dcm_cols.month_end_yearly_burnup.name} = enddate.{dd_cols.yearly_burnup.name},
197+
{holiday_colselect_str}
198+
FROM
199+
DistinctMonths AS base
200+
INNER JOIN {dd_conf.table_schema}.{dd_conf.table_name} AS startdate
201+
ON base.{dcm_cols.month_start_key.name} = startdate.{dd_cols.date_key.name}
202+
INNER JOIN {dd_conf.table_schema}.{dd_conf.table_name} AS enddate
203+
ON base.{dcm_cols.month_end_key.name} = enddate.{dd_cols.date_key.name};"""

src/awesome_date_dimension/_internal/tsql_templates/dim_fiscal_month_insert_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def dim_fiscal_month_insert_template(config: Config) -> str:
4545
GROUP BY {dd_cols.fiscal_current_month_start.name}, {dd_cols.fiscal_current_month_end.name}
4646
)
4747
48-
INSERT INTO dbo.DimFiscalMonth (
48+
INSERT INTO {dfm_conf.table_schema}.{dfm_conf.table_name} (
4949
{dfm_cols.month_start_key.name},
5050
{dfm_cols.month_end_key.name},
5151
{dfm_cols.month_start_date.name},

src/awesome_date_dimension/tsql.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
from pathlib import Path
44
from typing import Iterable
55

6+
from ._internal.tsql_templates.dim_calendar_month_insert_template import (
7+
dim_calendar_month_insert_template,
8+
)
69
from ._internal.tsql_templates.dim_date_insert_template import dim_date_insert_template
710
from ._internal.tsql_templates.dim_fiscal_month_insert_template import (
811
dim_fiscal_month_insert_template,
@@ -713,8 +716,13 @@ def _generate_dim_fiscal_month_build_scripts(
713716
def _generate_dim_calendar_month_build_scripts(
714717
self, file_no: int, base_path: Path
715718
) -> int:
716-
# raise NotImplementedError()
717-
pass
719+
scriptdef = dim_calendar_month_insert_template(self._config)
720+
file_path = base_path / TSQLGenerator._get_sql_filename(
721+
file_no, self._config.dim_calendar_month.table_name
722+
)
723+
TSQLGenerator._assert_filepath_available(file_path)
724+
file_path.write_text(scriptdef)
725+
return file_no + 1
718726

719727
def _generate_refresh_procs(self, folder_no: int) -> int:
720728
file_no = 0

0 commit comments

Comments
 (0)