kayak / pypika

PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting query. PyPika excels at all sorts of SQL queries but is especially useful for data analysis.
http://pypika.readthedocs.io/en/latest/
Apache License 2.0
2.43k stars 293 forks source link

NullCriterion's term should add parentheses for sql #724

Open xinglin-zhao opened 1 year ago

xinglin-zhao commented 1 year ago

why? when the term is complicated, for example term.sql = "user_id = 'a' and customer_id = '1'", then now term.isnull().sql = "user_id = 'a' and customer_id = '1' IS NULL" which isn't expected. Now mitigate by Bracket(term).isnull().

suggested change on code:

    def get_sql(self, with_alias: bool = False, **kwargs: Any) -> str:
        sql = "({term}) IS NULL".format(
            term=self.term.get_sql(**kwargs),
        )
        return format_alias_sql(sql, self.alias, **kwargs)