Issue Details (XML | Word | Printable)

Key: DERBY-4013
Type: Improvement Improvement
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Dag H. Wanvik
Reporter: Dag H. Wanvik
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Derby

Allow standard SQL syntax: ALTER TABLE ALTER [COLUMN] <col> SET DEFAULT <default>

Created: 09/Jan/09 06:31 PM   Updated: 04/May/09 06:22 PM
Component/s: SQL
Affects Version/s: 10.0.2.0, 10.0.2.1, 10.1.1.0, 10.1.2.1, 10.1.3.1, 10.2.1.6, 10.2.2.0, 10.3.1.4, 10.3.2.1, 10.3.3.0, 10.4.1.3, 10.4.2.0
Fix Version/s: 10.5.1.1

Time Tracking:
Not Specified

File Attachments:
  Size
File Licensed for inclusion in ASF works derby-4013.diff 2009-01-09 08:11 PM Dag H. Wanvik 5 kB
File Licensed for inclusion in ASF works derby-4013.stat 2009-01-09 08:11 PM Dag H. Wanvik 0.2 kB
File Licensed for inclusion in ASF works derby-4013b.diff 2009-01-12 04:23 PM Dag H. Wanvik 6 kB
File Licensed for inclusion in ASF works derby-4013b.stat 2009-01-12 04:23 PM Dag H. Wanvik 0.2 kB
File Licensed for inclusion in ASF works derby-4013c.diff 2009-01-22 10:25 PM Dag H. Wanvik 7 kB
File Licensed for inclusion in ASF works derby-4013c.stat 2009-01-22 10:25 PM Dag H. Wanvik 0.2 kB
File Licensed for inclusion in ASF works derby-4013d.diff 2009-01-26 05:39 PM Dag H. Wanvik 8 kB
File Licensed for inclusion in ASF works derby-4013d.stat 2009-01-26 05:39 PM Dag H. Wanvik 0.2 kB
Issue Links:
Dependants
 

Resolution Date: 26/Jan/09 06:29 PM
Labels:


 Description  « Hide
Presently, the Derby syntax is ALTER TABLE ALTER [COLUMN] <col> [WITH] DEFAULT <default>.
The "SET" keyword is not accepted, only an optional "WITH". It would be good to accept the standard syntax here as well.
Cf. SQL 2003, section 11.12 <alter column definition> and section 11.13 <set column default clause>.

Also, DROP DEFAULT is standard syntax not supported, so we should add that, too.

Repro on trunk:

$ java org.apache.derby.tools.ij
ij version 10.5
ij> connect 'jdbc:derby:wombat;create=true';
ij> create table t(i int default 0, j int);
0 rows inserted/updated/deleted
ij> alter table t alter column j with default 1;
0 rows inserted/updated/deleted
ij> insert into t values (default, default);
1 row inserted/updated/deleted
ij> select * from t;
I |J
-----------------------
0 |1

1 row selected
ij> alter table t alter column j default 2;
0 rows inserted/updated/deleted
ij> insert into t values (default, default);
1 row inserted/updated/deleted
ij> select * from t;
I |J
-----------------------
0 |1
0 |2

2 rows selected
ij> alter table t alter column j set default 3;
ERROR 42X01: Syntax error: Encountered "set" at line 1, column 30.



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Dag H. Wanvik added a comment - 09/Jan/09 08:11 PM
Uploading a small patch for this with new test cases for the syntax variations. I did not find a natural place for them, so I started a new test class, AlterColumnTest.

Knut Anders Hatlen added a comment - 09/Jan/09 11:37 PM
I haven't tested the patch, only taken a brief look at it, but it looks correct to me. Why do you think it needs a release note?

Minor nits:
  - javadoc for the test class describes another test ("compressing tables" it says)
  - javadoc to explain tryAndExpect()'s parameters might be helpful
  - tryAndExpect() reinvents JDBC.assertSingleValueResultSet()

Dag H. Wanvik added a comment - 10/Jan/09 01:04 AM
Thanks for looking at this, Knut! Since this isssue is not a bug and too small to merit a feature write-up,
I figured it would be best to mark it as requiring a note in the release notes (although it is upwards compatible).
What do you think?

Ill respin the patch to fix your nits.

Knut Anders Hatlen added a comment - 11/Jan/09 06:45 PM
Even though it's a small feature, I think it makes more sense to list it under new features than under potential incompatibilities. The Release Note Needed flag is used for issues that require special attention, and adding this issue to that list will take attention away from the issues that really require special attention. Since there's no incompatibility, I'd suggest that you just add it under Features at the 10.5.1 release wiki: http://wiki.apache.org/db-derby/DerbyTenFiveOneRelease (I think that's where the release manager takes the New Features list from when compiling the final release notes, but I may be mistaken.)

Kim Haase added a comment - 12/Jan/09 03:17 PM
Should we have an associated documentation issue to update the syntax in the ALTER TABLE topic of the Reference Manual? I can file one, or you can.

Dag H. Wanvik added a comment - 12/Jan/09 03:52 PM
Yes, we should definitely change the docs too! Feel free to file an issue for it!

Dag H. Wanvik added a comment - 12/Jan/09 04:23 PM
Uploading a new version of this patch, derby-4013b, which addresses Knut's nits.
Regressions were run on the previous version with no errors.

Dag H. Wanvik added a comment - 12/Jan/09 04:30 PM
I will remove the release note flag, and add an entry to
DerbyTenFiveOneRelease, then.

It seems the semantics of this flag "Release Note Needed" is now
effectively: "Release note attached (based on
/tools/release/templates/releaseNote.html), because this issue
potentially breaks existing applications". In other cases, we should
either enter an entry to the upcoming release page, and/or rely on the
release manager to find issues marked "Improvement" or "New features"
so something can be said about them in the release notes.

Knut Anders Hatlen added a comment - 13/Jan/09 09:20 AM
Thanks, Dag. The 4013b patch looks good to me. I tested a couple of queries in ij, and it seems to work fine. +1 to commit.

Dag H. Wanvik added a comment - 16/Jan/09 07:49 PM - edited
Uploading patch derby-4013c, which also introduced the standard syntax
for ALTER [COLUMN] <column-name> DROP DEFAULT which is also not supported
presently (thank to Kim for discovering that) and added tests for
that.

Re-running regressions.

Knut Anders Hatlen added a comment - 19/Jan/09 01:38 PM
SQL 2003, 11.14 <drop column default clause> says the following in Syntax Rules:
> 2) The descriptor of C shall include a default value.

And in General Rules:
> 1) The default value is removed from the column descriptor of C.

Does this imply that if you drop the default value for the same column twice, the second one should fail because C has no default value? And similarly, if you try to drop the default on an identity column, it should fail, whereas the current patch will make it an ordinary (non-identity) column with default NULL?

Knut Anders Hatlen added a comment - 19/Jan/09 04:11 PM
I tested DROP DEFAULT on PostgreSQL, and it appears to behave the same way as Derby would with the latest patch. DROP DEFAULT sets the default to NULL, and it can also be used to set the default of an identity column to NULL. Dropping the default twice does not raise an error.

Dag H. Wanvik added a comment - 19/Jan/09 05:15 PM
Thanks for checking the standard on the semantics here, Knut. Derby
does treat no default the same as a default of null, I find.

If this is strictly according to the standard, I am not sure. At
least, if there is no defined default, the null value should be used,
according to section 11.5 default clause, General Rule 3 e):
"Otherwise, S is set to the null value."

The way I read it, this is not technically a default, but rather the
null is used in lieu of a defined default. By that reasoning, the DROP
default should probably have failed if there is no defined default on
that column. Up to now, Derby makes no such distinction, since setting
a default back to "null" is the only way to get rid of another defined
default [*].

But I suggest we be lenient here, as is Postgres in your experiment,
and let DROP DEFAULT be a no-op if there is no default defined for a
column, otherwise we would have to start distinguishing between an
explicitly created "null" default (whose dropping would be legal) and
an implicit null default (by rule 3e above, in which case dropping the
default would be an error).

[*]
This can be seen by first making an explicit non-null default, and
then changing its value to null:

ij> alter table t alter i DEFAULT 1;
ij> select columndefault, columndefaultid from sys.syscolumns where columnname='I';
COLUMNDEFAULT |COLUMNDEFAULTID
----------------------------------------------------
1 |286cc01e-011e-efd9-cb02-000003ffe058

ij> alter table t alter i DEFAULT null;
ij> select columndefault, columndefaultid from sys.syscolumns where columnname='I';
COLUMNDEFAULT |COLUMNDEFAULTID
----------------------------------------------------
NULL |NULL

There no longer a UUID identifying to a defined default (i.e. it has been dropped).

Dag H. Wanvik added a comment - 19/Jan/09 05:16 PM
Regressions ran ok.

Knut Anders Hatlen added a comment - 20/Jan/09 09:04 AM
I'm fine with the suggested approach. If someone thinks it's important that we distinguish between the no default case and the NULL default case, that could be fixed later, but for now this looks like a fine incremental improvement to me.

Two tiny nits:
  - there's a pure whitespace diff right above dropTableConstraintDefinition() in sqlgrammar.jj
  - the javadoc comment for wrapAlterColumnDefaultValue() just says "Minion", which is not very descriptive (but it's great that this common code was factored out!)

Kathey Marsden added a comment - 22/Jan/09 09:31 PM
I see the test was added to the suite:
+ suite.addTest(AlterColumnTest.suite());

But I don't see AlterColumnTest as part of the patch. Did you maybe not svn add or am I just missing it?

Thanks

Kathey

Dag H. Wanvik added a comment - 22/Jan/09 10:25 PM
Sorry about that, Kathey, the last rev. lacked the new test. Uploading it again.

Dag H. Wanvik added a comment - 26/Jan/09 05:39 PM
Uploaded derby-4013d, which fixes Knut's nits.

Dag H. Wanvik added a comment - 26/Jan/09 06:29 PM
Resolving, committed patch derby-4013d as svn 737774.
If nobody argues that this should be backported, I will close the issue in a bit.