drop domain if exists char3_domain_not_null CASCADE;
drop domain if exists hello CASCADE;
drop domain if exists int42 CASCADE;
drop type if exists comp_domain_with_typmod;
create domain char3_domain_not_null as char(3) NOT NULL;
create domain hello as text NOT NULL check (value = 'hello');
create domain int42 as int check (value = 42);
CREATE TYPE comp_domain_with_typmod AS (a char3_domain_not_null, b int42);

SELECT JSON_QUERY(jsonb'{"rec": "(abcd,42)"}', '$.rec' returning comp_domain_with_typmod omit quotes);
SELECT JSON_QUERY(jsonb'{"rec": "(abcd,42)"}', '$.rec' returning comp_domain_with_typmod keep quotes);
SELECT JSON_QUERY(jsonb'{"rec": "(abcd,42)"}', '$.rec' returning char(3) omit quotes);
SELECT JSON_QUERY(jsonb'{"rec": "(abcd,42)"}', '$.rec' returning char(3) keep quotes);
SELECT JSON_QUERY(jsonb'{"rec": "hello"}', '$.rec' returning hello keep quotes);
SELECT JSON_QUERY(jsonb'{"rec": "hello"}', '$.rec' returning hello omit quotes);
SELECT JSON_QUERY(jsonb'{"rec": "hello1"}', '$.rec' returning hello keep quotes);
SELECT JSON_QUERY(jsonb'{"rec": "hello1"}', '$.rec' returning hello omit quotes);
SELECT JSON_QUERY(jsonb'"hello"','$' RETURNING char3_domain_not_null omit quotes) ;
SELECT JSON_QUERY(jsonb'"hello"','$' RETURNING char3_domain_not_null keep quotes) ;
SELECT JSON_QUERY(jsonb '"[1,2]"', '$' RETURNING char(3) omit quotes);
SELECT JSON_QUERY(jsonb '"[1,2]"', '$' RETURNING char(3) keep quotes);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING int8);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING int2);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING int4);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING int8 OMIT QUOTES);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING numeric);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING numeric keep quotes);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING numeric keep quotes null on error);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING numeric keep quotes error on error);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING numeric omit quotes);
SELECT JSON_QUERY(jsonb '123',   '$' RETURNING numeric);
SELECT JSON_QUERY(jsonb '"hello"','$' RETURNING char(3));
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING bool);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING real);
SELECT JSON_QUERY(jsonb '"123"', '$' RETURNING float8);
SELECT JSON_QUERY(jsonb '"[1,2]"', '$' RETURNING char(3) omit quotes);
SELECT JSON_QUERY(jsonb '"[1,2]"', '$' RETURNING char(3) keep quotes);
SELECT JSON_QUERY(jsonb '"[1,2]"', '$' RETURNING varchar(3) keep quotes);
SELECT JSON_QUERY(jsonb '"[1,2]"', '$' RETURNING varchar keep quotes);

SELECT JSON_VALUE(jsonb'{"rec": "abcd"}', '$.rec' returning char(3));
SELECT JSON_VALUE(jsonb'{"rec": "abcd"}', '$.rec' returning char3_domain_not_null);
SELECT JSON_VALUE(jsonb'{"rec": "(abcd,42)"}', '$.rec' returning comp_domain_with_typmod);
SELECT JSON_VALUE(jsonb'{"rec": "(abc,42)"}', '$.rec' returning comp_domain_with_typmod);
SELECT JSON_VALUE(jsonb'{"rec": "hello"}', '$.rec' returning hello);
SELECT JSON_VALUE(jsonb'{"rec": "hello1"}', '$.rec' returning hello);
SELECT JSON_VALUE(jsonb'"hello"','$' RETURNING char3_domain_not_null) ;
SELECT JSON_VALUE(jsonb'"hello"','$' RETURNING char(3));
SELECT JSON_VALUE(jsonb '"[1,2]"', '$' RETURNING varchar);

---domain type with not null and null issue
drop domain if exists test;
create domain test as int[] check ( array_length(value,1) =2 and (value[1] = 1 or value[2] = 2));
create domain test1 as int[] NOT NULL check (array_length(value,1) =2 and (value[1] = 1 or value[2] = 2));
SELECT * from JSON_QUERY(jsonb'{"rec": "{1,2,3}"}', '$.rec' returning test omit quotes);
SELECT * from JSON_QUERY(jsonb'{"rec": "{1,2,3}"}', '$.rec' returning test keep quotes);
SELECT * from JSON_VALUE(jsonb'{"rec": "{1,2,3}"}', '$.rec' returning test);

SELECT * from JSON_QUERY(jsonb'{"rec": "{1,2,3}"}', '$.rec' returning test1 omit quotes);
SELECT * from JSON_QUERY(jsonb'{"rec": "{1,2,3}"}', '$.rec' returning test1 keep quotes);
SELECT * from JSON_VALUE(jsonb'{"rec": "{1,2,3}"}', '$.rec' returning test1);
