Skip to content

Commit 68ed827

Browse files
authored
Expands parser/lexer tests to test future possible keywords (#223)
1 parent 4623cf7 commit 68ed827

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

partiql-parser/src/lexer.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,43 @@ mod tests {
11261126
Ok(())
11271127
}
11281128

1129+
/// In the future, the following identifiers may be converted into reserved keywords. In that case,
1130+
/// the following test will need to be modified.
1131+
#[test]
1132+
fn select_non_reserved_keywords() -> Result<(), ParseError<'static, BytePosition>> {
1133+
let query =
1134+
"SELECT acyclic, BoTh, DOMAIN, SiMpLe, Trail, leading, TRailing, USER\nfrom @\"foo\"";
1135+
let mut offset_tracker = LineOffsetTracker::default();
1136+
let lexer = PartiqlLexer::new(query, &mut offset_tracker);
1137+
let toks: Vec<_> = lexer.collect::<Result<_, _>>()?;
1138+
1139+
assert_eq!(
1140+
vec![
1141+
Token::Select,
1142+
Token::UnquotedIdent("acyclic"),
1143+
Token::Comma,
1144+
Token::UnquotedIdent("BoTh"),
1145+
Token::Comma,
1146+
Token::UnquotedIdent("DOMAIN"),
1147+
Token::Comma,
1148+
Token::UnquotedIdent("SiMpLe"),
1149+
Token::Comma,
1150+
Token::UnquotedIdent("Trail"),
1151+
Token::Comma,
1152+
Token::UnquotedIdent("leading"),
1153+
Token::Comma,
1154+
Token::UnquotedIdent("TRailing"),
1155+
Token::Comma,
1156+
Token::UnquotedIdent("USER"),
1157+
Token::From,
1158+
Token::QuotedAtIdentifier("foo"),
1159+
],
1160+
toks.into_iter().map(|(_s, t, _e)| t).collect::<Vec<_>>()
1161+
);
1162+
assert_eq!(offset_tracker.num_lines(), 2);
1163+
Ok(())
1164+
}
1165+
11291166
#[test]
11301167
fn err_invalid_input() {
11311168
let query = "SELECT # FROM data GROUP BY a";

partiql-parser/src/parse/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,48 @@ mod tests {
679679
}
680680
}
681681

682+
/// In the future, the following identifiers may be converted into reserved keywords. In that case,
683+
/// the following tests will need to be modified.
684+
mod non_reserved_keywords {
685+
use super::*;
686+
687+
#[test]
688+
fn projection_list_trim_spec() {
689+
parse!(r#"SELECT leading FROM t"#);
690+
parse!(r#"SELECT leading, a FROM t"#);
691+
parse!(r#"SELECT leading + trailing, b FROM t"#);
692+
parse!(r#"SELECT both + leading + trailing, a, b, c FROM t"#);
693+
}
694+
695+
#[test]
696+
fn from_source_trim_spec() {
697+
parse!(r#"SELECT leading, trailing, both FROM leading, trailing, both"#);
698+
}
699+
700+
#[test]
701+
fn complex_trim() {
702+
parse!(
703+
r#"SELECT leading + trim(leading leading FROM ' hello world'), both FROM leading, trailing, both"#
704+
);
705+
}
706+
707+
#[test]
708+
fn graph_pattern_matching() {
709+
parse!(r#"SELECT acyclic, trail, simple FROM t"#);
710+
parse!(r#"AcYcLiC"#);
711+
parse!(r#"TrAiL"#);
712+
parse!(r#"SiMpLe"#);
713+
}
714+
715+
#[test]
716+
fn user_public_domain() {
717+
parse!(r#"SELECT user, puBlIC, DOMAIN FROM USER, pUbLIc, domain"#);
718+
parse!(r#"USER"#);
719+
parse!(r#"pUbLIC"#);
720+
parse!(r#"domain"#);
721+
}
722+
}
723+
682724
mod errors {
683725
use super::*;
684726
use crate::error::{LexError, UnexpectedToken, UnexpectedTokenData};

partiql-parser/src/preprocessor.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,45 @@ mod tests {
706706
preprocess(r#"trim(LEADING 'Foo' from 'FooBar')"#)?,
707707
lex(r#"trim(LEADING : 'Foo', "from" : 'FooBar')"#)?
708708
);
709+
710+
// Trim Specification in all 3 spots
711+
assert_eq!(
712+
preprocess(r#"trim(BOTH TrAiLiNg from TRAILING)"#)?,
713+
lex(r#"trim(BOTH : TrAiLiNg, "from" : TRAILING)"#)?
714+
);
715+
716+
// Trim specification in 1st and 2nd spot
717+
assert_eq!(
718+
preprocess(r#"trim(LEADING LEADING from 'FooBar')"#)?,
719+
lex(r#"trim(LEADING : LEADING, "from" : 'FooBar')"#)?
720+
);
721+
assert_eq!(
722+
preprocess(r#"trim(LEADING TrAiLiNg from 'FooBar')"#)?,
723+
lex(r#"trim(LEADING : TrAiLiNg, "from" : 'FooBar')"#)?
724+
);
725+
assert_eq!(
726+
preprocess(r#"trim(tRaIlInG TrAiLiNg from 'FooBar')"#)?,
727+
lex(r#"trim(tRaIlInG : TrAiLiNg, "from" : 'FooBar')"#)?
728+
);
729+
730+
// Trim specification in 1st and 3rd spot
709731
assert_eq!(
710732
preprocess(r#"trim(LEADING 'Foo' from leaDing)"#)?,
711733
lex(r#"trim(LEADING : 'Foo', "from" : leaDing)"#)?
712734
);
735+
736+
// Trim Specification (quoted) in 2nd and 3rd spot
737+
assert_eq!(
738+
preprocess(r#"trim('LEADING' from leaDing)"#)?,
739+
lex(r#"trim('LEADING', "from" : leaDing)"#)?
740+
);
741+
742+
// Trim Specification in 3rd spot only
743+
assert_eq!(
744+
preprocess(r#"trim('a' from leaDing)"#)?,
745+
lex(r#"trim('a', "from" : leaDing)"#)?
746+
);
747+
713748
assert_eq!(
714749
preprocess(r#"trim(leading from ' Bar')"#)?,
715750
lex(r#"trim(leading : ' ', "from" : ' Bar')"#)?

0 commit comments

Comments
 (0)