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

Add transform functionality for AST manipulation #2022

Open
rantibi opened this issue Jul 24, 2024 · 0 comments
Open

Add transform functionality for AST manipulation #2022

rantibi opened this issue Jul 24, 2024 · 0 comments
Assignees

Comments

@rantibi
Copy link

rantibi commented Jul 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants