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

Fix issue of queries not reaching completed state #210

Closed
wants to merge 1 commit into from

Conversation

guyco33
Copy link
Member

@guyco33 guyco33 commented Jul 28, 2022

Fix issue of queries not reaching completed state - see discussion on https://trinodb.slack.com/archives/CGB0QHWSW/p1658934510749439

Adds a test to demonstrate effect of trinodb/trino#13055

@cla-bot cla-bot bot added the cla-signed label Jul 28, 2022
@guyco33 guyco33 added the bug Something isn't working label Jul 28, 2022
@@ -317,7 +317,7 @@ def _get_server_version_info(self, connection: Connection) -> Any:
query = "SELECT version()"
try:
res = connection.execute(sql.text(query))
version = res.scalar()
version = res.scalar_one()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that 351 behaves differently with query state ..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc: @arhimondr @losipiuk Is there consensus on whether the new behaviour is working as expected? I think this is related to trinodb/trino#13055

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a non-op change as the only difference between scalar and scalar_one is that scalar_one would additionally throw if no rows are returned.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SELECT version() should always return exactly one row and we want to raise exception when it doesn't.
Alternative solution might be to execute res.fetchall() after version = res.scalar() but it's cumbersome.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is not limited to sqlalchemy. Following dbapi code will also result in cancelled query while the result is scalar.

cur.execute('SELECT VERSION()')
cur.fetchone()
cur.cancel()

It is indeed related to the PR on Trino that @hashhar refers to. The issue is that the python client doesn't acknowledge the reception of the data by calling the next_uri on the Trino API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe #220 is the way to go. Thanks @mdesmet for the thorough investigation and the fix.

@guyco33 guyco33 force-pushed the fix_result_fetching_issue branch 2 times, most recently from ca8add6 to 2f3493a Compare July 30, 2022 15:45
@ebyhr ebyhr requested review from hashhar and mdesmet July 31, 2022 22:48
@guyco33 guyco33 changed the title Fix issue of queries not reaching completed state Add test for queries reaching completed state Aug 18, 2022
@hashhar hashhar requested a review from ebyhr August 18, 2022 14:18
@hashhar
Copy link
Member

hashhar commented Aug 18, 2022

@mdesmet @ebyhr Can you PTAL again. This just adds a regression test now (which asserts the broken behaviour) - the fix will be part of #220.

@hashhar hashhar force-pushed the fix_result_fetching_issue branch 2 times, most recently from 8467ba7 to 323c15f Compare August 18, 2022 14:40
@mdesmet
Copy link
Contributor

mdesmet commented Aug 18, 2022

This code can be used to simulate this using dbapi on the #220 branch. This didn't result in any unfinished queries. Now again I don't know enough about the related Trino internals what's the chance of this becoming a flaky test on github CI.

import trino

count_not_finished = 0
trino_connection = trino.dbapi.connect(
    host="localhost",
    port=8080,
    user="admin"
)
for _ in range(0, 10000):
    cur = trino_connection.cursor()
    cur.execute("SELECT VERSION()")
    cur.fetchone()
    cur.fetchone() # simulate sqlalchemy scalar_one
    if not cur._query.finished:
        count_not_finished += 1

print(str(count_not_finished) + " unfinished queries")

In other words we need to be sure that doing 2 fetchone() calls (actually fetching up to 3 requests as long next_uri is not null) is enough to fully consume all results and mark the query as finished.

@guyco33 guyco33 changed the title Add test for queries reaching completed state Fix issue of queries not reaching completed state Aug 21, 2022
@hashhar
Copy link
Member

hashhar commented Sep 22, 2022

This has been addressed via #220 now. It has a different test though since the test here is potentially flaky (and also fails some of the existing tests regarding INSERTs).

@hashhar hashhar closed this Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cla-signed
Development

Successfully merging this pull request may close these issues.

5 participants