You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by "Brad Schoening (Jira)" <ji...@apache.org> on 2026/04/18 23:52:00 UTC

[jira] [Comment Edited] (CASSANDRA-19985) Enhance CQLSH to support machine-readable output formatting

    [ https://issues.apache.org/jira/browse/CASSANDRA-19985?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18074604#comment-18074604 ] 

Brad Schoening edited comment on CASSANDRA-19985 at 4/18/26 11:51 PM:
----------------------------------------------------------------------

[~arvindk12] let's consider a more OO approach here and move the display logic out of the main shell class into displaying.py.

Something like:
{code:python}
import csv
import json
import sys

class ResultPrinter:
    def __init__(self, output=sys.stdout, encoding='utf-8'):
        self.output = output
        self.encoding = encoding

    def print_results(self, colnames, rows):
        """Must be implemented by subclasses"""
        raise NotImplementedError

class GridPrinter(ResultPrinter):
    def print_results(self, colnames, rows):
        # 1. Calculate max widths for each column
        # 2. Draw header with pipes: | col1 | col2 |
        # 3. Draw separators: +------+------+
        # 4. Print rows
        pass

class CSVPrinter(ResultPrinter):
    def print_results(self, colnames, rows):
        writer = csv.DictWriter(self.output, fieldnames=colnames)
        writer.writeheader()
        for row in rows:
            # Map column names to row values
            writer.writerow(dict(zip(colnames, row)))

class JSONPrinter(ResultPrinter):
    def print_results(self, colnames, rows):
        data = [dict(zip(colnames, row)) for row in rows]
        json.dump(data, self.output, indent=4, default=str)
        self.output.write('\n')
{code}

Then, in cqlshmain we could simply have:


{code:java}
# Inside cqlmain.py
def get_printer(format_type, output_stream):
    printers = {
        'grid': GridPrinter,
        'csv':  CSVPrinter,
        'json': JSONPrinter
    }
    return printers.get(format_type, GridPrinter)(output_stream)

# Usage in Shell.print_result:
printer = get_printer(self.output_format, self.query_out)
printer.print_results(colnames, formatted_rows)
{code}

Note: the above code is non-working pseudo-code




was (Author: bschoeni):
[~arvindk12] let's consider a more OO approach here and move the display logic out of the main shell class into displaying.py.

Something like:
{code:python}
import csv
import json
import sys

class ResultPrinter:
    def __init__(self, output=sys.stdout, encoding='utf-8'):
        self.output = output
        self.encoding = encoding

    def print_results(self, colnames, rows):
        """Must be implemented by subclasses"""
        raise NotImplementedError

class GridPrinter(ResultPrinter):
    def print_results(self, colnames, rows):
        # 1. Calculate max widths for each column
        # 2. Draw header with pipes: | col1 | col2 |
        # 3. Draw separators: +------+------+
        # 4. Print rows
        pass

class CSVPrinter(ResultPrinter):
    def print_results(self, colnames, rows):
        writer = csv.DictWriter(self.output, fieldnames=colnames)
        writer.writeheader()
        for row in rows:
            # Map column names to row values
            writer.writerow(dict(zip(colnames, row)))

class JSONPrinter(ResultPrinter):
    def print_results(self, colnames, rows):
        data = [dict(zip(colnames, row)) for row in rows]
        json.dump(data, self.output, indent=4, default=str)
        self.output.write('\n')
{code}

Then, in cqlshmain we could simply have:


{code:java}
# Inside cqlmain.py
def get_printer(format_type, output_stream):
    printers = {
        'grid': GridPrinter,
        'csv':  CSVPrinter,
        'json': JSONPrinter
    }
    return printers.get(format_type, GridPrinter)(output_stream)

# Usage in Shell.print_result:
printer = get_printer(self.output_format, self.query_out)
printer.print_results(colnames, formatted_rows)
{code}



> Enhance CQLSH to support machine-readable output formatting
> -----------------------------------------------------------
>
>                 Key: CASSANDRA-19985
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-19985
>             Project: Apache Cassandra
>          Issue Type: New Feature
>          Components: CQL/Interpreter
>            Reporter: Brad Schoening
>            Assignee: Arvind Kandpal
>            Priority: Normal
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> Existing CQLSH output formatting provides tabular formatting using grid-like separators and text alignment which is designed for user readability. 
> When CQL is run for non-interactive tasks as part of a pipeline this formatting complicates the machine processing of the output. As an example, [Sqlite|https://www.sqlite.org/cli.html] has a command line flag -mode to switch between tablular, csv, and several other supported formats.
> This enhancement will provide a new mode argument which will initially offer tabular and csv output formats. Tabular will remain the default so there will be no change for existing users.  In the future, other modes such as json and insert could be added.
> {noformat}
> --mode={tabular, csv}
> Specify an output display format. The default is tabular.
> {noformat}
> The existing Copy To can be used for exporting to CSV, but it doesn't allow for query criteria and thus isn't a general solution for this issue.
> In cqlshmain.py, the EXPAND CQLSH option which uses print_formatted_result() for result output is an example of alternative formatting.
> Paging will have to be properly managed. A good formatter should handle a stream of rows rather than requiring the entire list in memory, especially for large SELECT * queries.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

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