@@ -59,6 +59,17 @@ def transform_null_int(value: int | None) -> float:
5959DERIVED = "derived"
6060COLUMN_MAP = "column_map"
6161
62+ ALIAS_CLM = "clm"
63+ ALIAS_DCMTN = "dcmtn"
64+ ALIAS_SGNTR = "sgntr"
65+ ALIAS_LINE = "line"
66+ ALIAS_PROCEDURE = "prod"
67+ ALIAS_INSTNL = "instnl"
68+ ALIAS_PRFNL = "prfnl"
69+ ALIAS_VAL = "val"
70+ ALIAS_HSTRY = "hstry"
71+ ALIAS_XREF = "xref"
72+
6273
6374class IdrBaseModel (BaseModel ):
6475 @staticmethod
@@ -160,7 +171,8 @@ def insert_keys(cls) -> list[str]:
160171
161172
162173class IdrBeneficiary (IdrBaseModel ):
163- bene_sk : Annotated [int , {PRIMARY_KEY : True }]
174+ # columns from V2_MDCR_BENE_HSTRY
175+ bene_sk : Annotated [int , {PRIMARY_KEY : True , ALIAS : ALIAS_HSTRY }]
164176 bene_xref_efctv_sk : int
165177 bene_mbi_id : Annotated [str , BeforeValidator (transform_null_string )]
166178 bene_1st_name : str
@@ -184,9 +196,26 @@ class IdrBeneficiary(IdrBaseModel):
184196 idr_ltst_trans_flg : Annotated [str , BeforeValidator (transform_null_string )]
185197 idr_trans_efctv_ts : Annotated [datetime , {PRIMARY_KEY : True }]
186198 idr_trans_obslt_ts : datetime
187- idr_insrt_ts : Annotated [datetime , {BATCH_TIMESTAMP : True }]
188- idr_updt_ts : Annotated [
189- datetime , {UPDATE_TIMESTAMP : True }, BeforeValidator (transform_null_date_to_min )
199+ idr_insrt_ts_bene : Annotated [
200+ datetime , {BATCH_TIMESTAMP : True , ALIAS : ALIAS_HSTRY , COLUMN_MAP : "idr_insrt_ts" }
201+ ]
202+ idr_updt_ts_bene : Annotated [
203+ datetime ,
204+ {UPDATE_TIMESTAMP : True , ALIAS : ALIAS_HSTRY , COLUMN_MAP : "idr_updt_ts" },
205+ BeforeValidator (transform_null_date_to_min ),
206+ ]
207+ # columns from V2_MDCR_BENE_XREF
208+ bene_kill_cred_cd : Annotated [str , BeforeValidator (transform_default_string )]
209+ src_rec_updt_ts : Annotated [datetime , BeforeValidator (transform_null_date_to_min )]
210+ idr_insrt_ts_xref : Annotated [
211+ datetime ,
212+ {BATCH_TIMESTAMP : True , ALIAS : ALIAS_XREF , COLUMN_MAP : "idr_insrt_ts" },
213+ BeforeValidator (transform_null_date_to_min ),
214+ ]
215+ idr_updt_ts_xref : Annotated [
216+ datetime ,
217+ {UPDATE_TIMESTAMP : True , ALIAS : ALIAS_XREF , COLUMN_MAP : "idr_updt_ts" },
218+ BeforeValidator (transform_null_date_to_min ),
190219 ]
191220
192221 @staticmethod
@@ -199,11 +228,47 @@ def computed_keys() -> list[str]:
199228
200229 @staticmethod
201230 def _current_fetch_query (start_time : datetime ) -> str : # noqa: ARG004
202- return """
203- SELECT {COLUMNS}
204- FROM cms_vdm_view_mdcr_prd.v2_mdcr_bene_hstry
205- {WHERE_CLAUSE}
206- {ORDER_BY}
231+ hstry = ALIAS_HSTRY
232+ xref = ALIAS_XREF
233+ # There can be multiple xref records for the same bene_sk/bene_ref_sk combo
234+ # so we need to find the most recent one based on src_rec_updt_ts.
235+
236+ # Unlike idr_updt_ts, src_rec_updt_ts will be set to the created timestamp
237+ # if no update has been applied. Therefore, we can just check the updated timestamp
238+ # without caring about the created timestamp.
239+
240+ # There can also be duplicate values with the same idr_insrt_ts, so we have to rely on
241+ # src_rec_insrt_ts/src_rec_updt_ts for this.
242+ return f"""
243+ WITH ordered_xref AS (
244+ SELECT bene_sk, bene_xref_sk, ROW_NUMBER() OVER (
245+ PARTITION BY bene_sk, bene_xref_sk
246+ ORDER BY src_rec_updt_ts DESC
247+ ) AS row_order
248+ FROM cms_vdm_view_mdcr_prd.v2_mdcr_bene_xref
249+ ),
250+ current_xref AS (
251+ SELECT
252+ ox.bene_sk,
253+ ox.bene_xref_sk,
254+ bx.bene_kill_cred_cd,
255+ bx.src_rec_updt_ts,
256+ bx.idr_insrt_ts,
257+ bx.idr_updt_ts
258+ FROM ordered_xref ox
259+ JOIN cms_vdm_view_mdcr_prd.v2_mdcr_bene_xref bx
260+ ON bx.bene_sk = ox.bene_sk AND bx.bene_xref_sk = ox.bene_xref_sk
261+ WHERE ox.row_order = 1
262+ )
263+ SELECT {{COLUMNS}}
264+ FROM cms_vdm_view_mdcr_prd.v2_mdcr_bene_hstry { hstry }
265+ -- NOTE: the join condition is intentionally inverted here
266+ -- In the xref table, the bene_sk and bene_xref_sk fields are mirrored
267+ LEFT JOIN current_xref { xref }
268+ ON { xref } .bene_sk = { hstry } .bene_xref_sk
269+ AND { xref } .bene_xref_sk = { hstry } .bene_sk
270+ {{WHERE_CLAUSE}}
271+ {{ORDER_BY}}
207272 """
208273
209274
@@ -344,33 +409,6 @@ def _current_fetch_query(start_time: datetime) -> str: # noqa: ARG004
344409 """
345410
346411
347- class IdrBeneficiaryXref (IdrBaseModel ):
348- bene_hicn_num : Annotated [str , {PRIMARY_KEY : True }]
349- bene_sk : Annotated [int , {PRIMARY_KEY : True }]
350- bene_xref_sk : int
351- bene_kill_cred_cd : Annotated [str , BeforeValidator (transform_default_string )]
352- idr_trans_efctv_ts : datetime
353- idr_trans_obslt_ts : datetime
354- idr_insrt_ts : datetime
355- idr_updt_ts : Annotated [
356- datetime , {UPDATE_TIMESTAMP : True }, BeforeValidator (transform_null_date_to_min )
357- ]
358- src_rec_crte_ts : Annotated [datetime , {PRIMARY_KEY : True , BATCH_TIMESTAMP : True }]
359-
360- @staticmethod
361- def table () -> str :
362- return "idr.beneficiary_xref"
363-
364- @staticmethod
365- def _current_fetch_query (start_time : datetime ) -> str : # noqa: ARG004
366- return """
367- SELECT {COLUMNS}
368- FROM cms_vdm_view_mdcr_prd.v2_mdcr_bene_xref
369- {WHERE_CLAUSE}
370- {ORDER_BY}
371- """
372-
373-
374412class IdrElectionPeriodUsage (IdrBaseModel ):
375413 bene_sk : Annotated [int , {PRIMARY_KEY : True }]
376414 cntrct_pbp_sk : Annotated [int , {PRIMARY_KEY : True }]
@@ -422,16 +460,6 @@ def _current_fetch_query(start_time: datetime) -> str: # noqa: ARG004
422460 """
423461
424462
425- ALIAS_CLM = "clm"
426- ALIAS_DCMTN = "dcmtn"
427- ALIAS_SGNTR = "sgntr"
428- ALIAS_LINE = "line"
429- ALIAS_PROCEDURE = "prod"
430- ALIAS_INSTNL = "instnl"
431- ALIAS_PRFNL = "prfnl"
432- ALIAS_VAL = "val"
433-
434-
435463def claim_type_clause (start_time : datetime ) -> str : # noqa: ARG001
436464 latest_claims_env = "IDR_LATEST_CLAIMS"
437465 if latest_claims_env in os .environ and os .environ [latest_claims_env ] in ("1" , "true" ):
0 commit comments