You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Lee-W (via GitHub)" <gi...@apache.org> on 2024/07/29 13:23:50 UTC

[PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Lee-W opened a new pull request, #41097:
URL: https://github.com/apache/airflow/pull/41097

   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you 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.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #41097:
URL: https://github.com/apache/airflow/pull/41097#discussion_r1696536017


##########
airflow/models/dag.py:
##########
@@ -3318,12 +3317,12 @@ def bulk_write_to_db(
             curr_outlet_references = curr_orm_dag and curr_orm_dag.task_outlet_dataset_references
             for task in dag.tasks:
                 dataset_outlets: list[Dataset] = []
-                dataset_alias_outlets: list[DatasetAlias] = []
+                dataset_alias_outlets: set[DatasetAlias] = set()
                 for outlet in task.outlets:
                     if isinstance(outlet, Dataset):
                         dataset_outlets.append(outlet)
                     elif isinstance(outlet, DatasetAlias):
-                        dataset_alias_outlets.append(outlet)
+                        dataset_alias_outlets.add(outlet)

Review Comment:
   I feel it might not be required for dataset alias (or maybe dataset even?)
   
   I would like to know whether @dstandish has some thoughts on this. Thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "ephraimbuddy (via GitHub)" <gi...@apache.org>.
ephraimbuddy commented on code in PR #41097:
URL: https://github.com/apache/airflow/pull/41097#discussion_r1695359509


##########
airflow/migrations/versions/0151_2_10_0_dataset_alias_unique_constraint.py:
##########
@@ -0,0 +1,49 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+"""
+Add unique constraint to dataset_alias table.
+
+Revision ID: d57c3e740878
+Revises: 8684e37832e6
+Create Date: 2024-07-29 12:59:07.793836
+
+"""
+
+from __future__ import annotations
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision = "d57c3e740878"
+down_revision = "8684e37832e6"
+branch_labels = None
+depends_on = None
+airflow_version = "2.10.0"
+
+
+def upgrade():
+    """Add unique constraint to dataset_alias table."""
+    with op.batch_alter_table("dataset_alias", schema=None) as batch_op:
+        batch_op.create_index("idx_name_unique", ["name"], unique=True)

Review Comment:
   Since we haven't released the previous migration, can we update this in that migration instead of a new migration file



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #41097:
URL: https://github.com/apache/airflow/pull/41097#discussion_r1696534564


##########
airflow/models/dag.py:
##########
@@ -3318,12 +3317,12 @@ def bulk_write_to_db(
             curr_outlet_references = curr_orm_dag and curr_orm_dag.task_outlet_dataset_references
             for task in dag.tasks:
                 dataset_outlets: list[Dataset] = []
-                dataset_alias_outlets: list[DatasetAlias] = []
+                dataset_alias_outlets: set[DatasetAlias] = set()
                 for outlet in task.outlets:
                     if isinstance(outlet, Dataset):
                         dataset_outlets.append(outlet)
                     elif isinstance(outlet, DatasetAlias):
-                        dataset_alias_outlets.append(outlet)
+                        dataset_alias_outlets.add(outlet)

Review Comment:
   According to https://github.com/apache/airflow/blob/a7b0dc25c0201e3e69726aa6033102128d8d9046/airflow/models/dag.py#L3300, it seems we want to preserve the order.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #41097:
URL: https://github.com/apache/airflow/pull/41097#discussion_r1696530626


##########
airflow/models/dataset.py:
##########
@@ -124,6 +128,15 @@ def from_public(cls, obj: DatasetAlias) -> DatasetAliasModel:
     def __repr__(self):
         return f"{self.__class__.__name__}(name={self.name!r})"
 
+    def __hash__(self):
+        return hash(self.name)
+
+    def __eq__(self, other):
+        if isinstance(other, (self.__class__, DatasetAlias)):
+            return self.name == other.name
+        else:
+            return NotImplemented

Review Comment:
   Not sure whether it's ideal, but it's at least ok I think. DatasetModel is doing the same thing



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W merged PR #41097:
URL: https://github.com/apache/airflow/pull/41097


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #41097:
URL: https://github.com/apache/airflow/pull/41097#discussion_r1695383138


##########
airflow/migrations/versions/0151_2_10_0_dataset_alias_unique_constraint.py:
##########
@@ -0,0 +1,49 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+"""
+Add unique constraint to dataset_alias table.
+
+Revision ID: d57c3e740878
+Revises: 8684e37832e6
+Create Date: 2024-07-29 12:59:07.793836
+
+"""
+
+from __future__ import annotations
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision = "d57c3e740878"
+down_revision = "8684e37832e6"
+branch_labels = None
+depends_on = None
+airflow_version = "2.10.0"
+
+
+def upgrade():
+    """Add unique constraint to dataset_alias table."""
+    with op.batch_alter_table("dataset_alias", schema=None) as batch_op:
+        batch_op.create_index("idx_name_unique", ["name"], unique=True)

Review Comment:
   Thanks! Didn't know we should do that. Just updated it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #41097:
URL: https://github.com/apache/airflow/pull/41097#discussion_r1696567757


##########
airflow/models/dag.py:
##########
@@ -3318,12 +3317,12 @@ def bulk_write_to_db(
             curr_outlet_references = curr_orm_dag and curr_orm_dag.task_outlet_dataset_references
             for task in dag.tasks:
                 dataset_outlets: list[Dataset] = []
-                dataset_alias_outlets: list[DatasetAlias] = []
+                dataset_alias_outlets: set[DatasetAlias] = set()
                 for outlet in task.outlets:
                     if isinstance(outlet, Dataset):
                         dataset_outlets.append(outlet)
                     elif isinstance(outlet, DatasetAlias):
-                        dataset_alias_outlets.append(outlet)
+                        dataset_alias_outlets.add(outlet)

Review Comment:
   As this comment is not critical and the PR fixes a serious issue, I will merge it after CI passes, but we can continue the discussion here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #41097:
URL: https://github.com/apache/airflow/pull/41097#discussion_r1696527424


##########
airflow/models/dataset.py:
##########
@@ -124,6 +128,15 @@ def from_public(cls, obj: DatasetAlias) -> DatasetAliasModel:
     def __repr__(self):
         return f"{self.__class__.__name__}(name={self.name!r})"
 
+    def __hash__(self):
+        return hash(self.name)
+
+    def __eq__(self, other):
+        if isinstance(other, (self.__class__, DatasetAlias)):
+            return self.name == other.name
+        else:
+            return NotImplemented

Review Comment:
   Not sure what I feel about implementing these on a SQLAlchemy class. Probably doable? We should find some resource on if these are OK.
   
   Or we could instead just use a dict instead (using the name as key).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #41097:
URL: https://github.com/apache/airflow/pull/41097#discussion_r1696572240


##########
airflow/models/dataset.py:
##########
@@ -124,6 +128,15 @@ def from_public(cls, obj: DatasetAlias) -> DatasetAliasModel:
     def __repr__(self):
         return f"{self.__class__.__name__}(name={self.name!r})"
 
+    def __hash__(self):
+        return hash(self.name)
+
+    def __eq__(self, other):
+        if isinstance(other, (self.__class__, DatasetAlias)):
+            return self.name == other.name
+        else:
+            return NotImplemented

Review Comment:
   I tried to trace the SQLAlchemy code a bit. Didn't find special handling for `__eq__`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Add dataset alias unique constraint and remove wrong dataset alias removing logic [airflow]

Posted by "uranusjr (via GitHub)" <gi...@apache.org>.
uranusjr commented on code in PR #41097:
URL: https://github.com/apache/airflow/pull/41097#discussion_r1696525440


##########
airflow/models/dag.py:
##########
@@ -3318,12 +3317,12 @@ def bulk_write_to_db(
             curr_outlet_references = curr_orm_dag and curr_orm_dag.task_outlet_dataset_references
             for task in dag.tasks:
                 dataset_outlets: list[Dataset] = []
-                dataset_alias_outlets: list[DatasetAlias] = []
+                dataset_alias_outlets: set[DatasetAlias] = set()
                 for outlet in task.outlets:
                     if isinstance(outlet, Dataset):
                         dataset_outlets.append(outlet)
                     elif isinstance(outlet, DatasetAlias):
-                        dataset_alias_outlets.append(outlet)
+                        dataset_alias_outlets.add(outlet)

Review Comment:
   Makes me wonder why the datasets are in a list instead. Maybe out of scope for this PR though.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org