-
Notifications
You must be signed in to change notification settings - Fork 265
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
Adding cockroach-db intergration support for EvaDB #1423
Open
VrtanoskiAndrej
wants to merge
2
commits into
georgia-tech-db:staging
Choose a base branch
from
VrtanoskiAndrej:cockroach-int
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,7 @@ env.bak/ | |
venv.bak/ | ||
env38/ | ||
env_eva/ | ||
evadb-venv/ | ||
test_eva_db/ | ||
|
||
# Spyder project settings | ||
|
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,36 @@ | ||
CockroachDB | ||
========== | ||
|
||
The connection to CockroackDB is based on the `psycopg2 <https://pypi.org/project/psycopg2/>`_ library. | ||
|
||
Dependency | ||
---------- | ||
|
||
* psycopg2 | ||
|
||
|
||
|
||
Parameters | ||
---------- | ||
|
||
Required: | ||
|
||
* `user` is the database user. | ||
* `password` is the database password. | ||
* `host` is the host name or IP address. | ||
* `port` is the port used to make TCP/IP connection to the CockroachDB server. | ||
* `database` is the database name. | ||
|
||
|
||
Create Connection | ||
----------------- | ||
|
||
.. code-block:: text | ||
|
||
CREATE DATABASE cockroachdb_data WITH ENGINE = 'cockroachdb', PARAMETERS = { | ||
"user": "eva", | ||
"password": "password", | ||
"host": "localhost", | ||
"port": "26257", | ||
"database": "evadb" | ||
}; |
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,15 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""cockroach db integrations""" |
200 changes: 200 additions & 0 deletions
200
evadb/third_party/databases/cockroachdb/cockroachdb_handler.py
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,200 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import psycopg2 | ||
|
||
from evadb.third_party.databases.types import ( | ||
DBHandler, | ||
DBHandlerResponse, | ||
DBHandlerStatus, | ||
) | ||
|
||
class CockroachDBHandler(DBHandler): | ||
def __init__(self, name: str, **kwargs): | ||
""" | ||
Initialize the handler. | ||
Args: | ||
name (str): name of the DB handler instance | ||
**kwargs: arbitrary keyword arguments for establishing the connection. | ||
""" | ||
super().__init__(name) | ||
self.host = kwargs.get("host") | ||
self.port = kwargs.get("port") | ||
self.user = kwargs.get("user") | ||
self.password = kwargs.get("password") | ||
self.database = kwargs.get("database") | ||
self.uri = kwargs.get("uri") | ||
self.connection = None | ||
|
||
def connect(self) -> DBHandlerStatus: | ||
""" | ||
Set up the connection required by the handler. | ||
Returns: | ||
DBHandlerStatus | ||
""" | ||
try: | ||
if self.uri: | ||
self.connection = psycopg2.connect(self.uri, | ||
cursor_factory=psycopg2.extras.RealDictCursor) | ||
else: | ||
self.connection = psycopg2.connect( | ||
host=self.host, | ||
port=self.port, | ||
user=self.user, | ||
password=self.password, | ||
database=self.database, | ||
) | ||
self.connection.autocommit = True | ||
return DBHandlerStatus(status=True) | ||
except psycopg2.Error as e: | ||
return DBHandlerStatus(status=False, error=str(e)) | ||
|
||
def disconnect(self): | ||
""" | ||
Close any existing connections. | ||
""" | ||
if self.connection: | ||
self.connection.close() | ||
|
||
def check_connection(self) -> DBHandlerStatus: | ||
""" | ||
Check connection to the handler. | ||
Returns: | ||
DBHandlerStatus | ||
""" | ||
if self.connection: | ||
return DBHandlerStatus(status=True) | ||
else: | ||
return DBHandlerStatus(status=False, error="Not connected to the database.") | ||
|
||
def get_tables(self) -> DBHandlerResponse: | ||
""" | ||
Return the list of tables in the database. | ||
Returns: | ||
DBHandlerResponse | ||
""" | ||
if not self.connection: | ||
return DBHandlerResponse(data=None, error="Not connected to the database.") | ||
|
||
try: | ||
query = "SELECT table_name FROM information_schema.tables WHERE table_schema NOT IN ('information_schema', 'pg_catalog')" | ||
cursor = self.connection.cursor() | ||
cursor.execute(query) | ||
res = cursor.fetchall() | ||
res_df = pd.DataFrame( | ||
res, columns=[desc[0].lower() for desc in cursor.description] | ||
) | ||
return DBHandlerResponse(data=res_df) | ||
except psycopg2.Error as e: | ||
return DBHandlerResponse(data=None, error=str(e)) | ||
|
||
def get_columns(self, table_name: str) -> DBHandlerResponse: | ||
""" | ||
Returns the list of columns for the given table. | ||
Args: | ||
table_name (str): name of the table whose columns are to be retrieved. | ||
Returns: | ||
DBHandlerResponse | ||
""" | ||
if not self.connection: | ||
return DBHandlerResponse(data=None, error="Not connected to the database.") | ||
|
||
try: | ||
query = f"SELECT column_name as name, data_type as dtype, udt_name FROM information_schema.columns WHERE table_name='{table_name}'" | ||
self.connection.cursor().execute(query) | ||
cursor = self.connection.cursor() | ||
|
||
cursor.execute(query) | ||
res = cursor.fetchall() | ||
columns_df = pd.DataFrame( | ||
res, columns=[desc[0].lower() for desc in cursor.description] | ||
) | ||
columns_df["dtype"] = columns_df.apply( | ||
lambda x: self._pg_to_python_types(x["dtype"], x["udt_name"]), axis=1 | ||
) | ||
return DBHandlerResponse(data=columns_df) | ||
except psycopg2.Error as e: | ||
return DBHandlerResponse(data=None, error=str(e)) | ||
|
||
def _fetch_results_as_df(self, cursor): | ||
""" | ||
This is currently the only clean solution that we have found so far. | ||
Reference to Postgres API: https://www.psycopg.org/docs/cursor.html#fetch | ||
In short, currently there is no very clean programming way to differentiate | ||
CREATE, INSERT, SELECT. CREATE and INSERT do not return any result, so calling | ||
fetchall() on those will yield a programming error. Cursor has an attribute | ||
rowcount, but it indicates # of rows that are affected. In that case, for both | ||
INSERT and SELECT rowcount is not 0, so we also cannot use this API to | ||
differentiate INSERT and SELECT. | ||
""" | ||
try: | ||
res = cursor.fetchall() | ||
res_df = pd.DataFrame( | ||
res, columns=[desc[0].lower() for desc in cursor.description] | ||
) | ||
return res_df | ||
except psycopg2.ProgrammingError as e: | ||
if str(e) == "no results to fetch": | ||
return pd.DataFrame({"status": ["success"]}) | ||
raise e | ||
|
||
def execute_native_query(self, query_string: str) -> DBHandlerResponse: | ||
""" | ||
Executes the native query on the database. | ||
Args: | ||
query_string (str): query in native format | ||
Returns: | ||
DBHandlerResponse | ||
""" | ||
if not self.connection: | ||
return DBHandlerResponse(data=None, error="Not connected to the database.") | ||
|
||
try: | ||
cursor = self.connection.cursor() | ||
cursor.execute(query_string) | ||
return DBHandlerResponse(data=self._fetch_results_as_df(cursor)) | ||
except psycopg2.Error as e: | ||
return DBHandlerResponse(data=None, error=str(e)) | ||
|
||
def _pg_to_python_types(self, pg_type: str, udt_name: str): | ||
primitive_type_mapping = { | ||
"integer": int, | ||
"bigint": int, | ||
"smallint": int, | ||
"numeric": float, | ||
"real": float, | ||
"double precision": float, | ||
"character": str, | ||
"character varying": str, | ||
"text": str, | ||
"boolean": bool, | ||
# Add more mappings as needed | ||
} | ||
|
||
user_defined_type_mapping = { | ||
"vector": np.ndarray | ||
# Handle user defined types constructed by Postgres extension. | ||
} | ||
|
||
if pg_type in primitive_type_mapping: | ||
return primitive_type_mapping[pg_type] | ||
elif pg_type == "USER-DEFINED" and udt_name in user_defined_type_mapping: | ||
return user_defined_type_mapping[udt_name] | ||
else: | ||
raise Exception( | ||
f"Unsupported column {pg_type} encountered in the postgres table. Please raise a feature request!" | ||
) |
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
Oops, something went wrong.
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.
The docs build is failing in this line. Maybe we need a few more
=
?