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

 ##########
 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:
   @feluelle  Yes, you are right, not functional change. But using `with` 
statement code style are different from others that what I confuse. And what 
you said
   > On the other side you can argue that if you do it like I did it that you 
are already pulling data from the source without knowing whether the 
destination connections is working or not.
   is other point.

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