Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(regress): enable jsonb #8181

Merged
merged 5 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions e2e_test/batch/types/jsonb.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ values ('{"a":[2, true, "", {}]}'::jsonb), ('1'), ('true'), ('null'), (null), ('
----
1
NULL
[1,true]
[1, true]
null
true
{"a":[2,true,"",{}]}
{"a": [2, true, "", {}]}

statement ok
create table t (v1 jsonb);
Expand Down Expand Up @@ -123,3 +123,27 @@ select jsonb_array_length('null');

statement ok
drop table t;

# Tests moved from regress tests due to not matching exactly.

# PostgreSQL sorts shorter key "two" before longer key "three"
# https://www.postgresql.org/docs/current/datatype-json.html#JSON-INDEXING:~:text=shorter%20keys%20are%20stored%20before%20longer%20keys
query T
SELECT '{
"one": 1,
"two":"two",
"three":
true}'::jsonb; -- OK
----
{"one": 1, "three": true, "two": "two"}

# We do not support jsonb IS NULL yet.
query TTTT
SELECT
'{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'ff',
'{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'qq',
-- ('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'Y') IS NULL AS f,
('{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb ->> 'Y') IS NULL AS t,
'{"ff":{"a":12,"b":16},"qq":123,"x":[1,2],"Y":null}'::jsonb -> 'x';
----
{"a": 12, "b": 16} 123 t [1, 2]
58 changes: 56 additions & 2 deletions src/common/src/array/jsonb_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,25 @@ impl Ord for JsonbRef<'_> {

impl crate::types::to_text::ToText for JsonbRef<'_> {
fn write<W: std::fmt::Write>(&self, f: &mut W) -> std::fmt::Result {
write!(f, "{}", self.0)
struct FmtToIoUnchecked<F>(F);
impl<F: std::fmt::Write> std::io::Write for FmtToIoUnchecked<F> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let s = unsafe { std::str::from_utf8_unchecked(buf) };
self.0.write_str(s).map_err(|_| std::io::ErrorKind::Other)?;
Ok(buf.len())
}

fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}

// Use custom [`ToTextFormatter`] to serialize. If we are okay with the default, this can be
// just `write!(f, "{}", self.0)`
use serde::Serialize as _;
let mut ser =
serde_json::ser::Serializer::with_formatter(FmtToIoUnchecked(f), ToTextFormatter);
self.0.serialize(&mut ser).map_err(|_| std::fmt::Error)
}

fn write_with_type<W: std::fmt::Write>(
Expand Down Expand Up @@ -231,7 +249,8 @@ impl JsonbRef<'_> {
Value::String(v) => writer.write_str(v),
Value::Null => Ok(()),
Value::Bool(_) | Value::Number(_) | Value::Array(_) | Value::Object(_) => {
write!(writer, "{}", self.0)
use crate::types::to_text::ToText as _;
self.write_with_type(&crate::types::DataType::Jsonb, writer)
}
}
}
Expand Down Expand Up @@ -386,3 +405,38 @@ impl Array for JsonbArray {
super::ArrayBuilderImpl::Jsonb(array_builder)
}
}

/// A custom implementation for [`serde_json::ser::Formatter`] to match PostgreSQL, which adds extra
/// space after `,` and `:` in array and object.
struct ToTextFormatter;

impl serde_json::ser::Formatter for ToTextFormatter {
fn begin_array_value<W>(&mut self, writer: &mut W, first: bool) -> std::io::Result<()>
where
W: ?Sized + std::io::Write,
{
if first {
Ok(())
} else {
writer.write_all(b", ")
}
}

fn begin_object_key<W>(&mut self, writer: &mut W, first: bool) -> std::io::Result<()>
where
W: ?Sized + std::io::Write,
{
if first {
Ok(())
} else {
writer.write_all(b", ")
}
}

fn begin_object_value<W>(&mut self, writer: &mut W) -> std::io::Result<()>
where
W: ?Sized + std::io::Write,
{
writer.write_all(b": ")
}
}
90 changes: 46 additions & 44 deletions src/tests/regress/data/expected/jsonb.out
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ SELECT jsonb_agg(q ORDER BY x NULLS FIRST, y)
(1 row)

-- jsonb extraction functions
CREATE TEMP TABLE test_jsonb (
CREATE TABLE test_jsonb (
json_type text,
test_json jsonb
);
Expand Down Expand Up @@ -581,6 +581,8 @@ SELECT (test_json->>3) IS NULL AS expect_true FROM test_jsonb WHERE json_type =
t
(1 row)

DROP TABLE test_jsonb;

-- corner cases
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
?column?
Expand Down Expand Up @@ -5405,70 +5407,70 @@ select ts_headline('[]'::jsonb, tsquery('aaa & bbb'));
(1 row)

-- casts
select 'true'::jsonb::bool;
bool
------
select 'true'::jsonb::bool AS v;
v
---
t
(1 row)

select '[]'::jsonb::bool;
select '[]'::jsonb::bool AS v;
ERROR: cannot cast jsonb array to type boolean
select '1.0'::jsonb::float;
float8
--------
1
select '1.0'::jsonb::float AS v;
v
---
1
(1 row)

select '[1.0]'::jsonb::float;
select '[1.0]'::jsonb::float AS v;
ERROR: cannot cast jsonb array to type double precision
select '12345'::jsonb::int4;
int4
select '12345'::jsonb::int4 AS v;
v
-------
12345
(1 row)

select '"hello"'::jsonb::int4;
select '"hello"'::jsonb::int4 AS v;
ERROR: cannot cast jsonb string to type integer
select '12345'::jsonb::numeric;
numeric
---------
12345
select '12345'::jsonb::numeric AS v;
v
-------
12345
(1 row)

select '{}'::jsonb::numeric;
select '{}'::jsonb::numeric AS v;
ERROR: cannot cast jsonb object to type numeric
select '12345.05'::jsonb::numeric;
numeric
select '12345.05'::jsonb::numeric AS v;
v
----------
12345.05
(1 row)

select '12345.05'::jsonb::float4;
float4
select '12345.05'::jsonb::float4 AS v;
v
----------
12345.05
(1 row)

select '12345.05'::jsonb::float8;
float8
select '12345.05'::jsonb::float8 AS v;
v
----------
12345.05
(1 row)

select '12345.05'::jsonb::int2;
int2
select '12345.05'::jsonb::int2 AS v;
v
-------
12345
(1 row)

select '12345.05'::jsonb::int4;
int4
select '12345.05'::jsonb::int4 AS v;
v
-------
12345
(1 row)

select '12345.05'::jsonb::int8;
int8
select '12345.05'::jsonb::int8 AS v;
v
-------
12345
(1 row)
Expand All @@ -5479,32 +5481,32 @@ select '12345.0000000000000000000000000000000000000000000005'::jsonb::numeric;
12345.0000000000000000000000000000000000000000000005
(1 row)

select '12345.0000000000000000000000000000000000000000000005'::jsonb::float4;
float4
--------
12345
select '12345.0000000000000000000000000000000000000000000005'::jsonb::float4 AS v;
v
-------
12345
(1 row)

select '12345.0000000000000000000000000000000000000000000005'::jsonb::float8;
float8
--------
12345
select '12345.0000000000000000000000000000000000000000000005'::jsonb::float8 AS v;
v
-------
12345
(1 row)

select '12345.0000000000000000000000000000000000000000000005'::jsonb::int2;
int2
select '12345.0000000000000000000000000000000000000000000005'::jsonb::int2 AS v;
v
-------
12345
(1 row)

select '12345.0000000000000000000000000000000000000000000005'::jsonb::int4;
int4
select '12345.0000000000000000000000000000000000000000000005'::jsonb::int4 AS v;
v
-------
12345
(1 row)

select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8;
int8
select '12345.0000000000000000000000000000000000000000000005'::jsonb::int8 AS v;
v
-------
12345
(1 row)
Expand Down
1 change: 1 addition & 0 deletions src/tests/regress/data/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
# test: tablespace

test: boolean varchar int2 int4 int8 float4 float8 comments
test: jsonb
Loading