Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New approach to function call matching #307

Merged
merged 24 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 18 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
3 changes: 2 additions & 1 deletion src/formatter/AliasAs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export default class AliasAs {
isToken.CAST(this.getPreviousReservedToken()) &&
isToken.AS(this.lookAhead()) &&
(this.lookAhead(2).type === TokenType.IDENTIFIER ||
this.lookAhead(2).type === TokenType.RESERVED_KEYWORD) &&
this.lookAhead(2).type === TokenType.RESERVED_KEYWORD ||
this.lookAhead(2).type === TokenType.RESERVED_FUNCTION_NAME) &&
this.lookAhead(3).value === ')'
);
}
Expand Down
1 change: 1 addition & 0 deletions src/formatter/ExpressionFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export default class ExpressionFormatter {
case TokenType.RESERVED_LOGICAL_OPERATOR:
return this.formatLogicalOperator(token);
case TokenType.RESERVED_KEYWORD:
case TokenType.RESERVED_FUNCTION_NAME:
return this.formatKeyword(token);
case TokenType.RESERVED_CASE_START:
return this.formatCaseStart(token);
Expand Down
11 changes: 6 additions & 5 deletions src/languages/bigquery.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const reservedFunctions = {
],
// https://cloud.google.com/bigquery/docs/reference/standard-sql/array_functions
array: [
'ARRAY',
// 'ARRAY',
'ARRAY_CONCAT',
'ARRAY_LENGTH',
'ARRAY_TO_STRING',
Expand Down Expand Up @@ -545,6 +545,9 @@ const reservedFunctions = {
],
other: ['BQ.JOBS.CANCEL', 'BQ.REFRESH_MATERIALIZED_VIEW'],
pivot: ['PIVOT', 'UNPIVOT'],
// Data types with parameters like VARCHAR(100)
// https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#parameterized_data_types
dataTypes: ['BYTES', 'NUMERIC', 'DECIMAL', 'BIGNUMERIC', 'BIGDECIMAL', 'STRING'],
};

/**
Expand Down Expand Up @@ -836,10 +839,8 @@ export default class BigQueryFormatter extends Formatter {
reservedBinaryCommands,
reservedJoins,
reservedDependentClauses,
reservedKeywords: dedupe([
...Object.values(reservedFunctions).flat(),
...Object.values(reservedKeywords).flat(),
]),
reservedKeywords: dedupe(Object.values(reservedKeywords).flat()),
reservedFunctionNames: dedupe(Object.values(reservedFunctions).flat()),
openParens: ['(', '['],
closeParens: [')', ']'],
stringTypes: [
Expand Down
10 changes: 5 additions & 5 deletions src/languages/db2.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ const reservedFunctions = {
row: ['UNPACK'],
// https://www.ibm.com/docs/en/db2-for-zos/12?topic=expressions-olap-specification
olap: ['FIRST_VALUE', 'LAG', 'LAST_VALUE', 'LEAD', 'NTH_VALUE', 'NTILE', 'RATIO_TO_REPORT'],
// Type casting
cast: ['CAST'],
};

/**
Expand Down Expand Up @@ -342,7 +344,7 @@ const reservedKeywords = {
'BY',
'CAPTURE',
'CASCADED',
'CAST',
// 'CAST',
'CCSID',
'CHARACTER',
'CHECK',
Expand Down Expand Up @@ -862,10 +864,8 @@ export default class Db2Formatter extends Formatter {
reservedBinaryCommands,
reservedJoins,
reservedDependentClauses,
reservedKeywords: dedupe([
...Object.values(reservedFunctions).flat(),
...Object.values(reservedKeywords).flat(),
]),
reservedKeywords: dedupe(Object.values(reservedKeywords).flat()),
reservedFunctionNames: dedupe(Object.values(reservedFunctions).flat()),
stringTypes: [{ quote: "''", prefixes: ['X', 'G', 'N', 'GX', 'UX', 'U&'] }],
identTypes: [`""`],
positionalParams: true,
Expand Down
11 changes: 7 additions & 4 deletions src/languages/hive.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ const reservedFunctions = {
'VARIANCE',
],
table: ['EXPLODE', 'INLINE', 'JSON_TUPLE', 'PARSE_URL_TUPLE', 'POSEXPLODE', 'STACK'],
// Parameterized data types
// https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=82706456
// Though in reality Hive only supports parameters for DECIMAL(),
// it doesn't hurt to allow others in here as well.
dataTypes: ['DECIMAL', 'NUMERIC', 'VARCHAR', 'CHAR'],
};

/**
Expand Down Expand Up @@ -617,10 +622,8 @@ export default class HiveFormatter extends Formatter {
reservedBinaryCommands,
reservedJoins,
reservedDependentClauses,
reservedKeywords: dedupe([
...Object.values(reservedFunctions).flat(),
...Object.values(reservedKeywords).flat(),
]),
reservedKeywords: dedupe(Object.values(reservedKeywords).flat()),
reservedFunctionNames: dedupe(Object.values(reservedFunctions).flat()),
openParens: ['(', '['],
closeParens: [')', ']'],
stringTypes: ['""', "''"],
Expand Down
41 changes: 38 additions & 3 deletions src/languages/mariadb.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ const reservedFunctions = [
'NAME_CONST',
'NVL',
'NVL2',
'NULLIF',
'OCT',
'OCTET_LENGTH',
'ORD',
Expand Down Expand Up @@ -244,6 +243,41 @@ const reservedFunctions = [
'WSREP_LAST_SEEN_GTID',
'WSREP_SYNC_WAIT_UPTO_GTID',
'YEARWEEK',
// CASE expression shorthands
'COALESCE',
'NULLIF',
// Data types with parameters
// https://mariadb.com/kb/en/data-types/
'TINYINT',
'SMALLINT',
'MEDIUMINT',
'INT',
'INTEGER',
'BIGINT',
'DECIMAL',
'DEC',
'NUMERIC',
'FIXED',
// 'NUMBER', // ?? In oracle mode only
'FLOAT',
'DOUBLE',
'DOUBLE PRECISION',
'REAL',
'BIT',
'BINARY',
'BLOB',
'CHAR',
'NATIONAL CHAR',
'CHAR BYTE',
'ENUM',
'VARBINARY',
'VARCHAR',
'NATIONAL VARCHAR',
// 'SET' // handled as special-case in postProcess
'TIME',
'DATETIME',
'TIMESTAMP',
'YEAR',
];

/**
Expand Down Expand Up @@ -1160,7 +1194,8 @@ export default class MariaDbFormatter extends Formatter {
reservedJoins,
reservedDependentClauses,
reservedLogicalOperators: ['AND', 'OR', 'XOR'],
reservedKeywords: dedupe([...reservedKeywords, ...reservedFunctions]),
reservedKeywords: dedupe(reservedKeywords),
reservedFunctionNames: dedupe(reservedFunctions),
stringTypes: ['""', { quote: "''", prefixes: ['X'] }],
identTypes: ['``'],
identChars: { first: '$', rest: '$' },
Expand All @@ -1183,7 +1218,7 @@ function postProcess(tokens: Token[]) {
const nextToken = tokens[i + 1] || EOF_TOKEN;
if (isToken.SET(token) && nextToken.value === '(') {
// This is SET datatype, not SET statement
return { ...token, type: TokenType.RESERVED_KEYWORD };
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
return token;
});
Expand Down
39 changes: 36 additions & 3 deletions src/languages/mysql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const reservedFunctions = [
'ICU_VERSION',
'IF',
'IFNULL',
'IN',
// 'IN',
'INET_ATON',
'INET_NTOA',
'INET6_ATON',
Expand Down Expand Up @@ -432,6 +432,37 @@ const reservedFunctions = [
// 'XOR',
'YEAR',
'YEARWEEK',
// Data types with parameters
// https://dev.mysql.com/doc/refman/8.0/en/data-types.html
'BIT',
'TINYINT',
'SMALLINT',
'MEDIUMINT',
'INT',
'INTEGER',
'BIGINT',
'DECIMAL',
'DEC',
'NUMERIC',
'FIXED',
'FLOAT',
'DOUBLE',
'DOUBLE PRECISION',
'REAL',
'DATETIME',
'TIMESTAMP',
'TIME',
'YEAR',
'CHAR',
'NATIONAL CHAR',
'VARCHAR',
'NATIONAL VARCHAR',
'BINARY',
'VARBINARY',
'BLOB',
'TEXT',
'ENUM',
// 'SET' // handled as special-case in postProcess
];

/**
Expand Down Expand Up @@ -656,6 +687,7 @@ const reservedKeywords = [
'INSERT_METHOD',
'INSTALL',
'INSTANCE',
'IN', // <-- moved over from functions
'INT',
'INT1',
'INT2',
Expand Down Expand Up @@ -1323,7 +1355,8 @@ export default class MySqlFormatter extends Formatter {
reservedJoins,
reservedDependentClauses,
reservedLogicalOperators: ['AND', 'OR', 'XOR'],
reservedKeywords: dedupe([...reservedKeywords, ...reservedFunctions]),
reservedKeywords: dedupe(reservedKeywords),
reservedFunctionNames: dedupe(reservedFunctions),
stringTypes: ['""', { quote: "''", prefixes: ['X'] }],
identTypes: ['``'],
identChars: { first: '$', rest: '$' },
Expand All @@ -1346,7 +1379,7 @@ function postProcess(tokens: Token[]) {
const nextToken = tokens[i + 1] || EOF_TOKEN;
if (isToken.SET(token) && nextToken.value === '(') {
// This is SET datatype, not SET statement
return { ...token, type: TokenType.RESERVED_KEYWORD };
return { ...token, type: TokenType.RESERVED_FUNCTION_NAME };
}
return token;
});
Expand Down
7 changes: 6 additions & 1 deletion src/languages/n1ql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ const reservedFunctions = [
'VAR_SAMP',
'WEEKDAY_MILLIS',
'WEEKDAY_STR',
// type casting
// not implemented in N1QL, but added here now for the sake of tests
// https://docs.couchbase.com/server/current/analytics/3_query.html#Vs_SQL-92
'CAST',
];

/**
Expand Down Expand Up @@ -516,7 +520,8 @@ export default class N1qlFormatter extends Formatter {
reservedJoins,
reservedDependentClauses,
reservedLogicalOperators: ['AND', 'OR', 'XOR'],
reservedKeywords: dedupe([...reservedKeywords, ...reservedFunctions]),
reservedKeywords: dedupe(reservedKeywords),
reservedFunctionNames: dedupe(reservedFunctions),
// NOTE: single quotes are actually not supported in N1QL,
// but we support them anyway as all other SQL dialects do,
// which simplifies writing tests that are shared between all dialects.
Expand Down
33 changes: 32 additions & 1 deletion src/languages/plsql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,36 @@ const reservedFunctions = {
window: ['FIRST_VALUE', 'LAG', 'LAST_VALUE', 'LEAD', 'NTILE', 'RATIO_TO_REPORT', 'ROW_NUMBER'],
objectReference: ['DEREF', 'MAKE_REF', 'REF', 'REFTOHEX', 'VALUE'],
model: ['CV', 'ITERATION_NUMBER', 'PRESENTNNV', 'PRESENTV', 'PREVIOUS'],
// Parameterized data types
// https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Data-Types.html
dataTypes: [
// Oracle builtin data types
'VARCHAR2',
'NVARCHAR2',
'NUMBER',
'FLOAT',
'TIMESTAMP',
'INTERVAL YEAR',
'INTERVAL DAY',
'RAW',
'UROWID',
'NCHAR',
// ANSI Data Types
'CHARACTER',
'CHAR',
'CHARACTER VARYING',
'CHAR VARYING',
'NATIONAL CHARACTER',
'NATIONAL CHAR',
'NATIONAL CHARACTER VARYING',
'NATIONAL CHAR VARYING',
'NCHAR VARYING',
'NUMERIC',
'DECIMAL',
'FLOAT',
// SQL/DS and DB2 Data Types
'VARCHAR',
],
};

/**
Expand Down Expand Up @@ -691,7 +721,8 @@ export default class PlSqlFormatter extends Formatter {
reservedJoins,
reservedDependentClauses,
reservedLogicalOperators: ['AND', 'OR', 'XOR'],
reservedKeywords: dedupe([...reservedKeywords, ...Object.values(reservedFunctions).flat()]),
reservedKeywords: dedupe(reservedKeywords),
reservedFunctionNames: dedupe(Object.values(reservedFunctions).flat()),
// TODO: support custom-delimited strings: Q'{..}' q'<..>' etc
stringTypes: [{ quote: "''", prefixes: ['N'] }],
identTypes: [`""`],
Expand Down
25 changes: 22 additions & 3 deletions src/languages/postgresql.formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ const reservedFunctions = {
],
// https://www.postgresql.org/docs/14/functions-aggregate.html
aggregate: [
'ANY',
// 'ANY',
'ARRAY_AGG',
'AVG',
'BIT_AND',
Expand Down Expand Up @@ -456,7 +456,7 @@ const reservedFunctions = {
'REGR_SXX',
'REGR_SXY',
'REGR_SYY',
'SOME',
// 'SOME',
'STDDEV',
'STDDEV_POP',
'STDDEV_SAMP',
Expand Down Expand Up @@ -719,6 +719,22 @@ const reservedFunctions = {
],
// https://www.postgresql.org/docs/14/functions-statistics.html
stats: ['PG_MCV_LIST_ITEMS'],
cast: ['CAST'],
// Parameterized data types
// https://www.postgresql.org/docs/current/datatype.html
dataTypes: [
'BIT',
'BIT VARYING',
'CHARACTER',
'CHARACTER VARYING',
'VARCHAR',
'CHAR',
'DECIMAL',
'NUMERIC',
'TIME',
'TIMESTAMP',
'ENUM',
],
};

/**
Expand All @@ -742,6 +758,7 @@ const reservedKeywords = [
'ALTER',
'ALWAYS',
'ANALYSE',
'ANY', // <-- moved over from functions
'ARE',
'ARRAY',
'ARRAY_MAX_CARDINALITY',
Expand Down Expand Up @@ -1241,6 +1258,7 @@ const reservedKeywords = [
'SKIP',
'SMALLINT',
'SNAPSHOT',
'SOME', // <-- moved over from functions
'SOURCE',
'SPACE',
'SPECIFIC',
Expand Down Expand Up @@ -1700,7 +1718,8 @@ export default class PostgreSqlFormatter extends Formatter {
reservedBinaryCommands,
reservedJoins,
reservedDependentClauses,
reservedKeywords: dedupe([...reservedKeywords, ...Object.values(reservedFunctions).flat()]),
reservedKeywords: dedupe(reservedKeywords),
reservedFunctionNames: dedupe(Object.values(reservedFunctions).flat()),
openParens: ['(', '['],
closeParens: [')', ']'],
stringTypes: [{ quote: "''", prefixes: ['U&', 'E', 'X', 'B'] }, '$$'],
Expand Down
Loading