Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] compile_sql with order_by failing #53

Closed
3 tasks done
CooperData opened this issue Oct 13, 2024 · 2 comments
Closed
3 tasks done

[Bug] compile_sql with order_by failing #53

CooperData opened this issue Oct 13, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@CooperData
Copy link

Is this a new bug in the SDK?

  • I believe this is a new bug in the SDK
  • I have searched the existing issues, and I could not find an existing issue for this bug
  • I am using the latest version of the SDK

Current Behavior

The same query is failing when compiling the query with order_by but succeeding when returning the data.

Expected Behavior

To return the correct compiled SQL.

Steps To Reproduce

All this function does is to return the compiled SQL if explain is true, otherwise return the result.

def query_sl(sdk_query, explain=False):
    client = SemanticLayerClient(
        environment_id=*****,
        auth_token=DBT_SL_TOKEN_PROD,
        host="semantic-layer.cloud.getdbt.com",
    )

    with client.session():
        if explain:
            sql = client.compile_sql(**sdk_query)
            return sql
        else:
            table = client.query(**sdk_query)
            pandas_df = table.to_pandas()
            return pandas_df

My query:

sdk_query = {'metrics': ['sale_count'],
 'group_by': ['metric_time__WEEK'],
 'where': ["{{ TimeDimension('metric_time', 'WEEK') }} >= dateadd('month', -3, current_date)"],
 'order_by': ['-metric_time__WEEK']}

When calling this:

df = query_sl(sdk_query)

it returns my dataframe as expected.

When calling this:

df = query_sl(sdk_query, explain=True)

It fails with following message:

/tmp/ipykernel_12/3049356820.py in query_sl(sdk_query, explain)
     16     with client.session():
     17         if explain:
---> 18             sql = client.compile_sql(**sdk_query)
     19             return sql
     20         else:

~/.cache/pypoetry/virtualenvs/python-kernel-OtKFaj5M-py3.9/lib/python3.9/site-packages/dbtsl/api/graphql/client/sync.py in _run(self, op, **kwargs)
     74             res = self._gql_session.execute(gql_query, variable_values=variables)
     75         except Exception as err:
---> 76             raise self._refine_err(err)
     77 
     78         return op.parse_response(res)

~/.cache/pypoetry/virtualenvs/python-kernel-OtKFaj5M-py3.9/lib/python3.9/site-packages/dbtsl/api/graphql/client/sync.py in _run(self, op, **kwargs)
     72 
     73         try:
---> 74             res = self._gql_session.execute(gql_query, variable_values=variables)
     75         except Exception as err:
     76             raise self._refine_err(err)

~/.cache/pypoetry/virtualenvs/python-kernel-OtKFaj5M-py3.9/lib/python3.9/site-packages/gql/client.py in execute(self, document, variable_values, operation_name, serialize_variables, parse_result, get_execution_result, **kwargs)
    858 
    859     def __enter__(self):
--> 860         return self.connect_sync()
    861 
    862     def __exit__(self, *args):

TransportQueryError: {'message': "Variable '$orderBy' got invalid value {'name': '-metric_time__WEEK'} at 'orderBy[0]'; Field 'name' is not defined by type 'OrderByInput'.", 'locations': [{'line': 1, 'column': 144}]}

Relevant log output

No response

Environment

- OS: Hex
- Python: 3.9
- Semantic Layer SDK: 0.5.0

Additional Context

No response

@CooperData CooperData added the bug Something isn't working label Oct 13, 2024
@serramatutu
Copy link
Collaborator

serramatutu commented Oct 16, 2024

Hey @CooperData! Thanks for the feedback.

This was an oversight by me, and there's a PR to fix it here, which I just merged. It should be out in a new version soon :)

@serramatutu
Copy link
Collaborator

Fixed as of version 0.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants