Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

View file

@ -0,0 +1 @@
create type set as VARCHAR[];

View file

@ -0,0 +1,17 @@
create or replace function to_set(lst) as (
list_sort(list_distinct(lst))
);
create or replace function list_update(lst, i, value) as (
lst[1:i-1] || [value] || lst[i+1:]
);
# This will work even if set1 and set2 are just lists
create or replace function set_intersection(set1, set2) as (
list_sort(list_intersect(set1, set2))
);
# This will work even if set1 and set2 are just lists
create or replace function set_union(set1, set2) as (
to_set( ( set1 || set2 ) )
);

View file

@ -0,0 +1,61 @@
# If listofsets[j] can be merged with listofsets[i]
# then set the former to [] and the latter to the merger
create or replace function merge(listofsets, i, j) as (
if (i=j,
listofsets,
case when list_has_any(listofsets[i]::set, listofsets[j]::set) -- casts needed here
then list_update(
list_update(listofsets,
i,
set_union(listofsets[i], listofsets[j])),
j,
[])
else listofsets
end)
);
create or replace function merge_i(listofsets, i) as table (
with recursive cte(j,s) as (
select 1, listofsets::SET[] -- advisable!!
union all
select j+1, merge(s, i, j)
from cte
where j <= length(listofsets)
)
select last(s order by j) as s from cte
);
# Compute the consolidation as a set
# Note: as of DuckDB V1.1, the CTE here must be given a different name from the one in merge_i
create or replace function consolidation(listofsets) as (
with recursive cte1(i,s) as (
select 1, listofsets::SET[]
union all
select i+1, (from merge_i(s, i) limit 1) as s
from cte1
where i <= length(listofsets)
)
select to_set(
list_transform(
list_filter(last(s order by i), x -> length(x)>0),
x -> to_set(x))) as consolidation from cte1
);
# Verify that the consolidation of x is equal to to_set(y),
# so be sure that each of the elements of y is in fact a set.
create or replace function verify(x,y) as (
select consolidation(x) = to_set(y)
);
.print Example 1:
select verify( [ ['A','B'], ['C','D']], [ ['A','B'], ['C','D']] ) as "Example 1";
.print Example 2:
select verify( [ ['A','B'], ['B','D'] ], [['A', 'B','D']]) as "Example 2";
.print Example 3:
select verify( [['A','B'], ['C','D'], ['D','B']], [['A','B','C','D']]) as "Example 3";
.print Example 4:
select verify( [['H','I','K'], ['A','B'], ['C','D'], ['D','B'], ['F','G','H'] ],
[['A', 'B', 'C', 'D'], ['F', 'G', 'H', 'I', 'K']] ) as "Example 4";

View file

@ -0,0 +1,38 @@
require "map"
require "table2"
local fmt = require "fmt"
local function consolidate_sets(sets)
local size = #sets
local consolidated = table.rep(size, false)
for i = 1, size - 1 do
if !consolidated[i] then
repeat
local intersects = 0
for j = i + 1, size do
if !consolidated[j] then
if !sets[i]:intersect(sets[j]):empty() then
sets[i]:merge(sets[j])
consolidated[j] = true
++intersects
end
end
end
until intersects == 0
end
end
return range(1, size):filter(|i| -> !consolidated[i]):reorder():map(|i| -> sets[i])
end
local unconsolidated_sets = {
{set.of("A", "B"), set.of("C", "D")},
{set.of("A", "B"), set.of("B", "D")},
{set.of("A", "B"), set.of("C", "D"), set.of("D", "B")},
{set.of("H", "I", "K"), set.of("A", "B"), set.of("C", "D"),
set.of("D", "B"), set.of("F", "G", "H")}
}
for unconsolidated_sets as sets do
fmt.print("Unconsolidated: %s", fmt.swrite(sets))
fmt.print("Consolidated : %s", fmt.swrite(consolidate_sets(sets)))
print()
end

View file

@ -34,5 +34,5 @@ var unconsolidatedSets = [
]
for (sets in unconsolidatedSets) {
System.print("Unconsolidated: %(sets)")
System.print("Cosolidated : %(consolidateSets.call(sets))\n")
System.print("Consolidated : %(consolidateSets.call(sets))\n")
}