Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds non-reserved keywords (POC) #221

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 70 additions & 2 deletions partiql-parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ pub enum Token<'input> {
Ion(&'input str),

// Keywords
#[regex("(?i:Acyclic)")]
Acyclic,
#[regex("(?i:All)")]
All,
#[regex("(?i:Asc)")]
Expand All @@ -522,6 +524,8 @@ pub enum Token<'input> {
At,
#[regex("(?i:Between)")]
Between,
#[regex("(?i:Both)")]
Both,
#[regex("(?i:By)")]
By,
#[regex("(?i:Case)")]
Expand All @@ -534,6 +538,8 @@ pub enum Token<'input> {
Desc,
#[regex("(?i:Distinct)")]
Distinct,
#[regex("(?i:Domain)")]
Domain,
#[regex("(?i:Else)")]
Else,
#[regex("(?i:End)")]
Expand Down Expand Up @@ -570,6 +576,8 @@ pub enum Token<'input> {
Last,
#[regex("(?i:Lateral)")]
Lateral,
#[regex("(?i:Leading)")]
Leading,
#[regex("(?i:Left)")]
Left,
#[regex("(?i:Like)")]
Expand Down Expand Up @@ -602,10 +610,14 @@ pub enum Token<'input> {
Pivot,
#[regex("(?i:Preserve)")]
Preserve,
#[regex("(?i:Public)")]
Public,
#[regex("(?i:Right)")]
Right,
#[regex("(?i:Select)")]
Select,
#[regex("(?i:Simple)")]
Simple,
#[regex("(?i:Table)")]
Table,
#[regex("(?i:Time)")]
Expand All @@ -614,12 +626,18 @@ pub enum Token<'input> {
Timestamp,
#[regex("(?i:Then)")]
Then,
#[regex("(?i:Trail)")]
Trail,
#[regex("(?i:Trailing)")]
Trailing,
#[regex("(?i:True)")]
True,
#[regex("(?i:Union)")]
Union,
#[regex("(?i:Unpivot)")]
Unpivot,
#[regex("(?i:User)")]
User,
#[regex("(?i:Using)")]
Using,
#[regex("(?i:Value)")]
Expand All @@ -642,17 +660,20 @@ impl<'input> Token<'input> {
pub fn is_keyword(&self) -> bool {
matches!(
self,
Token::All
Token::Acyclic
| Token::All
| Token::Asc
| Token::And
| Token::As
| Token::At
| Token::Between
| Token::Both
| Token::By
| Token::Cross
| Token::Date
| Token::Desc
| Token::Distinct
| Token::Domain
| Token::Escape
| Token::Except
| Token::First
Expand All @@ -668,6 +689,7 @@ impl<'input> Token<'input> {
| Token::Join
| Token::Last
| Token::Lateral
| Token::Leading
| Token::Left
| Token::Like
| Token::Limit
Expand All @@ -684,14 +706,19 @@ impl<'input> Token<'input> {
| Token::Partial
| Token::Pivot
| Token::Preserve
| Token::Public
| Token::Right
| Token::Select
| Token::Simple
| Token::Table
| Token::Time
| Token::Timestamp
| Token::Then
| Token::Trail
| Token::Trailing
| Token::Union
| Token::Unpivot
| Token::User
| Token::Using
| Token::Value
| Token::Values
Expand Down Expand Up @@ -748,18 +775,21 @@ impl<'input> fmt::Display for Token<'input> {
Token::EmbeddedIonQuote => write!(f, "<ION>"),
Token::Ion(txt) => write!(f, "<{}:ION>", txt),

Token::All
Token::Acyclic
| Token::All
| Token::Asc
| Token::And
| Token::As
| Token::At
| Token::Between
| Token::Both
| Token::By
| Token::Case
| Token::Cross
| Token::Date
| Token::Desc
| Token::Distinct
| Token::Domain
| Token::Else
| Token::End
| Token::Escape
Expand All @@ -778,6 +808,7 @@ impl<'input> fmt::Display for Token<'input> {
| Token::Join
| Token::Last
| Token::Lateral
| Token::Leading
| Token::Left
| Token::Like
| Token::Limit
Expand All @@ -794,15 +825,20 @@ impl<'input> fmt::Display for Token<'input> {
| Token::Partial
| Token::Pivot
| Token::Preserve
| Token::Public
| Token::Right
| Token::Select
| Token::Simple
| Token::Table
| Token::Time
| Token::Timestamp
| Token::Then
| Token::Trail
| Token::Trailing
| Token::True
| Token::Union
| Token::Unpivot
| Token::User
| Token::Using
| Token::Value
| Token::Values
Expand Down Expand Up @@ -1107,6 +1143,38 @@ mod tests {
Ok(())
}

#[test]
fn select_non_reserved_keywords() -> Result<(), ParseError<'static, BytePosition>> {
let query = "SELECT acyclic, BoTh, DOMAIN, SImple, Trail, TRailing, USER\nfrom @\"foo\"";
let mut offset_tracker = LineOffsetTracker::default();
let lexer = PartiqlLexer::new(query, &mut offset_tracker);
let toks: Vec<_> = lexer.collect::<Result<_, _>>()?;

assert_eq!(
vec![
Token::Select,
Token::Acyclic,
Token::Comma,
Token::Both,
Token::Comma,
Token::Domain,
Token::Comma,
Token::Simple,
Token::Comma,
Token::Trail,
Token::Comma,
Token::Trailing,
Token::Comma,
Token::User,
Token::From,
Token::QuotedAtIdentifier("foo"),
],
toks.into_iter().map(|(_s, t, _e)| t).collect::<Vec<_>>()
);
assert_eq!(offset_tracker.num_lines(), 2);
Ok(())
}

#[test]
fn select_comment_block() -> Result<(), ParseError<'static, BytePosition>> {
let query = "SELECT /*comment*/ g";
Expand Down
49 changes: 49 additions & 0 deletions partiql-parser/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,55 @@ mod tests {
}
}

// PROOF OF CONCEPT: NON-RESERVED KEYWORDS
mod non_reserved {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests all pass without any of the above parser changes, except for with_order (thought I think the gpml tests would fail when the gpml parsing rules are included).

I'm also not sure that i feel treating order as an unreserved keyword is a good idea...

use super::*;

#[test]
fn projection_list_trim_spec() {
parse!(r#"SELECT leading FROM t"#);
parse!(r#"SELECT leading, a FROM t"#);
parse!(r#"SELECT leading + trailing, b FROM t"#);
parse!(r#"SELECT both + leading + trailing, a, b, c FROM t"#);
}

#[test]
fn from_source() {
parse!(r#"SELECT leading, trailing, both FROM leading, trailing, both"#);
}

#[test]
fn with_trim() {
parse!(
r#"SELECT leading + trim(leading leading FROM ' hello world'), both FROM leading, trailing, both"#
);
}

#[test]
fn with_order() {
parse!(r#"SELECT order FROM t ORDER BY order + 5"#);
parse!(r#"SELECT order FROM order ORDER BY order + 5"#);
parse!(r#"SELECT ORDER FROM ORDER ORDER BY ORDER + 5"#);
}

#[test]
fn with_gpml() {
parse!(r#"SELECT acyclic, trail, simple FROM t"#);
parse!(r#"AcYcLiC"#);
parse!(r#"TrAiL"#);
parse!(r#"SiMpLe"#);
}

//
#[test]
fn external_customer_request() {
parse!(r#"SELECT user, puBlIC, DOMAIN FROM USER, pUbLIc, domain"#);
parse!(r#"USER"#);
parse!(r#"pUbLIC"#);
parse!(r#"domain"#);
}
}

mod errors {
use super::*;
use crate::error::{LexError, UnexpectedToken, UnexpectedTokenData};
Expand Down
30 changes: 30 additions & 0 deletions partiql-parser/src/parse/partiql.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,27 @@ VarRefExpr: ast::Expr = {
name: ast::SymbolPrimitive { value: ident.to_owned(), case: ast::CaseSensitivity::CaseSensitive },
qualifier: ast::ScopeQualifier::Unqualified
},lo..hi)),
<lo:@L> <ident:NonReservedKeyword> <hi:@R> => ast::Expr::VarRef(state.node(ast::VarRef {
name: ast::SymbolPrimitive { value: ident.to_owned(), case: ast::CaseSensitivity::CaseInsensitive },
qualifier: ast::ScopeQualifier::Unqualified
},lo..hi)),
}

// PROOF OF CONCEPT
// These are all the proposed non-reserved keywords (except ORDER)
// ORDER is only in this list to prove that even ORDER can be parsed as a non-reserved keyword.
#[inline]
NonReservedKeyword: &'static str = {
"ACYCLIC" => "ACYCLIC",
"BOTH" => "BOTH",
"DOMAIN" => "DOMAIN",
"LEADING" => "LEADING",
"ORDER" => "ORDER",
"PUBLIC" => "PUBLIC",
"SIMPLE" => "SIMPLE",
"TRAIL" => "TRAIL",
"TRAILING" => "TRAILING",
"USER" => "USER",
}

// ------------------------------------------------------------------------------ //
Expand Down Expand Up @@ -1267,18 +1288,21 @@ extern {
"Ion" => lexer::Token::Ion(<&'input str>),

// Keywords
"ACYCLIC" => lexer::Token::Acyclic,
"ALL" => lexer::Token::All,
"ASC" => lexer::Token::Asc,
"AND" => lexer::Token::And,
"AS" => lexer::Token::As,
"AT" => lexer::Token::At,
"BETWEEN" => lexer::Token::Between,
"BOTH" => lexer::Token::Both,
"BY" => lexer::Token::By,
"CASE" => lexer::Token::Case,
"CROSS" => lexer::Token::Cross,
"DATE" => lexer::Token::Date,
"DESC" => lexer::Token::Desc,
"DISTINCT" => lexer::Token::Distinct,
"DOMAIN" => lexer::Token::Domain,
"ELSE" => lexer::Token::Else,
"END" => lexer::Token::End,
"ESCAPE" => lexer::Token::Escape,
Expand All @@ -1297,6 +1321,7 @@ extern {
"JOIN" => lexer::Token::Join,
"LAST" => lexer::Token::Last,
"LATERAL" => lexer::Token::Lateral,
"LEADING" => lexer::Token::Leading,
"LEFT" => lexer::Token::Left,
"LIKE" => lexer::Token::Like,
"LIMIT" => lexer::Token::Limit,
Expand All @@ -1313,15 +1338,20 @@ extern {
"PARTIAL" => lexer::Token::Partial,
"PIVOT" => lexer::Token::Pivot,
"PRESERVE" => lexer::Token::Preserve,
"PUBLIC" => lexer::Token::Public,
"RIGHT" => lexer::Token::Right,
"SELECT" => lexer::Token::Select,
"SIMPLE" => lexer::Token::Simple,
"TABLE" => lexer::Token::Table,
"TIME" => lexer::Token::Time,
"TIMESTAMP" => lexer::Token::Timestamp,
"THEN" => lexer::Token::Then,
"TRAIL" => lexer::Token::Trail,
"TRAILING" => lexer::Token::Trailing,
"TRUE" => lexer::Token::True,
"UNION" => lexer::Token::Union,
"UNPIVOT" => lexer::Token::Unpivot,
"USER" => lexer::Token::User,
"USING" => lexer::Token::Using,
"VALUE" => lexer::Token::Value,
"VALUES" => lexer::Token::Values,
Expand Down
Loading