Comments (3)
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.
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.
I think there's nothing more to be done here, right? Otherwise, feel free to reopen the issue!
from pglast.
Related Issues (20)
- Extra parens when formatting different operations HOT 2
- Support PG 15 HOT 5
- deparse_protobuf() broken on big-endian HOT 9
- Deparsing of modified queries and query parts HOT 3
- Double Quotes NOT preserved after parsing HOT 2
- Support statement types similar to referenced relations HOT 3
- Failed to install with pip (python 3.10.11) HOT 1
- ParseError when handling a statement containing a UUID HOT 1
- Token's reported location shifts when query has Turkish characters HOT 6
- Safety belt triggered HOT 2
- Duplicate `DEFERRABLE INITIALLY DEFERRED` in output HOT 3
- Can't compile with glibc >= 2.38 HOT 2
- pgpp remove important parentheses HOT 3
- Support PG16 HOT 6
- TypeError while init RawStmt from query parse tree dict which has A_Star. HOT 2
- (32-bit) FETCH ALL -> FETCH 2147483647 HOT 3
- Printing of AlterOwnerStmt for operator class results in error. HOT 3
- pglast>-6 install from wheel is missing symbol HOT 11
- referenced_relations: incorrectly treating LATERAL-ly-joined aliases as real relations HOT 4
- Question about the traverse method on v6 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pglast.