Comments (7)
and one more question, how can I get access to the original token/expression in the node (transformer)?
from sqlglot.
found one more func with this problem: timestamp_sub
from sqlglot.
Hey @TacoBel42, thanks for reporting these issues.
how can I get access to the original token/expression in the node
Can you explain with an example what you mean here? Generally during parse time (i.e during expression / node construction) tokens are consumed & thrown away, so accessing them at an AST level is not possible.
from sqlglot.
@VaggelisD
okey, let me explane my use case
I have some sql with many calc column, they can be calc with other same cols, and i do some "expander". So, i use your lib for replacing cols on expanded cols. Excluding this, I want to get the same sql as I had before.
DICT_WITH_COL_MAPPING = {
col_1: (hidden_col_1+ 10)::string
col_2: hidden_col_2 + 100
}
from sqlglot import parse_one
from sqlglot.dialects.clickhouse import ClickHouse
def transformer(node):
if isinstance(node, exp.Column) and node.name in DICT_WITH_COL_MAPPING:
return parse_one(DICT_WITH_COL_MAPPING[node.name])
return node
tree = parse_one("SELECT col_1, col2 FROM table", read=ClickHouse)
tree.trasform()
result = tree.sql(dialect=ClickHouse))
# result: SELECT (hidden_col_1+ 10)::string, hidden_col_2 + 100 FROM table
I want more control on parsing/transformation process. No any hidden processes.
Your lib works really cool with parsing ast, but for me it makes many other not requested transforms, sql != parse_one(sql).sql()
I tried to use Anonimus func for all funcs (using dict which always returns Anonimus.from_args) but messed up on getting same case (as was in sql) func call name (Parser.FUNCTIONS.get(upper) getting always upper func name)
from sqlglot.
Hey @TacoBel42, in your example, exactly function gets generated differently? I assume it's the ::
-> CAST
? You can generally override stuff like that if you want to, just subclass the dialect in your project and override the relevant generators / parsers. The AST expressions don't contain info about the source tokens or their positions and it's also not something in our roadmap currently.
from sqlglot.
@georgesittas that was example, in production sqls can be very different, and we want to escape cases like date_sub, timestamp_sub, date_trunc(CH v22). We need replace col on some our another sql stmt, without any optimizations/changes on it.
We just don't want non-explicit transformations
from sqlglot.
I hear you. SQLGlot canonicalizes some function names / other parts of queries, aiming to canonicalize them into more standard forms and make them compatible with other dialects. Also, these changes shouldn't change the semantics of the input SQL. We don't plan to change this, but if you want finer-grained control over the generation you can override / patch the relevant parts of the codebase in your application to achieve it.
from sqlglot.
Related Issues (20)
- Dialect: trino(presto), apply_index_offset (in the parse_one function) get wrong result HOT 2
- Transpiling timestampadd from Spark to Presto returns BigQuery function HOT 3
- ClickHouse tuple field named `select` fails to parse without quotes
- Transpile functions to DuckDB using "CREATE MACRO" HOT 1
- Please add the DB2 dialect HOT 1
- tSQL If condition ParseError HOT 1
- Improve pretty-printing for `CASE WHEN .. THEN ..` expressions HOT 1
- Prefix comments with `--` when pretty-printing whole-line comments HOT 1
- DuckDB base text types incorrect HOT 7
- Support ALTER VIEW in expression HOT 3
- Issue parsing `gen_random_uuid()` in postgres script.
- Support parsing of comma separated alter table clauses (snowflake) HOT 1
- Incorrect type cast in conversion from Spark to Presto in timestampad function HOT 2
- NULLS FIRST added to ORDER BY with oracle dialect. At least it should be NULLS LAST when sort direction is not provided. HOT 1
- Parsing falls back to Command when using BEGIN in BigQuery multi-statement HOT 1
- SAS dialect HOT 1
- duckdb binder error after json_extract_string is converted to ->> HOT 2
- `CREATE INDEX CONCURRENTLY` not parsed by postgres dialect
- dialect: postgres, `CREATE AGGREGATE` and `CREATE EXTENSION` treated as invalid syntax, unsure if expected or bug. HOT 7
- INSERT incorrectly roundtripped in mysql dialect.
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 sqlglot.