You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "arvindKandpal-ksolves (via GitHub)" <gi...@apache.org> on 2026/08/05 08:11:33 UTC

[PR] CASSANDRA-21548: cqlsh prompt does not reset to default after dropping the current keyspace [cassandra]

arvindKandpal-ksolves opened a new pull request, #4994:
URL: https://github.com/apache/cassandra/pull/4994

   ### Summary
   This PR fixes an issue where the `cqlsh` shell prompt remains stuck displaying a keyspace name even after that keyspace has been successfully dropped.
   
   ### Problem
   When a user is currently inside a keyspace in `cqlsh` (e.g., `cqlsh:test_ks>`) and executes `DROP KEYSPACE test_ks;`, the server deletes the keyspace, but the client-side `cqlsh` prompt does not reset to the default `cqlsh>` prompt. Executing subsequent queries inside the dropped keyspace context throws a `Keyspace does not exist` error.
   
   ### Solution
   - Updated `perform_simple_statement` in `pylib/cqlshlib/cqlshmain.py` to check if an executed statement is a `DROP KEYSPACE` statement.
   - If the dropped keyspace matches `self.current_keyspace`, `self.current_keyspace` is reset to `None`, which automatically updates the shell prompt back to `cqlsh>`.
   - Included safe string manipulation and `.lower()` normalization to handle edge cases like quoted keyspace names (`"Test_KS"`) and `IF EXISTS` clauses.
   
   ### Testing
   - Added automated unit tests in `pylib/cqlshlib/test/test_cqlsh_output.py` covering:
     1. Standard keyspace drop resetting the prompt.
     2. Dropping a non-active keyspace without altering the current prompt.
     3. Case-sensitive quoted keyspace names and `DROP KEYSPACE IF EXISTS` syntax.
   - Verified test execution using pytest (`python3 -m pytest pylib/cqlshlib/test/test_cqlsh_output.py -k "test_drop_keyspace_prompt_behavior"`).
   
   ---
   
   patch by Arvind Kandpal; reviewed by TBA for CASSANDRA-21548


-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] CASSANDRA-21548: cqlsh prompt does not reset to default after dropping the current keyspace [cassandra]

Posted by "driftx (via GitHub)" <gi...@apache.org>.
driftx commented on code in PR #4994:
URL: https://github.com/apache/cassandra/pull/4994#discussion_r3721414675


##########
pylib/cqlshlib/cqlshmain.py:
##########
@@ -911,6 +911,25 @@ def perform_simple_statement(self, statement):
         result = None
         try:
             result = future.result()
+            # If we just dropped the keyspace we are currently in, clear current_keyspace so the
+            # prompt falls back to the default 'cqlsh>' (CASSANDRA-21548). Only DROP statements are
+            # candidates, so cheaply skip parsing everything else. For a candidate we parse with the
+            # cqlsh grammar (handles whitespace, IF EXISTS and quoting), require it to actually be
+            # DROP KEYSPACE (DROP TABLE also binds ksname), and normalise the dropped name via
+            # cql_unprotect_name so it matches how do_use stores current_keyspace (quoted identifiers
+            # stay case-sensitive, unquoted ones fold to lower case).
+            qs = getattr(statement, 'query_string', str(statement))
+            if qs.lstrip().lower().startswith('drop'):
+                try:
+                    parsed = ruleset.cql_parse(qs)[1]
+                except IndexError:
+                    parsed = None
+                if (parsed is not None and len(parsed.matched) >= 2
+                        and parsed.matched[0][1].upper() == 'DROP'
+                        and parsed.matched[1][1].upper() == 'KEYSPACE'):

Review Comment:
   what about SCHEMA?



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] CASSANDRA-21548: cqlsh prompt does not reset to default after dropping the current keyspace [cassandra]

Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic commented on code in PR #4994:
URL: https://github.com/apache/cassandra/pull/4994#discussion_r3722690439


##########
pylib/cqlshlib/cqlshmain.py:
##########
@@ -911,6 +911,25 @@ def perform_simple_statement(self, statement):
         result = None
         try:
             result = future.result()
+            # If we just dropped the keyspace we are currently in, clear current_keyspace so the
+            # prompt falls back to the default 'cqlsh>' (CASSANDRA-21548). Only DROP statements are
+            # candidates, so cheaply skip parsing everything else. For a candidate we parse with the
+            # cqlsh grammar (handles whitespace, IF EXISTS and quoting), require it to actually be
+            # DROP KEYSPACE (DROP TABLE also binds ksname), and normalise the dropped name via
+            # cql_unprotect_name so it matches how do_use stores current_keyspace (quoted identifiers
+            # stay case-sensitive, unquoted ones fold to lower case).
+            qs = getattr(statement, 'query_string', str(statement))
+            if qs.lstrip().lower().startswith('drop'):
+                try:
+                    parsed = ruleset.cql_parse(qs)[1]
+                except IndexError:
+                    parsed = None
+                if (parsed is not None and len(parsed.matched) >= 2
+                        and parsed.matched[0][1].upper() == 'DROP'
+                        and parsed.matched[1][1].upper() == 'KEYSPACE'):

Review Comment:
   @arvindKandpal-ksolves I think just trunk is fine (6.0 + trunk)



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] CASSANDRA-21548: cqlsh prompt does not reset to default after dropping the current keyspace [cassandra]

Posted by "driftx (via GitHub)" <gi...@apache.org>.
driftx commented on code in PR #4994:
URL: https://github.com/apache/cassandra/pull/4994#discussion_r3722027247


##########
pylib/cqlshlib/cqlshmain.py:
##########
@@ -911,6 +911,25 @@ def perform_simple_statement(self, statement):
         result = None
         try:
             result = future.result()
+            # If we just dropped the keyspace we are currently in, clear current_keyspace so the
+            # prompt falls back to the default 'cqlsh>' (CASSANDRA-21548). Only DROP statements are
+            # candidates, so cheaply skip parsing everything else. For a candidate we parse with the
+            # cqlsh grammar (handles whitespace, IF EXISTS and quoting), require it to actually be
+            # DROP KEYSPACE (DROP TABLE also binds ksname), and normalise the dropped name via
+            # cql_unprotect_name so it matches how do_use stores current_keyspace (quoted identifiers
+            # stay case-sensitive, unquoted ones fold to lower case).
+            qs = getattr(statement, 'query_string', str(statement))
+            if qs.lstrip().lower().startswith('drop'):
+                try:
+                    parsed = ruleset.cql_parse(qs)[1]
+                except IndexError:
+                    parsed = None
+                if (parsed is not None and len(parsed.matched) >= 2
+                        and parsed.matched[0][1].upper() == 'DROP'
+                        and parsed.matched[1][1].upper() == 'KEYSPACE'):

Review Comment:
   Ah sorry, I didn't check trunk and it doesn't look like it needs it.
   
   > because of this SyntaxException on trunk, we cannot directly add a test case for DROP SCHEMA in the current test suite, as it will naturally fail. What we do here ?
   
   If we are only fixing this on trunk then we don't need to be concerned.  If we were fixing it on branches where DROP SCHEMA is present, then we'd handle it via multiple PRs.



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] CASSANDRA-21548: cqlsh prompt does not reset to default after dropping the current keyspace [cassandra]

Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic closed pull request #4994: CASSANDRA-21548: cqlsh prompt does not reset to default after dropping the current keyspace
URL: https://github.com/apache/cassandra/pull/4994


-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] CASSANDRA-21548: cqlsh prompt does not reset to default after dropping the current keyspace [cassandra]

Posted by "arvindKandpal-ksolves (via GitHub)" <gi...@apache.org>.
arvindKandpal-ksolves commented on code in PR #4994:
URL: https://github.com/apache/cassandra/pull/4994#discussion_r3721991111


##########
pylib/cqlshlib/cqlshmain.py:
##########
@@ -911,6 +911,25 @@ def perform_simple_statement(self, statement):
         result = None
         try:
             result = future.result()
+            # If we just dropped the keyspace we are currently in, clear current_keyspace so the
+            # prompt falls back to the default 'cqlsh>' (CASSANDRA-21548). Only DROP statements are
+            # candidates, so cheaply skip parsing everything else. For a candidate we parse with the
+            # cqlsh grammar (handles whitespace, IF EXISTS and quoting), require it to actually be
+            # DROP KEYSPACE (DROP TABLE also binds ksname), and normalise the dropped name via
+            # cql_unprotect_name so it matches how do_use stores current_keyspace (quoted identifiers
+            # stay case-sensitive, unquoted ones fold to lower case).
+            qs = getattr(statement, 'query_string', str(statement))
+            if qs.lstrip().lower().startswith('drop'):
+                try:
+                    parsed = ruleset.cql_parse(qs)[1]
+                except IndexError:
+                    parsed = None
+                if (parsed is not None and len(parsed.matched) >= 2
+                        and parsed.matched[0][1].upper() == 'DROP'
+                        and parsed.matched[1][1].upper() == 'KEYSPACE'):

Review Comment:
   Hi @driftx,
   
   I tried testing DROP SCHEMA locally on trunk, but it throws a SyntaxException:
   
   ```Plaintext
   cqlsh:test_schema> DROP SCHEMA test_schema;
   SyntaxException: line 1:5 no viable alternative at input 'SCHEMA' ([DROP] SCHEMA...)
   ```
   
   Since I couldn't find the server-side logic for DROP SCHEMA in the latest trunk, I just wanted to ask that should we add SCHEMA here primarily for backward compatibility with older clusters?
   
   Also, because of this SyntaxException on trunk, we cannot directly add a test case for DROP SCHEMA in the current test suite, as it will naturally fail. What we do here ? 
   
   Please let me know !



-- 
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: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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