From f3b6cdbf5251060b04bcf70c3e9bf4f9f288c01a Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Sun, 17 Nov 2024 17:46:06 +0100 Subject: [PATCH] Support PostgreSQL OPERATOR(...) syntax Fixes #711 --- src/languages/postgresql/postgresql.formatter.ts | 1 + src/lexer/Tokenizer.ts | 8 ++++++++ src/lexer/TokenizerOptions.ts | 2 ++ test/postgresql.test.ts | 12 ++++++++++++ 4 files changed, 23 insertions(+) diff --git a/src/languages/postgresql/postgresql.formatter.ts b/src/languages/postgresql/postgresql.formatter.ts index 32eb840517..b00014fcdd 100644 --- a/src/languages/postgresql/postgresql.formatter.ts +++ b/src/languages/postgresql/postgresql.formatter.ts @@ -368,6 +368,7 @@ export const postgresql: DialectOptions = { '::', ':', ], + operatorKeyword: true, }, formatOptions: { alwaysDenseOperators: ['::', ':'], diff --git a/src/lexer/Tokenizer.ts b/src/lexer/Tokenizer.ts index 59571720aa..bfbe111d0a 100644 --- a/src/lexer/Tokenizer.ts +++ b/src/lexer/Tokenizer.ts @@ -130,6 +130,14 @@ export default class Tokenizer { regex: cfg.supportsXor ? /XOR\b/iuy : undefined, text: toCanonical, }, + ...(cfg.operatorKeyword + ? [ + { + type: TokenType.OPERATOR, + regex: /OPERATOR *\([^)]+\)/iuy, + }, + ] + : []), { type: TokenType.RESERVED_FUNCTION_NAME, regex: regex.reservedWord(cfg.reservedFunctionNames, cfg.identChars), diff --git a/src/lexer/TokenizerOptions.ts b/src/lexer/TokenizerOptions.ts index c5adea27b8..f7bd3b25fa 100644 --- a/src/lexer/TokenizerOptions.ts +++ b/src/lexer/TokenizerOptions.ts @@ -98,6 +98,8 @@ export interface TokenizerOptions { // Additional operators for property access, in addition to . // Like in table.column propertyAccessOperators?: string[]; + // Enables PostgreSQL-specific OPERATOR(...) syntax + operatorKeyword?: boolean; // Allows custom modifications on the token array. // Called after the whole input string has been split into tokens. // The result of this will be the output of the tokenizer. diff --git a/test/postgresql.test.ts b/test/postgresql.test.ts index f046cff4cc..51d9e388de 100644 --- a/test/postgresql.test.ts +++ b/test/postgresql.test.ts @@ -365,4 +365,16 @@ describe('PostgreSqlFormatter', () => { CREATE TABLE foo (text VARCHAR(100)); `); }); + + // Issue #711 + it('supports OPERATOR() syntax', () => { + expect(format(`SELECT foo OPERATOR(public.===) bar;`)).toBe(dedent` + SELECT + foo OPERATOR(public.===) bar; + `); + expect(format(`SELECT foo operator ( !== ) bar;`)).toBe(dedent` + SELECT + foo operator ( !== ) bar; + `); + }); });