11import { DialectOptions } from '../../dialect.js' ;
22import { expandPhrases } from '../../expandPhrases.js' ;
3- import { EOF_TOKEN , Token , TokenType } from '../../lexer/token.js' ;
3+ import { EOF_TOKEN , isToken , Token , TokenType } from '../../lexer/token.js' ;
44import { functions } from './clickhouse.functions.js' ;
55import { dataTypes , keywords } from './clickhouse.keywords.js' ;
66
@@ -298,34 +298,22 @@ function postProcess(tokens: Token[]): Token[] {
298298 const nextToken = tokens [ i + 1 ] || EOF_TOKEN ;
299299 const prevToken = tokens [ i - 1 ] || EOF_TOKEN ;
300300
301- // Only process IN and ANY that are currently RESERVED_FUNCTION_NAME
302- // Check text (uppercase canonical form) for matching, but preserve raw (original casing)
301+ // We want to convert IN from RESERVED_KEYWORD to RESERVED_FUNCTION_NAME
302+ // when it's used as a function. Whether it's used as an operator depends on
303+ // the previous token and whether the next token is an opening parenthesis.
303304 if (
304- token . type === TokenType . RESERVED_FUNCTION_NAME &&
305- ( token . text === 'IN' || token . text === 'ANY' )
305+ isToken . IN ( token ) &&
306+ ! (
307+ prevToken . type === TokenType . IDENTIFIER ||
308+ prevToken . type === TokenType . QUOTED_IDENTIFIER ||
309+ prevToken . type === TokenType . NUMBER ||
310+ prevToken . type === TokenType . STRING ||
311+ prevToken . type === TokenType . CLOSE_PAREN ||
312+ prevToken . type === TokenType . ASTERISK
313+ ) &&
314+ nextToken . text === '('
306315 ) {
307- // Must be followed by ( to be a function
308- if ( nextToken . text !== '(' ) {
309- // Not followed by ( means it's an operator/keyword, convert to uppercase
310- return { ...token , type : TokenType . RESERVED_KEYWORD , raw : token . text } ;
311- }
312-
313- // For IN: convert to keyword if previous token is an expression token
314- // For ANY: convert to keyword if previous token is an operator
315- if (
316- ( token . text === 'IN' &&
317- ( prevToken . type === TokenType . IDENTIFIER ||
318- prevToken . type === TokenType . QUOTED_IDENTIFIER ||
319- prevToken . type === TokenType . NUMBER ||
320- prevToken . type === TokenType . STRING ||
321- prevToken . type === TokenType . CLOSE_PAREN ||
322- prevToken . type === TokenType . ASTERISK ) ) ||
323- ( token . text === 'ANY' && prevToken . type === TokenType . OPERATOR )
324- ) {
325- // Convert to keyword (operator) - use uppercase for display
326- return { ...token , type : TokenType . RESERVED_KEYWORD , raw : token . text } ;
327- }
328- // Otherwise, keep as RESERVED_FUNCTION_NAME to preserve original casing via functionCase option
316+ return { ...token , type : TokenType . RESERVED_FUNCTION_NAME } ;
329317 }
330318
331319 // If we have queries like
@@ -351,12 +339,8 @@ function postProcess(tokens: Token[]): Token[] {
351339 }
352340
353341 // We should format `set(100)` as-is rather than `SET (100)`
354- if (
355- token . type === TokenType . RESERVED_CLAUSE &&
356- token . text === 'SET' &&
357- nextToken . type === TokenType . OPEN_PAREN
358- ) {
359- return { ...token , type : TokenType . RESERVED_FUNCTION_NAME , text : token . raw } ;
342+ if ( isToken . SET ( token ) && nextToken . type === TokenType . OPEN_PAREN ) {
343+ return { ...token , type : TokenType . RESERVED_FUNCTION_NAME } ;
360344 }
361345
362346 return token ;
0 commit comments