Skip to content

Commit fc3809e

Browse files
add: debug scripts for negative integer transformation analysis
- debug_context.js: analyze A_Const transformation context information - debug_insert_negative.js: focused debugging of INSERT negative integer case - debug_sql_parsing.js: compare PG15 vs PG16 parsing patterns for various SQL statements - These scripts help understand the context-dependent transformation requirements Co-Authored-By: Dan Lynch <[email protected]>
1 parent c848109 commit fc3809e

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const { Parser } = require('@pgsql/parser');
2+
const { ASTTransformer } = require('./dist/transformer');
3+
4+
class DebugTransformer extends ASTTransformer {
5+
A_Const(node, context) {
6+
console.log('=== A_Const Context Debug ===');
7+
console.log('Node:', JSON.stringify(node, null, 2));
8+
console.log('Context parentNodeTypes:', context.parentNodeTypes);
9+
console.log('Context path:', context.path);
10+
console.log('================================');
11+
12+
return super.A_Const(node, context);
13+
}
14+
}
15+
16+
async function debugContext() {
17+
const parser15 = new Parser(15);
18+
const transformer = new DebugTransformer();
19+
20+
const testCases = [
21+
"insert into atacc2 (test2) values (-3)",
22+
"ALTER TABLE onek ADD CONSTRAINT onek_check_constraint CHECK (unique1 >= 0)"
23+
];
24+
25+
console.log("=== DEBUGGING A_CONST CONTEXT ===");
26+
27+
for (const sql of testCases) {
28+
console.log(`\n=== SQL: ${sql} ===`);
29+
30+
try {
31+
const pg15Result = await parser15.parse(sql);
32+
33+
const astToTransform = JSON.parse(JSON.stringify(pg15Result));
34+
if (astToTransform.stmts && Array.isArray(astToTransform.stmts)) {
35+
astToTransform.stmts = astToTransform.stmts.map((stmtWrapper) => {
36+
if (stmtWrapper.stmt) {
37+
const transformedStmt = transformer.transform(stmtWrapper.stmt, 15, 16);
38+
return { ...stmtWrapper, stmt: transformedStmt };
39+
}
40+
return stmtWrapper;
41+
});
42+
}
43+
44+
} catch (error) {
45+
console.error("Error:", error.message);
46+
}
47+
}
48+
}
49+
50+
debugContext();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const { Parser } = require('@pgsql/parser');
2+
const { ASTTransformer } = require('./dist/transformer');
3+
4+
async function debugInsertNegative() {
5+
const parser15 = new Parser(15);
6+
const parser16 = new Parser(16);
7+
const transformer = new ASTTransformer();
8+
9+
const sql = "insert into atacc2 (test2) values (-3)";
10+
11+
console.log("=== DEBUGGING INSERT NEGATIVE INTEGER ===");
12+
console.log("SQL:", sql);
13+
14+
try {
15+
const pg15Result = await parser15.parse(sql);
16+
console.log("\n=== PG15 PARSED RESULT ===");
17+
console.log(JSON.stringify(pg15Result, null, 2));
18+
19+
const pg16Result = await parser16.parse(sql);
20+
console.log("\n=== PG16 PARSED RESULT ===");
21+
console.log(JSON.stringify(pg16Result, null, 2));
22+
23+
const pg15AConst = findAConstInInsert(pg15Result);
24+
const pg16AConst = findAConstInInsert(pg16Result);
25+
26+
console.log("\n=== PG15 A_Const ===");
27+
console.log(JSON.stringify(pg15AConst, null, 2));
28+
29+
console.log("\n=== PG16 A_Const ===");
30+
console.log(JSON.stringify(pg16AConst, null, 2));
31+
32+
const astToTransform = JSON.parse(JSON.stringify(pg15Result));
33+
if (astToTransform.stmts && Array.isArray(astToTransform.stmts)) {
34+
astToTransform.stmts = astToTransform.stmts.map((stmtWrapper) => {
35+
if (stmtWrapper.stmt) {
36+
const transformedStmt = transformer.transform(stmtWrapper.stmt, 15, 16);
37+
return { ...stmtWrapper, stmt: transformedStmt };
38+
}
39+
return stmtWrapper;
40+
});
41+
}
42+
astToTransform.version = pg16Result.version;
43+
44+
const transformedAConst = findAConstInInsert(astToTransform);
45+
console.log("\n=== TRANSFORMED A_Const ===");
46+
console.log(JSON.stringify(transformedAConst, null, 2));
47+
48+
console.log("\n=== COMPARISON ===");
49+
console.log("PG15 ival:", JSON.stringify(pg15AConst?.ival));
50+
console.log("PG16 ival:", JSON.stringify(pg16AConst?.ival));
51+
console.log("Transformed ival:", JSON.stringify(transformedAConst?.ival));
52+
53+
} catch (error) {
54+
console.error("Error:", error.message);
55+
}
56+
}
57+
58+
function findAConstInInsert(obj) {
59+
if (!obj || typeof obj !== 'object') return null;
60+
61+
try {
62+
return obj.stmts[0].stmt.InsertStmt.selectStmt.SelectStmt.valuesLists[0].List.items[0].A_Const;
63+
} catch (e) {
64+
return null;
65+
}
66+
}
67+
68+
debugInsertNegative();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const { Parser } = require('@pgsql/parser');
2+
3+
async function debugSQLParsing() {
4+
const parser15 = new Parser(15);
5+
const parser16 = new Parser(16);
6+
7+
const testCases = [
8+
"insert into atacc2 (test2) values (-3)",
9+
"insert into atacc2 (test2) values (3)",
10+
"insert into atacc2 (test2) values (0)",
11+
"ALTER TABLE tmp ADD COLUMN j abstime[]"
12+
];
13+
14+
console.log("=== DEBUGGING SQL PARSING PATTERNS ===");
15+
16+
for (const sql of testCases) {
17+
console.log(`\n=== SQL: ${sql} ===`);
18+
19+
try {
20+
const pg15Result = await parser15.parse(sql);
21+
const pg16Result = await parser16.parse(sql);
22+
23+
const pg15Integers = findAllIntegers(pg15Result);
24+
const pg16Integers = findAllIntegers(pg16Result);
25+
26+
console.log("PG15 Integer nodes:", JSON.stringify(pg15Integers, null, 2));
27+
console.log("PG16 Integer nodes:", JSON.stringify(pg16Integers, null, 2));
28+
29+
} catch (error) {
30+
console.error("Error:", error.message);
31+
}
32+
}
33+
}
34+
35+
function findAllIntegers(obj, path = []) {
36+
const results = [];
37+
38+
if (typeof obj !== 'object' || obj === null) {
39+
return results;
40+
}
41+
42+
if (obj.Integer !== undefined) {
43+
results.push({
44+
path: path.concat(['Integer']),
45+
node: obj.Integer
46+
});
47+
}
48+
49+
for (const [key, value] of Object.entries(obj)) {
50+
if (typeof value === 'object' && value !== null) {
51+
results.push(...findAllIntegers(value, path.concat([key])));
52+
}
53+
}
54+
55+
return results;
56+
}
57+
58+
debugSQLParsing();

0 commit comments

Comments
 (0)