Skip to content

MySQL/MariaDB column attributes: DEFAULT current_timestamp() and ON UPDATE current_timestamp fail to parse #388

Closed

Description

@AirRunner

MariaDB (10.2 and later) writes CURRENT_TIMESTAMP back as current_timestamp() in the DDL it outputs (MDEV-13377), so every mysqldump of a table with a timestamp default contains this form:

CREATE TABLE foo (
  created_at datetime NOT NULL DEFAULT current_timestamp(),
  updated_at datetime DEFAULT NULL ON UPDATE current_timestamp()
);

Both column attributes produce an ERROR node. Tested on v0.3.11 and on main (987a64a).

Input Result
a datetime DEFAULT current_timestamp ok
a datetime DEFAULT current_timestamp() ERROR
a datetime DEFAULT current_timestamp(6) ERROR
a datetime DEFAULT now() ok
a datetime DEFAULT NULL ON UPDATE current_timestamp ERROR
a datetime DEFAULT NULL ON UPDATE current_timestamp() ERROR
SELECT current_timestamp(); ok

1. current_timestamp() after DEFAULT

CREATE TABLE t (a datetime DEFAULT current_timestamp());

(program
  (statement
    (create_table
      (keyword_create) (keyword_table)
      (object_reference name: (identifier))
      (column_definitions
        (column_definition
          name: (identifier)
          type: (keyword_datetime)
          (keyword_default)
          (keyword_current_timestamp))
        (ERROR [0, 52] - [0, 53]))))
  (ERROR [0, 54] - [0, 55]))

In a default context current_timestamp is lexed as keyword_current_timestamp, and _inner_default_expression accepts that keyword only bare, so the ( that follows has nowhere to go. In a SELECT the same text is lexed as an identifier and parses as an invocation.

A possible fix in grammar/column-lists.js, which accepts the empty parentheses and the fractional-seconds precision:

       $.invocation,
-      $.keyword_current_timestamp,
+      prec.right(seq(
+        $.keyword_current_timestamp,
+        optional(wrapped_in_parenthesis(optional($.literal))),
+      )),
       alias($.implicit_cast, $.cast),

With it, DEFAULT current_timestamp(), DEFAULT current_timestamp(6) and DEFAULT (current_timestamp()) parse without errors.

2. ON UPDATE <expr> as a column attribute

CREATE TABLE t (a datetime DEFAULT NULL ON UPDATE current_timestamp);

(column_definition
  name: (identifier)
  type: (keyword_datetime)
  (keyword_default)
  (ERROR [0, 35] - [0, 49]
    (literal (keyword_null))
    (keyword_update))
  (keyword_current_timestamp))

The grammar has no rule for the MySQL ON UPDATE column attribute: ON UPDATE is only accepted as a referential action after REFERENCES. I tried adding seq($.keyword_on, $.keyword_update, $._inner_default_expression) to _column_constraint. It parses the MySQL form, but it breaks REFERENCES p (id) ON UPDATE CASCADE (corpus test "Create table column REFERENCES constraint" fails), with or without prec.dynamic(-1, …). So this one needs a proper resolution of the conflict with the referential actions.

Also seen: datetime(6)

CREATE TABLE t (a datetime(6)); fails as well, while timestamp(6) and time(3) parse. MariaDB dumps use datetime(6) for columns with fractional seconds.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions