duckdb_engine icon indicating copy to clipboard operation
duckdb_engine copied to clipboard

test: add example test for double creation of enums

Open NickCrews opened this issue 9 months ago • 2 comments

With a single Enum column definition in a schema, we don't actaully create and use the custom type, but make an inline copy every time. This is what currently happens:

CREATE TYPE severity AS ENUM ( 'LOW', 'MEDIUM', 'HIGH' );
CREATE TABLE bugs(severity ENUM('LOW', 'MEDIUM', 'HIGH'));

This is what I would expect:

CREATE TYPE severity AS ENUM ( 'LOW', 'MEDIUM', 'HIGH' );
CREATE TABLE bugs(severity severity);

If you point me in the right direction I can do the grunt work of actually writing the fix, but I just tried exploring where in our engine we would need to make adjustments so that sqlalchemy picks up the needed DDL hooks before table creation, and I got overwhelmed by the complexity of sqlalchemy.

NickCrews avatar Mar 22 '25 01:03 NickCrews

unfortunately i don't think this is really fixable from our side - duckdb itself doesn't retain a reference (last i checked) to the enum, but copies the enum definition itself. we can fix the sql at least though - can you fix the tests?

Mause avatar Mar 29 '25 02:03 Mause

duckdb itself doesn't retain a reference

Sorry, I don't think I understand, can you elaborate a bit more?

If I do

CREATE TYPE severity AS ENUM ( 'LOW', 'MEDIUM', 'HIGH' );
CREATE TABLE bugs(severity severity);
INSERT INTO bugs VALUES ('LOW'), ('HIGH');
DROP TYPE severity;

I get

Dependency Error: Cannot drop entry "severity" because there are entries that depend on it.
table "bugs" depends on type "severity".
Use DROP...CASCADE to drop all dependents.

which looks to me like the table is holding onto a reference to the type? This is the behavior that I want. Say I decide to change my enum to be 'L', 'M', 'H', instead. If I have 'LOW', 'MEDIUM', 'HIGH' stored inside the table, I want to get an error, because I need to explicitly do the conversion myself.

NickCrews avatar Mar 30 '25 20:03 NickCrews