Git Product home page Git Product logo

Comments (7)

TacoBel42 avatar TacoBel42 commented on July 22, 2024

and one more question, how can I get access to the original token/expression in the node (transformer)?

from sqlglot.

TacoBel42 avatar TacoBel42 commented on July 22, 2024

found one more func with this problem: timestamp_sub

from sqlglot.

VaggelisD avatar VaggelisD commented on July 22, 2024

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.

TacoBel42 avatar TacoBel42 commented on July 22, 2024

@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.

georgesittas avatar georgesittas commented on July 22, 2024

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.

TacoBel42 avatar TacoBel42 commented on July 22, 2024

@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.

georgesittas avatar georgesittas commented on July 22, 2024

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)

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.