Skip to content

Commit 1d0dc7c

Browse files
Postgres: Add support for text search types (apache#1889)
1 parent 204d3b4 commit 1d0dc7c

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/ast/data_type.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,14 @@ pub enum DataType {
446446
///
447447
/// [PostgreSQL]: https://www.postgresql.org/docs/9.5/functions-geometry.html
448448
GeometricType(GeometricTypeKind),
449+
/// PostgreSQL text search vectors, see [PostgreSQL].
450+
///
451+
/// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-textsearch.html
452+
TsVector,
453+
/// PostgreSQL text search query, see [PostgreSQL].
454+
///
455+
/// [PostgreSQL]: https://www.postgresql.org/docs/17/datatype-textsearch.html
456+
TsQuery,
449457
}
450458

451459
impl fmt::Display for DataType {
@@ -738,6 +746,8 @@ impl fmt::Display for DataType {
738746
write!(f, "{} TABLE ({})", name, display_comma_separated(columns))
739747
}
740748
DataType::GeometricType(kind) => write!(f, "{}", kind),
749+
DataType::TsVector => write!(f, "TSVECTOR"),
750+
DataType::TsQuery => write!(f, "TSQUERY"),
741751
}
742752
}
743753
}

src/keywords.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,8 @@ define_keywords!(
935935
TRY,
936936
TRY_CAST,
937937
TRY_CONVERT,
938+
TSQUERY,
939+
TSVECTOR,
938940
TUPLE,
939941
TYPE,
940942
UBIGINT,

src/parser/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9909,6 +9909,12 @@ impl<'a> Parser<'a> {
99099909
Ok(DataType::Unsigned)
99109910
}
99119911
}
9912+
Keyword::TSVECTOR if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
9913+
Ok(DataType::TsVector)
9914+
}
9915+
Keyword::TSQUERY if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
9916+
Ok(DataType::TsQuery)
9917+
}
99129918
_ => {
99139919
self.prev_token();
99149920
let type_name = self.parse_object_name(false)?;

tests/sqlparser_postgres.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6201,3 +6201,34 @@ fn parse_alter_table_replica_identity() {
62016201
_ => unreachable!(),
62026202
}
62036203
}
6204+
6205+
#[test]
6206+
fn parse_ts_datatypes() {
6207+
match pg_and_generic().verified_stmt("CREATE TABLE foo (x TSVECTOR)") {
6208+
Statement::CreateTable(CreateTable { columns, .. }) => {
6209+
assert_eq!(
6210+
columns,
6211+
vec![ColumnDef {
6212+
name: "x".into(),
6213+
data_type: DataType::TsVector,
6214+
options: vec![],
6215+
}]
6216+
);
6217+
}
6218+
_ => unreachable!(),
6219+
}
6220+
6221+
match pg_and_generic().verified_stmt("CREATE TABLE foo (x TSQUERY)") {
6222+
Statement::CreateTable(CreateTable { columns, .. }) => {
6223+
assert_eq!(
6224+
columns,
6225+
vec![ColumnDef {
6226+
name: "x".into(),
6227+
data_type: DataType::TsQuery,
6228+
options: vec![],
6229+
}]
6230+
);
6231+
}
6232+
_ => unreachable!(),
6233+
}
6234+
}

0 commit comments

Comments
 (0)