Skip to content

Instantly share code, notes, and snippets.

@mgartner
Last active October 20, 2023 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgartner/403f68d0e6e62f38a0ee93966da4cf94 to your computer and use it in GitHub Desktop.
Save mgartner/403f68d0e6e62f38a0ee93966da4cf94 to your computer and use it in GitHub Desktop.
Examples in Postgres and CockroachDB showing that the st_union aggregate function does not ignore duplicates. The number of duplicate values input to the function affects the output.
-- 1 duplicate.
SELECT st_union(g) FROM (
SELECT '01060000000200000001030000000100000004000000000000000000444000000000000044400000000000003440000000000080464000000000008046400000000000003E4000000000000044400000000000004440010300000002000000060000000000000000003440000000000080414000000000000024400000000000003E40000000000000244000000000000024400000000000003E4000000000000014400000000000804640000000000000344000000000000034400000000000804140040000000000000000003E40000000000000344000000000000034400000000000002E40000000000000344000000000000039400000000000003E400000000000003440'::GEOMETRY
FROM generate_series(1, 1)
) gen(g);
-- st_union
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 01060000000200000001030000000100000004000000000000000000444000000000000044400000000000003440000000000080464000000000008046400000000000003E4000000000000044400000000000004440010300000002000000060000000000000000003440000000000080414000000000000024400000000000003E40000000000000244000000000000024400000000000003E4000000000000014400000000000804640000000000000344000000000000034400000000000804140040000000000000000003E40000000000000344000000000000034400000000000002E40000000000000344000000000000039400000000000003E400000000000003440
-- (1 row)
-- 2 duplicates.
SELECT st_union(g) FROM (
SELECT '01060000000200000001030000000100000004000000000000000000444000000000000044400000000000003440000000000080464000000000008046400000000000003E4000000000000044400000000000004440010300000002000000060000000000000000003440000000000080414000000000000024400000000000003E40000000000000244000000000000024400000000000003E4000000000000014400000000000804640000000000000344000000000000034400000000000804140040000000000000000003E40000000000000344000000000000034400000000000002E40000000000000344000000000000039400000000000003E400000000000003440'::GEOMETRY
FROM generate_series(1, 2)
) gen(g);
-- st_union
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 010600000002000000010300000001000000040000000000000000004440000000000000444000000000008046400000000000003E4000000000000034400000000000804640000000000000444000000000000044400103000000020000000600000000000000000034400000000000804140000000000080464000000000000034400000000000003E4000000000000014400000000000002440000000000000244000000000000024400000000000003E4000000000000034400000000000804140040000000000000000003E4000000000000034400000000000003440000000000000394000000000000034400000000000002E400000000000003E400000000000003440
-- (1 row)
-- 5 duplicates.
SELECT st_union(g) FROM (
SELECT '01060000000200000001030000000100000004000000000000000000444000000000000044400000000000003440000000000080464000000000008046400000000000003E4000000000000044400000000000004440010300000002000000060000000000000000003440000000000080414000000000000024400000000000003E40000000000000244000000000000024400000000000003E4000000000000014400000000000804640000000000000344000000000000034400000000000804140040000000000000000003E40000000000000344000000000000034400000000000002E40000000000000344000000000000039400000000000003E400000000000003440'::GEOMETRY
FROM generate_series(1, 5)
) gen(g);
-- st_union
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 010600000002000000010300000001000000040000000000000000004440000000000000444000000000008046400000000000003E400000000000003440000000000080464000000000000044400000000000004440010300000002000000060000000000000000002440000000000000244000000000000024400000000000003E4000000000000034400000000000804140000000000080464000000000000034400000000000003E40000000000000144000000000000024400000000000002440040000000000000000003E4000000000000034400000000000003440000000000000394000000000000034400000000000002E400000000000003E400000000000003440
-- (1 row)
-- 10 duplicates.
SELECT st_union(g) FROM (
SELECT '01060000000200000001030000000100000004000000000000000000444000000000000044400000000000003440000000000080464000000000008046400000000000003E4000000000000044400000000000004440010300000002000000060000000000000000003440000000000080414000000000000024400000000000003E40000000000000244000000000000024400000000000003E4000000000000014400000000000804640000000000000344000000000000034400000000000804140040000000000000000003E40000000000000344000000000000034400000000000002E40000000000000344000000000000039400000000000003E400000000000003440'::GEOMETRY
FROM generate_series(1, 10)
) gen(g);
-- st_union
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 01060000000200000001030000000100000004000000000000000000344000000000008046400000000000004440000000000000444000000000008046400000000000003E4000000000000034400000000000804640010300000002000000060000000000000000002440000000000000244000000000000024400000000000003E4000000000000034400000000000804140000000000080464000000000000034400000000000003E400000000000001440000000000000244000000000000024400400000000000000000034400000000000002E400000000000003E4000000000000034400000000000003440000000000000394000000000000034400000000000002E40
-- (1 row)
-- 1 duplicate.
SELECT st_union(g) FROM (
SELECT '01060000000200000001030000000100000004000000000000000000444000000000000044400000000000003440000000000080464000000000008046400000000000003E4000000000000044400000000000004440010300000002000000060000000000000000003440000000000080414000000000000024400000000000003E40000000000000244000000000000024400000000000003E4000000000000014400000000000804640000000000000344000000000000034400000000000804140040000000000000000003E40000000000000344000000000000034400000000000002E40000000000000344000000000000039400000000000003E400000000000003440'::GEOMETRY
FROM generate_series(1, 1)
) gen(g);
-- st_union
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 01060000000200000001030000000200000006000000000000000080464000000000000034400000000000003E4000000000000014400000000000002440000000000000244000000000000024400000000000003E400000000000003440000000000080414000000000008046400000000000003440040000000000000000003440000000000000394000000000000034400000000000002E400000000000003E400000000000003440000000000000344000000000000039400103000000010000000400000000000000008046400000000000003E40000000000000344000000000008046400000000000004440000000000000444000000000008046400000000000003E40
-- (1 row)
-- 2 duplicates.
SELECT st_union(g) FROM (
SELECT '01060000000200000001030000000100000004000000000000000000444000000000000044400000000000003440000000000080464000000000008046400000000000003E4000000000000044400000000000004440010300000002000000060000000000000000003440000000000080414000000000000024400000000000003E40000000000000244000000000000024400000000000003E4000000000000014400000000000804640000000000000344000000000000034400000000000804140040000000000000000003E40000000000000344000000000000034400000000000002E40000000000000344000000000000039400000000000003E400000000000003440'::GEOMETRY
FROM generate_series(1, 2)
) gen(g);
-- st_union
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 01060000000200000001030000000200000006000000000000000080464000000000000034400000000000003E4000000000000014400000000000002440000000000000244000000000000024400000000000003E400000000000003440000000000080414000000000008046400000000000003440040000000000000000003440000000000000394000000000000034400000000000002E400000000000003E400000000000003440000000000000344000000000000039400103000000010000000400000000000000008046400000000000003E40000000000000344000000000008046400000000000004440000000000000444000000000008046400000000000003E40
-- (1 row)
-- 5 duplicates.
SELECT st_union(g) FROM (
SELECT '01060000000200000001030000000100000004000000000000000000444000000000000044400000000000003440000000000080464000000000008046400000000000003E4000000000000044400000000000004440010300000002000000060000000000000000003440000000000080414000000000000024400000000000003E40000000000000244000000000000024400000000000003E4000000000000014400000000000804640000000000000344000000000000034400000000000804140040000000000000000003E40000000000000344000000000000034400000000000002E40000000000000344000000000000039400000000000003E400000000000003440'::GEOMETRY
FROM generate_series(1, 5)
) gen(g);
-- st_union
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 010600000002000000010300000002000000060000000000000000003E4000000000000014400000000000002440000000000000244000000000000024400000000000003E4000000000000034400000000000804140000000000080464000000000000034400000000000003E4000000000000014400400000000000000000034400000000000002E400000000000003E4000000000000034400000000000003440000000000000394000000000000034400000000000002E4001030000000100000004000000000000000000344000000000008046400000000000004440000000000000444000000000008046400000000000003E4000000000000034400000000000804640
-- (1 row)
-- 10 duplicates.
SELECT st_union(g) FROM (
SELECT '01060000000200000001030000000100000004000000000000000000444000000000000044400000000000003440000000000080464000000000008046400000000000003E4000000000000044400000000000004440010300000002000000060000000000000000003440000000000080414000000000000024400000000000003E40000000000000244000000000000024400000000000003E4000000000000014400000000000804640000000000000344000000000000034400000000000804140040000000000000000003E40000000000000344000000000000034400000000000002E40000000000000344000000000000039400000000000003E400000000000003440'::GEOMETRY
FROM generate_series(1, 5)
) gen(g);
-- st_union
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 010600000002000000010300000002000000060000000000000000003E4000000000000014400000000000002440000000000000244000000000000024400000000000003E4000000000000034400000000000804140000000000080464000000000000034400000000000003E4000000000000014400400000000000000000034400000000000002E400000000000003E4000000000000034400000000000003440000000000000394000000000000034400000000000002E4001030000000100000004000000000000000000344000000000008046400000000000004440000000000000444000000000008046400000000000003E4000000000000034400000000000804640
-- (1 row)
@mgartner
Copy link
Author

NOTE: It's easiest to tell the difference in the values by looking at the last ~3 characters of the return value.

@mgartner
Copy link
Author

Postgres version 14.9
Postgis version 3.3.4

CRDB version v23.2.0-alpha.4-dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment