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

Queries with SETTINGS clauses no longer get AST errors #530

Merged
merged 8 commits into from
Oct 3, 2023
Merged
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
11 changes: 10 additions & 1 deletion src/data/ast.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getFields } from './ast';
import { getFields, sqlToStatement } from './ast';
import { toSql } from 'pgsql-ast-parser'

describe('ast', () => {
describe('getFields', () => {
Expand All @@ -7,4 +8,12 @@ describe('ast', () => {
expect(stm.length).toBe(1);
});
});
describe('sqlToStatement', () => {
it('settings parse correctly', () => {
const sql = 'SELECT count(*) FROM foo SETTINGS setting1=stuff setting2=stuff';
const stm = sqlToStatement(sql);
// this is formatted like this to match how pgsql generates its sql
expect(toSql.statement(stm)).toEqual('SELECT (count (*) ) FROM foo');
});
});
});
7 changes: 6 additions & 1 deletion src/data/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function sqlToStatement(sql: string): Statement {
replacementName: string;
}> = [];
//default is a key word in this grammar, but it can be used in CH
const re = /(\$__|\$|default)/gi;
const re = /(\$__|\$|default|settings)/gi;
let regExpArray: RegExpExecArray | null;
while ((regExpArray = re.exec(sql)) !== null) {
replaceFuncs.push({ startIndex: regExpArray.index, name: regExpArray[0], replacementName: '' });
Expand All @@ -18,6 +18,11 @@ export function sqlToStatement(sql: string): Statement {
const si = replaceFuncs[i].startIndex;
const replacementName = 'f' + (Math.random() + 1).toString(36).substring(7);
replaceFuncs[i].replacementName = replacementName;
// settings do not parse and we do not need information from them so we will remove them
if (replaceFuncs[i].name.toLowerCase() === "settings") {
sql = sql.substring(0, si)
continue;
}
sql = sql.substring(0, si) + replacementName + sql.substring(si + replaceFuncs[i].name.length);
}

Expand Down