Compare commits
No commits in common. "8bba1d95d905712a45155b51ba7a8e250b5e3940" and "60d30b8c92e25338d04b6e82d020b9dcf9c83da8" have entirely different histories.
8bba1d95d9
...
60d30b8c92
@ -1,5 +1,13 @@
|
|||||||
|
/*
|
||||||
|
first get distinct target json values
|
||||||
|
then apply regex
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
WITH
|
WITH
|
||||||
--------------------apply regex operations to transactions-----------------------------------------------------------------------------------
|
|
||||||
|
--------------------apply regex operations to transactions---------------------------------------------------------------------------------
|
||||||
|
|
||||||
rx AS (
|
rx AS (
|
||||||
SELECT
|
SELECT
|
||||||
t.srce,
|
t.srce,
|
||||||
@ -7,26 +15,28 @@ SELECT
|
|||||||
t.rec,
|
t.rec,
|
||||||
m.target,
|
m.target,
|
||||||
m.seq,
|
m.seq,
|
||||||
regex->'regex'->>'function' regex_function,
|
regex->>'function' regex_function,
|
||||||
defn.v ->> 'field' result_key_name,
|
e.v ->> 'field' result_key_name,
|
||||||
defn.v ->> 'key' target_json_path,
|
e.v ->> 'key' target_json_path,
|
||||||
defn.v ->> 'flag' regex_options_flag,
|
e.v ->> 'flag' regex_options_flag,
|
||||||
defn.v->>'map' map_intention,
|
e.v->>'map' map_intention,
|
||||||
defn.v->>'retain' retain_result,
|
e.v->>'retain' retain_result,
|
||||||
defn.v->>'regex' regex_expression,
|
e.v->>'regex' regex_expression,
|
||||||
defn.rn target_item_number,
|
e.rn target_item_number,
|
||||||
COALESCE(mt.rn,rp.rn,1) result_number,
|
COALESCE(mt.rn,rp.rn,1) result_number,
|
||||||
mt.mt rx_match,
|
mt.mt rx_match,
|
||||||
rp.rp rx_replace,
|
rp.rp rx_replace,
|
||||||
CASE defn.v->>'map'
|
--------------------------json key name assigned to return value-----------------------------------------------------------------------
|
||||||
|
CASE e.v->>'map'
|
||||||
WHEN 'y' THEN
|
WHEN 'y' THEN
|
||||||
defn.v->>'field'
|
e.v->>'field'
|
||||||
ELSE
|
ELSE
|
||||||
null
|
null
|
||||||
END map_key,
|
END map_key,
|
||||||
CASE defn.v->>'map'
|
--------------------------json value resulting from regular expression-----------------------------------------------------------------
|
||||||
|
CASE e.v->>'map'
|
||||||
WHEN 'y' THEN
|
WHEN 'y' THEN
|
||||||
CASE regex->'regex'->>'function'
|
CASE regex->>'function'
|
||||||
WHEN 'extract' THEN
|
WHEN 'extract' THEN
|
||||||
CASE WHEN array_upper(mt.mt,1)=1
|
CASE WHEN array_upper(mt.mt,1)=1
|
||||||
THEN to_json(mt.mt[1])
|
THEN to_json(mt.mt[1])
|
||||||
@ -40,15 +50,17 @@ SELECT
|
|||||||
ELSE
|
ELSE
|
||||||
NULL
|
NULL
|
||||||
END map_val,
|
END map_val,
|
||||||
CASE defn.v->>'retain'
|
--------------------------flag for if retruned regex result is stored as a new part of the final json output---------------------------
|
||||||
|
CASE e.v->>'retain'
|
||||||
WHEN 'y' THEN
|
WHEN 'y' THEN
|
||||||
defn.v->>'field'
|
e.v->>'field'
|
||||||
ELSE
|
ELSE
|
||||||
NULL
|
NULL
|
||||||
END retain_key,
|
END retain_key,
|
||||||
CASE defn.v->>'retain'
|
--------------------------push regex result into json object---------------------------------------------------------------------------
|
||||||
|
CASE e.v->>'retain'
|
||||||
WHEN 'y' THEN
|
WHEN 'y' THEN
|
||||||
CASE regex->'regex'->>'function'
|
CASE regex->>'function'
|
||||||
WHEN 'extract' THEN
|
WHEN 'extract' THEN
|
||||||
CASE WHEN array_upper(mt.mt,1)=1
|
CASE WHEN array_upper(mt.mt,1)=1
|
||||||
THEN to_json(trim(mt.mt[1]))
|
THEN to_json(trim(mt.mt[1]))
|
||||||
@ -66,32 +78,37 @@ FROM
|
|||||||
--------------------------start with all regex maps------------------------------------------------------------------------------------
|
--------------------------start with all regex maps------------------------------------------------------------------------------------
|
||||||
tps.map_rm m
|
tps.map_rm m
|
||||||
--------------------------isolate matching basis to limit map to only look at certain json---------------------------------------------
|
--------------------------isolate matching basis to limit map to only look at certain json---------------------------------------------
|
||||||
LEFT JOIN LATERAL jsonb_array_elements(m.regex->'regex'->'where') wh(v) ON TRUE
|
JOIN LATERAL jsonb_array_elements(m.regex->'where') w(v) ON TRUE
|
||||||
|
--------------------------break out array of regluar expressions in the map------------------------------------------------------------
|
||||||
|
JOIN LATERAL jsonb_array_elements(m.regex->'defn') WITH ORDINALITY e(v, rn) ON true
|
||||||
--------------------------join to main transaction table but only certain key/values are included--------------------------------------
|
--------------------------join to main transaction table but only certain key/values are included--------------------------------------
|
||||||
INNER JOIN tps.trans t ON
|
INNER JOIN tps.trans t ON
|
||||||
t.srce = m.srce AND
|
t.srce = m.srce AND
|
||||||
t.rec @> wh.v
|
t.rec @> w.v
|
||||||
--------------------------break out array of regluar expressions in the map------------------------------------------------------------
|
|
||||||
LEFT JOIN LATERAL jsonb_array_elements(m.regex->'regex'->'defn') WITH ORDINALITY defn(v, rn) ON true
|
|
||||||
--------------------------each regex references a path to the target value, extract the target from the reference and do regex---------
|
--------------------------each regex references a path to the target value, extract the target from the reference and do regex---------
|
||||||
LEFT JOIN LATERAL regexp_matches(t.rec #>> ((defn.v ->> 'key')::text[]), defn.v ->> 'regex'::text,COALESCE(defn.v ->> 'flag','')) WITH ORDINALITY mt(mt, rn) ON
|
LEFT JOIN LATERAL regexp_matches(t.rec #>> ((e.v ->> 'key')::text[]), e.v ->> 'regex'::text,COALESCE(e.v ->> 'flag','')) WITH ORDINALITY mt(mt, rn) ON
|
||||||
m.regex->'regex'->>'function' = 'extract'
|
m.regex->>'function' = 'extract'
|
||||||
--------------------------same as above but for a replacement type function------------------------------------------------------------
|
--------------------------same as above but for a replacement type function------------------------------------------------------------
|
||||||
LEFT JOIN LATERAL regexp_replace(t.rec #>> ((defn.v ->> 'key')::text[]), defn.v ->> 'regex'::text, defn.v ->> 'replace'::text,defn.v ->> 'flag') WITH ORDINALITY rp(rp, rn) ON
|
LEFT JOIN LATERAL regexp_replace(t.rec #>> ((e.v ->> 'key')::text[]), e.v ->> 'regex'::text, e.v ->> 'replace'::text,e.v ->> 'flag') WITH ORDINALITY rp(rp, rn) ON
|
||||||
m.regex->'regex'->>'function' = 'replace'
|
m.regex->>'function' = 'replace'
|
||||||
WHERE
|
WHERE
|
||||||
--t.allj IS NULL
|
--t.allj IS NULL
|
||||||
t.srce = 'dcard'
|
--t.srce = 'PNCC' AND
|
||||||
|
e.v @> '{"map":"y"}'::jsonb
|
||||||
--rec @> '{"Transaction":"ACH Credits","Transaction":"ACH Debits"}'
|
--rec @> '{"Transaction":"ACH Credits","Transaction":"ACH Debits"}'
|
||||||
--rec @> '{"Description":"CHECK 93013270 086129935"}'::jsonb
|
--rec @> '{"Description":"CHECK 93013270 086129935"}'::jsonb
|
||||||
|
/*
|
||||||
ORDER BY
|
ORDER BY
|
||||||
t.id DESC,
|
t.id DESC,
|
||||||
m.target,
|
m.target,
|
||||||
defn.rn,
|
e.rn,
|
||||||
COALESCE(mt.rn,rp.rn,1)
|
COALESCE(mt.rn,rp.rn,1)
|
||||||
|
*/
|
||||||
)
|
)
|
||||||
--SELECT * FROM rx
|
|
||||||
--SELECT count(*) FROM rx LIMIT 100
|
--SELECT * FROM rx LIMIT 100
|
||||||
|
|
||||||
|
|
||||||
, agg_to_target_items AS (
|
, agg_to_target_items AS (
|
||||||
SELECT
|
SELECT
|
||||||
srce
|
srce
|
||||||
@ -146,6 +163,10 @@ GROUP BY
|
|||||||
,map_key
|
,map_key
|
||||||
,retain_key
|
,retain_key
|
||||||
)
|
)
|
||||||
|
|
||||||
|
--SELECT * FROM agg_to_target_items LIMIT 100
|
||||||
|
|
||||||
|
|
||||||
, agg_to_target AS (
|
, agg_to_target AS (
|
||||||
SELECT
|
SELECT
|
||||||
srce
|
srce
|
||||||
@ -163,27 +184,62 @@ GROUP BY
|
|||||||
,target
|
,target
|
||||||
,seq
|
,seq
|
||||||
,map_intention
|
,map_intention
|
||||||
ORDER BY
|
|
||||||
id
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
, agg_to_ret AS (
|
||||||
|
SELECT
|
||||||
|
srce
|
||||||
|
,target
|
||||||
|
,seq
|
||||||
|
,map_intention
|
||||||
|
,map_val
|
||||||
|
,retain_val
|
||||||
|
,count(*) "count"
|
||||||
|
FROM
|
||||||
|
agg_to_target
|
||||||
|
GROUP BY
|
||||||
|
srce
|
||||||
|
,target
|
||||||
|
,seq
|
||||||
|
,map_intention
|
||||||
|
,map_val
|
||||||
|
,retain_val
|
||||||
|
)
|
||||||
|
|
||||||
, link_map AS (
|
, link_map AS (
|
||||||
SELECT
|
SELECT
|
||||||
a.srce
|
a.srce
|
||||||
,a.id
|
|
||||||
,a.target
|
,a.target
|
||||||
,a.seq
|
,a.seq
|
||||||
,a.map_intention
|
,a.map_intention
|
||||||
,a.map_val
|
,a.map_val
|
||||||
,a.retain_val retain_value
|
,a."count"
|
||||||
,v.map
|
,a.retain_val
|
||||||
|
,v.map mapped_val
|
||||||
FROM
|
FROM
|
||||||
agg_to_target a
|
agg_to_ret a
|
||||||
LEFT OUTER JOIN tps.map_rv v ON
|
LEFT OUTER JOIN tps.map_rv v ON
|
||||||
v.srce = a.srce AND
|
v.srce = a.srce AND
|
||||||
v.target = a.target AND
|
v.target = a.target AND
|
||||||
v.retval = a.map_val
|
v.retval = a.map_val
|
||||||
)
|
)
|
||||||
--SELECT * FROM rx
|
|
||||||
--SELECT * FROM agg_to_target_items
|
SELECT
|
||||||
--SELECT * FROM agg_to_target
|
l.srce
|
||||||
SELECT * FROM link_map
|
,l.target
|
||||||
|
,l.seq
|
||||||
|
,l.map_intention
|
||||||
|
,l.map_val
|
||||||
|
,l."count"
|
||||||
|
,l.retain_val
|
||||||
|
,l.mapped_val
|
||||||
|
FROM
|
||||||
|
link_map l
|
||||||
|
ORDER BY
|
||||||
|
l.srce
|
||||||
|
,l.target
|
||||||
|
,l.seq
|
||||||
|
,l."count" desc
|
||||||
|
,l.map_val
|
||||||
|
,l.mapped_val
|
@ -1,21 +1,19 @@
|
|||||||
SELECT
|
SELECT
|
||||||
m.srce
|
m.srce,
|
||||||
,m.target
|
m.target,
|
||||||
,m.regex->'regex'->>'function' func_name
|
regex->>'function' regex_function,
|
||||||
,m.regex->'regex'->>'description' descr
|
regex->>'where' where_clause,
|
||||||
,filter.v filters
|
e.v ->> 'field' result_key_name,
|
||||||
,defn.v->> 'field' result_key_name
|
e.v ->> 'key' target_json_path,
|
||||||
,defn.v->> 'key' target_json_path
|
e.v ->> 'flag' regex_options_flag,
|
||||||
,defn.v->>'regex' regex_expression
|
e.v->>'map' map_intention,
|
||||||
,defn.v->> 'flag' regex_options_flag
|
e.v->>'retain' retain_result,
|
||||||
,defn.v->>'map' map_intention
|
e.v->>'regex' regex_expression,
|
||||||
,defn.v->>'retain' retain_result
|
e.rn target_item_number
|
||||||
,defn.rn target_item_number
|
|
||||||
FROM
|
FROM
|
||||||
tps.map_rm m
|
tps.map_rm m
|
||||||
LEFT JOIN LATERAL jsonb_array_elements(m.regex->'regex'->'defn') WITH ORDINALITY defn(v, rn) ON true
|
LEFT JOIN LATERAL jsonb_array_elements(m.regex->'defn') WITH ORDINALITY e(v, rn) ON true
|
||||||
LEFT JOIN LATERAL jsonb_array_elements(m.regex->'regex'->'where') WITH ORDINALITY filter(v, rn) ON true
|
|
||||||
ORDER BY
|
ORDER BY
|
||||||
m.srce
|
m.srce,
|
||||||
,m.target
|
m.target,
|
||||||
,defn.rn
|
e.rn
|
@ -61,7 +61,6 @@ dcard AS (
|
|||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT * FROM chase
|
SELECT * FROM chase
|
||||||
)
|
)
|
||||||
,format AS (
|
|
||||||
SELECT
|
SELECT
|
||||||
s.srce
|
s.srce
|
||||||
,s.id
|
,s.id
|
||||||
@ -82,5 +81,3 @@ FROM
|
|||||||
ORDER BY
|
ORDER BY
|
||||||
s.pdate desc
|
s.pdate desc
|
||||||
,s.id desc
|
,s.id desc
|
||||||
)
|
|
||||||
SELECT * FROM format
|
|
||||||
|
Loading…
Reference in New Issue
Block a user