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/07/03 08:32:46 UTC

Re: [PR] CASSANDRA-19985: Enhance CQLSH to support machine-readable output formatting [cassandra]

arvindKandpal-ksolves commented on code in PR #4750:
URL: https://github.com/apache/cassandra/pull/4750#discussion_r3518552101


##########
pylib/cqlshlib/displaying.py:
##########
@@ -126,3 +126,86 @@ def color_ljust(self, width, fill=' '):
                                  )
 
 NO_COLOR_MAP = dict()
+
+class TablePrinter:
+    def print_header(self, formatted_names):
+        raise NotImplementedError
+
+    def print_rows(self, formatted_names, formatted_values):
+        raise NotImplementedError
+
+    def finish(self):
+        pass
+
+class TabularTablePrinter(TablePrinter):
+    def __init__(self, shell, tty, row_count_offset=0):
+        self._shell = shell
+        self._tty = tty
+        self._row_count_offset = row_count_offset
+        self._with_header = True
+
+    def print_header(self, formatted_names):
+        pass
+
+    def print_rows(self, formatted_names, formatted_values, with_header=None):
+        if with_header is None:
+            with_header = self._with_header
+        if self._shell.expand_enabled:
+            self._shell.print_formatted_result_vertically(
+                formatted_names, formatted_values, self._row_count_offset)
+        else:
+            self._shell.print_formatted_result(
+                formatted_names, formatted_values, with_header, self._tty)
+        if formatted_values:
+            self._row_count_offset += len(formatted_values)
+        self._with_header = self._tty
+
+    def finish(self):
+        pass
+
+class CsvTablePrinter(TablePrinter):
+    def __init__(self, shell):
+        import csv
+        self._writer = csv.writer(shell.query_out)
+        self._header_written = False
+
+    def print_header(self, formatted_names):
+        self._colnames = [n.strval for n in formatted_names]
+
+    def print_rows(self, formatted_names, formatted_values):
+        if not self._header_written:
+            self._writer.writerow(self._colnames)
+            self._header_written = True
+        if formatted_values is None:
+            return
+        for row in formatted_values:
+            self._writer.writerow([col.strval for col in row])
+
+    def finish(self):
+        pass
+

Review Comment:
   I tested this Gemini-suggested simplified approach, but it unfortunately breaks when Cassandra's paging is involved. 
   
   Because `print_rows` is called multiple times as Cassandra fetches data in chunks (pages), using `json.dump(data, ...)` in `print_rows` causes it to output multiple separate JSON arrays back-to-back. This results in an invalid JSON stream. 
   
   For example, testing this with `PAGING 2` yields the following invalid output:
   ```json
   Connected to Test Cluster at 127.0.0.1:9042
   [cqlsh 6.3.0 | Cassandra 7.0-SNAPSHOT | CQL spec 3.4.8 | Native protocol v5]
   Use HELP for help.
   cqlsh> PAGING 2
   Page size: 2
   cqlsh> SELECT keyspace_name FROM system_schema.keyspaces;
   [
       {
           "keyspace_name": "system_cluster_metadata"
       },
       {
           "keyspace_name": "test"
       }
   ]
   [
       {
           "keyspace_name": "system_auth"
       },
       {
           "keyspace_name": "system_schema"
       }
   ]
   [
       {
           "keyspace_name": "test_bugs"
       },
       {
           "keyspace_name": "system_distributed"
       }
   ]
   [
       {
           "keyspace_name": "system"
       },
       {
           "keyspace_name": "system_traces"
       }
   ]
   cqlsh> 
   ```
   My current streaming approach—printing the opening `[` in `print_header`, handling commas row-by-row, and closing with `]` in `finish()`—ensures we output a single, perfectly valid JSON array regardless of how many pages are fetched, while keeping memory usage strictly low for large `SELECT *` queries.



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