feluelle commented on a change in pull request #4405: [AIRFLOW-3598] Add tests 
for MsSqlToHiveTransfer
URL: https://github.com/apache/airflow/pull/4405#discussion_r256736722
 
 

 ##########
 File path: airflow/operators/mssql_to_hive.py
 ##########
 @@ -98,38 +99,34 @@ def __init__(
 
     @classmethod
     def type_map(cls, mssql_type):
-        t = pymssql
-        d = {
-            t.BINARY.value: 'INT',
-            t.DECIMAL.value: 'FLOAT',
-            t.NUMBER.value: 'INT',
+        map_dict = {
+            pymssql.BINARY.value: 'INT',
+            pymssql.DECIMAL.value: 'FLOAT',
+            pymssql.NUMBER.value: 'INT',
         }
-        return d[mssql_type] if mssql_type in d else 'STRING'
+        return map_dict[mssql_type] if mssql_type in map_dict else 'STRING'
 
     def execute(self, context):
-        hive = HiveCliHook(hive_cli_conn_id=self.hive_cli_conn_id)
         mssql = MsSqlHook(mssql_conn_id=self.mssql_conn_id)
-
         self.log.info("Dumping Microsoft SQL Server query results to local 
file")
-        conn = mssql.get_conn()
-        cursor = conn.cursor()
-        cursor.execute(self.sql)
-        with NamedTemporaryFile("w") as f:
-            csv_writer = csv.writer(f, delimiter=self.delimiter, 
encoding='utf-8')
-            field_dict = OrderedDict()
-            col_count = 0
-            for field in cursor.description:
-                col_count += 1
-                col_position = "Column{position}".format(position=col_count)
-                field_dict[col_position if field[0] == '' else field[0]] \
-                    = self.type_map(field[1])
-            csv_writer.writerows(cursor)
-            f.flush()
-            cursor.close()
-            conn.close()
+        with closing(mssql.get_conn()) as conn:
+            with closing(conn.cursor()) as cursor:
+                cursor.execute(self.sql)
+                with NamedTemporaryFile("w") as tmp_file:
+                    csv_writer = csv.writer(tmp_file, 
delimiter=self.delimiter, encoding='utf-8')
+                    field_dict = OrderedDict()
+                    col_count = 0
+                    for field in cursor.description:
+                        col_count += 1
+                        col_position = 
"Column{position}".format(position=col_count)
+                        field_dict[col_position if field[0] == '' else 
field[0]] = self.type_map(field[1])
+                    csv_writer.writerows(cursor)
+                    tmp_file.flush()
+
+            hive = HiveCliHook(hive_cli_conn_id=self.hive_cli_conn_id)
 
 Review comment:
   That's true. It is a different code style. But that is called _Refactoring_ 
:)
   
   The code will change from time to time as the language improves and our code 
understanding increases.
   
   I don't think these kind of changes need to be made all at once for all 
operators.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to