-
Notifications
You must be signed in to change notification settings - Fork 72
Feature/jdbc #351
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
Merged
charlesbluca
merged 8 commits into
dask-contrib:main
from
systematicmethods:feature/jdbc
Jan 31, 2022
Merged
Feature/jdbc #351
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
063f5f8
Presto compatible JDBC driver
PeterLappo cf323f6
Presto compatible JDBC driver
PeterLappo 0c983d9
Update dask_sql/server/presto_jdbc.py
PeterLappo 0d6c7e6
Update dask_sql/server/app.py
PeterLappo 95b7561
Presto compatible JDBC driver
PeterLappo 8b572b7
Update dask_sql/server/presto_jdbc.py
PeterLappo 37785ed
Update dask_sql/server/app.py
PeterLappo 1dc4812
Merge branch 'dask-contrib:main' into feature/jdbc
PeterLappo 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 hidden or 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 hidden or 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,149 @@ | ||
| import logging | ||
|
|
||
| import pandas as pd | ||
|
|
||
| from dask_sql.context import Context | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def create_meta_data(c: Context): | ||
| """ | ||
| Creates the schema, table and column data for prestodb JDBC driver so that data can be viewed | ||
| in a database tool like DBeaver. It doesn't create a catalog entry although JDBC expects one | ||
| as dask-sql doesn't support catalogs. For both catalogs and procedures empty placeholder | ||
| tables are created. | ||
|
|
||
| The meta-data appears in a separate schema called system_jdbc largely because the JDBC driver | ||
| tries to access system.jdbc and it sufficiently so shouldn't clash with other schemas. | ||
|
|
||
| A function is required in the /v1/statement to change system.jdbc to system_jdbc and ignore | ||
| order by statements from the driver (as adjust_for_presto_sql above) | ||
|
|
||
| :param c: Context containing created tables | ||
| :return: | ||
| """ | ||
|
|
||
| if c is None: | ||
| logger.warn("Context None: jdbc meta data not created") | ||
| return | ||
| catalog = "" | ||
| system_schema = "system_jdbc" | ||
| c.create_schema(system_schema) | ||
|
|
||
| # TODO: add support for catalogs in presto interface | ||
| # see https://github.com/dask-contrib/dask-sql/pull/351 | ||
| # if catalog and len(catalog.strip()) > 0: | ||
| # catalogs = pd.DataFrame().append(create_catalog_row(catalog), ignore_index=True) | ||
| # c.create_table("catalogs", catalogs, schema_name=system_schema) | ||
|
|
||
| schemas = pd.DataFrame().append(create_schema_row(), ignore_index=True) | ||
| c.create_table("schemas", schemas, schema_name=system_schema) | ||
| schema_rows = [] | ||
|
|
||
| tables = pd.DataFrame().append(create_table_row(), ignore_index=True) | ||
| c.create_table("tables", tables, schema_name=system_schema) | ||
| table_rows = [] | ||
|
|
||
| columns = pd.DataFrame().append(create_column_row(), ignore_index=True) | ||
| c.create_table("columns", columns, schema_name=system_schema) | ||
| column_rows = [] | ||
|
|
||
| for schema_name, schema in c.schema.items(): | ||
| schema_rows.append(create_schema_row(catalog, schema_name)) | ||
| for table_name, dc in schema.tables.items(): | ||
| df = dc.df | ||
| logger.info(f"schema ${schema_name}, table {table_name}, {df}") | ||
| table_rows.append(create_table_row(catalog, schema_name, table_name)) | ||
| pos: int = 0 | ||
| for column in df.columns: | ||
| pos = pos + 1 | ||
| logger.debug(f"column {column}") | ||
| dtype = "VARCHAR" | ||
| if df[column].dtype == "int64" or df[column].dtype == "int": | ||
| dtype = "INTEGER" | ||
| elif df[column].dtype == "float64" or df[column].dtype == "float": | ||
| dtype = "FLOAT" | ||
| elif ( | ||
| df[column].dtype == "datetime" | ||
| or df[column].dtype == "datetime64[ns]" | ||
| ): | ||
| dtype = "TIMESTAMP" | ||
| column_rows.append( | ||
| create_column_row( | ||
| catalog, | ||
| schema_name, | ||
| table_name, | ||
| dtype, | ||
| df[column].name, | ||
| str(pos), | ||
| ) | ||
| ) | ||
|
|
||
| schemas = pd.DataFrame(schema_rows) | ||
| c.create_table("schemas", schemas, schema_name=system_schema) | ||
| tables = pd.DataFrame(table_rows) | ||
| c.create_table("tables", tables, schema_name=system_schema) | ||
| columns = pd.DataFrame(column_rows) | ||
| c.create_table("columns", columns, schema_name=system_schema) | ||
|
|
||
| logger.info(f"jdbc meta data ready for {len(table_rows)} tables") | ||
|
|
||
|
|
||
| def create_catalog_row(catalog: str = ""): | ||
| return {"TABLE_CAT": catalog} | ||
|
|
||
|
|
||
| def create_schema_row(catalog: str = "", schema: str = ""): | ||
| return {"TABLE_CATALOG": catalog, "TABLE_SCHEM": schema} | ||
|
|
||
|
|
||
| def create_table_row(catalog: str = "", schema: str = "", table: str = ""): | ||
| return { | ||
| "TABLE_CAT": catalog, | ||
| "TABLE_SCHEM": schema, | ||
| "TABLE_NAME": table, | ||
| "TABLE_TYPE": "", | ||
| "REMARKS": "", | ||
| "TYPE_CAT": "", | ||
| "TYPE_SCHEM": "", | ||
| "TYPE_NAME": "", | ||
| "SELF_REFERENCING_COL_NAME": "", | ||
| "REF_GENERATION": "", | ||
| } | ||
|
|
||
|
|
||
| def create_column_row( | ||
| catalog: str = "", | ||
| schema: str = "", | ||
| table: str = "", | ||
| dtype: str = "", | ||
| column: str = "", | ||
| pos: str = "", | ||
| ): | ||
| return { | ||
| "TABLE_CAT": catalog, | ||
| "TABLE_SCHEM": schema, | ||
| "TABLE_NAME": table, | ||
| "COLUMN_NAME": column, | ||
| "DATA_TYPE": dtype, | ||
| "TYPE_NAME": dtype, | ||
| "COLUMN_SIZE": "", | ||
| "BUFFER_LENGTH": "", | ||
| "DECIMAL_DIGITS": "", | ||
| "NUM_PREC_RADIX": "", | ||
| "NULLABLE": "", | ||
| "REMARKS": "", | ||
| "COLUMN_DEF": "", | ||
| "SQL_DATA_TYPE": dtype, | ||
| "SQL_DATETIME_SUB": "", | ||
| "CHAR_OCTET_LENGTH": "", | ||
| "ORDINAL_POSITION": pos, | ||
| "IS_NULLABLE": "", | ||
| "SCOPE_CATALOG": "", | ||
| "SCOPE_SCHEMA": "", | ||
| "SCOPE_TABLE": "", | ||
| "SOURCE_DATA_TYPE": "", | ||
| "IS_AUTOINCREMENT": "", | ||
| "IS_GENERATEDCOLUMN": "", | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.