-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add support for Dremio integration #135
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from typing import Optional, Dict, Any, List, Union, Tuple | ||
|
||
from pyarrow.flight import FlightDescriptor, FlightCallOptions | ||
|
||
from apollo.integrations.db.base_db_proxy_client import BaseDbProxyClient | ||
|
||
from pyarrow import flight, Schema, types as pyarrow_types | ||
|
||
_ATTR_CONNECT_ARGS = "connect_args" | ||
|
||
|
||
class DremioProxyClient(BaseDbProxyClient): | ||
""" | ||
Proxy client for Dremio. Credentials are expected to be supplied under "connect_args" and | ||
will be passed directly to `pyarrow.flight.connect`, so only attributes supported as parameters by | ||
`flight.connect` should be passed. | ||
""" | ||
|
||
def __init__(self, credentials: Optional[Dict], **kwargs: Any): | ||
super().__init__(connection_type="dremio") | ||
if not credentials or _ATTR_CONNECT_ARGS not in credentials: | ||
raise ValueError( | ||
f"Dremio agent client requires {_ATTR_CONNECT_ARGS} in credentials" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: if |
||
self._connection = flight.connect(**credentials[_ATTR_CONNECT_ARGS]) # type: ignore | ||
self._headers = [ | ||
( | ||
b"authorization", | ||
f"bearer {credentials.get('token')}".encode("utf-8"), | ||
) | ||
] | ||
|
||
@property | ||
def wrapped_client(self): | ||
return self._connection | ||
|
||
def execute(self, query: str) -> Dict: | ||
# Send the query to the server and save the ticket identifier | ||
flight_info = self._connection.get_flight_info( | ||
FlightDescriptor.for_command(command=query), | ||
FlightCallOptions(headers=self._headers), | ||
) | ||
ticket = flight_info.endpoints[0].ticket | ||
|
||
# create a FlightStreamReader from the ticket | ||
reader = self._connection.do_get( | ||
ticket, FlightCallOptions(headers=self._headers) | ||
) | ||
|
||
# read all results of the query | ||
results = reader.read_all() | ||
|
||
# turn results into a serializable list | ||
records = [ | ||
list(row) | ||
for row in zip(*[column.to_pylist() for column in results.itercolumns()]) | ||
] | ||
|
||
return { | ||
"records": records, | ||
"description": self._get_dbapi_description(reader.schema), | ||
"rowcount": len(records), | ||
} | ||
|
||
def _get_dbapi_description(self, schema: Schema) -> List[Tuple]: | ||
""" | ||
Create a DB API compliant description object from the FlightStreamReader schema | ||
""" | ||
|
||
def arrow_type_to_cursor_type(arrow_type: Any) -> Union[int, str]: | ||
if pyarrow_types.is_string(arrow_type): | ||
return 1 | ||
elif pyarrow_types.is_int32(arrow_type): | ||
return 2 | ||
elif pyarrow_types.is_float32(arrow_type): | ||
return 3 | ||
# Add more type mappings as needed | ||
return "unknown" | ||
|
||
return [ | ||
( | ||
field.name, | ||
arrow_type_to_cursor_type(field.type), | ||
None, | ||
None, | ||
None, | ||
None, | ||
None, | ||
) | ||
for field in schema | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from unittest import TestCase | ||
from unittest.mock import patch, MagicMock | ||
|
||
from apollo.integrations.db.dremio_proxy_client import DremioProxyClient | ||
|
||
import pyarrow | ||
|
||
|
||
class DremioClientTests(TestCase): | ||
def setUp(self): | ||
self.credentials = { | ||
"connect_args": {"location": "localhost"}, | ||
"token": "dummy_token", | ||
} | ||
self.query = "SELECT * FROM test_table" | ||
|
||
@patch("apollo.integrations.db.dremio_proxy_client.flight.connect") | ||
def test_init_success(self, mock_connect): | ||
mock_connection = MagicMock() | ||
mock_connect.return_value = mock_connection | ||
|
||
client = DremioProxyClient(credentials=self.credentials) | ||
|
||
self.assertEqual(client._connection, mock_connection) | ||
self.assertEqual(client._headers, [(b"authorization", b"bearer dummy_token")]) | ||
mock_connect.assert_called_once_with(location="localhost") | ||
|
||
def test_init_missing_connect_args(self): | ||
with self.assertRaises(ValueError): | ||
DremioProxyClient(credentials={}) | ||
|
||
@patch("apollo.integrations.db.dremio_proxy_client.flight.connect") | ||
@patch("apollo.integrations.db.dremio_proxy_client.FlightCallOptions") | ||
def test_execute(self, mock_flight_call_options, mock_connect): | ||
mock_connection = MagicMock() | ||
mock_flight_info = MagicMock() | ||
mock_flight_call_options_instance = MagicMock() | ||
|
||
mock_flight_call_options.return_value = mock_flight_call_options_instance | ||
|
||
mock_connect.return_value = mock_connection | ||
mock_connection.get_flight_info.return_value = mock_flight_info | ||
mock_flight_info.endpoints = [MagicMock(ticket="dummy_ticket")] | ||
|
||
mock_reader = MagicMock() | ||
mock_reader.read_all.return_value = pyarrow.table( | ||
{"col1": [1, 2, 3], "col2": ["a", "b", "c"]} | ||
) | ||
|
||
mock_reader.schema = pyarrow.schema( | ||
[("col1", pyarrow.int32()), ("col2", pyarrow.string())] | ||
) | ||
mock_connection.do_get.return_value = mock_reader | ||
|
||
client = DremioProxyClient(credentials=self.credentials) | ||
|
||
result = client.execute(self.query) | ||
|
||
self.assertEqual(result["records"], [[1, "a"], [2, "b"], [3, "c"]]) | ||
self.assertEqual(result["rowcount"], 3) | ||
self.assertEqual( | ||
result["description"], | ||
[ | ||
("col1", 2, None, None, None, None, None), | ||
("col2", 1, None, None, None, None, None), | ||
], | ||
) | ||
|
||
@patch("apollo.integrations.db.dremio_proxy_client.flight.connect") | ||
def test_get_description(self, mock_connect): | ||
schema = pyarrow.schema([("col1", pyarrow.int32()), ("col2", pyarrow.string())]) | ||
|
||
client = DremioProxyClient(credentials=self.credentials) | ||
description = client._get_dbapi_description(schema) | ||
|
||
self.assertEqual( | ||
description, | ||
[ | ||
("col1", 2, None, None, None, None, None), | ||
("col2", 1, None, None, None, None, None), | ||
], | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
connect_args
is intended to be used when we're passing everything in connect_args to a connect method likeconnect(**connect_args)
, and the idea is that we can pass new parameters without having to update the agent codein this case it seems you're getting only
host
andtoken
from connect_args, so no need to use it, you can check the code forLooker
where we're gettingclient_id
andclient_secret
directly from credentialsAlso, in this case, if
flight.connect
accepts more parameters than host, even when we're not using them now, might be good to keep connect_args and useflight.connect(**connect_args)
and specifytoken
in a separate property in credentials.makes sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes total sense! implemented those changes