-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ibis backend db helper to adapt to different databases
- Loading branch information
1 parent
b1cdd38
commit 592e8b5
Showing
5 changed files
with
67 additions
and
39 deletions.
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 |
---|---|---|
@@ -1,19 +1,63 @@ | ||
import base64 | ||
|
||
import pandas as pd | ||
|
||
BASE64_PREFIX = 'base64:' | ||
|
||
|
||
class Base64Mixin: | ||
def convert_data_format(self, data): | ||
"""Convert byte data to base64 format for storage in the database.""" | ||
if isinstance(data, bytes): | ||
return BASE64_PREFIX + base64.b64encode(data).decode('utf-8') | ||
else: | ||
return data | ||
|
||
def recover_data_format(self, data): | ||
"""Recover byte data from base64 format stored in the database.""" | ||
if isinstance(data, str) and data.startswith(BASE64_PREFIX): | ||
return base64.b64decode(data[len(BASE64_PREFIX) :]) | ||
else: | ||
return data | ||
|
||
def process_schema_types(self, schema_mapping): | ||
"""Convert bytes to string in the schema.""" | ||
for key, value in schema_mapping.items(): | ||
if value == 'Bytes': | ||
schema_mapping[key] = 'String' | ||
return schema_mapping | ||
|
||
|
||
class DBHelper: | ||
match_dialect = 'base' | ||
|
||
def _default_insert_processor(table_name, datas): | ||
"""Default insert processor for SQL dialects.""" | ||
return table_name, datas | ||
def __init__(self, dialect): | ||
self.dialect = dialect | ||
|
||
def process_before_insert(self, table_name, datas): | ||
return table_name, pd.DataFrame(datas) | ||
|
||
def _clickhouse_insert_processor(table_name, datas): | ||
"""Insert processor for ClickHouse.""" | ||
return f'`{table_name}`', pd.DataFrame(datas) | ||
def process_schema_types(self, schema_mapping): | ||
return schema_mapping | ||
|
||
def convert_data_format(self, data): | ||
return data | ||
|
||
def get_insert_processor(dialect): | ||
def recover_data_format(self, data): | ||
return data | ||
|
||
|
||
class ClickHouseHelper(Base64Mixin, DBHelper): | ||
match_dialect = 'clickhouse' | ||
|
||
def process_before_insert(self, table_name, datas): | ||
return f'`{table_name}`', pd.DataFrame(datas) | ||
|
||
|
||
def get_db_helper(dialect) -> DBHelper: | ||
"""Get the insert processor for the given dialect.""" | ||
funcs = { | ||
'clickhouse': _clickhouse_insert_processor, | ||
} | ||
return funcs.get(dialect, _default_insert_processor) | ||
for helper in DBHelper.__subclasses__(): | ||
if helper.match_dialect == dialect: | ||
return helper(dialect) | ||
|
||
return DBHelper(dialect) |
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
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