Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66974e0

Browse files
committed
Added unique key name as part of compare results
Small fixes and optimizations
1 parent ac042d0 commit 66974e0

7 files changed

Lines changed: 297 additions & 47 deletions

File tree

source/expectations/data_values/ut_compound_data_diff_tmp.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ create global temporary table ut_compound_data_diff_tmp(
1515
diff_id raw(128),
1616
item_no integer,
1717
pk_hash raw(128),
18-
pk_value varchar2(4000),
1918
item_hash raw(128),
2019
duplicate_no integer,
2120
constraint ut_compound_data_diff_tmp_uk1 unique (diff_id,duplicate_no,item_no,item_hash, pk_hash),

source/expectations/data_values/ut_compound_data_helper.pkb

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,19 @@ create or replace package body ut_compound_data_helper is
186186

187187
return l_results;
188188
end;
189-
189+
190+
function get_pk_value (a_join_by_xpath varchar2,a_item_data xmltype) return varchar2 is
191+
l_pk_value varchar2(4000);
192+
begin
193+
select listagg(extractvalue(xmlelement("ROW",column_value),a_join_by_xpath),':') within group ( order by 1)
194+
into l_pk_value
195+
from table(xmlsequence(extract(a_item_data,'/*/*')));
196+
197+
return l_pk_value;
198+
exception when no_data_found then
199+
return 'null ';
200+
end;
201+
190202
function get_rows_diff(
191203
a_expected_dataset_guid raw, a_actual_dataset_guid raw, a_diff_id raw,
192204
a_max_rows integer, a_exclude_xpath varchar2, a_include_xpath varchar2,
@@ -195,36 +207,38 @@ create or replace package body ut_compound_data_helper is
195207
l_column_filter varchar2(32767);
196208
l_results tt_row_diffs;
197209
begin
198-
l_column_filter := get_columns_filter(a_exclude_xpath,a_include_xpath);
210+
l_column_filter := get_columns_row_filter(a_exclude_xpath,a_include_xpath);
199211

200212
/**
201213
* Since its unordered search we cannot select max rows from diffs as we miss some comparision records
202214
* We will restrict output on higher level of select
215+
* NO_MERGE hint was introduced to prevent optimizer from merging views and rewriting query which in some cases
216+
* lead to second value being null depend on execution plan that been chosen
203217
**/
204-
205218
execute immediate q'[
206-
with diff_info as (select item_hash,pk_hash,pk_value from ut_compound_data_diff_tmp ucdc where diff_id = :diff_guid)
219+
with diff_info as (select item_hash,pk_hash,duplicate_no from ut_compound_data_diff_tmp ucdc where diff_id = :diff_guid)
207220
select rn,diff_type,diffed_row,pk_value from
208221
(
209222
select diff_type,diffed_row, dense_rank() over (order by pk_hash) rn,pk_value from
210223
(
211224
select diff_type,diffed_row,pk_hash,pk_value from
212225
(select diff_type,data_item diffed_row,pk_hash,pk_value
213226
from
214-
(select nvl(exp.pk_hash, act.pk_hash) pk_hash,nvl(exp.pk_value, act.pk_value) pk_value,
227+
(select /*+NO_MERGE*/ nvl(exp.pk_hash, act.pk_hash) pk_hash,nvl(exp.pk_value, act.pk_value) pk_value,
215228
xmlserialize(content exp.row_data no indent) exp_item,
216229
xmlserialize(content act.row_data no indent) act_item
217230
from
218-
(select ucd.*, row_number() over(partition by pk_hash order by row_hash) duplicate_no
231+
(select ucd.*
219232
from
220233
(select ucd.column_value row_data,
221234
r.item_hash row_hash,
222235
r.pk_hash ,
223-
r.pk_value,
236+
r.duplicate_no,
237+
ucd.column_value.getclobval() col_val,
224238
ucd.column_value.getRootElement() col_name,
225-
ucd.column_value.getclobval() col_val
239+
ut_compound_data_helper.get_pk_value(:join_xpath,r.item_data) pk_value
226240
from
227-
(select ]'||l_column_filter||q'[, ucd.item_no, ucd.item_hash, i.pk_hash, i.pk_value
241+
(select ]'||l_column_filter||q'[, ucd.item_no, ucd.item_hash, i.pk_hash, i.duplicate_no
228242
from ut_compound_data_tmp ucd,
229243
diff_info i
230244
where ucd.data_id = :self_guid
@@ -234,16 +248,17 @@ create or replace package body ut_compound_data_helper is
234248
) ucd
235249
) exp
236250
join (
237-
select ucd.*, row_number() over(partition by pk_hash order by row_hash) duplicate_no
251+
select ucd.*
238252
from
239253
(select ucd.column_value row_data,
240254
r.item_hash row_hash,
241255
r.pk_hash ,
242-
r.pk_value,
256+
r.duplicate_no,
257+
ucd.column_value.getclobval() col_val,
243258
ucd.column_value.getRootElement() col_name,
244-
ucd.column_value.getclobval() col_val
259+
ut_compound_data_helper.get_pk_value(:join_xpath,r.item_data) pk_value
245260
from
246-
(select ]'||l_column_filter||q'[, ucd.item_no, ucd.item_hash, i.pk_hash, i.pk_value
261+
(select ]'||l_column_filter||q'[, ucd.item_no, ucd.item_hash, i.pk_hash, i.duplicate_no
247262
from ut_compound_data_tmp ucd,
248263
diff_info i
249264
where ucd.data_id = :other_guid
@@ -252,7 +267,8 @@ create or replace package body ut_compound_data_helper is
252267
table( xmlsequence( extract(r.item_data,'/*/*') ) ) ucd
253268
) ucd
254269
) act
255-
on exp.pk_hash = act.pk_hash and exp.duplicate_no = act.duplicate_no
270+
on exp.pk_hash = act.pk_hash and exp.col_name = act.col_name
271+
and exp.duplicate_no = act.duplicate_no
256272
where dbms_lob.compare(exp.col_val, act.col_val) != 0
257273
)
258274
unpivot ( data_item for diff_type in (exp_item as 'Expected:', act_item as 'Actual:') )
@@ -261,15 +277,17 @@ create or replace package body ut_compound_data_helper is
261277
select case when exp.pk_hash is null then 'Extra:' else 'Missing:' end as diff_type,
262278
xmlserialize(content nvl(exp.item_data, act.item_data) no indent) diffed_row,
263279
coalesce(exp.pk_hash,act.pk_hash) pk_hash,
264-
null pk_value
265-
from (select extract(ucd.item_data,'/*/*') item_data,i.pk_hash
280+
coalesce(exp.pk_value,act.pk_value) pk_value
281+
from (select extract(ucd.item_data,'/*/*') item_data,i.pk_hash,
282+
ut_compound_data_helper.get_pk_value(:join_by,item_data) pk_value
266283
from ut_compound_data_tmp ucd,
267284
diff_info i
268285
where ucd.data_id = :self_guid
269286
and ucd.item_hash = i.item_hash
270287
) exp
271288
full outer join (
272-
select extract(ucd.item_data,'/*/*') item_data,i.pk_hash
289+
select extract(ucd.item_data,'/*/*') item_data,i.pk_hash,
290+
ut_compound_data_helper.get_pk_value(:join_by,item_data) pk_value
273291
from ut_compound_data_tmp ucd,
274292
diff_info i
275293
where ucd.data_id = :other_guid
@@ -282,10 +300,13 @@ create or replace package body ut_compound_data_helper is
282300
]'
283301
bulk collect into l_results
284302
using a_diff_id,
303+
a_join_by_xpath,
285304
a_exclude_xpath, a_include_xpath, a_expected_dataset_guid,
305+
a_join_by_xpath,
286306
a_exclude_xpath, a_include_xpath, a_actual_dataset_guid,
287-
a_expected_dataset_guid, a_actual_dataset_guid,
307+
a_join_by_xpath,a_expected_dataset_guid,a_join_by_xpath, a_actual_dataset_guid,
288308
a_max_rows;
309+
289310
return l_results;
290311
end;
291312

source/expectations/data_values/ut_compound_data_helper.pks

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ create or replace package ut_compound_data_helper authid definer is
6060
a_max_rows integer, a_exclude_xpath varchar2, a_include_xpath varchar2
6161
) return tt_row_diffs;
6262

63+
function get_pk_value (a_join_by_xpath varchar2,a_item_data xmltype) return varchar2;
64+
6365
function get_rows_diff(
6466
a_expected_dataset_guid raw, a_actual_dataset_guid raw, a_diff_id raw,
6567
a_max_rows integer, a_exclude_xpath varchar2, a_include_xpath varchar2,

source/expectations/data_values/ut_compound_data_value.tpb

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,6 @@ create or replace type body ut_compound_data_value as
251251
l_row_diffs ut_compound_data_helper.tt_row_diffs;
252252
c_max_rows constant integer := 20;
253253

254-
function get_pk_value(a_join_by_xpath varchar2) return varchar2 is
255-
l_column varchar2(32767);
256-
begin
257-
/* due to possibility of key being to columns we cannot use xmlextractvalue
258-
usage of xmlagg is possible however it greatly complicates code and performance is impacted.
259-
xpath to be looked at or regex
260-
*/
261-
if a_join_by_xpath is not null then
262-
l_column := ' extract(t.item_data,:join_by_xpath).GetStringVal() pk_value ';
263-
else
264-
l_column := ' :join_by_xpath pk_value';
265-
end if;
266-
return l_column;
267-
end;
268-
269254
function get_column_pk_hash(a_join_by_xpath varchar2) return varchar2 is
270255
l_column varchar2(32767);
271256
begin
@@ -313,35 +298,35 @@ create or replace type body ut_compound_data_value as
313298
using a_exclude_xpath, a_include_xpath,a_join_by_xpath,self.data_id, l_other.data_id;
314299

315300
/* Peform minus on two sets two get diffrences that will be used later on to print results */
316-
execute immediate 'insert into ' || l_ut_owner || '.ut_compound_data_diff_tmp ( diff_id,item_hash,pk_hash,duplicate_no,pk_value)
301+
execute immediate 'insert into ' || l_ut_owner || '.ut_compound_data_diff_tmp ( diff_id,item_hash,pk_hash,duplicate_no)
317302
with source_data as
318303
( select t.data_id,t.item_hash,row_number() over (partition by t.pk_hash,t.item_hash,t.data_id order by 1,2) duplicate_no,
319-
pk_hash, '||get_pk_value(a_join_by_xpath)||'
304+
pk_hash
320305
from ' || l_ut_owner || '.ut_compound_data_tmp t
321306
where data_id = :self_guid or data_id = :other_guid
322307
)
323-
select distinct :diff_id,tmp.item_hash,tmp.pk_hash,tmp.duplicate_no,pk_value
308+
select distinct :diff_id,tmp.item_hash,tmp.pk_hash,tmp.duplicate_no
324309
from(
325310
(
326-
select t.item_hash,t. duplicate_no,t.pk_hash, t.pk_value
311+
select t.item_hash,t. duplicate_no,t.pk_hash
327312
from source_data t
328313
where t.data_id = :self_guid
329314
minus
330-
select t.item_hash,t. duplicate_no,t.pk_hash, t.pk_value
315+
select t.item_hash,t. duplicate_no,t.pk_hash
331316
from source_data t
332317
where t.data_id = :other_guid
333318
)
334319
union all
335320
(
336-
select t.item_hash,t. duplicate_no,t.pk_hash, t.pk_value
321+
select t.item_hash,t. duplicate_no,t.pk_hash
337322
from source_data t
338323
where t.data_id = :other_guid
339324
minus
340-
select t.item_hash,t. duplicate_no,t.pk_hash, t.pk_value
325+
select t.item_hash,t. duplicate_no,t.pk_hash
341326
from source_data t
342327
where t.data_id = :self_guid
343328
))tmp'
344-
using a_join_by_xpath,self.data_id, l_other.data_id,
329+
using self.data_id, l_other.data_id,
345330
l_diff_id,
346331
self.data_id, l_other.data_id,
347332
l_other.data_id,self.data_id;

source/expectations/data_values/ut_data_value_refcursor.tpb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ create or replace type body ut_data_value_refcursor as
198198
ut_utils.append_to_clob(l_result,chr(10) || 'Unable to join sets:' || chr(10));
199199
for i in 1 .. l_missing_pk.count loop
200200
l_results.extend;
201-
ut_utils.append_to_clob(l_result, get_missing_key_message(l_missing_pk(i)));
201+
ut_utils.append_to_clob(l_result, get_missing_key_message(l_missing_pk(i))|| chr(10));
202202
end loop;
203203
end if;
204204
else

0 commit comments

Comments
 (0)