You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by mc...@apache.org on 2020/08/20 11:59:36 UTC

[cassandra] branch trunk updated: Strip comment blocks from cqlsh input before processing statements

This is an automated email from the ASF dual-hosted git repository.

mck pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/cassandra.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 1b1b87c  Strip comment blocks from cqlsh input before processing statements
1b1b87c is described below

commit 1b1b87cfe3a9a93c393d1f3c1e003394260edeb5
Author: Rens Groothuijsen <l....@alumni.maastrichtuniversity.nl>
AuthorDate: Wed May 20 20:31:14 2020 +0200

    Strip comment blocks from cqlsh input before processing statements
    
     patch by Rens Groothuijsen; reviewed by Mick Semb Wever for CASSANDRA-15802
---
 CHANGES.txt                             |  1 +
 bin/cqlsh.py                            | 19 ++++++++++
 pylib/cqlshlib/test/test_cql_parsing.py | 65 +++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)

diff --git a/CHANGES.txt b/CHANGES.txt
index 3e23053..3272226 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 4.0-beta2
+ * Strip comment blocks from cqlsh input before processing statements (CASSANDRA-15802)
  * Fix unicode chars error input (CASSANDRA-15990)
  * Improved testability for CacheMetrics and ChunkCacheMetrics (CASSANDRA-15788)
  * Handle errors in StreamSession#prepare (CASSANDRA-15852)
diff --git a/bin/cqlsh.py b/bin/cqlsh.py
index 1ef0c91..3f60094 100644
--- a/bin/cqlsh.py
+++ b/bin/cqlsh.py
@@ -39,6 +39,7 @@ import getpass
 import optparse
 import os
 import platform
+import re
 import sys
 import traceback
 import warnings
@@ -910,12 +911,30 @@ class Shell(cmd.Cmd):
                     self.reset_statement()
                     print('')
 
+    def strip_comment_blocks(self, statementtext):
+        comment_block_in_literal_string = re.search('["].*[/][*].*[*][/].*["]', statementtext)
+        if not comment_block_in_literal_string:
+            result = re.sub('[/][*].*[*][/]', "", statementtext)
+            if '*/' in result and '/*' not in result and not self.in_comment:
+                raise SyntaxError("Encountered comment block terminator without being in comment block")
+            if '/*' in result:
+                result = re.sub('[/][*].*', "", result)
+                self.in_comment = True
+            if '*/' in result:
+                result = re.sub('.*[*][/]', "", result)
+                self.in_comment = False
+            if self.in_comment and not re.findall('[/][*]|[*][/]', statementtext):
+                result = ''
+            return result
+        return statementtext
+
     def onecmd(self, statementtext):
         """
         Returns true if the statement is complete and was handled (meaning it
         can be reset).
         """
         statementtext = ensure_text(statementtext)
+        statementtext = self.strip_comment_blocks(statementtext)
         try:
             statements, endtoken_escaped = cqlruleset.cql_split_statements(statementtext)
         except pylexotron.LexingError as e:
diff --git a/pylib/cqlshlib/test/test_cql_parsing.py b/pylib/cqlshlib/test/test_cql_parsing.py
index 10be99f..8631d7a 100644
--- a/pylib/cqlshlib/test/test_cql_parsing.py
+++ b/pylib/cqlshlib/test/test_cql_parsing.py
@@ -711,6 +711,71 @@ class TestCqlParsing(TestCase):
     def test_parse_select_token(self):
         pass
 
+    def test_strip_comment_blocks_from_input(self):
+
+        parsed = parse_cqlsh_statements('SELECT FROM /* comment block */ "MyTable";')
+        self.assertSequenceEqual(tokens_with_types(parsed),
+                                 [('SELECT', 'reserved_identifier'),
+                                  ('FROM', 'reserved_identifier'),
+                                  ('"MyTable"', 'quotedName'),
+                                  (';', 'endtoken')])
+
+        parsed = parse_cqlsh_statements('SELECT FROM /* \n comment block starts here; \n and continues here \n */ "MyTable";')
+        self.assertSequenceEqual(tokens_with_types(parsed),
+                                 [('SELECT', 'reserved_identifier'),
+                                  ('FROM', 'reserved_identifier'),
+                                  ('"MyTable"', 'quotedName'),
+                                  (';', 'endtoken')])
+
+        parsed = parse_cqlsh_statements('''
+                                        SELECT FROM /*
+                                         comment block starts here;
+                                         and continues here
+                                         */ "MyTable";
+                                        ''')
+        self.assertSequenceEqual(tokens_with_types(parsed),
+                                 [('SELECT', 'reserved_identifier'),
+                                  ('FROM', 'reserved_identifier'),
+                                  ('"MyTable"', 'quotedName'),
+                                  (';', 'endtoken')])
+
+        parsed = parse_cqlsh_statements('''
+                                        /* comment block */
+                                        SELECT FROM "MyTable";
+                                        ''')
+        self.assertSequenceEqual(tokens_with_types(parsed),
+                                 [('SELECT', 'reserved_identifier'),
+                                  ('FROM', 'reserved_identifier'),
+                                  ('"MyTable"', 'quotedName'),
+                                  (';', 'endtoken')])
+
+        parsed = parse_cqlsh_statements('''
+                                        /* comment block */
+                                        /* another comment */ SELECT FROM /*
+                                         comment block starts here;
+                                         and continues here
+                                         */ "MyTable";
+                                        ''')
+        self.assertSequenceEqual(tokens_with_types(parsed),
+                                 [('SELECT', 'reserved_identifier'),
+                                  ('FROM', 'reserved_identifier'),
+                                  ('"MyTable"', 'quotedName'),
+                                  (';', 'endtoken')])
+
+        parsed = parse_cqlsh_statements('''
+                                        SELECT FROM "/*MyTable*/";
+                                        ''')
+        self.assertSequenceEqual(tokens_with_types(parsed),
+                                 [('SELECT', 'reserved_identifier'),
+                                  ('FROM', 'reserved_identifier'),
+                                  ('"/*MyTable*/"', 'quotedName'),
+                                  (';', 'endtoken')])
+
+        parse_cqlsh_statements('''
+                               */ SELECT FROM "MyTable";
+                               ''')
+        self.assertRaises(SyntaxError)
+
 
 def parse_cqlsh_statements(text):
     '''


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org