Skip to content

Commit

Permalink
fix select * from pg_get_keywords() and impl pg_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 75a8f54 commit a166de8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
25 changes: 1 addition & 24 deletions src/frontend/src/binder/relation/table_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ use std::str::FromStr;

use itertools::Itertools;
use risingwave_common::bail_not_implemented;
use risingwave_common::catalog::{
Field, Schema, PG_CATALOG_SCHEMA_NAME, RW_INTERNAL_TABLE_FUNCTION_NAME,
};
use risingwave_common::catalog::{Field, Schema, RW_INTERNAL_TABLE_FUNCTION_NAME};
use risingwave_common::types::DataType;
use risingwave_sqlparser::ast::{Function, FunctionArg, ObjectName, TableAlias};

use super::watermark::is_watermark_func;
use super::{Binder, Relation, Result, WindowTableFunctionKind};
use crate::binder::bind_context::Clause;
use crate::catalog::system_catalog::pg_catalog::{
PG_GET_KEYWORDS_FUNC_NAME, PG_KEYWORDS_TABLE_NAME,
};
use crate::error::ErrorCode;
use crate::expr::{Expr, ExprImpl};

Expand Down Expand Up @@ -57,24 +52,6 @@ impl Binder {
}
return self.bind_internal_table(args, alias);
}
if func_name.eq_ignore_ascii_case(PG_GET_KEYWORDS_FUNC_NAME)
|| name.real_value().eq_ignore_ascii_case(
format!("{}.{}", PG_CATALOG_SCHEMA_NAME, PG_GET_KEYWORDS_FUNC_NAME).as_str(),
)
{
if with_ordinality {
bail_not_implemented!(
"WITH ORDINALITY for internal/system table function {}",
func_name
);
}
return self.bind_relation_by_name_inner(
Some(PG_CATALOG_SCHEMA_NAME),
PG_KEYWORDS_TABLE_NAME,
alias,
None,
);
}
}
// window table functions (tumble/hop)
if let Ok(kind) = WindowTableFunctionKind::from_str(func_name) {
Expand Down
2 changes: 0 additions & 2 deletions src/frontend/src/catalog/system_catalog/pg_catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,3 @@ mod pg_trigger;
mod pg_type;
mod pg_user;
mod pg_views;

pub use pg_keywords::*;
53 changes: 47 additions & 6 deletions src/frontend/src/catalog/system_catalog/pg_catalog/pg_keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,60 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// The code is same as `expr/impl/src/table_function/pg_get_keywords.rs`.

use risingwave_common::types::Fields;
use risingwave_frontend_macro::system_catalog;
use risingwave_sqlparser::keywords::{
ALL_KEYWORDS_INDEX, RESERVED_FOR_COLUMN_ALIAS, RESERVED_FOR_COLUMN_OR_TABLE_NAME,
};

pub const PG_KEYWORDS_TABLE_NAME: &str = "pg_keywords";
pub const PG_GET_KEYWORDS_FUNC_NAME: &str = "pg_get_keywords";
use crate::catalog::system_catalog::SysCatalogReaderImpl;

/// The catalog `pg_keywords` stores keywords. `pg_get_keywords` returns the content of this table.
/// Ref: [`https://www.postgresql.org/docs/15/functions-info.html`]
// TODO: change to read reserved keywords here
#[system_catalog(view, "pg_catalog.pg_keywords")]
///
/// # Example
///
/// ```slt
/// query TTT
/// select * from pg_keywords where word = 'add';
/// ----
/// add U unreserved
/// ```
#[derive(Fields)]
struct PgKeywords {
#[primary_key]
word: String,
catcode: String,
catdesc: String,
catcode: char,
catdesc: &'static str,
}

#[system_catalog(table, "pg_catalog.pg_keywords")]
fn read_pg_keywords(_reader: &SysCatalogReaderImpl) -> Vec<PgKeywords> {
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!(),
};
PgKeywords {
word: keyword.to_string().to_lowercase(),
catcode,
catdesc,
}
})
.collect()
}

0 comments on commit a166de8

Please sign in to comment.