GithubHelp home page GithubHelp logo

Comments (2)

tobymao avatar tobymao commented on June 15, 2024

check out transform, or replace methods.

https://github.com/tobymao/sqlglot/blob/main/posts/ast_primer.md

https://github.com/tobymao/sqlglot/blob/main/sqlglot/expressions.py#L583

from sqlglot.

davlee1972 avatar davlee1972 commented on June 15, 2024

Got it working..


import sqlglot

def remove_in_sql(sql_exp):
    while True:
        in_sqls = [s for s in sql_exp.find_all(sqlglot.exp.In) if 'expressions' in s.args]
        if in_sqls:
            saved_parent = in_sqls[0].parent
            in_sqls[0].pop()
            if isinstance(saved_parent, sqlglot.exp.And):
                if 'this' in saved_parent.args and saved_parent.args['this']:
                    saved_parent.parent.args['this'] = saved_parent.args['this']
                elif 'expression' in saved_parent.args and saved_parent.args['expression']:
                    saved_parent.parent.args['this'] = saved_parent.args['expression']
                else:
                    saved_parent.parent.parent.args['where'] = None
            elif isinstance(saved_parent, sqlglot.exp.Where):
                saved_parent.parent.args['where'] = None
        else:
            break

>>> z = sqlglot.parse_one("select a, b from abc where a in (1,2,3) and b in (7,8,9)")
>>> remove_in_sql(z)
>>> z.sql()
'SELECT a, b FROM abc'
>>>
>>> z = sqlglot.parse_one("select a, b from abc where a in (1,2,3)")
>>> remove_in_sql(z)
>>> z.sql()
'SELECT a, b FROM abc'
>>>
>>> z = sqlglot.parse_one("select * from abc where (a in (1,2,3) and c = 'abc') and b in (7,8,9)")
>>> remove_in_sql(z)
>>> z.sql()
"SELECT * FROM abc WHERE (c = 'abc')"
>>>
>>> z = sqlglot.parse_one("select * from abc where a in (1,2,3) and c = 'abc' and b in (select z from zzz where d in (1,2,3))")
>>> remove_in_sql(z)
>>> z.sql()
"SELECT * FROM abc WHERE c = 'abc' AND b IN (SELECT z FROM zzz)"
>>>
>>> z = sqlglot.parse_one("select * from abc where a in (1,2,3) and b in (select z from zzz where d in (1,2,3))")
>>> remove_in_sql(z)
>>> z.sql()
'SELECT * FROM abc WHERE b IN (SELECT z FROM zzz)'

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.