-
Notifications
You must be signed in to change notification settings - Fork 82
fix(ibis): added primary key management on postgres and updated mssql and mysql to manage composite keys #1106
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
Open
yalexbadan
wants to merge
16
commits into
Canner:main
Choose a base branch
from
yalexbadan:issues/1105
base: main
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
16 commits
Select commit
Hold shift + click to select a range
e159f5a
fix(ibis): added primary key management on postgres and updated mssql…
yalexbadan 0b5eb0a
fix(ibis): runned just for code formatting
yalexbadan bfcd4ed
fix(ibis): fixed tests
yalexbadan 8076da8
fix(ibis): fmt code
goldmedal 7bcce3b
fix(ibis): fixed clickhouse and trino metadata classes
yalexbadan dbff7d8
fix(ibis): fixed test models
yalexbadan 592ed28
fix(ibis): updated java servers with new models
yalexbadan 3238116
fix(ibis): reverted to initial proposal
yalexbadan 2337028
fix(ibis): chagend composed primary key logic
yalexbadan feb19ba
Merge branch 'main' into issues/1105
yalexbadan 10fc6f6
fix(ibis): minor fix on postgres foreign key extraction query
yalexbadan a119b55
fix(ibis): fixed rever issues
yalexbadan 43cef18
fix(ibis): fixed revert issues
yalexbadan 7c8ed81
fix(ibis): fixed revert regressions
yalexbadan 721d978
fix(ibis): fixed revert regressions
yalexbadan e6e7c02
fix(ibis): fixed other revert regressions
yalexbadan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,5 @@ __pycache__/ | |
venv/ | ||
**/.env* | ||
**/*.so | ||
.devcontainer | ||
.vscode |
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 |
---|---|---|
|
@@ -26,6 +26,7 @@ def get_table_list(self) -> list[Table]: | |
c.data_type, | ||
c.is_nullable, | ||
c.ordinal_position, | ||
tc.constraint_type, | ||
obj_description(cls.oid) AS table_comment, | ||
col_description(cls.oid, a.attnum) AS column_comment | ||
FROM | ||
|
@@ -44,13 +45,22 @@ def get_table_list(self) -> list[Table]: | |
pg_attribute a | ||
ON a.attrelid = cls.oid | ||
AND a.attname = c.column_name | ||
LEFT JOIN information_schema.key_column_usage kcu | ||
ON c.table_name = kcu.table_name | ||
AND c.column_name = kcu.column_name | ||
AND c.table_schema = kcu.table_schema | ||
LEFT JOIN information_schema.table_constraints tc | ||
ON kcu.constraint_name = tc.constraint_name | ||
AND kcu.table_schema = tc.table_schema | ||
WHERE | ||
t.table_type IN ('BASE TABLE', 'VIEW') | ||
AND t.table_schema NOT IN ('information_schema', 'pg_catalog'); | ||
AND t.table_schema NOT IN ('information_schema', 'pg_catalog') | ||
AND (tc.constraint_type = 'PRIMARY KEY' OR tc.constraint_type IS NULL); | ||
""" | ||
response = self.connection.sql(sql).to_pandas().to_dict(orient="records") | ||
|
||
unique_tables = {} | ||
unique_tables_primary_key = {} | ||
for row in response: | ||
# generate unique table name | ||
schema_table = self._format_postgres_compact_table_name( | ||
|
@@ -71,15 +81,38 @@ def get_table_list(self) -> list[Table]: | |
) | ||
|
||
# table exists, and add column to the table | ||
unique_tables[schema_table].columns.append( | ||
Column( | ||
name=row["column_name"], | ||
type=self._transform_postgres_column_type(row["data_type"]), | ||
notNull=row["is_nullable"].lower() == "no", | ||
description=row["column_comment"], | ||
column = Column( | ||
name=row["column_name"], | ||
type=self._transform_postgres_column_type(row["data_type"]), | ||
notNull=row["is_nullable"].lower() == "no", | ||
description=row["column_comment"], | ||
properties=None, | ||
) | ||
unique_tables[schema_table].columns.append(column) | ||
|
||
if row["constraint_type"] == "PRIMARY KEY": | ||
if schema_table in unique_tables_primary_key: | ||
unique_tables_primary_key[schema_table].append(column) | ||
else: | ||
unique_tables_primary_key[schema_table] = [column] | ||
|
||
for schema_table, columns in unique_tables_primary_key.items(): | ||
if len(columns) == 0: | ||
continue | ||
elif len(columns) == 1: | ||
unique_tables[schema_table].primaryKey = columns[0].name | ||
else: | ||
composite_primary_key_column = Column( | ||
name="composed_primary_key", | ||
type="json", | ||
notNull=True, | ||
description=f"Composed primary key based on fields: {[col.name for col in columns]}", | ||
properties=None, | ||
nestedColumns=columns, | ||
) | ||
Comment on lines
+104
to
112
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. Is it possible to have multiple primary keys in the same table? IMO, the postgres only allows the table has only one primary key. Given a table
|
||
) | ||
unique_tables[schema_table].columns.append(composite_primary_key_column) | ||
unique_tables[schema_table].primaryKey = "composed_primary_key" | ||
|
||
return list(unique_tables.values()) | ||
|
||
def get_constraints(self) -> list[Constraint]: | ||
|
@@ -97,6 +130,7 @@ def get_constraints(self) -> list[Constraint]: | |
AND tc.table_schema = kcu.table_schema | ||
JOIN information_schema.constraint_column_usage AS ccu | ||
ON ccu.constraint_name = tc.constraint_name | ||
AND ccu.table_schema = tc.table_schema | ||
WHERE tc.constraint_type = 'FOREIGN KEY' | ||
""" | ||
res = self.connection.sql(sql).to_pandas().to_dict(orient="records") | ||
|
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
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.
What's the comment propose? Could you explain more?