Skip to content

Commit

Permalink
basic e2e with casting from and into string
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangjinwu committed Feb 17, 2023
1 parent 7488dd9 commit 7c16d15
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 12 deletions.
26 changes: 14 additions & 12 deletions e2e_test/batch/catalog/pg_cast.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,20 @@ SELECT * FROM pg_catalog.pg_cast
51 1043 1114 EXPLICIT
52 1043 1184 EXPLICIT
53 1043 1186 EXPLICIT
54 1083 1043 ASSIGN
55 1083 1186 IMPLICIT
56 1114 1082 ASSIGN
57 1114 1043 ASSIGN
58 1114 1083 ASSIGN
59 1114 1184 IMPLICIT
60 1184 1082 ASSIGN
61 1184 1043 ASSIGN
62 1184 1083 ASSIGN
63 1184 1114 ASSIGN
64 1186 1043 ASSIGN
65 1186 1083 ASSIGN
54 1043 3802 EXPLICIT
55 1083 1043 ASSIGN
56 1083 1186 IMPLICIT
57 1114 1082 ASSIGN
58 1114 1043 ASSIGN
59 1114 1083 ASSIGN
60 1114 1184 IMPLICIT
61 1184 1082 ASSIGN
62 1184 1043 ASSIGN
63 1184 1083 ASSIGN
64 1184 1114 ASSIGN
65 1186 1043 ASSIGN
66 1186 1083 ASSIGN
67 3802 1043 ASSIGN

query TT rowsort
SELECT s.typname, t.typname
Expand Down
37 changes: 37 additions & 0 deletions e2e_test/batch/types/jsonb.slt.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
statement ok
SET RW_IMPLICIT_FLUSH TO true;

query T rowsort
values ('{"a":[2, true, "", {}]}'::jsonb), ('1'), ('true'), ('null'), (null), ('[1, true]');
----
1
NULL
[1,true]
null
true
{"a":[2,true,"",{}]}

statement ok
create table t (v1 jsonb);

statement ok
insert into t values ('1'), ('true'), ('null'), (null);

query T rowsort
select * from t;
----
1
NULL
null
true

query T
select * from t order by v1::varchar;
----
1
null
true
NULL

statement ok
drop table t;
44 changes: 44 additions & 0 deletions e2e_test/batch/types/jsonb_ord.slt.part
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# We do not intend to support using `jsonb` type for `group by` / `order by` / `primary key`
# Before #7981 is done, we need these tests to make sure our system do not panic.
# After #7981, we need them to make sure proper errors are returned to user.

statement ok
SET RW_IMPLICIT_FLUSH TO true;

statement ok
values ('{"a":[2, true, "", {}]}'::jsonb), ('1'), ('true'), ('null'), (null), ('[1, true]') order by 1;

statement ok
create table t (v1 jsonb);

statement ok
insert into t values ('1'), ('true'), ('null'), (null);

statement ok
select * from t order by v1;

# deserialize length
statement ok
create materialized view mv1 as select * from t group by v1;

statement ok
select * from mv1;

statement ok
drop materialized view mv1;

# deserialize pk
statement ok
create table t2 (v1 jsonb primary key);

statement ok
insert into t2 values ('1'), ('true'), ('null'), (null);

statement ok
select * from t2;

statement ok
drop table t2;

statement ok
drop table t;
9 changes: 9 additions & 0 deletions src/common/src/array/jsonb_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ impl crate::types::to_binary::ToBinary for JsonbRef<'_> {
}
}

impl std::str::FromStr for JsonbVal {
type Err = <Value as std::str::FromStr>::Err;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let v: Value = s.parse()?;
Ok(Self(v.into()))
}
}

impl JsonbVal {
/// Avoid this function (or `impl From<Value>`) which is leak of abstraction.
/// In most cases you would be using `JsonbRef`.
Expand Down
12 changes: 12 additions & 0 deletions src/expr/src/expr/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ macro_rules! list {

pub(crate) use list;

#[macro_export]
macro_rules! jsonb {
($macro:ident) => {
$macro! {
risingwave_common::types::DataType::Jsonb,
risingwave_common::array::JsonbArray
}
};
}

pub(crate) use jsonb;

#[macro_export]
macro_rules! int16 {
($macro:ident) => {
Expand Down
1 change: 1 addition & 0 deletions src/expr/src/sig/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub static CAST_MAP: LazyLock<CastMap> = LazyLock::new(|| {
T::Timestamptz,
T::Time,
T::Interval,
T::Jsonb,
] {
m.insert((t, T::Varchar), CastContext::Assign);
m.insert((T::Varchar, t), CastContext::Explicit);
Expand Down
2 changes: 2 additions & 0 deletions src/expr/src/vector_op/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ macro_rules! for_all_cast_variants {
{ varchar, decimal, str_parse, false },
{ varchar, boolean, str_to_bool, false },
{ varchar, bytea, str_to_bytea, false },
{ varchar, jsonb, str_parse, false },
// `str_to_list` requires `target_elem_type` and is handled elsewhere

{ boolean, varchar, bool_to_varchar, false },
Expand All @@ -469,6 +470,7 @@ macro_rules! for_all_cast_variants {
{ interval, varchar, general_to_text, false },
{ date, varchar, general_to_text, false },
{ timestamp, varchar, general_to_text, false },
{ jsonb, varchar, |x, w| general_to_text(x, w), false },
{ list, varchar, |x, w| general_to_text(x, w), false },

{ boolean, int32, try_cast, false },
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/binder/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ pub fn bind_data_type(data_type: &AstDataType) -> Result<DataType> {
"float4" => DataType::Float32,
"float8" => DataType::Float64,
"timestamptz" => DataType::Timestamptz,
"jsonb" => DataType::Jsonb,
_ => return Err(new_err().into()),
}
}
Expand Down

0 comments on commit 7c16d15

Please sign in to comment.