Skip to content

Finish Semicolon Delimited Statement Support for cdb2sql #5642

@morgando

Description

@morgando

Project Context

Other database client CLIs allow statements to be delimited by semicolons. However, cdb2sql only allows statements to be delimited by newlines—with the exception of DDL statements, which use $$ as a delimiter.

This is frustrating for developers who want to break long queries onto multiple lines for readability. Additionally, using newlines for most statements but $$ for DDL is confusing and inconsistent.

Goal: Enable cdb2sql to support a conventional delimiter (like ;) for all statements.

Technical Description

A version of this feature was merged into our open source cdb2sql in this PR. The algorithm is shown below, with ⚠️ marking the lines that have limitations (explained in the next section).

for each line from client:
    if line is not a comment or empty:
        cdb2sql appends line to sql buffer
        while sql buffer contains a delimiter:
            cdb2sql sends sql to server
            if server finds a complete semicolon-delimited statement:  ⚠️
                server runs statement
                cdb2sql gets return code (if error, return to client and stop)
                cdb2sql receives end offset of executed statement from server  ⚠️
                cdb2sql trims executed statement from sql buffer
            else:
                cdb2sql receives 'incomplete' error from server
                break  # request next line from client

if sql buffer is non-empty after all input processed:
    cdb2sql returns error: incomplete statement

Limitations

This feature does not work for SQL that bypasses the sqlite parser. Comdb2 uses sqlite's parser on most queries, but these statement types are handled separately (See handle_non_sqlite_requests):

  • Stored procedure invocations
  • Explain mode queries
  • Expert mode queries

The two ⚠️ lines in the algorithm—detecting delimited statement boundaries and saving the offset of the end of the previously executed statement—are implemented only within the sqlite parser. If a statement doesn't go through sqlite, these operations fail and the algorithm has undefined behavior.

Future Work

Deliver a fully functional version of this project by either implementing the missing algorithm components for the statement types that bypass the sqlite parser or limiting this feature to statement types that use the sqlite parser (returning a clear error for unsupported types).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions