@@ -978,7 +978,10 @@ def test_scd_type_2_by_time(make_mocked_engine_adapter: t.Callable):
978978 source_table = t .cast (
979979 exp .Select , parse_one ("SELECT id, name, price, test_updated_at FROM source" )
980980 ),
981- unique_key = [exp .func ("COALESCE" , "id" , "''" )],
981+ unique_key = [
982+ parse_one ("""COALESCE("id", '') || '|' || COALESCE("name", '')""" ),
983+ parse_one ("""COALESCE("name", '')""" ),
984+ ],
982985 valid_from_name = "test_valid_from" ,
983986 valid_to_name = "test_valid_to" ,
984987 updated_at_name = "test_updated_at" ,
@@ -999,7 +1002,7 @@ def test_scd_type_2_by_time(make_mocked_engine_adapter: t.Callable):
9991002 """
10001003CREATE OR REPLACE TABLE "target" AS
10011004WITH "source" AS (
1002- SELECT DISTINCT ON (COALESCE("id", ''))
1005+ SELECT DISTINCT ON (COALESCE("id", '') || '|' || COALESCE("name", ''), COALESCE("name", '') )
10031006 TRUE AS "_exists",
10041007 "id",
10051008 "name",
@@ -1045,17 +1048,24 @@ def test_scd_type_2_by_time(make_mocked_engine_adapter: t.Callable):
10451048 "static"."test_valid_to"
10461049 FROM "static"
10471050 LEFT JOIN "latest"
1048- ON COALESCE("static"."id", '') = COALESCE("latest"."id", '')
1051+ ON (
1052+ COALESCE("static"."id", '') || '|' || COALESCE("static"."name", '')
1053+ ) = (
1054+ COALESCE("latest"."id", '') || '|' || COALESCE("latest"."name", '')
1055+ )
1056+ AND COALESCE("static"."name", '') = COALESCE("latest"."name", '')
10491057 WHERE
10501058 "latest"."test_valid_to" IS NULL
10511059), "latest_deleted" AS (
10521060 SELECT
10531061 TRUE AS "_exists",
1054- COALESCE("id", '') AS "_key0",
1062+ COALESCE("id", '') || '|' || COALESCE("name", '') AS "_key0",
1063+ COALESCE("name", '') AS "_key1",
10551064 MAX("test_valid_to") AS "test_valid_to"
10561065 FROM "deleted"
10571066 GROUP BY
1058- COALESCE("id", '')
1067+ COALESCE("id", '') || '|' || COALESCE("name", ''),
1068+ COALESCE("name", '')
10591069), "joined" AS (
10601070 SELECT
10611071 "source"."_exists",
@@ -1071,7 +1081,12 @@ def test_scd_type_2_by_time(make_mocked_engine_adapter: t.Callable):
10711081 "source"."test_updated_at" AS "test_updated_at"
10721082 FROM "latest"
10731083 LEFT JOIN "source"
1074- ON COALESCE("latest"."id", '') = COALESCE("source"."id", '')
1084+ ON (
1085+ COALESCE("latest"."id", '') || '|' || COALESCE("latest"."name", '')
1086+ ) = (
1087+ COALESCE("source"."id", '') || '|' || COALESCE("source"."name", '')
1088+ )
1089+ AND COALESCE("latest"."name", '') = COALESCE("source"."name", '')
10751090 UNION
10761091 SELECT
10771092 "source"."_exists",
@@ -1087,7 +1102,12 @@ def test_scd_type_2_by_time(make_mocked_engine_adapter: t.Callable):
10871102 "source"."test_updated_at" AS "test_updated_at"
10881103 FROM "latest"
10891104 RIGHT JOIN "source"
1090- ON COALESCE("latest"."id", '') = COALESCE("source"."id", '')
1105+ ON (
1106+ COALESCE("latest"."id", '') || '|' || COALESCE("latest"."name", '')
1107+ ) = (
1108+ COALESCE("source"."id", '') || '|' || COALESCE("source"."name", '')
1109+ )
1110+ AND COALESCE("latest"."name", '') = COALESCE("source"."name", '')
10911111), "updated_rows" AS (
10921112 SELECT
10931113 COALESCE("joined"."t_id", "joined"."id") AS "id",
@@ -1114,7 +1134,10 @@ def test_scd_type_2_by_time(make_mocked_engine_adapter: t.Callable):
11141134 END AS "test_valid_to"
11151135 FROM "joined"
11161136 LEFT JOIN "latest_deleted"
1117- ON COALESCE("joined"."id", '') = "latest_deleted"."_key0"
1137+ ON (
1138+ COALESCE("joined"."id", '') || '|' || COALESCE("joined"."name", '')
1139+ ) = "latest_deleted"."_key0"
1140+ AND COALESCE("joined"."name", '') = "latest_deleted"."_key1"
11181141), "inserted_rows" AS (
11191142 SELECT
11201143 "id",
0 commit comments