potiuk commented on code in PR #27912:
URL: https://github.com/apache/airflow/pull/27912#discussion_r1032394307


##########
airflow/providers/common/sql/hooks/sql.py:
##########
@@ -246,40 +270,48 @@ def run(
         :param return_last: Whether to return result for only last statement 
or for all after split
         :return: return only result of the ALL SQL expressions if handler was 
provided.
         """
-        self.scalar_return_last = isinstance(sql, str) and return_last
+        self.descriptions = []
+
         if isinstance(sql, str):
             if split_statements:
-                sql = self.split_sql_string(sql)
+                sql_list: Iterable[str] = self.split_sql_string(sql)
             else:
-                sql = [sql]
+                sql_list = [sql] if sql.strip() else []
+        else:
+            sql_list = sql
 
-        if sql:
-            self.log.debug("Executing following statements against DB: %s", 
list(sql))
+        if sql_list:
+            self.log.debug("Executing following statements against DB: %s", 
sql_list)
         else:
             raise ValueError("List of SQL statements is empty")
-
+        _last_result = None
         with closing(self.get_conn()) as conn:
             if self.supports_autocommit:
                 self.set_autocommit(conn, autocommit)
 
             with closing(conn.cursor()) as cur:
                 results = []
-                for sql_statement in sql:
+                for sql_statement in sql_list:
                     self._run_command(cur, sql_statement, parameters)
 
                     if handler is not None:
                         result = handler(cur)
-                        results.append(result)
-                self.last_description = cur.description
+                        if has_scalar_return_value(sql, return_last, 
split_statements):
+                            _last_result = result
+                            _last_description = cur.description
+                        else:
+                            results.append(result)
+                            self.descriptions.append(cur.description)
 
             # If autocommit was set to False or db does not support 
autocommit, we do a manual commit.
             if not self.get_autocommit(conn):
                 conn.commit()
 
         if handler is None:
             return None
-        elif self.scalar_return_last:
-            return results[-1]
+        if has_scalar_return_value(sql, return_last, split_statements):
+            self.descriptions = [_last_description]

Review Comment:
   The `self.descriptions` is always an array - no matter if scalar return is 
used. 
   
   The assumption here is that if scalar is expected - you can use 
"last_decription" property. In all other cases you can use descriptions array . 



-- 
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]

Reply via email to