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,4 @@
set variable base= { 'name': 'Rocket Skates', 'price': 12.75, 'color':'yellow'}::JSON;
set variable update = { 'price': 15.25, 'color': 'red', 'year': 1974 }::JSON;
select json_merge_patch(getvariable('base'),getvariable('update')) as add;

View file

@ -0,0 +1,11 @@
create or replace function map_merge_patch(a,b) as (
with keys_t as (
select (map_keys(a) + map_keys(b)).list_distinct() as keys
),
values_t as (
select list_transform(keys, key -> coalesce(b[key][1],a[key][1])) as values
from keys_t)
select MAP(keys, values) from keys_t, values_t
);
select map_merge_patch( MAP([1,2],[1,2]), MAP([1,3],[10,30])) as add;

View file

@ -0,0 +1,11 @@
create or replace function struct_merge_patch(base, update) as (
<syntaxhighlight lang="sql">
with cte as (
from (select unnest(update))
positional join
(select COLUMNS(x -> x not in json_keys(update::JSON)) from (select unnest(base)))
)
select cte from cte
);
select struct_merge_patch( getvariable('base'), getvariable('update')) as merged;

View file

@ -0,0 +1,94 @@
module assoc_array_mod
implicit none
private
public :: KeyValue, merge_assoc_arrays
! Derived type for key-value pairs
type :: KeyValue
character(len=:), allocatable :: key
character(len=:), allocatable :: value
end type KeyValue
contains
! Function to merge two associative arrays
function merge_assoc_arrays(base, update) result(merged)
type(KeyValue), intent(in) :: base(:), update(:)
type(KeyValue), allocatable :: merged(:)
type(KeyValue), allocatable :: temp(:)
integer :: base_size, update_size, i, j, k, merged_size
logical :: key_exists
base_size = size(base)
update_size = size(update)
! Allocate temporary array to hold all possible keys (worst case: no overlap)
allocate(temp(base_size + update_size))
merged_size = 0
! Add all keys from update array (update takes precedence)
do i = 1, update_size
merged_size = merged_size + 1
temp(merged_size)%key = update(i)%key
temp(merged_size)%value = update(i)%value
end do
! Add keys from base array that are not in update
do i = 1, base_size
key_exists = .false.
do j = 1, update_size
if (base(i)%key == update(j)%key) then
key_exists = .true.
exit
end if
end do
if (.not. key_exists) then
merged_size = merged_size + 1
temp(merged_size)%key = base(i)%key
temp(merged_size)%value = base(i)%value
end if
end do
! Allocate merged array with exact size
allocate(merged(merged_size))
do i = 1, merged_size
merged(i)%key = temp(i)%key
merged(i)%value = temp(i)%value
end do
! Clean up temporary array
deallocate(temp)
end function merge_assoc_arrays
end module assoc_array_mod
program main
use assoc_array_mod
implicit none
type(KeyValue), allocatable :: base(:), update(:), merged(:)
integer :: i
! Initialize base associative array
allocate(base(3))
base(1) = KeyValue("name", "Rocket Skates")
base(2) = KeyValue("price", "12.75")
base(3) = KeyValue("color", "yellow")
! Initialize update associative array
allocate(update(3))
update(1) = KeyValue("price", "15.25")
update(2) = KeyValue("color", "red")
update(3) = KeyValue("year", "1974")
! Merge the arrays
merged = merge_assoc_arrays(base, update)
! Print the merged array
print *, "Merged associative array:"
do i = 1, size(merged)
print *, "Key: ", merged(i)%key, ", Value: ", merged(i)%value
end do
! Clean up
deallocate(base, update, merged)
end program main

View file

@ -0,0 +1,11 @@
local function merge_maps(m1, m2)
local m3 = {}
for m1:keys() as key do m3[key] = m1[key] end
for m2:keys() as key do m3[key] = m2[key] end
return m3
end
local base = {name = "Rocket Skates", price = 12.75, color = "yellow"}
local update = {price = 15.25, color = "red", year = 1974}
local merged = merge_maps(base, update)
for k, v in merged do print(k, v) end

View file

@ -1,33 +1,78 @@
/*REXX program merges two associative arrays (requiring an external list of indices). */
$.= /*define default value(s) for arrays. */
@.wAAn= 21; @.wKey= 7; @.wVal= 7 /*max widths of: AAname, keys, values.*/
call defAA 'base', "name Rocket Skates", 'price 12.75', "color yellow"
call defAA 'update', "price 15.25", "color red", 'year 1974'
call show 'base'
call show 'update'
call show 'new'
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
defAA: procedure expose $. @.; parse arg AAn; new= 'new' /*get AA name; set NEW.*/
do j=2 to arg(); parse value arg(j) with key val /*obtain key and value.*/
$.AAn.key= val /*assign a value to a key for AAn. */
if wordpos(key, $.AAN.?keys)==0 then $.AAn.?keys= $.AAn.?keys key
/* [↑] add to key list if not in list.*/
$.new.key= val /*assign a value to a key for "new".*/
if wordpos(key, $.new.?keys)==0 then $.new.?keys= $.new.?keys key
/* [↑] add to key list if not in list.*/
@.wKey= max(@.wKey, length(key) ) /*find max width of a name of a key. */
@.wVal= max(@.wVal, length(val) ) /* " " " " " " " " value.*/
@.wAA = max(@.wAAn, length(AAn) ) /* " " " " " " " array.*/
end /*j*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: procedure expose $. @.; parse arg AAn; say; _= '' /*set title char.*/
do j=1 for words($.AAn.?keys) /*process keys. */
if j==1 then say center('associate array', @.wAAn, _) ,
center("key" , @.wKey, _) ,
center('value' , @.wVal + 2, _)
key= word($.AAn.?keys, j) /*get the name of a key.*/
say center(AAn, @.wAAn) right(key, @.wKey) $.AAn.key /*show some information.*/
end /*j*/
return
-- 8 Aug 2025
include Settings
say 'ASSOCIATIVE ARRAY: MERGE'
say version
say
call CreateBase
call ShowBase 'Original'
call CreateUpdate
call ShowUpdate
call MergeBaseUpdate
call ShowBase 'Merged'
exit
CreateBase:
base=''; base.=''
call ProcBase 'name','Rocket Skates'
call ProcBase 'price',12.75
call ProcBase 'color','Yellow'
return
ShowBase:
parse arg xx
sep=Copies('-',20)
say 'Base array' xx
say sep
say 'Key value'
say sep
do i = 1 to Words(base)
key=Word(base,i)
say Left(key,6) base.key
end
say sep
say
return
CreateUpdate:
upda=''; upda.=''
call ProcUpdate 'price',15.25
call ProcUpdate 'color','Red'
call ProcUpdate 'year',1974
return
ShowUpdate:
say 'Update array'
say sep
say 'Key value'
say sep
do i = 1 to Words(upda)
key=Word(upda,i)
say Left(key,6) upda.key
end
say sep
say
return
MergeBaseUpdate:
do w = 1 to Words(upda)
key=Word(upda,w); val=upda.key
call ProcBase key,val
end
return
ProcBase:
parse arg k,v
if WordPos(k,base) = 0 then
base=base k
base.k=v
return
ProcUpdate:
parse arg k,v
if WordPos(k,upda) = 0 then
upda=upda k
upda.k=v
return
include Abend