From fca5d58a1ec6c3f4cbd54edd0c058edf893f7637 Mon Sep 17 00:00:00 2001 From: Ian Rose Date: Fri, 8 Sep 2023 16:31:48 -0700 Subject: [PATCH] Guard against nulls in max_by so that we don't accidentally null out all of our columns in the deduplication step. --- transform/macros/spatial_join_with_deduplication.sql | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/transform/macros/spatial_join_with_deduplication.sql b/transform/macros/spatial_join_with_deduplication.sql index 504a4b1f..80637b09 100644 --- a/transform/macros/spatial_join_with_deduplication.sql +++ b/transform/macros/spatial_join_with_deduplication.sql @@ -46,10 +46,14 @@ with {{ prefix }}_left_model_with_id as ( -- by _tmp_sjoin_id, so we can just choose any_value. any_value({{ left_geom }}) as {{ left_geom }}, {% for lcol in left_cols -%} - max_by({{ lcol }}, _tmp_sjoin_intersection) as {{ lcol }}, + -- max_by returns null if all the values in a group are null. So if we have a left + -- join, we need to guard against nulls with a coalesce to return the single value + max_by({{ lcol }}, coalesce(_tmp_sjoin_intersection, 1.0)) as {{ lcol }}, {% endfor -%} {% for rcol in right_cols -%} - max_by({{ rcol }}, _tmp_sjoin_intersection) as {{ rcol }}{{ "," if not loop.last }} + -- max_by returns null if all the values in a group are null. So if we have a left + -- join, we need to guard against nulls with a coalesce to return the single value + max_by({{ rcol }}, coalesce(_tmp_sjoin_intersection, 1.0)) as {{ rcol }}{{ "," if not loop.last }} {% endfor -%} from {{ prefix }}_joined group by _tmp_sjoin_id