Skip to content

get_sql() throws exception on MSSQLQuery or OracleQuery that includes where clause with subquery #298

Open
@paul-ko

Description

I've noticed that when I use the MSSQLQuery dialect to construct a query including a where predicate such as where SomeColumn not in (select SomeOtherColumn from SomeTable), get_sql() throws what appears to be a spurious exception. If I swap in a Query instead of the MSSQLQuery, no such exception occurs, and the generated query runs without issue against a SQL Server database.

Pypika version: 0.33.1, installed via pip
Python version / platform (reproduces on both setups):

  • 3.5.2 / Linux Mint v18 (Sarah)
  • 3.7.3 / Windows Server 2012R2

Stack frames from within pypika:

  File "<snip>\venv\lib\site-packages\pypika\queries.py", line 836, in __str__
    return self.get_sql(quote_char=self.quote_char, dialect=self.dialect)
  File "<snip>\venv\lib\site-packages\pypika\dialects.py", line 308, in get_sql
    return super(MSSQLQueryBuilder, self).get_sql(*args, groupby_alias=False, **kwargs)
  File "<snip>\venv\lib\site-packages\pypika\queries.py", line 936, in get_sql
    querystring += self._where_sql(**kwargs)
  File "<snip>\venv\lib\site-packages\pypika\queries.py", line 1034, in _where_sql
    return ' WHERE {where}'.format(where=self._wheres.get_sql(quote_char=quote_char, subquery=True, **kwargs))
  File "<snip>\venv\lib\site-packages\pypika\terms.py", line 708, in get_sql
    container=self.container.get_sql(**kwargs),
  File "<snip>\venv\lib\site-packages\pypika\dialects.py", line 308, in get_sql
    return super(MSSQLQueryBuilder, self).get_sql(*args, groupby_alias=False, **kwargs)
TypeError: get_sql() got multiple values for keyword argument 'groupby_alias'

Ugly demonstration code:

from pypika import Field, MSSQLQuery, Query, Table


def generate_query(query_class):
    tbl = Table('query_tbl', schema='scm')
    col = Field('query_col', table=tbl)
    tbl2 = Table('sub_tbl', schema='scm')
    col2 = Field('sub_col', table=tbl2)

    print(query_class.__name__)
    subquery = query_class().from_(tbl2).select(col2)
    print('Subquery:', subquery)
    main_query_base = query_class().from_(tbl).select(col)
    print('Main query base:', main_query_base)
    predicate = col.notin(subquery)
    print('Predicate:', predicate)
    full_query = main_query_base.where(predicate)
    try:
        print('Fullquery:', full_query)
    except Exception as e:
        print('Exception:', e.args)
    finally:
        print('')


generate_query(Query)
generate_query(MSSQLQuery

Output of demonstration code:

Query
Subquery: SELECT "sub_col" FROM "scm"."sub_tbl"
Main query base: SELECT "query_col" FROM "scm"."query_tbl"
Predicate: "query_col" NOT IN SELECT "sub_col" FROM "scm"."sub_tbl"
Full query: SELECT "query_col" FROM "scm"."query_tbl" WHERE "query_col" NOT IN (SELECT "sub_col" FROM "scm"."sub_tbl")

MSSQLQuery
Subquery: SELECT "sub_col" FROM "scm"."sub_tbl"
Main query base: SELECT "query_col" FROM "scm"."query_tbl"
Predicate: "query_col" NOT IN SELECT "sub_col" FROM "scm"."sub_tbl"
Full query: Exception: ("get_sql() got multiple values for keyword argument 'groupby_alias'",)

Apologies if I'm inadvertently doing something wrong here, and thanks for the wonderful library!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions