Skip to content

Commit 78e6ddb

Browse files
Postgres: Add support for text search types
Adding support for psql-specific `tsvector` and `tsquery` datatypes.
1 parent 6f42396 commit 78e6ddb

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

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
@@ -934,6 +934,8 @@ define_keywords!(
934934
TRY,
935935
TRY_CAST,
936936
TRY_CONVERT,
937+
TSQUERY,
938+
TSVECTOR,
937939
TUPLE,
938940
TYPE,
939941
UBIGINT,

src/parser/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9913,6 +9913,12 @@ impl<'a> Parser<'a> {
99139913
Ok(DataType::Unsigned)
99149914
}
99159915
}
9916+
Keyword::TSVECTOR if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
9917+
Ok(DataType::TsVector)
9918+
}
9919+
Keyword::TSQUERY if dialect_is!(dialect is PostgreSqlDialect | GenericDialect) => {
9920+
Ok(DataType::TsQuery)
9921+
}
99169922
_ => {
99179923
self.prev_token();
99189924
let type_name = self.parse_object_name(false)?;

tests/sqlparser_postgres.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6201,3 +6201,49 @@ fn parse_alter_table_replica_identity() {
62016201
_ => unreachable!(),
62026202
}
62036203
}
6204+
6205+
#[test]
6206+
fn parse_tsvector_datatype() {
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+
6222+
#[test]
6223+
fn parse_tsquery_datatype() {
6224+
match pg_and_generic().verified_stmt("CREATE TABLE foo (x TSQUERY)") {
6225+
Statement::CreateTable(CreateTable { columns, .. }) => {
6226+
assert_eq!(
6227+
columns,
6228+
vec![ColumnDef {
6229+
name: "x".into(),
6230+
data_type: DataType::TsQuery,
6231+
options: vec![],
6232+
}]
6233+
);
6234+
}
6235+
_ => unreachable!(),
6236+
}
6237+
}
6238+
6239+
#[test]
6240+
fn parse_to_tsvector_function() {
6241+
let sql = "SELECT to_tsvector('english', 'foo bar baz')";
6242+
pg_and_generic().verified_only_select(sql);
6243+
}
6244+
6245+
#[test]
6246+
fn parse_to_tsquery_function() {
6247+
let sql = "SELECT to_tsquery('Fat:ab & Cats')";
6248+
pg_and_generic().verified_only_select(sql);
6249+
}

0 commit comments

Comments
 (0)