Explorar o código

HUE-5406 [editor] Do not restart at zero when adding or removing a multi query

Romain Rigaux %!s(int64=8) %!d(string=hai) anos
pai
achega
37aa76d5af

+ 10 - 6
desktop/libs/notebook/src/notebook/connectors/hiveserver2.py

@@ -32,7 +32,7 @@ from desktop.conf import USE_DEFAULT_CONFIGURATION
 from desktop.lib.conf import BoundConfig
 from desktop.lib.exceptions import StructuredException
 from desktop.lib.exceptions_renderable import PopupException
-from desktop.lib.i18n import force_unicode
+from desktop.lib.i18n import force_unicode, smart_str
 from desktop.lib.rest.http_client import RestException
 from desktop.models import DefaultConfiguration
 from metadata.optimizer_client import OptimizerApi
@@ -626,7 +626,8 @@ DROP TABLE IF EXISTS `%(table)s`;
     statements_count = snippet['result']['handle'].get('statements_count', 1)
 
     statements = self._get_statements(snippet['statement'])
-    previous_statement_hash = hashlib.sha224(statements[statement_id]['statement']).hexdigest()
+    statement_id = min(statement_id, len(statements) - 1) # In case of removal of statements
+    previous_statement_hash = self.__compute_statement_hash(statements[statement_id]['statement'])
     non_edited_statement = previous_statement_hash == snippet['result']['handle'].get('previous_statement_hash') or not snippet['result']['handle'].get('previous_statement_hash')
 
     if snippet['result']['handle'].get('has_more_statements'):
@@ -642,21 +643,24 @@ DROP TABLE IF EXISTS `%(table)s`;
       if non_edited_statement:
         statement_id = 0
 
+    if statements_count != len(statements):
+      statement_id = min(statement_id, len(statements) - 1)
 
     resp = {
       'statement_id': statement_id,
       'has_more_statements': statement_id < len(statements) - 1,
       'statements_count': len(statements),
-      'previous_statement_hash': hashlib.sha224(statements[statement_id]['statement']).hexdigest()
+      'previous_statement_hash': self.__compute_statement_hash(statements[statement_id]['statement'])
     }
 
-    if statements_count != len(statements):
-      statement_id = 0
-
     resp.update(statements[statement_id])
     return resp
 
 
+  def __compute_statement_hash(self, statement):
+    return hashlib.sha224(smart_str(statement)).hexdigest()
+
+
   def _prepare_hql_query(self, snippet, statement, session):
     settings = snippet['properties'].get('settings', None)
     file_resources = snippet['properties'].get('files', None)

+ 2 - 2
desktop/libs/notebook/src/notebook/connectors/tests/tests_hiveserver2.py

@@ -456,12 +456,12 @@ class TestHiveserver2Api(object):
                 "settings": []
             }
         }
-      """ % {'statement': "SELECT * FROM sample_07;"}
+      """ % {'statement': u"SELECT 'Привет', '你好';"}
     )
 
     statement = self.api._get_current_statement(MockDb(), snippet)
 
-    assert_equal('7d283ad4794a3d2efd48a3ab44b1d9672625837c7dfc73f010ce82ab', statement['previous_statement_hash'])
+    assert_equal('086ecec9a8b89b1b47cce358bdbb343be23b1f8b54ca76bc81927e27', statement['previous_statement_hash'])
 
 
 def MockDb():