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 get/delete requests in Presto to accept headers #200

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions pyhive/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ class will use the default requests behavior of making a new session per HTTP re
self._poll_interval = poll_interval
self._source = source
self._session_props = session_props if session_props is not None else {}
self._headers = {
'X-Presto-Catalog': self._catalog,
'X-Presto-Schema': self._schema,
'X-Presto-Source': self._source,
'X-Presto-User': self._username,
}

if protocol not in ('http', 'https'):
raise ValueError("Protocol must be http/https, was {!r}".format(protocol))
Expand Down Expand Up @@ -174,15 +180,8 @@ def execute(self, operation, parameters=None):

Return values are not defined.
"""
headers = {
'X-Presto-Catalog': self._catalog,
'X-Presto-Schema': self._schema,
'X-Presto-Source': self._source,
'X-Presto-User': self._username,
}

if self._session_props:
headers['X-Presto-Session'] = ','.join(
self._headers['X-Presto-Session'] = ','.join(
'{}={}'.format(propname, propval)
for propname, propval in self._session_props.items()
)
Expand All @@ -200,9 +199,9 @@ def execute(self, operation, parameters=None):
self._protocol,
'{}:{}'.format(self._host, self._port), '/v1/statement', None, None, None))
_logger.info('%s', sql)
_logger.debug("Headers: %s", headers)
_logger.debug("Headers: %s", self._headers)
response = self._requests_session.post(
url, data=sql.encode('utf-8'), headers=headers, **self._requests_kwargs)
url, data=sql.encode('utf-8'), headers=self._headers, **self._requests_kwargs)
self._process_response(response)

def cancel(self):
Expand All @@ -212,7 +211,7 @@ def cancel(self):
assert self._state == self._STATE_FINISHED, "Should be finished if nextUri is None"
return

response = self._requests_session.delete(self._nextUri, **self._requests_kwargs)
response = self._requests_session.delete(self._nextUri, headers=self._headers, **self._requests_kwargs)
if response.status_code != requests.codes.no_content:
fmt = "Unexpected status code after cancel {}\n{}"
raise OperationalError(fmt.format(response.status_code, response.content))
Expand All @@ -234,13 +233,13 @@ def poll(self):
if self._nextUri is None:
assert self._state == self._STATE_FINISHED, "Should be finished if nextUri is None"
return None
response = self._requests_session.get(self._nextUri, **self._requests_kwargs)
response = self._requests_session.get(self._nextUri, headers=self._headers, **self._requests_kwargs)
self._process_response(response)
return response.json()

def _fetch_more(self):
"""Fetch the next URI and update state"""
self._process_response(self._requests_session.get(self._nextUri, **self._requests_kwargs))
self._process_response(self._requests_session.get(self._nextUri, headers=self._headers, **self._requests_kwargs))

def _decode_binary(self, rows):
# As of Presto 0.69, binary data is returned as the varbinary type in base64 format
Expand Down