taozhi8833998 / taozhi8833998/node-sql-parser

Add transform functionality for AST manipulation

Open
#2,022 1 comment 1 reaction 1 assignee View on GitHub

@taozhi8833998 is already working on this.

Since Jul 24, 2024.

Dominant language
PEG.js
Stars
1k
Forks
244
PR merge metrics
No merged PRs in 30d

Description

I would like to propose adding a transform functionality to enable easier manipulation of the Abstract Syntax Tree (AST).
This feature would allow users to traverse and modify the AST without having to implement complex tree traversal logic themselves.

The proposed functionality would be similar to the transform method available in sqlglot, a Python SQL parser and transpiler. In sqlglot, users can apply a transformation function to each node in the parsed expression tree.

Example of how it could work in node-sql-parser:

const ast = parser.astify('SELECT * FROM users');
const transformedAst = parser.transform(ast, (expr) => {
  if (expr.type === 'column_ref') {
    return {
      type: "column_ref",
      table: "new_table",
      column: "new_column",
    };
  }
  return expr;
});

const transformedSql = parser.sqlify(transformedAst);

This would allow users to easily modify the AST for various purposes, such as:

  • Renaming tables or columns
  • Adding or removing clauses
  • Modifying expressions
  • Implementing custom SQL transformations

Reference:
sqlglot's transform functionality

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.