alamb commented on code in PR #1591: URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1591#discussion_r1882401509
########## 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(); Review Comment: if we can figure out how to remove this to_uppercase call I think this approach will likely be very fast. I dug around in the rust API docs and it seems like we could change the comparison to ignore ascii case. See comment below 🤔 ########## 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 wonder if we could use something like ```suggestion let keyword = ALL_KEYWORDS[start..end].binary_search_by(|s| s.eq_ignore_ascii_case(&word)) ``` https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search_by https://doc.rust-lang.org/std/primitive.str.html#method.eq_ignore_ascii_case -- 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