Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/lexer/disambiguateTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { isReserved, Token, TokenType } from './token.js';
export function disambiguateTokens(tokens: Token[]): Token[] {
return tokens
.map(propertyNameKeywordToIdent)
.map(propertyNameOperatorToIdent)
.map(keywordAliasAfterAs)
.map(funcNameToIdent)
.map(dataTypeToParameterizedDataType)
Expand All @@ -43,6 +44,20 @@ const propertyNameKeywordToIdent = (token: Token, i: number, tokens: Token[]): T
return token;
};

const propertyNameOperatorToIdent = (token: Token, i: number, tokens: Token[]): Token => {
if (token.type === TokenType.OPERATOR) {
const prevToken = prevNonCommentToken(tokens, i);
const nextToken = nextNonCommentToken(tokens, i);
if (
prevToken?.type === TokenType.PROPERTY_ACCESS_OPERATOR &&
nextToken?.type === TokenType.OPEN_PAREN
) {
return { ...token, type: TokenType.IDENTIFIER, text: token.raw };
}
}
return token;
};

/**
* Some dialects allow reserved words as aliases, as in `SELECT id AS set FROM tbl`.
* Such a word is tokenized as a RESERVED_* token, which the parser then treats as
Expand Down
17 changes: 17 additions & 0 deletions test/postgresql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ describe('PostgreSqlFormatter', () => {
});

// Issue #711
// Issue #997
it('supports schema-qualified operator names', () => {
expect(
format(
'create operator pg_catalog.!! (rightarg = tsquery, function = pg_catalog.tsquery_not);'
)
).toBe(dedent`
create operator pg_catalog.!! (
rightarg = tsquery,
function = pg_catalog.tsquery_not
);
`);
expect(format('DROP OPERATOR pg_catalog.!! (none, tsquery);')).toBe(dedent`
DROP OPERATOR pg_catalog.!! (none, tsquery);
`);
});

it('supports OPERATOR() syntax', () => {
expect(format(`SELECT foo OPERATOR(public.===) bar;`)).toBe(dedent`
SELECT
Expand Down