Skip to content

Commit

Permalink
implement pg_get_keywords
Browse files Browse the repository at this point in the history
Signed-off-by: Runji Wang <[email protected]>
  • Loading branch information
wangrunji0408 committed May 30, 2024
1 parent c31d1d8 commit 75a8f54
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proto/expr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ message TableFunction {
GENERATE_SUBSCRIPTS = 5;
// buf:lint:ignore ENUM_VALUE_UPPER_SNAKE_CASE
_PG_EXPANDARRAY = 6;
PG_GET_KEYWORDS = 18;
// Jsonb functions
JSONB_ARRAY_ELEMENTS = 10;
JSONB_ARRAY_ELEMENTS_TEXT = 11;
Expand Down
1 change: 1 addition & 0 deletions src/expr/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ regex = "1"
risingwave_common = { workspace = true }
risingwave_common_estimate_size = { workspace = true }
risingwave_expr = { workspace = true }
risingwave_sqlparser = { workspace = true }
risingwave_pb = { workspace = true }
rust_decimal = { version = "1", features = ["db-postgres", "maths"] }
self_cell = "1.0.1"
Expand Down
1 change: 1 addition & 0 deletions src/expr/impl/src/table_function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ mod generate_series;
mod generate_subscripts;
mod jsonb;
mod pg_expandarray;
mod pg_get_keywords;
mod regexp_matches;
mod unnest;
58 changes: 58 additions & 0 deletions src/expr/impl/src/table_function/pg_get_keywords.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use risingwave_expr::function;
use risingwave_sqlparser::keywords::{
ALL_KEYWORDS_INDEX, RESERVED_FOR_COLUMN_ALIAS, RESERVED_FOR_COLUMN_OR_TABLE_NAME,
};

/// Returns a set of records describing the SQL keywords recognized by the server.
///
/// The word column contains the keyword.
///
/// The catcode column contains a category code:
/// - U for an unreserved keyword
/// - C for a keyword that can be a column name
/// - T for a keyword that can be a type or function name
/// - R for a fully reserved keyword.
///
/// The catdesc column contains a possibly-localized string describing the keyword's category.
///
/// ```slt
/// query TTT
/// select * from pg_get_keywords() where word = 'add';
/// ----
/// add U unreserved
/// ```
#[function("pg_get_keywords() -> setof struct<word varchar, catcode varchar, catdesc varchar>")]
fn pg_get_keywords() -> impl Iterator<Item = (Box<str>, &'static str, &'static str)> {
ALL_KEYWORDS_INDEX.iter().map(|keyword| {
// FIXME: The current category is not correct. Many are different from the PostgreSQL.
let catcode = if !RESERVED_FOR_COLUMN_OR_TABLE_NAME.contains(keyword) {
"U"
} else if !RESERVED_FOR_COLUMN_ALIAS.contains(keyword) {
"C"
} else {
"R"
};
let catdesc = match catcode {
"U" => "unreserved",
"C" => "unreserved (cannot be function or type name)",
"T" => "reserved (can be function or type name)",
"R" => "reserved",
_ => unreachable!(),
};
(keyword.to_string().to_lowercase().into(), catcode, catdesc)
})
}
4 changes: 2 additions & 2 deletions src/sqlparser/src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ macro_rules! define_keywords {
];

$(kw_def!($ident $(= $string_keyword)?);)*
pub const ALL_KEYWORDS: &[&str] = &[
pub const ALL_KEYWORDS: &[&'static str] = &[
$($ident),*
];
};
Expand Down Expand Up @@ -651,7 +651,7 @@ pub const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
/// Can't be used as a column or table name in PostgreSQL.
///
/// This list is taken from the following table, for all "reserved" words in the PostgreSQL column,
/// includinhg "can be function or type" and "requires AS". <https://www.postgresql.org/docs/14/sql-keywords-appendix.html#KEYWORDS-TABLE>
/// including "can be function or type" and "requires AS". <https://www.postgresql.org/docs/14/sql-keywords-appendix.html#KEYWORDS-TABLE>
///
/// `SELECT` and `WITH` were commented out because the following won't parse:
/// `SELECT (SELECT 1)` or `SELECT (WITH a AS (SELECT 1) SELECT 1)`
Expand Down

0 comments on commit 75a8f54

Please sign in to comment.