Skip to content

Commit 2bec10e

Browse files
committed
Execute should block until at least one row is received
1 parent 9d898a8 commit 2bec10e

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

tests/integration/test_dbapi_integration.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ def test_execute_many_without_params(trino_connection):
153153
cur = trino_connection.cursor()
154154
cur.execute("CREATE TABLE memory.default.test_execute_many_without_param (value varchar)")
155155
cur.fetchall()
156-
cur.executemany("INSERT INTO memory.default.test_execute_many_without_param (value) VALUES (?)", [])
157156
with pytest.raises(TrinoUserError) as e:
157+
cur.executemany("INSERT INTO memory.default.test_execute_many_without_param (value) VALUES (?)", [])
158158
cur.fetchall()
159159
assert "Incorrect number of parameters: expected 1 but found 0" in str(e.value)
160160

@@ -883,13 +883,12 @@ def test_transaction_autocommit(trino_connection_in_autocommit):
883883
with trino_connection_in_autocommit as connection:
884884
connection.start_transaction()
885885
cur = connection.cursor()
886-
cur.execute(
887-
"""
888-
CREATE TABLE memory.default.nation
889-
AS SELECT * from tpch.tiny.nation
890-
""")
891-
892886
with pytest.raises(TrinoUserError) as transaction_error:
887+
cur.execute(
888+
"""
889+
CREATE TABLE memory.default.nation
890+
AS SELECT * from tpch.tiny.nation
891+
""")
893892
cur.fetchall()
894893
assert "Catalog only supports writes using autocommit: memory" \
895894
in str(transaction_error.value)

tests/unit/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def sample_post_response_data():
3737
"""
3838

3939
yield {
40-
"nextUri": "coordinator:8080/v1/statement/20210817_140827_00000_arvdv/1",
40+
"nextUri": "https://coordinator:8080/v1/statement/20210817_140827_00000_arvdv/1",
4141
"id": "20210817_140827_00000_arvdv",
4242
"taskDownloadUris": [],
43-
"infoUri": "http://coordinator:8080/query.html?20210817_140827_00000_arvdv",
43+
"infoUri": "https://coordinator:8080/query.html?20210817_140827_00000_arvdv",
4444
"stats": {
4545
"scheduled": False,
4646
"runningSplits": 0,

tests/unit/test_dbapi.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,28 @@ def test_http_session_is_defaulted_when_not_specified(mock_client):
5151

5252

5353
@httprettified
54-
def test_token_retrieved_once_per_auth_instance(sample_post_response_data):
54+
def test_token_retrieved_once_per_auth_instance(sample_post_response_data, sample_get_response_data):
5555
token = str(uuid.uuid4())
5656
challenge_id = str(uuid.uuid4())
5757

5858
redirect_server = f"{REDIRECT_RESOURCE}/{challenge_id}"
5959
token_server = f"{TOKEN_RESOURCE}/{challenge_id}"
6060

6161
post_statement_callback = PostStatementCallback(redirect_server, token_server, [token], sample_post_response_data)
62+
get_statement_callback = PostStatementCallback(redirect_server, token_server, [token], sample_get_response_data)
6263

63-
# bind post statement
64+
# bind post statement to submit query
6465
httpretty.register_uri(
6566
method=httpretty.POST,
6667
uri=f"{SERVER_ADDRESS}:8080{constants.URL_STATEMENT_PATH}",
6768
body=post_statement_callback)
6869

70+
# bind get statement for result retrieval
71+
httpretty.register_uri(
72+
method=httpretty.GET,
73+
uri=f"{SERVER_ADDRESS}:8080{constants.URL_STATEMENT_PATH}/20210817_140827_00000_arvdv/1",
74+
body=get_statement_callback)
75+
6976
# bind get token
7077
get_token_callback = GetTokenCallback(token_server, token)
7178
httpretty.register_uri(
@@ -108,21 +115,28 @@ def test_token_retrieved_once_per_auth_instance(sample_post_response_data):
108115

109116

110117
@httprettified
111-
def test_token_retrieved_once_when_authentication_instance_is_shared(sample_post_response_data):
118+
def test_token_retrieved_once_when_authentication_instance_is_shared(sample_post_response_data, sample_get_response_data):
112119
token = str(uuid.uuid4())
113120
challenge_id = str(uuid.uuid4())
114121

115122
redirect_server = f"{REDIRECT_RESOURCE}/{challenge_id}"
116123
token_server = f"{TOKEN_RESOURCE}/{challenge_id}"
117124

118125
post_statement_callback = PostStatementCallback(redirect_server, token_server, [token], sample_post_response_data)
126+
get_statement_callback = PostStatementCallback(redirect_server, token_server, [token], sample_get_response_data)
119127

120-
# bind post statement
128+
# bind post statement to submit query
121129
httpretty.register_uri(
122130
method=httpretty.POST,
123131
uri=f"{SERVER_ADDRESS}:8080{constants.URL_STATEMENT_PATH}",
124132
body=post_statement_callback)
125133

134+
# bind get statement for result retrieval
135+
httpretty.register_uri(
136+
method=httpretty.GET,
137+
uri=f"{SERVER_ADDRESS}:8080{constants.URL_STATEMENT_PATH}/20210817_140827_00000_arvdv/1",
138+
body=get_statement_callback)
139+
126140
# bind get token
127141
get_token_callback = GetTokenCallback(token_server, token)
128142
httpretty.register_uri(
@@ -166,21 +180,28 @@ def test_token_retrieved_once_when_authentication_instance_is_shared(sample_post
166180

167181

168182
@httprettified
169-
def test_token_retrieved_once_when_multithreaded(sample_post_response_data):
183+
def test_token_retrieved_once_when_multithreaded(sample_post_response_data, sample_get_response_data):
170184
token = str(uuid.uuid4())
171185
challenge_id = str(uuid.uuid4())
172186

173187
redirect_server = f"{REDIRECT_RESOURCE}/{challenge_id}"
174188
token_server = f"{TOKEN_RESOURCE}/{challenge_id}"
175189

176190
post_statement_callback = PostStatementCallback(redirect_server, token_server, [token], sample_post_response_data)
191+
get_statement_callback = PostStatementCallback(redirect_server, token_server, [token], sample_get_response_data)
177192

178-
# bind post statement
193+
# bind post statement to submit query
179194
httpretty.register_uri(
180195
method=httpretty.POST,
181196
uri=f"{SERVER_ADDRESS}:8080{constants.URL_STATEMENT_PATH}",
182197
body=post_statement_callback)
183198

199+
# bind get statement for result retrieval
200+
httpretty.register_uri(
201+
method=httpretty.GET,
202+
uri=f"{SERVER_ADDRESS}:8080{constants.URL_STATEMENT_PATH}/20210817_140827_00000_arvdv/1",
203+
body=get_statement_callback)
204+
184205
# bind get token
185206
get_token_callback = GetTokenCallback(token_server, token)
186207
httpretty.register_uri(

trino/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ def execute(self, additional_http_headers=None) -> TrinoResult:
697697
rows = self._row_mapper.map(status.rows) if self._row_mapper else status.rows
698698

699699
self._result = TrinoResult(self, rows)
700+
# Execute should block until at least one row is received
701+
while not self.finished and not self.cancelled and len(self._result._rows) == 0:
702+
self._result._rows += self.fetch()
700703
return self._result
701704

702705
def _update_state(self, status):

0 commit comments

Comments
 (0)