-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
New Query Runner: Apache Pinot #5446
Closed
Closed
Changes from all commits
Commits
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,141 @@ | ||
import logging | ||
import requests | ||
|
||
from redash.query_runner import * | ||
from redash.utils import json_dumps, json_loads | ||
|
||
try: | ||
from pinotdb import connect | ||
|
||
enabled = True | ||
except ImportError: | ||
enabled = False | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
PINOT_TYPES_MAPPING = { | ||
"INT": TYPE_INTEGER, | ||
"LONG": TYPE_INTEGER, | ||
"FLOAT": TYPE_FLOAT, | ||
"DOUBLE": TYPE_FLOAT, | ||
"STRING": TYPE_STRING, | ||
"BYTES": TYPE_STRING, | ||
} | ||
|
||
DEFAULT_BROKER_PORT = 8000 | ||
DEFAULT_CONTROLLER_PORT = 9000 | ||
|
||
|
||
class Pinot(BaseSQLQueryRunner): | ||
noop_query = "SELECT 1" | ||
|
||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
"type": "object", | ||
"properties": { | ||
"controller_host": { | ||
"type": "string", | ||
"title": "Controller service host", | ||
}, | ||
"controller_port": { | ||
"type": "number", | ||
"default": DEFAULT_CONTROLLER_PORT, | ||
"title": "Controller service port", | ||
}, | ||
"broker_host": {"type": "string", "title": "Broker host"}, | ||
"broker_port": { | ||
"type": "number", | ||
"default": DEFAULT_BROKER_PORT, | ||
"title": "Broker port", | ||
}, | ||
"use_ssl": {"type": "boolean", "default": False, "title": "Use SSL"}, | ||
}, | ||
"order": [ | ||
"controller_host", | ||
"controller_port", | ||
"broker_host", | ||
"broker_port", | ||
], | ||
"required": ["controller_host", "broker_host"], | ||
} | ||
|
||
@classmethod | ||
def type(cls): | ||
return "pinot" | ||
|
||
def __init__(self, configuration): | ||
super(Pinot, self).__init__(configuration) | ||
|
||
self._controller = f"{self.configuration['controller_host']}:{self.configuration.get('controller_port', DEFAULT_CONTROLLER_PORT)}" | ||
self._proto = "https" if self.configuration["use_ssl"] == True else "http" | ||
|
||
def _get_table_schema(self, schema, table_name): | ||
url = f"{self._proto}://{self._controller}/schemas/{table_name}" | ||
r = requests.get(url) | ||
|
||
if r.status_code != 200: | ||
raise Exception(f"Failed getting schema for table {table_name}.") | ||
|
||
result = r.json() | ||
|
||
for column in result.get("dimensionFieldSpecs", []) + result.get( | ||
"metricFieldSpecs", [] | ||
): | ||
c = { | ||
"name": column["name"], | ||
"type": PINOT_TYPES_MAPPING[column["dataType"]], | ||
} | ||
schema[table_name]["columns"].append(c) | ||
|
||
def _get_tables(self, schema): | ||
url = f"{self._proto}://{self._controller}/tables" | ||
r = requests.get(url) | ||
|
||
if r.status_code != 200: | ||
raise Exception("Failed getting tables.") | ||
|
||
for table_name in r.json()["tables"]: | ||
logger.debug(f"Discovered table {table_name}") | ||
if table_name not in schema: | ||
schema[table_name] = {"name": table_name, "columns": []} | ||
self._get_table_schema(schema, table_name) | ||
|
||
return list(schema.values()) | ||
|
||
def run_query(self, query, user): | ||
_broker = self.configuration["broker_host"] | ||
_port = self.configuration.get("broker_port", DEFAULT_BROKER_PORT) | ||
|
||
connection = connect( | ||
host=_broker, port=_port, path="/query/sql", scheme=self._proto | ||
) | ||
cursor = connection.cursor() | ||
|
||
try: | ||
json_data = None | ||
cursor.execute(query) | ||
|
||
if cursor.description is not None: | ||
columns = self.fetch_columns([(i[0], None) for i in cursor.description]) | ||
rows = [ | ||
dict(zip((column["name"] for column in columns), row)) | ||
for row in cursor | ||
] | ||
|
||
data = {"columns": columns, "rows": rows} | ||
error = None | ||
json_data = json_dumps(data) | ||
else: | ||
error = "Query completed but it returned no data." | ||
json_data = None | ||
except (KeyboardInterrupt, JobTimeoutException): | ||
connection.close() | ||
raise | ||
finally: | ||
connection.close() | ||
return json_data, error | ||
|
||
|
||
register(Pinot) |
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 |
---|---|---|
|
@@ -42,3 +42,4 @@ xlrd==2.0.1 | |
openpyxl==3.0.7 | ||
firebolt-sdk | ||
pandas==1.3.4 | ||
pinotdb==0.3.3 | ||
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. 0.3.11 is the latest version :p |
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.
Can you add more types here, especially the TIMESTAMP