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

Brad Schoening commented on CASSANDRA-19985:
--------------------------------------------

[~arvindk12] the output looks good.  

1) bug, validation is asymmetric between the two input paths:
 * Line 2275: {{--mode}} has {{{}choices=['tabular','csv','json']{}}}, so 
argparse rejects garbage.
 * Line 2070: {{option_with_default(configs.get, 'ui', 'mode', 'tabular')}} 
accepts *anything* from cqlshrc.

Standardizing the parsing with an enum class:
{code:java}
class OutputMode(str, Enum):
    TABULAR = 'tabular'
    CSV = 'csv'
    JSON = 'json'

    __str__ = str.__str__   

    @property
    def is_machine_readable(self):
        return self is not OutputMode.TABULAR

    @classmethod
    def parse(cls, value):
        if isinstance(value, cls):
            return value
        try:
            return cls(str(value).strip().lower())
        except ValueError:
            raise ValueError("Invalid output mode %r; expected one of: %s"
                             % (value, ', '.join(m.value for m in cls))){code}
Then line 296 becomes self.mode = OutputMode.parse(mode)

2) One thing I'd explicitly leave alone here: the `isinstance(printer, 
JsonTablePrinter)` branch at line 1029. Thirty-odd lines of JSON-specific 
formatting living in `print_static_result` rather than on the printer that owns 
it is poor style, but fixing it properly means a polymorphic `format_row` 
across all three printers and real test surface. Better as its own ticket than 
as scope creep on this one. Same for converting `TablePrinter.factory`'s 
dispatch to a mapping keyed on the enum.  [this from Claude]

> 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
>         Attachments: json_formatters_example.py, test_json_formatting.py
>
>          Time Spent: 1.5h
>  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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to