OpenMetadata icon indicating copy to clipboard operation
OpenMetadata copied to clipboard

Issue with Snowflake Ingestion: Duplicate Constraint Names Across Different Tables

Open sovanpanda-rgb opened this issue 8 months ago • 2 comments

Affected module Does it impact the UI, backend or Ingestion Framework? Failure in Snowflake Metadata Ingestion Pipeline under Database Service

Describe the bug We have encountered an issue with Snowflake ingestion while ingesting tables. The problem arises due to the presence of the same constraint name across different tables under the same schema. During the ingestion process, Metaspace uses snowflake.sqlalchemy to ingest data, and it internally uses the following code:.

 def _get_schema_unique_constraints(self, connection, schema, **kw):
        result = connection.execute(
            text(
                f"SHOW /* sqlalchemy:_get_schema_unique_constraints */ UNIQUE KEYS IN SCHEMA {schema}"
            )
        )
        unique_constraints = {}
        for row in result:
            name = self.normalize_name(row._mapping["constraint_name"])
            if name not in unique_constraints:
                unique_constraints[name] = {
                    "column_names": [self.normalize_name(row._mapping["column_name"])],
                    "name": name,
                    "table_name": self.normalize_name(row._mapping["table_name"]),
                }
            else:
                unique_constraints[name]["column_names"].append(
                    self.normalize_name(row._mapping["column_name"])
                )

        ans = defaultdict(list)
        for constraint in unique_constraints.values():
            table_name = constraint.pop("table_name")
            ans[table_name].append(constraint)
        return ans

Trackback of errors -

Image

Failed to ingest CreateTableRequest [DT_ACTIVITY_LOG_DAILY_METRICS]: Invalid column name found in table constraint

To Reproduce As there is no check at the table level, tables with the same constraint name will cause issues. For below example, we can consider all tables present under single schema and database.

columnName tableName constraintName
a t1 c1
b t2 c1
c t3 c1

In this case, the resulting dictionary will be:

{"c1": {"columns": [a, b, c], "name": "c1", "tableName": "t1"}} Since the first table t1 is encountered first, the table name is set to t1. However, logically, columns b and c should not be part of the t1 table column constraint.

Expected Behaviour: Each constraint should be correctly associated with its respective table, ensuring that columns from different tables are not incorrectly grouped under a single constraint.

Screenshots or steps to reproduce: Unable to provide as per corporate compliance policy

Version:

  • OS: [e.g. iOS] - ubuntu:22.04
  • Python version: - 3.10.13
  • OpenMetadata version: [e.g. 0.8] - 1.8.1.0
  • OpenMetadata Ingestion package version: [e.g. openmetadata-ingestion[docker]==XYZ] - 1.8.1.0

Additional context Detail trackback error available here - OpenMetadata | CreateTableRequest [DT_ACTIVITY_LOG_DAILY_METRICS] | Failed to ingest CreateTableRequest [DT_ACTIVITY_LOG_DAILY_METRICS] due to api request failure: Invalid column name found in table constraint | Traceback (most recent call last): | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/ingestion/ometa/client.py", line 261, in _one_request | | | | | resp.raise_for_status() | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/requests/models.py", line 1026, in raise_for_status | | | | | raise HTTPError(http_error_msg, response=self) | | | | | requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://metaspace:8585/api/v1/tables | | | | | | | | | | The above exception was the direct cause of the following exception: | | | | | | | | | | Traceback (most recent call last): | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/ingestion/sink/metadata_rest.py", line 149, in _run | | | | | return self._run_dispatch(record) | | | | | File "/usr/local/lib/python3.10/functools.py", line 926, in _method | | | | | return method.get(obj, cls)(*args, **kwargs) | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/ingestion/sink/metadata_rest.py", line 140, in _run_dispatch | | | | | return self.write_create_request(record) | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/ingestion/sink/metadata_rest.py", line 175, in write_create_request | | | | | created = self.metadata.create_or_update(entity_request) | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/ingestion/ometa/ometa_api.py", line 305, in create_or_update | | | | | return self._create(data=data, method="put") | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/ingestion/ometa/ometa_api.py", line 292, in _create | | | | | resp = fn( | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/utils/execution_time_tracker.py", line 195, in inner | | | | | result = func(*args, **kwargs) | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/ingestion/ometa/client.py", line 349, in put | | | | | return self._request("PUT", path, data) | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/ingestion/ometa/client.py", line 230, in _request | | | | | return self._one_request(method, url, opts, retry) | | | | | File "/home/airflow/.local/lib/python3.10/site-packages/metadata/ingestion/ometa/client.py", line 287, in _one_request | | | | | raise APIError(error, http_error) from http_error | | | | | metadata.ingestion.ometa.client.APIError: Invalid column name found in table constraint | +--------------+-------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

sovanpanda-rgb avatar Mar 03 '26 12:03 sovanpanda-rgb

hi sir I’ve reviewed the issue and have an idea for the fix. I’d like to work on this. Please assign it to me.

jatinmasaram avatar Apr 04 '26 10:04 jatinmasaram

Resolved via PR #27266

aniruddhaadak80 avatar Apr 11 '26 04:04 aniruddhaadak80