alamb commented on code in PR #1591: URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1591#discussion_r1885040511
########## src/keywords.rs: ########## @@ -973,3 +973,61 @@ pub const RESERVED_FOR_IDENTIFIER: &[Keyword] = &[ Keyword::STRUCT, Keyword::TRIM, ]; + +pub const NA: usize = usize::MAX; + +#[rustfmt::skip] +pub const KEYWORD_LOOKUP_INDEX_ROOT: &[usize; 26] = &[ + 0, 42, 67, 148, 198, 241, 281, 294, 305, 350, 357, 360, 390, + 430, 465, 497, 539, 543, 605, 683, 728, 761, 780, 793, 795, 796, +]; + +pub fn lookup(word: &str) -> Keyword { + if word.len() < 2 { + return Keyword::NoKeyword; + } + + let word = word.to_uppercase(); + let byte1 = word.as_bytes()[0]; + if !byte1.is_ascii_uppercase() { + return Keyword::NoKeyword; + } + + let start = KEYWORD_LOOKUP_INDEX_ROOT[(byte1 - b'A') as usize]; + + let end = if (byte1 + 1) <= b'Z' { + KEYWORD_LOOKUP_INDEX_ROOT[(byte1 - b'A' + 1) as usize] + } else { + ALL_KEYWORDS.len() + }; + + let keyword = ALL_KEYWORDS[start..end].binary_search(&word.as_str()); Review Comment: I actually tried to do this approach, and I found the `to_uppercase` call is being done as part of the token creation. So to save the allocation we need to to avoid https://github.com/alamb/sqlparser-rs/blob/4ab3ab91473d152c652e6582b63abb13535703f9/src/tokenizer.rs#L347-L346 I think we can change it to `make_ascii_uppercase` perhaps -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org