john-bodley commented on code in PR #20473:
URL: https://github.com/apache/superset/pull/20473#discussion_r904365143


##########
superset/datasets/dao.py:
##########
@@ -163,14 +163,10 @@ def update(
         """
 
         if "columns" in properties:
-            properties["columns"] = cls.update_columns(
-                model, properties.get("columns", []), commit=commit
-            )
+            properties["columns"] = cls.update_columns(model, 
properties["columns"])

Review Comment:
   `get(...)` is not needed here per the check on line #165.



##########
superset/datasets/dao.py:
##########
@@ -190,28 +185,37 @@ def update_columns(
         - If there are extra columns on the metadata db that are not defined 
on the List
         then we delete.
         """
+
+        column_by_id = {column.id: column for column in model.columns}
         new_columns = []
+
         for column in property_columns:
             column_id = column.get("id")
 
             if column_id:
-                column_obj = db.session.query(TableColumn).get(column_id)

Review Comment:
   Leverage the columns defined by the model rather than re-querying for each 
column.



##########
superset/datasets/dao.py:
##########
@@ -190,28 +185,37 @@ def update_columns(
         - If there are extra columns on the metadata db that are not defined 
on the List
         then we delete.
         """
+
+        column_by_id = {column.id: column for column in model.columns}
         new_columns = []
+
         for column in property_columns:
             column_id = column.get("id")
 
             if column_id:
-                column_obj = db.session.query(TableColumn).get(column_id)
-                column_obj = DatasetDAO.update_column(column_obj, column, 
commit=commit)
+                column_obj = DatasetDAO.update_column(
+                    column_by_id.get(column_id),
+                    commit=False,

Review Comment:
   No need to commit when updating the column. It can all be committed when the 
dataset is updated.



##########
superset/datasets/dao.py:
##########
@@ -190,28 +185,37 @@ def update_columns(
         - If there are extra columns on the metadata db that are not defined 
on the List
         then we delete.
         """
+
+        column_by_id = {column.id: column for column in model.columns}
         new_columns = []
+
         for column in property_columns:
             column_id = column.get("id")
 
             if column_id:
-                column_obj = db.session.query(TableColumn).get(column_id)
-                column_obj = DatasetDAO.update_column(column_obj, column, 
commit=commit)
+                column_obj = DatasetDAO.update_column(
+                    column_by_id.get(column_id),
+                    commit=False,
+                )
             else:
-                column_obj = DatasetDAO.create_column(column, commit=commit)
+                column_obj = DatasetDAO.create_column(column, commit=False)

Review Comment:
   No need to commit when creating the column. Also the `TableColumn` object 
has no `table_id` at this time and thus the record needs to be updated when the 
entire dataset is committed. Also `autoflush=True` by default so the new column 
should have a primary key associated with it.



##########
superset/datasets/dao.py:
##########
@@ -190,28 +185,37 @@ def update_columns(
         - If there are extra columns on the metadata db that are not defined 
on the List
         then we delete.
         """
+
+        column_by_id = {column.id: column for column in model.columns}
         new_columns = []
+
         for column in property_columns:
             column_id = column.get("id")
 
             if column_id:
-                column_obj = db.session.query(TableColumn).get(column_id)
-                column_obj = DatasetDAO.update_column(column_obj, column, 
commit=commit)
+                column_obj = DatasetDAO.update_column(
+                    column_by_id.get(column_id),
+                    commit=False,
+                )
             else:
-                column_obj = DatasetDAO.create_column(column, commit=commit)
+                column_obj = DatasetDAO.create_column(column, commit=False)
+
             new_columns.append(column_obj)
+
         # Checks if an exiting column is missing from properties and delete it
+        column_ids = {column.id for column in new_columns}
+
         for existing_column in model.columns:
-            if existing_column.id not in [column.id for column in new_columns]:

Review Comment:
   Optimized looping.



##########
superset/datasets/dao.py:
##########
@@ -190,28 +185,37 @@ def update_columns(
         - If there are extra columns on the metadata db that are not defined 
on the List
         then we delete.
         """
+
+        column_by_id = {column.id: column for column in model.columns}
         new_columns = []
+
         for column in property_columns:
             column_id = column.get("id")
 
             if column_id:
-                column_obj = db.session.query(TableColumn).get(column_id)
-                column_obj = DatasetDAO.update_column(column_obj, column, 
commit=commit)
+                column_obj = DatasetDAO.update_column(
+                    column_by_id.get(column_id),

Review Comment:
   Same behavior as before, i.e., the input `column_obj` is `None` if for some 
reason it doesn't exist—which shouldn't happen.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to