Skip to content

Commit f6758e5

Browse files
TennyZhuangLi0k
authored andcommitted
feat(catalog): add pg_type.typinput and support dummy casting to regproc (#12272)
Signed-off-by: TennyZhuang <[email protected]>
1 parent a82f328 commit f6758e5

File tree

12 files changed

+90
-52
lines changed

12 files changed

+90
-52
lines changed
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
query ITITT
2-
SELECT oid, typname, typelem, typnotnull, typtype FROM pg_catalog.pg_type order by oid;
2+
SELECT oid, typname, typelem, typnotnull, typtype, typinput FROM pg_catalog.pg_type order by oid;
33
----
4-
16 bool 0 f b
5-
17 bytea 0 f b
6-
20 int8 0 f b
7-
21 int2 0 f b
8-
23 int4 0 f b
9-
25 text 0 f b
10-
700 float4 0 f b
11-
701 float8 0 f b
12-
1043 varchar 0 f b
13-
1082 date 0 f b
14-
1083 time 0 f b
15-
1114 timestamp 0 f b
16-
1184 timestamptz 0 f b
17-
1186 interval 0 f b
18-
1301 rw_int256 0 f b
19-
1700 numeric 0 f b
20-
3802 jsonb 0 f b
4+
16 bool 0 f b boolin
5+
17 bytea 0 f b byteain
6+
20 int8 0 f b int8in
7+
21 int2 0 f b int2in
8+
23 int4 0 f b int4in
9+
25 text 0 f b textin
10+
700 float4 0 f b float4in
11+
701 float8 0 f b float8in
12+
1043 varchar 0 f b varcharin
13+
1082 date 0 f b date_in
14+
1083 time 0 f b time_in
15+
1114 timestamp 0 f b timestamp_in
16+
1184 timestamptz 0 f b timestamptz_in
17+
1186 interval 0 f b interval_in
18+
1301 rw_int256 0 f b rw_int256_in
19+
1700 numeric 0 f b numeric_in
20+
3802 jsonb 0 f b jsonb_in

src/common/src/types/postgres_type.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::error::ErrorCode;
1818
/// `DataType` information extracted from PostgreSQL `pg_type`
1919
///
2020
/// ```sql
21-
/// select oid, typarray, typname, typlen from pg_type
21+
/// select oid, typarray, typname, typinput, typlen from pg_type
2222
/// where oid in (16, 21, 23, 20, 1700, 700, 701, 1043, 17, 1082, 1114, 1184, 1083, 1186, 3802);
2323
/// ```
2424
///
@@ -30,21 +30,21 @@ macro_rules! for_all_base_types {
3030
($macro:ident $(, $x:tt)*) => {
3131
$macro! {
3232
$($x, )*
33-
{ Boolean | 16 | 1000 | bool | 1 }
34-
{ Bytea | 17 | 1001 | bytea | -1 }
35-
{ Int64 | 20 | 1016 | int8 | 8 }
36-
{ Int16 | 21 | 1005 | int2 | 2 }
37-
{ Int32 | 23 | 1007 | int4 | 4 }
38-
{ Float32 | 700 | 1021 | float4 | 4 }
39-
{ Float64 | 701 | 1022 | float8 | 8 }
40-
{ Varchar | 1043 | 1015 | varchar | -1 }
41-
{ Date | 1082 | 1182 | date | 4 }
42-
{ Time | 1083 | 1183 | time | 8 }
43-
{ Timestamp | 1114 | 1115 | timestamp | 8 }
44-
{ Timestamptz | 1184 | 1185 | timestamptz | 8 }
45-
{ Interval | 1186 | 1187 | interval | 16 }
46-
{ Decimal | 1700 | 1231 | numeric | -1 }
47-
{ Jsonb | 3802 | 3807 | jsonb | -1 }
33+
{ Boolean | 16 | 1000 | bool | boolin | 1 }
34+
{ Bytea | 17 | 1001 | bytea | byteain | -1 }
35+
{ Int64 | 20 | 1016 | int8 | int8in | 8 }
36+
{ Int16 | 21 | 1005 | int2 | int2in | 2 }
37+
{ Int32 | 23 | 1007 | int4 | int4in | 4 }
38+
{ Float32 | 700 | 1021 | float4 | float4in | 4 }
39+
{ Float64 | 701 | 1022 | float8 | float8in | 8 }
40+
{ Varchar | 1043 | 1015 | varchar | varcharin | -1 }
41+
{ Date | 1082 | 1182 | date | date_in | 4 }
42+
{ Time | 1083 | 1183 | time | time_in | 8 }
43+
{ Timestamp | 1114 | 1115 | timestamp | timestamp_in | 8 }
44+
{ Timestamptz | 1184 | 1185 | timestamptz | timestamptz_in | 8 }
45+
{ Interval | 1186 | 1187 | interval | interval_in | 16 }
46+
{ Decimal | 1700 | 1231 | numeric | numeric_in | -1 }
47+
{ Jsonb | 3802 | 3807 | jsonb | jsonb_in | -1 }
4848
}
4949
};
5050
}
@@ -53,7 +53,7 @@ macro_rules! for_all_base_types {
5353
impl DataType {
5454
pub fn type_len(&self) -> i16 {
5555
macro_rules! impl_type_len {
56-
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $len:literal } )*) => {
56+
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $input:ident | $len:literal } )*) => {
5757
match self {
5858
$(
5959
DataType::$enum => $len,
@@ -75,7 +75,7 @@ impl DataType {
7575
// For Numeric(aka Decimal): oid = 1700, array_type_oid = 1231
7676
pub fn from_oid(oid: i32) -> crate::error::Result<Self> {
7777
macro_rules! impl_from_oid {
78-
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $len:literal } )*) => {
78+
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $input:ident | $len:literal } )*) => {
7979
match oid {
8080
$(
8181
$oid => Ok(DataType::$enum),
@@ -95,7 +95,7 @@ impl DataType {
9595

9696
pub fn to_oid(&self) -> i32 {
9797
macro_rules! impl_to_oid {
98-
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $len:literal } )*) => {
98+
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $input:ident | $len:literal } )*) => {
9999
match self {
100100
$(
101101
DataType::$enum => $oid,
@@ -121,7 +121,7 @@ impl DataType {
121121

122122
pub fn pg_name(&self) -> &'static str {
123123
macro_rules! impl_pg_name {
124-
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $len:literal } )*) => {
124+
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $input:ident | $len:literal } )*) => {
125125
match self {
126126
$(
127127
DataType::$enum => stringify!($name),

src/frontend/planner_test/tests/testdata/input/pg_catalog.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@
2323
expected_outputs:
2424
- batch_plan
2525
- logical_plan
26+
- sql: |
27+
select 'boolin'::regproc
28+
expected_outputs:
29+
- logical_plan
30+
- batch_plan

src/frontend/planner_test/tests/testdata/output/pg_catalog.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
- sql: |
33
select * from pg_catalog.pg_type
44
logical_plan: |-
5-
LogicalProject { exprs: [rw_types.id, rw_types.name, 0:Int32, 0:Int32, false:Boolean, 0:Int32, -1:Int32, 0:Int32, 0:Int32, rw_schemas.id, 'b':Varchar, 0:Int32, null:Varchar, null:Varchar, null:Int32] }
5+
LogicalProject { exprs: [rw_types.id, rw_types.name, 0:Int32, 0:Int32, rw_types.input_oid, false:Boolean, 0:Int32, -1:Int32, 0:Int32, 0:Int32, rw_schemas.id, 'b':Varchar, 0:Int32, null:Varchar, null:Varchar, null:Int32] }
66
└─LogicalShare { id: 3 }
7-
└─LogicalProject { exprs: [rw_types.id, rw_types.name, 0:Int32, 0:Int32, false:Boolean, 0:Int32, -1:Int32, 0:Int32, 0:Int32, rw_schemas.id, 'b':Varchar, 0:Int32, null:Varchar, null:Varchar, null:Int32] }
7+
└─LogicalProject { exprs: [rw_types.id, rw_types.name, 0:Int32, 0:Int32, rw_types.input_oid, false:Boolean, 0:Int32, -1:Int32, 0:Int32, 0:Int32, rw_schemas.id, 'b':Varchar, 0:Int32, null:Varchar, null:Varchar, null:Int32] }
88
└─LogicalJoin { type: Inner, on: (rw_schemas.name = 'pg_catalog':Varchar), output: all }
9-
├─LogicalScan { table: rw_types, columns: [rw_types.id, rw_types.name] }
9+
├─LogicalScan { table: rw_types, columns: [rw_types.id, rw_types.name, rw_types.input_oid] }
1010
└─LogicalScan { table: rw_schemas, columns: [rw_schemas.id, rw_schemas.name, rw_schemas.owner, rw_schemas.acl] }
1111
batch_plan: |-
12-
BatchProject { exprs: [rw_types.id, rw_types.name, 0:Int32, 0:Int32, false:Boolean, 0:Int32, -1:Int32, 0:Int32, 0:Int32, rw_schemas.id, 'b':Varchar, 0:Int32, null:Varchar, null:Varchar, null:Int32] }
12+
BatchProject { exprs: [rw_types.id, rw_types.name, 0:Int32, 0:Int32, rw_types.input_oid, false:Boolean, 0:Int32, -1:Int32, 0:Int32, 0:Int32, rw_schemas.id, 'b':Varchar, 0:Int32, null:Varchar, null:Varchar, null:Int32] }
1313
└─BatchNestedLoopJoin { type: Inner, predicate: true, output: all }
14-
├─BatchScan { table: rw_types, columns: [rw_types.id, rw_types.name], distribution: Single }
14+
├─BatchScan { table: rw_types, columns: [rw_types.id, rw_types.name, rw_types.input_oid], distribution: Single }
1515
└─BatchProject { exprs: [rw_schemas.id] }
1616
└─BatchFilter { predicate: (rw_schemas.name = 'pg_catalog':Varchar) }
1717
└─BatchScan { table: rw_schemas, columns: [rw_schemas.id, rw_schemas.name], distribution: Single }
@@ -224,3 +224,9 @@
224224
LogicalProject { exprs: [2:Int32] }
225225
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
226226
batch_plan: 'BatchValues { rows: [[2:Int32]] }'
227+
- sql: |
228+
select 'boolin'::regproc
229+
logical_plan: |-
230+
LogicalProject { exprs: ['boolin':Varchar] }
231+
└─LogicalValues { rows: [[]], schema: Schema { fields: [] } }
232+
batch_plan: 'BatchValues { rows: [[''boolin'':Varchar]] }'

src/frontend/src/binder/expr/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,18 @@ impl Binder {
550550
self.resolve_regclass(class_name)
551551
.map(|id| ExprImpl::literal_int(id as i32))
552552
}
553+
AstDataType::Regproc => {
554+
let lhs = self.bind_expr_inner(expr)?;
555+
let lhs_ty = lhs.return_type();
556+
if lhs_ty == DataType::Varchar {
557+
// FIXME: Currently, we only allow VARCHAR to be casted to Regproc.
558+
// FIXME: Check whether it's a valid proc
559+
// FIXME: The return type should be casted to Regproc, but we don't have this type.
560+
Ok(lhs)
561+
} else {
562+
Err(ErrorCode::BindError(format!("Can't cast {} to regproc", lhs_ty)).into())
563+
}
564+
}
553565
_ => self.bind_cast_inner(expr, bind_data_type(&data_type)?),
554566
}
555567
}
@@ -655,6 +667,7 @@ pub fn bind_data_type(data_type: &AstDataType) -> Result<DataType> {
655667
}
656668
AstDataType::Bytea => DataType::Bytea,
657669
AstDataType::Regclass
670+
| AstDataType::Regproc
658671
| AstDataType::Uuid
659672
| AstDataType::Custom(_)
660673
| AstDataType::Decimal(_, _)

src/frontend/src/binder/select.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ fn data_type_to_alias(data_type: &AstDataType) -> Option<String> {
852852
}
853853
AstDataType::Interval => "interval".to_string(),
854854
AstDataType::Regclass => "regclass".to_string(),
855+
AstDataType::Regproc => "regproc".to_string(),
855856
AstDataType::Text => "text".to_string(),
856857
AstDataType::Bytea => "bytea".to_string(),
857858
AstDataType::Array(ty) => return data_type_to_alias(ty),

src/frontend/src/catalog/system_catalog/pg_catalog/pg_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub static PG_TYPE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
3131
(DataType::Int32, "typelem"),
3232
// 0
3333
(DataType::Int32, "typarray"),
34+
// FIXME: Should be regproc type
35+
(DataType::Varchar, "typinput"),
3436
// false
3537
(DataType::Boolean, "typnotnull"),
3638
// 0
@@ -58,6 +60,7 @@ pub static PG_TYPE: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
5860
t.name AS typname, \
5961
0 AS typelem, \
6062
0 AS typarray, \
63+
t.input_oid AS typinput, \
6164
false AS typnotnull, \
6265
0 AS typbasetype, \
6366
-1 AS typtypmod, \

src/frontend/src/catalog/system_catalog/rw_catalog/rw_types.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,41 @@ use risingwave_common::types::{DataType, ScalarImpl};
2424
use crate::catalog::system_catalog::{BuiltinTable, SysCatalogReaderImpl};
2525

2626
macro_rules! impl_pg_type_data {
27-
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $len:literal } )*) => {
27+
($( { $enum:ident | $oid:literal | $oid_array:literal | $name:ident | $input:ident | $len:literal } )*) => {
2828
&[
2929
$(
30-
($oid, stringify!($name)),
30+
($oid, stringify!($name), stringify!($input)),
3131
)*
3232
// Note: rw doesn't support `text` type, returning it is just a workaround to be compatible
3333
// with PostgreSQL.
34-
(25, "text"),
35-
(1301, "rw_int256"),
34+
(25, "text", "textin"),
35+
(1301, "rw_int256", "rw_int256_in"),
3636
]
3737
}
3838
}
39-
pub const RW_TYPE_DATA: &[(i32, &str)] = for_all_base_types! { impl_pg_type_data };
39+
pub const RW_TYPE_DATA: &[(i32, &str, &str)] = for_all_base_types! { impl_pg_type_data };
4040

4141
/// `rw_types` stores all supported types in the database.
4242
pub static RW_TYPES: LazyLock<BuiltinTable> = LazyLock::new(|| BuiltinTable {
4343
name: "rw_types",
4444
schema: RW_CATALOG_SCHEMA_NAME,
45-
columns: &[(DataType::Int32, "id"), (DataType::Varchar, "name")],
45+
columns: &[
46+
(DataType::Int32, "id"),
47+
(DataType::Varchar, "name"),
48+
(DataType::Varchar, "input_oid"),
49+
],
4650
pk: &[0],
4751
});
4852

4953
impl SysCatalogReaderImpl {
5054
pub fn read_rw_types(&self) -> Result<Vec<OwnedRow>> {
5155
Ok(RW_TYPE_DATA
5256
.iter()
53-
.map(|(id, name)| {
57+
.map(|(id, name, input)| {
5458
OwnedRow::new(vec![
5559
Some(ScalarImpl::Int32(*id)),
5660
Some(ScalarImpl::Utf8(name.to_string().into())),
61+
Some(ScalarImpl::Utf8(input.to_string().into())),
5762
])
5863
})
5964
.collect_vec())

src/meta/src/manager/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ use crate::manager::{
2525
NotificationManagerRef,
2626
};
2727
use crate::model::ClusterId;
28+
use crate::storage::MetaStoreRef;
2829
#[cfg(any(test, feature = "test"))]
29-
use crate::storage::MemStore;
30-
use crate::storage::{MetaStoreBoxExt, MetaStoreRef};
30+
use crate::storage::{MemStore, MetaStoreBoxExt};
3131
use crate::MetaResult;
3232

3333
/// [`MetaSrvEnv`] is the global environment in Meta service. The instance will be shared by all

src/sqlparser/src/ast/data_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ pub enum DataType {
5656
Interval,
5757
/// Regclass used in postgresql serial
5858
Regclass,
59+
/// Regproc used in postgresql function
60+
Regproc,
5961
/// Text
6062
Text,
6163
/// Bytea
@@ -97,6 +99,7 @@ impl fmt::Display for DataType {
9799
}
98100
DataType::Interval => write!(f, "INTERVAL"),
99101
DataType::Regclass => write!(f, "REGCLASS"),
102+
DataType::Regproc => write!(f, "REGPROC"),
100103
DataType::Text => write!(f, "TEXT"),
101104
DataType::Bytea => write!(f, "BYTEA"),
102105
DataType::Array(ty) => write!(f, "{}[]", ty),

src/sqlparser/src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ define_keywords!(
395395
REFERENCING,
396396
REGCLASS,
397397
REGISTRY,
398+
REGPROC,
398399
REGR_AVGX,
399400
REGR_AVGY,
400401
REGR_COUNT,

src/sqlparser/src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,6 +3285,7 @@ impl Parser {
32853285
// parse_interval_literal for a taste.
32863286
Keyword::INTERVAL => Ok(DataType::Interval),
32873287
Keyword::REGCLASS => Ok(DataType::Regclass),
3288+
Keyword::REGPROC => Ok(DataType::Regproc),
32883289
Keyword::TEXT => {
32893290
if self.consume_token(&Token::LBracket) {
32903291
// Note: this is postgresql-specific

0 commit comments

Comments
 (0)