Skip to content

Conversation

@wojtekn
Copy link
Contributor

@wojtekn wojtekn commented Jan 15, 2026

Summary

Adds full support for DEFAULT (expression) in CREATE TABLE statement column definitions.

Fixes #300 - Support for DEFAULT (now()).

Problem

The MySQL 8.0 syntax DEFAULT (function()) was failing because the translator only consumed a single token after the DEFAULT keyword. For expressions like DEFAULT (now()), it would only capture the opening parenthesis (, causing the query translation to fail.

Solution

  1. Full DEFAULT (expression) support in the AST driver.

  2. Enhanced the DEFAULT clause handler in WP_SQLite_Translator to:

  • Detect when a DEFAULT value starts with an opening parenthesis
  • Track parenthesis depth to consume the complete expression
  • Handle nested parentheses correctly

@wojtekn wojtekn force-pushed the fix/function-calls-in-parentheses branch from bdb6127 to a1fa523 Compare January 15, 2026 10:25
@JanJakes
Copy link
Member

@wojtekn Thank you for looking into this! Good job working with the AST and even fixing the old driver 👍

Generally, it looks good to me, and it is the right approach to work with the new driver. On the AST side, I spotted one issue that is related to only implementing a subset of the expression functionality, and that is that for expressions such as this one:

DEFAULT (DATE_ADD(NOW(), INTERVAL 1 YEAR))

It would detect and incorrectly return the value of NOW(). That's because the implementation checks whether there is that function call, but it can't easily determine that there is nothing else. Fixing that is not straightforward with arbitrary nesting, e.g. for DEFAULT (((now()))), and doing the full expression support is easy, so I think we could even go all the way. Let me see if I can push my idea somehow. I probably can't modify this branch as it's from a fork, but I can push another one.

Additionally, in the MySQL column metadata, there needs to be an indication that it has a DEFAULT_GENERATED value, but I can see that we already seem to be doing that, so I think we're safe on this one. Perhaps it needs a test in case there isn't one yet.

@JanJakes JanJakes force-pushed the fix/function-calls-in-parentheses branch from 14c63a7 to 04753f9 Compare January 16, 2026 15:55
@JanJakes JanJakes changed the title Fix CREATE TABLE with DEFAULT function calls in parentheses Add support for DEFAULT (expression) in column definitions Jan 16, 2026
@JanJakes
Copy link
Member

JanJakes commented Jan 16, 2026

@wojtekn It turns out I can push to this branch. I just have no idea how to manage PRs from forks 😆

I added one commit with a quick implementation of the full DEFAULT (expression) on the AST side. I will check it again after the weekend and see if it needs some polishing and/or more tests.

TODO:

  • Check if we need to test more expressions.
  • Add a test for metadata (DEFAULT_GENERATED ).
  • Check if we can quickly implement $node->get_bytes() without hindering performance.

Thanks again for pushing this forward!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create table with DEFAULT (now()) fails in AST driver

2 participants