Skip to content

Commit

Permalink
Add json & xml types data
Browse files Browse the repository at this point in the history
  • Loading branch information
robozmey committed Dec 2, 2024
1 parent 5c479b5 commit ed208e0
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 8 deletions.
29 changes: 29 additions & 0 deletions contrib/try_convert/data/tt_json.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}
{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}
{"widget": {"debug": "on","window": {"title": "Sample Konfabulator Widget","name": "main_window","width": 500,"height": 500},"image": { "src": "Images/Sun.png","name": "sun1","hOffset": 250,"vOffset": 250,"alignment": "center"},"text": {"data": "Click Here","size": 36,"style": "bold","name": "text1","hOffset": 250,"vOffset": 100,"alignment": "center","onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"}}}
{"menu": {"header": "SVG Viewer","items": [{"id": "Open"},{"id": "OpenNew", "label": "Open New"},null,{"id": "ZoomIn", "label": "Zoom In"},{"id": "ZoomOut", "label": "Zoom Out"},{"id": "OriginalView", "label": "Original View"},null,{"id": "Quality"},{"id": "Pause"},{"id": "Mute"},null,{"id": "Find", "label": "Find..."},{"id": "FindAgain", "label": "Find Again"},{"id": "Copy"},{"id": "CopyAgain", "label": "Copy Again"},{"id": "CopySVG", "label": "Copy SVG"},{"id": "ViewSVG", "label": "View SVG"},{"id": "ViewSource", "label": "View Source"},{"id": "SaveAs", "label": "Save As"},null,{"id": "Help"},{"id": "About", "label": "About Adobe CVG Viewer..."}]}}
{"line":1, "date":"CB", "node":"AA"}
{"cleaned":false, "status":59, "line":2, "disabled":false, "node":"CBB"}
{"indexed":true, "status":35, "line":3, "disabled":false, "wait":"CAA", "subtitle":"BA", "user":"CCA"}
{"line":4, "disabled":true, "space":"BB"}
{"cleaned":false, "line":5, "wait":"BB", "query":"CAC", "coauthors":"ACA", "node":"CBA"}
{"world":"CB", "query":"CBC", "indexed":false, "line":6, "pos":92, "date":"AAB", "space":"CB", "coauthors":"ACA", "node":"CBC"}
{"state":98, "org":43, "line":7, "pos":97}
{"auth":"BB", "title":"CAC", "query":"BA", "status":94, "line":8, "coauthors":"BBB"}
{"auth":"BAC", "title":"CAA", "wait":"CA", "bad":true, "query":"AA", "indexed":true, "line":9, "pos":56}
{"title":"AAC", "bad":true, "user":"AAB", "query":"AC", "line":10, "node":"AB"}
{"world":"CAC", "user":"AB", "query":"ACA", "indexed":true, "line":11, "space":"CB"}
{"line":12, "pos":72, "abstract":"BBA", "space":"AAC"}
{}
{"world":"CC", "query":"AA", "line":14, "disabled":false, "date":"CAC", "coauthors":"AB"}
{"org":68, "title":"BBB", "query":"BAC", "line":15, "public":false}
{"org":73, "user":"AA", "indexed":true, "line":16, "date":"CCC", "public":true, "coauthors":"AB"}
{"indexed":false, "line":17}
{"state":23, "auth":"BCC", "org":38, "status":28, "line":18, "disabled":false, "abstract":"CB"}
{"state":99, "auth":"CA", "indexed":true, "line":19, "date":"BA"}
{"wait":"CBA", "user":"BBA", "indexed":true, "line":20, "disabled":false, "abstract":"BA", "date":"ABA"}
{"org":10, "query":"AC", "indexed":false, "line":21, "disabled":true, "abstract":"CA", "pos":44}
{"state":65, "title":"AC", "user":"AAC", "cleaned":true, "status":93, "line":22, "abstract":"ABC", "node":"CCC"}
{"subtitle":"AC", "user":"CCC", "line":23}
{"state":67, "world":"ACB", "bad":true, "user":"CB", "line":24, "disabled":true}
{}
29 changes: 29 additions & 0 deletions contrib/try_convert/data/tt_jsonb.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}
{"menu": {"id": "file","value": "File","popup": {"menuitem": [{"value": "New", "onclick": "CreateNewDoc()"},{"value": "Open", "onclick": "OpenDoc()"},{"value": "Close", "onclick": "CloseDoc()"}]}}}
{"widget": {"debug": "on","window": {"title": "Sample Konfabulator Widget","name": "main_window","width": 500,"height": 500},"image": { "src": "Images/Sun.png","name": "sun1","hOffset": 250,"vOffset": 250,"alignment": "center"},"text": {"data": "Click Here","size": 36,"style": "bold","name": "text1","hOffset": 250,"vOffset": 100,"alignment": "center","onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"}}}
{"menu": {"header": "SVG Viewer","items": [{"id": "Open"},{"id": "OpenNew", "label": "Open New"},null,{"id": "ZoomIn", "label": "Zoom In"},{"id": "ZoomOut", "label": "Zoom Out"},{"id": "OriginalView", "label": "Original View"},null,{"id": "Quality"},{"id": "Pause"},{"id": "Mute"},null,{"id": "Find", "label": "Find..."},{"id": "FindAgain", "label": "Find Again"},{"id": "Copy"},{"id": "CopyAgain", "label": "Copy Again"},{"id": "CopySVG", "label": "Copy SVG"},{"id": "ViewSVG", "label": "View SVG"},{"id": "ViewSource", "label": "View Source"},{"id": "SaveAs", "label": "Save As"},null,{"id": "Help"},{"id": "About", "label": "About Adobe CVG Viewer..."}]}}
{"line":1, "date":"CB", "node":"AA"}
{"cleaned":false, "status":59, "line":2, "disabled":false, "node":"CBB"}
{"indexed":true, "status":35, "line":3, "disabled":false, "wait":"CAA", "subtitle":"BA", "user":"CCA"}
{"line":4, "disabled":true, "space":"BB"}
{"cleaned":false, "line":5, "wait":"BB", "query":"CAC", "coauthors":"ACA", "node":"CBA"}
{"world":"CB", "query":"CBC", "indexed":false, "line":6, "pos":92, "date":"AAB", "space":"CB", "coauthors":"ACA", "node":"CBC"}
{"state":98, "org":43, "line":7, "pos":97}
{"auth":"BB", "title":"CAC", "query":"BA", "status":94, "line":8, "coauthors":"BBB"}
{"auth":"BAC", "title":"CAA", "wait":"CA", "bad":true, "query":"AA", "indexed":true, "line":9, "pos":56}
{"title":"AAC", "bad":true, "user":"AAB", "query":"AC", "line":10, "node":"AB"}
{"world":"CAC", "user":"AB", "query":"ACA", "indexed":true, "line":11, "space":"CB"}
{"line":12, "pos":72, "abstract":"BBA", "space":"AAC"}
{}
{"world":"CC", "query":"AA", "line":14, "disabled":false, "date":"CAC", "coauthors":"AB"}
{"org":68, "title":"BBB", "query":"BAC", "line":15, "public":false}
{"org":73, "user":"AA", "indexed":true, "line":16, "date":"CCC", "public":true, "coauthors":"AB"}
{"indexed":false, "line":17}
{"state":23, "auth":"BCC", "org":38, "status":28, "line":18, "disabled":false, "abstract":"CB"}
{"state":99, "auth":"CA", "indexed":true, "line":19, "date":"BA"}
{"wait":"CBA", "user":"BBA", "indexed":true, "line":20, "disabled":false, "abstract":"BA", "date":"ABA"}
{"org":10, "query":"AC", "indexed":false, "line":21, "disabled":true, "abstract":"CA", "pos":44}
{"state":65, "title":"AC", "user":"AAC", "cleaned":true, "status":93, "line":22, "abstract":"ABC", "node":"CCC"}
{"subtitle":"AC", "user":"CCC", "line":23}
{"state":67, "world":"ACB", "bad":true, "user":"CB", "line":24, "disabled":true}
{}
5 changes: 5 additions & 0 deletions contrib/try_convert/data/tt_xml.data
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<menu id="file" value="File"> <popup> <menuitem value="New" onclick="CreateNewDoc()" /> <menuitem value="Open" onclick="OpenDoc()" /> <menuitem value="Close" onclick="CloseDoc()" /> </popup> </menu>
<widget> <debug>on</debug> <window title="Sample Konfabulator Widget"> <name>main_window</name> <width>500</width> <height>500</height> </window> <image src="Images/Sun.png" name="sun1"> <hOffset>250</hOffset> <vOffset>250</vOffset> <alignment>center</alignment> </image> <text data="Click Here" size="36" style="bold"> <name>text1</name> <hOffset>250</hOffset> <vOffset>100</vOffset> <alignment>center</alignment> <onMouseUp>sun1.opacity = (sun1.opacity / 100) * 90;</onMouseUp> </text> </widget>
<?xml version="1.0" encoding="UTF-8"?> <book> <name>A Song of Ice and Fire</name> <author>George R. R. Martin</author> <language>English</language> <genre>Epic fantasy</genre> </book>
<students> <student> <name>Rick Grimes</name> <age>35</age> <subject>Maths</subject> <gender>Male</gender> </student> <student> <name>Daryl Dixon </name> <age>33</age> <subject>Science</subject> <gender>Male</gender> </student> <student> <name>Maggie</name> <age>36</age> <subject>Arts</subject> <gender>Female</gender> </student> </students>
<menu> <header>Adobe SVG Viewer</header> <item action="Open" id="Open">Open</item> <item action="OpenNew" id="OpenNew">Open New</item> <separator/> <item action="ZoomIn" id="ZoomIn">Zoom In</item> <item action="ZoomOut" id="ZoomOut">Zoom Out</item> <item action="OriginalView" id="OriginalView">Original View</item> <separator/> <item action="Quality" id="Quality">Quality</item> <item action="Pause" id="Pause">Pause</item> <item action="Mute" id="Mute">Mute</item> <separator/> <item action="Find" id="Find">Find...</item> <item action="FindAgain" id="FindAgain">Find Again</item> <item action="Copy" id="Copy">Copy</item> <item action="CopyAgain" id="CopyAgain">Copy Again</item> <item action="CopySVG" id="CopySVG">Copy SVG</item> <item action="ViewSVG" id="ViewSVG">View SVG</item> <item action="ViewSource" id="ViewSource">View Source</item> <item action="SaveAs" id="SaveAs">Save As</item> <separator/> <item action="Help" id="Help">Help</item> <item action="About" id="About">About Adobe CVG Viewer...</item> </menu>
15 changes: 11 additions & 4 deletions contrib/try_convert/generate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
'cidr', # IP
'inet',
'macaddr',
# 'json', # JSON
# 'jsonb',
'json', # JSON
'jsonb',
'xml',
# 'bytea', # STRINGS
# 'char',
# 'varchar',
Expand All @@ -38,7 +39,11 @@
# # 'tsvector',
# # 'txid_snapshot',
# 'uuid',
# 'xml'
]

uncomparable_types = [
'json',
'xml',
]

print('Supported types:', ' '.join(supported_types))
Expand Down Expand Up @@ -179,13 +184,15 @@ def get_data(type_name):

def create_test(source_name, target_name, test_data):

test_filter = 'not (v1 = v2)' if target_name not in uncomparable_types else 'not (v1::text = v2::text)'

query = \
f'select * from (' \
f'select ' \
f'try_convert(v, NULL::{target_name}) as v1, ' \
f'try_convert_by_sql(v, NULL::{target_name}) as v2' \
f' from {test_data}' \
f') as t(v1, v2) where not (v1 = v2);'
f') as t(v1, v2) where {test_filter};'
result = \
' v1 | v2 \n' \
'----+----\n' \
Expand Down
59 changes: 57 additions & 2 deletions contrib/try_convert/input/try_convert.source
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- SCRIPT-GENERATED TEST for TRY_CONVERT
-- Tests 18 types of 91 from pg_types.h
-- Tests 60 cast of 206 from pg_cast.h
-- Tests 21 types of 91 from pg_types.h
-- Tests 64 cast of 206 from pg_cast.h

create schema tryconvert;
set search_path = tryconvert;
Expand Down Expand Up @@ -170,6 +170,36 @@ $func$
-- do nothing: _out already carries default
END
$func$;
CREATE OR REPLACE FUNCTION try_convert_by_sql(_in json, INOUT _out ANYELEMENT)
LANGUAGE plpgsql AS
$func$
BEGIN
EXECUTE format('SELECT %L::%s', $1, source_type)
INTO _out;
EXCEPTION WHEN others THEN
-- do nothing: _out already carries default
END
$func$;
CREATE OR REPLACE FUNCTION try_convert_by_sql(_in jsonb, INOUT _out ANYELEMENT)
LANGUAGE plpgsql AS
$func$
BEGIN
EXECUTE format('SELECT %L::%s', $1, source_type)
INTO _out;
EXCEPTION WHEN others THEN
-- do nothing: _out already carries default
END
$func$;
CREATE OR REPLACE FUNCTION try_convert_by_sql(_in xml, INOUT _out ANYELEMENT)
LANGUAGE plpgsql AS
$func$
BEGIN
EXECUTE format('SELECT %L::%s', $1, source_type)
INTO _out;
EXCEPTION WHEN others THEN
-- do nothing: _out already carries default
END
$func$;
CREATE OR REPLACE FUNCTION try_convert_by_sql(_in text, INOUT _out ANYELEMENT)
LANGUAGE plpgsql AS
$func$
Expand Down Expand Up @@ -241,6 +271,15 @@ INSERT INTO tt_inet(id, v) SELECT row_number() OVER(), v::inet from tt_temp;
CREATE TABLE tt_macaddr (id serial, v macaddr) DISTRIBUTED BY (id);
DELETE FROM tt_temp;COPY tt_temp from '@abs_srcdir@/data/tt_macaddr.data';
INSERT INTO tt_macaddr(id, v) SELECT row_number() OVER(), v::macaddr from tt_temp;
CREATE TABLE tt_json (id serial, v json) DISTRIBUTED BY (id);
DELETE FROM tt_temp;COPY tt_temp from '@abs_srcdir@/data/tt_json.data';
INSERT INTO tt_json(id, v) SELECT row_number() OVER(), v::json from tt_temp;
CREATE TABLE tt_jsonb (id serial, v jsonb) DISTRIBUTED BY (id);
DELETE FROM tt_temp;COPY tt_temp from '@abs_srcdir@/data/tt_jsonb.data';
INSERT INTO tt_jsonb(id, v) SELECT row_number() OVER(), v::jsonb from tt_temp;
CREATE TABLE tt_xml (id serial, v xml) DISTRIBUTED BY (id);
DELETE FROM tt_temp;COPY tt_temp from '@abs_srcdir@/data/tt_xml.data';
INSERT INTO tt_xml(id, v) SELECT row_number() OVER(), v::xml from tt_temp;
CREATE TABLE tt_text (id serial, v text) DISTRIBUTED BY (id);
DELETE FROM tt_temp;COPY tt_temp from '@abs_srcdir@/data/tt_text.data';
INSERT INTO tt_text(id, v) SELECT row_number() OVER(), v::text from tt_temp;
Expand Down Expand Up @@ -313,6 +352,18 @@ select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NU
DELETE FROM tt_temp; COPY tt_temp from '@abs_srcdir@/data/tt_macaddr.data';
select * from (select try_convert(v, NULL::macaddr) as v1, try_convert_by_sql(v, NULL::macaddr) as v2 from tt_temp) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::macaddr) as v1, try_convert_by_sql(v, NULL::macaddr) as v2 from (select ('!@#%^&*' || v || '!@#%^&*') from tt_macaddr) as t(v)) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NULL::text) as v2 from tt_json) as t(v1, v2) where not (v1 = v2);
DELETE FROM tt_temp; COPY tt_temp from '@abs_srcdir@/data/tt_json.data';
select * from (select try_convert(v, NULL::json) as v1, try_convert_by_sql(v, NULL::json) as v2 from tt_temp) as t(v1, v2) where not (v1::text = v2::text);
select * from (select try_convert(v, NULL::json) as v1, try_convert_by_sql(v, NULL::json) as v2 from (select ('!@#%^&*' || v || '!@#%^&*') from tt_json) as t(v)) as t(v1, v2) where not (v1::text = v2::text);
select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NULL::text) as v2 from tt_jsonb) as t(v1, v2) where not (v1 = v2);
DELETE FROM tt_temp; COPY tt_temp from '@abs_srcdir@/data/tt_jsonb.data';
select * from (select try_convert(v, NULL::jsonb) as v1, try_convert_by_sql(v, NULL::jsonb) as v2 from tt_temp) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::jsonb) as v1, try_convert_by_sql(v, NULL::jsonb) as v2 from (select ('!@#%^&*' || v || '!@#%^&*') from tt_jsonb) as t(v)) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NULL::text) as v2 from tt_xml) as t(v1, v2) where not (v1 = v2);
DELETE FROM tt_temp; COPY tt_temp from '@abs_srcdir@/data/tt_xml.data';
select * from (select try_convert(v, NULL::xml) as v1, try_convert_by_sql(v, NULL::xml) as v2 from tt_temp) as t(v1, v2) where not (v1::text = v2::text);
select * from (select try_convert(v, NULL::xml) as v1, try_convert_by_sql(v, NULL::xml) as v2 from (select ('!@#%^&*' || v || '!@#%^&*') from tt_xml) as t(v)) as t(v1, v2) where not (v1::text = v2::text);
select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NULL::text) as v2 from tt_text) as t(v1, v2) where not (v1 = v2);
DELETE FROM tt_temp; COPY tt_temp from '@abs_srcdir@/data/tt_text.data';
select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NULL::text) as v2 from tt_temp) as t(v1, v2) where not (v1 = v2);
Expand Down Expand Up @@ -376,10 +427,14 @@ select * from (select try_convert(v, NULL::cidr) as v1, try_convert_by_sql(v, NU
select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NULL::text) as v2 from tt_cidr) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NULL::text) as v2 from tt_inet) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NULL::text) as v2 from tt_bool) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::text) as v1, try_convert_by_sql(v, NULL::text) as v2 from tt_xml) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::xml) as v1, try_convert_by_sql(v, NULL::xml) as v2 from tt_text) as t(v1, v2) where not (v1::text = v2::text);
select * from (select try_convert(v, NULL::time) as v1, try_convert_by_sql(v, NULL::time) as v2 from tt_time) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::timestamp) as v1, try_convert_by_sql(v, NULL::timestamp) as v2 from tt_timestamp) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::timestamptz) as v1, try_convert_by_sql(v, NULL::timestamptz) as v2 from tt_timestamptz) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::interval) as v1, try_convert_by_sql(v, NULL::interval) as v2 from tt_interval) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::timetz) as v1, try_convert_by_sql(v, NULL::timetz) as v2 from tt_timetz) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::numeric) as v1, try_convert_by_sql(v, NULL::numeric) as v2 from tt_numeric) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::jsonb) as v1, try_convert_by_sql(v, NULL::jsonb) as v2 from tt_json) as t(v1, v2) where not (v1 = v2);
select * from (select try_convert(v, NULL::json) as v1, try_convert_by_sql(v, NULL::json) as v2 from tt_jsonb) as t(v1, v2) where not (v1::text = v2::text);
reset search_path;
Loading

0 comments on commit ed208e0

Please sign in to comment.