Skip to content

Column DEFAULT only accepts integer literals #24

Description

@aciddust

expr is still the stub at src/pegjs/oracle.pegjs#L2711:

// TODO:
expr = integer

column_default_clause (L2067) is the only thing standing between it and a
column definition, so a default parses only when it is a signed whole number.
Everything else fails, including the very common default 'N'.

const { Parser } = require('oracle-sql-parser');
const parser = new Parser();

const t = (d) => {
  try { parser.parse(`create table t (a varchar2(10) ${d});`); console.log('OK  ', d); }
  catch (e) { console.log('FAIL', d, '->', e.message); }
};

t("default 0");            // OK
t("default 'N'");          // FAIL Expected "on", [ \t\n\r], [+\-], or [0-9] but "'" found.
t("default null");         // FAIL Expected "on", [ \t\n\r], [+\-], or [0-9] but "n" found.
t("default sysdate");      // FAIL Expected "on", [ \t\n\r], [+\-], or [0-9] but "s" found.
t("default sys_guid()");   // FAIL Expected "on", [ \t\n\r], [+\-], or [0-9] but "s" found.
t("default 0.5");          // FAIL Expected ")", ... or [0-9] but "." found.

Note that default 0.5 fails too:
integer matches the 0 and the . is then unexpected,
even though the grammar already has a number rule that handles it.

Most of the pieces are already there. literal (L2652) covers strings, numbers,
national character literals, q'[..]' quoting and datetime literals, it just is
not reachable from expr. Missing entirely are function calls, the datetime
keywords (sysdate, systimestamp, current_date, ...) and sequence
pseudocolumns (some_seq.nextval).

Where I ran into it: drawdb's "Import from SQL" with the Oracle dialect fails on
any table whose DDL carries a string default, which for a real schema is most of
them. drawdb also has a matching // TODO: reconstruct default when implemented in parser in its Oracle importer, so I assume this is known.

One thing that would help a consumer: literal normalises what it returns, so
the original spelling of a default is lost. Reporting the source text of the
default expression alongside it would let a consumer reproduce it verbatim.

Environment: oracle-sql-parser 0.1.0, Node v24.18.0.

Happy to open a PR if you would like — I have a branch that points expr at
literal and adds rules for the function/keyword/pseudocolumn forms, with tests.

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions