Skip to content
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
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ jobs:
retention-days: 1

test:
name: Test ${{ matrix.package.name }} on ${{ matrix.os }} ${{ matrix.os == 'ubuntu-latest' && '🐧' || matrix.os == 'macos-latest' && '🍎' || '🪟' }}
name: Test ${{ matrix.package.name }} on ${{ matrix.os }}${{ matrix.node == '22.x' && ' Node 22' || '' }} ${{ matrix.os == 'ubuntu-latest' && '🐧' || matrix.os == 'macos-latest' && '🍎' || '🪟' }}
needs: build-wasm
strategy:
matrix:
node: ['20.x']
os: [ubuntu-latest, macos-latest, windows-latest]
package:
- { name: 'v13', path: 'versions/13', version: '13' }
Expand All @@ -74,6 +75,9 @@ jobs:
- { name: 'v16', path: 'versions/16', version: '16' }
- { name: 'v17', path: 'versions/17', version: '17' }
- { name: 'v18', path: 'versions/18', version: '18' }
# Node 20 cannot run the fingerprint and normalize depth tests.
include:
- { os: ubuntu-latest, node: '22.x', package: { name: 'v18', path: 'versions/18', version: '18' } }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
fail-fast: false
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -83,7 +87,7 @@ jobs:
- name: Setup Node.js 🌐
uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: ${{ matrix.node }}

- name: Setup pnpm 📦
uses: pnpm/action-setup@v2
Expand Down
2 changes: 1 addition & 1 deletion versions/18/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"wasm:rebuild": "pnpm wasm:make rebuild",
"wasm:clean": "pnpm wasm:make clean",
"wasm:clean-cache": "pnpm wasm:make clean-cache",
"test": "node --test test/parsing.test.js test/pg18.test.js test/fingerprint.test.js test/normalize.test.js test/plpgsql.test.js test/scan.test.js test/errors.test.js test/input-encoding.test.js test/wasm-recovery.test.js test/load-module.test.js"
"test": "node --test test/parsing.test.js test/pg18.test.js test/fingerprint.test.js test/normalize.test.js test/plpgsql.test.js test/scan.test.js test/errors.test.js test/input-encoding.test.js test/wasm-recovery.test.js test/limits.test.js test/load-module.test.js"
},
"author": "Constructive <developers@constructive.io>",
"license": "MIT",
Expand Down
88 changes: 88 additions & 0 deletions versions/18/test/limits.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const { describe, it, before } = require('node:test');
const assert = require('node:assert/strict');
const lib = require('../wasm/index.cjs');

const nest = (open, inner, close) => (n) => open.repeat(n) + inner + close.repeat(n);
const list = (item, separator) => (n) => Array.from({ length: n }, (_, i) => item(i)).join(separator);

// The depth each function must handle for each shape of SQL: about half of
// what 18.1.5 and this build handle on Linux, so a build that loses depth
// fails here whatever the last release did. Raise a number when the build
// gains depth; lower one only for a change the release notes describe.
const SQL = {
'chained +': [(n) => 'SELECT 1' + '+1'.repeat(n), { parse: 4000, fingerprint: 5000, normalize: 500 }],
'chained ||': [(n) => "SELECT 'a'" + " || 'a'".repeat(n), { parse: 2500, fingerprint: 5000, normalize: 500 }],
'chained AND': [(n) => 'SELECT 1 WHERE ' + list(() => 'a = 1', ' AND ')(n), { parse: 5000, fingerprint: 5000, normalize: 5000 }],
'chained OR': [(n) => 'SELECT 1 WHERE ' + list(() => 'a = 1', ' OR ')(n), { parse: 5000, fingerprint: 5000, normalize: 5000 }],
'chained casts': [(n) => 'SELECT 1' + '::int'.repeat(n), { parse: 2500, fingerprint: 5000, normalize: 1000 }],
'chained ->': [(n) => 'SELECT j' + " -> 'a'".repeat(n), { parse: 2500, fingerprint: 5000, normalize: 1000 }],
'chained unary minus': [(n) => 'SELECT ' + '- '.repeat(n) + 'a', { parse: 2500, fingerprint: 4000, normalize: 1000 }],
'chained NOT': [(n) => 'SELECT ' + 'NOT '.repeat(n) + 'true', { parse: 2500, fingerprint: 4000, normalize: 500 }],
'nested parentheses': [(n) => 'SELECT ' + nest('(', '1', ')')(n), { parse: 4000, fingerprint: 4000, normalize: 4000 }],
'nested function calls': [(n) => 'SELECT ' + nest('f(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }],
'nested COALESCE': [(n) => 'SELECT ' + nest('COALESCE(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }],
'nested ROW': [(n) => 'SELECT ' + nest('ROW(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }],
'nested arrays': [(n) => 'SELECT ' + nest('ARRAY[', '1', ']')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }],
'nested CASE': [(n) => 'SELECT ' + nest('CASE WHEN a THEN ', '1', ' END')(n), { parse: 1000, fingerprint: 1000, normalize: 1000 }],
'nested subqueries': [(n) => 'SELECT ' + nest('(SELECT ', '1', ')')(n), { parse: 1000, fingerprint: 1500, normalize: 250 }],
'nested FROM subqueries': [(n) => 'SELECT * FROM ' + nest('(SELECT * FROM ', 't', ') s')(n), { parse: 750, fingerprint: 750, normalize: 250 }],
'nested CTEs': [(n) => nest('WITH c AS (', 'SELECT 1', ') SELECT * FROM c')(n), { parse: 750, fingerprint: 750, normalize: 250 }],
'joins': [(n) => 'SELECT 1 FROM t0' + list((i) => ` JOIN t${i + 1} ON true`, '')(n), { parse: 2500, fingerprint: 5000, normalize: 1000 }],
'UNION ALL': [list(() => 'SELECT 1', ' UNION ALL '), { parse: 4000, fingerprint: 5000, normalize: 1000 }],
'CTE list': [(n) => 'WITH ' + list((i) => `c${i} AS (SELECT 1)`, ', ')(n) + ' SELECT 1', { parse: 5000, fingerprint: 5000, normalize: 5000 }],
};

const PLPGSQL = {
'nested IF blocks': [nest('IF true THEN ', 'NULL;', ' END IF;'), 2000],
'nested BEGIN blocks': [nest('BEGIN ', 'NULL;', ' END;'), 2000],
'nested loops': [nest('LOOP ', 'EXIT;', ' END LOOP;'), 2000],
'nested CASE blocks': [nest('CASE WHEN true THEN ', 'NULL;', ' END CASE;'), 1000],
'chained + in an assignment': [(n) => 'x := 1' + '+1'.repeat(n) + ';', 5000],
};
const plpgsql = (body) =>
`CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ DECLARE x int; BEGIN ${body} END $$`;

// Node 20 runs out of stack in fingerprint and normalize at a few dozen
// levels once V8 has optimized them, on every release including 18.1.5.
const node20 = Number(process.versions.node.split('.')[0]) < 22 &&
'Node 20 overflows its stack in optimized fingerprint and normalize code';

describe('Depth every exported function handles', { timeout: 300000 }, () => {
before(async () => {
await lib.loadModule();
});

for (const [shape, [make, depth]] of Object.entries(SQL)) {
it(`parse and parseSync: ${shape}, ${depth.parse} deep`, async () => {
const sql = make(depth.parse);
assert.equal(lib.parseSync(sql).stmts.length, 1);
assert.equal((await lib.parse(sql)).stmts.length, 1);
});

it(`fingerprint and fingerprintSync: ${shape}, ${depth.fingerprint} deep`, { skip: node20 }, async () => {
const sql = make(depth.fingerprint);
assert.match(lib.fingerprintSync(sql), /^[0-9a-f]{16}$/);
assert.match(await lib.fingerprint(sql), /^[0-9a-f]{16}$/);
});

it(`normalize and normalizeSync: ${shape}, ${depth.normalize} deep`, { skip: node20 }, async () => {
const sql = make(depth.normalize);
assert.ok(lib.normalizeSync(sql).length > 0);
assert.ok((await lib.normalize(sql)).length > 0);
});

it(`scan and scanSync: ${shape}, 10000 deep`, async () => {
const sql = make(10000);
assert.ok(lib.scanSync(sql).tokens.length > 10000);
assert.ok((await lib.scan(sql)).tokens.length > 10000);
});
}

for (const [shape, [make, depth]] of Object.entries(PLPGSQL)) {
it(`parsePlPgSQL and parsePlPgSQLSync: ${shape}, ${depth} deep`, async () => {
const sql = plpgsql(make(depth));
assert.equal(lib.parsePlPgSQLSync(sql).plpgsql_funcs.length, 1);
assert.equal((await lib.parsePlPgSQL(sql)).plpgsql_funcs.length, 1);
});
}
});
Loading