Giter VIP home page Giter VIP logo

Comments (3)

lelit avatar lelit commented on June 12, 2024

The following script does that:

from pglast.parser import parse_sql
from pglast.stream import RawStream
from pglast.visitors import Visitor


class TargetColumnNames(Visitor):
    def __call__(self, node):
        self.tcnames = {}
        super().__call__(node)
        return self.tcnames

    def visit_ResTarget(self, ancestors, node):
        self.tcnames[node.name] = RawStream()(node.val)


def target_columns(stmt):
    return TargetColumnNames()(parse_sql(stmt) if isinstance(stmt, str) else stmt)


print(target_columns("SELECT t1.name AS name, t1.name||' '||t2.name AS new_name"
                     " FROM t1 inner join t2 on t1.id = t2.par_id"))

As always, details matter, and your problem is to define more clear rules that describe what you mean with "list of columns": for example, what should happen if the statement has subselects?

from pglast.

AzatYakupov avatar AzatYakupov commented on June 12, 2024

Thanks @lelit , that's really cool! thanks!
Regarding your comment, yes you are right, need to get a ETL transformation for every column, and SQL can contain several ETLs for one column (like CTE , subqueries~subselects).
So, currently, I am going to work on your code to get a branch of transformation for one column

from pglast.

lelit avatar lelit commented on June 12, 2024

I think there's nothing more to be done here, right? Otherwise, feel free to reopen the issue!

from pglast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.