Skip to content

Ignore line-start # comments in DuckDB #1011

Backlog

Description

@gltjk

Describe the Feature

DuckDB CLI silently ignores #-prefixed lines at statement boundaries (when no SQL statement is currently in progress). Running duckdb -f script.sql on such files executes cleanly, whereas sql-formatter's DuckDB dialect fails with a parse error.

Proposed behavior: Treat lines starting with # as line comments when outside of an active SQL statement (i.e., at the start of a file or immediately after a semicolon ;). No command parsing is needed—simply preserve/skip them like standard line comments.

Why do you want this feature?

This is deliberate CLI behavior in DuckDB, supporting shebangs (#!/usr/bin/env -S duckdb -f) and # comments in standalone .sql scripts.
In DuckDB's tools/shell/shell.cpp (commit 8d78114 as of latest v2.0.1 release), # and . commands are handled in the exact same branch:

if ((zLine[0] == '.' || zLine[0] == '#') && nSql == 0) {
  if (ShellHasFlag(ShellFlags::SHFLG_Echo)) {
    printf("%s\n", zLine);
  }
  if (zLine[0] == '.') {
    if (mode == InputMode::STANDARD && *zLine && *zLine != '\3') {
      ShellAddHistory(zLine);
    }
    rc = DoMetaCommand(zLine);
    if (rc == 2) { /* exit requested */
      break;
    } else if (rc) {
      errCnt++;
    }
  }
  continue;
}

(Simplified from the surrounding input loop for clarity.)

Unlike .commands, # requires no meta-command grammar—it is purely a line-level skip when nSql == 0.

Precedents in this project

  • MySQL/MariaDB dialect: already treats # as a line comment (SQL-layer)
  • PostgreSQL dialect: supports \ meta-commands (client-layer, fix: support pg_dump restrict commands #966)
    DuckDB's # is closer to the latter category (CLI-layer skip), but syntactically simpler: it needs no command parser, just "ignore line-start # when no statement is in progress." This is the minimal subset that makes real-world DuckDB scripts formatable.

P.S. .commands in DuckDB dialect remains out of scope (tracked in #939).

Activity

  1. changed the title [-]Feature Request: DuckDB dialect — ignore line-start `#` comments (CLI compatibility)[/-] [+]Ignore line-start `#` comments in DuckDB[/+] on Nov 24, 2026
  2. nene commented on Nov 24, 2026

    @nene
    Collaborator

    Thanks for reporting. I would have appreciated a non-LLM-generated bug report though.

    I've cleaned up all the excess noise from the issue title and descriptions.

  3. added and removed on Nov 24, 2026
  4. gltjk commented on Nov 24, 2026

    @gltjk
    Author

    Thanks for reporting. I would have appreciated a non-LLM-generated bug report though.

    I've cleaned up all the excess noise from the issue title and descriptions.

    Sorry for causing a misunderstanding but all the research was done by myself (human) including locating the relevant code in the DuckDB codebase and searching your repo issues for similar issues. I was even worried that you wouldn't accept a non-documented syntax so I tried to give as much proof as I could think of. Being a non-native English speaker, I only used LLM (and spell checking) to rephrase my words to seem more fitting here in Github, but I think being called "LLM-generated" is too discouraging that almost denied all my actual effort. (I don't mean to bring in negativity in this thread but I really need to say these out.)

  5. nene commented on Nov 24, 2026

    @nene
    Collaborator

    Hey, sorry for jumping too promptly to conclusions. It was just the case that this bug report seemed way too precise to be human-written. Normally nobody bothers to put in so much effort into a bug report.

    Normally I try to lean on the side of assuming things aren't LLM-generated, even when I have a feeling that they might be. But this time I clearly failed to do so. Sorry.

  6. nene commented on Nov 24, 2026

    @nene
    Collaborator

    I've restored your original issue description to its former glory.

  7. gltjk commented on Nov 24, 2026

    @gltjk
    Author

    No worries at all, totally understandable! Thank you so much for restoring my issue and for maintaining such an awesome tool. Looking forward to seeing this supported in a future release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions