sql: Normalize storage of `NULL` and `DEFAULT NULL` qualifiers in `CREATE TABLE`
After working on Issue #37948, @rolandcrosby and I agreed it makes the most sense to have an agreed upon normalization for adding NULL to column definitions. This was inspired by the fact that when a user creates a new column using a query such as ALTER TABLE t ADD COLUMN c INT NULL, the default expression is nil. However, if they create it in a more explicit fashion such as ALTER TABLE t ADD COLUMN c INT DEFAULT NULL, the default expression is NULL.
The expected behavior is the same for both, so normalizing the default expression to be nil in both cases makes the most sense to our team. The idea is to normalize these column definitions of NULL to have their respective default expression be nil.
Hey @Tyler314, I noticed this in the SQL execution backlog but it seems like you're looking at it - is that right? I'll move this to the bulk io team and assign you for now, but if I got that wrong, please feel free to unassign and move back!
@jordanlewis It seems like it may have some overlap between our two teams. I spent some time this weekend exploring it further with only marginal success, was hoping to pick the brains of some of you sql folk. We came across this bug also #38975
Gotcha. What did you try so far?
I began looking at sql.y to see how queries are being parsed, in hopes I could mimic the behavior of an explicit DEFAULT NULL for the implicit case. As of right now, I seem to be having success with issue #38975, mentioned above, where I set the default expression of the column descriptor to nil in sqlbase/table.go if I find that it was explicitly NULL. This seems like it will work, but it still doesn't sit well with me. Since there is already a defined path for the explicit case, my dream is that we can normalize the explicit case further up the tree, so to speak.
I don't really like the idea of normalizing "no default" to DEFAULT NULL - it seems like your current idea is better, to normalize DEFAULT NULL to "no default" (nil).
You could probably figure out a way to do this in the parser, so that SHOW CREATE TABLE on the table doesn't show that DEFAULT NULL clause, but it's not necessary to do that - it's fine to perform the normalization where you suggest.
We can validate this idea by (surprise surprise) checking out what Postgres does:
jordan=# create table a (a int not null, b int not null default null, c int null, d int null default null);
CREATE TABLE
jordan=# \d a
Table "public.a"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
a | integer | | not null |
b | integer | | not null |
c | integer | | |
d | integer | | |
jordan=# insert into a default values;
ERROR: null value in column "a" violates not-null constraint
DETAIL: Failing row contains (null, null, null, null).
As you can see, in both cases, there's no listed default expression.
It might be quicker to iterate on this idea if you send up your work in a PR - we can continue to discuss there.