Skip to content

Commit

Permalink
Add raw query response to cursor class (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
piby180 authored Jul 13, 2024
1 parent eee1663 commit e3f4822
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pinotdb/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def __init__(
self.schema = None
self.rowcount = -1
self._results = None
self.raw_query_response = None
self.timeUsedMs = -1
self._debug = debug
self._preserve_types = preserve_types
Expand Down Expand Up @@ -396,7 +397,15 @@ def finalize_query_payload(
def normalize_query_response(self, input_query, query_response):
try:
payload = query_response.json()
self.raw_query_response = {
"response" : payload,
"status_code" : query_response.status_code
}
except Exception as e:
self.raw_query_response = {
"response" : query_response.text,
"status_code" : query_response.status_code
}
raise exceptions.DatabaseError(
f"Error when querying {input_query} from {self.url}, "
f"raw response is:\n{query_response.text}"
Expand Down
114 changes: 114 additions & 0 deletions tests/unit/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,120 @@ def test_does_nothing_for_setoutputsizes(self):
cursor.setoutputsizes(123)

# All good, nothing happened

def test_checks_raw_query_response_with_single_stage_and_status_code_200(self):
cursor = self.create_cursor(
{
'dataSchema': {
'columnNames': ['age'],
'columnDataTypes': ['INT'],
},
'rows': [[1], [2], [3]],
},
status_code=200,
extra_payload={'numGroupsLimitReached': True},
use_multistage_engine=False
)
cursor.execute('some statement')
raw_query_response = {
'response': {
'numServersResponded': 1,
'numServersQueried': 1,
'resultTable': {
'dataSchema': {'columnNames': ['age'], 'columnDataTypes': ['INT']},
'rows': [[1], [2], [3]],
},
'numGroupsLimitReached': True,
},
'status_code': 200,
}
self.assertEqual(cursor.raw_query_response, raw_query_response)

def test_checks_raw_query_response_with_multi_stage_and_status_code_200(self):
cursor = self.create_cursor(
{
'dataSchema': {
'columnNames': ['age'],
'columnDataTypes': ['INT'],
},
'rows': [[1], [2], [3]],
},
status_code=200,
extra_payload={'numGroupsLimitReached': True},
use_multistage_engine=True
)
cursor.execute('some statement')
raw_query_response = {
'response': {
'numServersResponded': 1,
'numServersQueried': 1,
'resultTable': {
'dataSchema': {'columnNames': ['age'], 'columnDataTypes': ['INT']},
'rows': [[1], [2], [3]],
},
'numGroupsLimitReached': True,
},
'status_code': 200,
}
self.assertEqual(cursor.raw_query_response, raw_query_response)

def test_checks_raw_query_response_with_single_stage_and_status_code_400(self):
cursor = self.create_cursor(
{
'dataSchema': {
'columnNames': ['age'],
'columnDataTypes': ['INT'],
},
'rows': [[1], [2], [3]],
},
status_code=400,
extra_payload={'exceptions': ['something', 'wrong']},
use_multistage_engine=False
)
with self.assertRaises(exceptions.ProgrammingError):
cursor.execute('some statement')
raw_query_response = {
'response': {
'numServersResponded': 1,
'numServersQueried': 1,
'resultTable': {
'dataSchema': {'columnNames': ['age'], 'columnDataTypes': ['INT']},
'rows': [[1], [2], [3]],
},
'exceptions': ['something', 'wrong'],
},
'status_code': 400,
}
self.assertEqual(cursor.raw_query_response, raw_query_response)

def test_checks_raw_query_response_with_multi_stage_and_status_code_400(self):
cursor = self.create_cursor(
{
'dataSchema': {
'columnNames': ['age'],
'columnDataTypes': ['INT'],
},
'rows': [[1], [2], [3]],
},
status_code=400,
extra_payload={'exceptions': ['something', 'wrong']},
use_multistage_engine=True
)
with self.assertRaises(exceptions.ProgrammingError):
cursor.execute('some statement')
raw_query_response = {
'response': {
'numServersResponded': 1,
'numServersQueried': 1,
'resultTable': {
'dataSchema': {'columnNames': ['age'], 'columnDataTypes': ['INT']},
'rows': [[1], [2], [3]],
},
'exceptions': ['something', 'wrong'],
},
'status_code': 400,
}
self.assertEqual(cursor.raw_query_response, raw_query_response)


class AsyncCursorTest(IsolatedAsyncioTestCase):
Expand Down

0 comments on commit e3f4822

Please sign in to comment.