Wednesday, July 7, 2010

Idea: polymorphic column types

One thing I've always thought would be useful is polymorphic column types, to reduce the need in "generic" records of having stuff like

create table foo (id int, b varchar (list of typenames), c1 type1, c2 type2, c3 type3, ...);

A polymorphic column type would be a column that can take on a type specified in another column, so the above would be replaced by

create table foo (id int, b typename, c typespec(b));

where column "b" specifies a type, and "typename" is a special pseudo-type that would contain names like "int", "varchar(10)", etc.

Polymorphic column types would behave like variable-length columns for storage purposes, and would probably only be retrievable as opposed to searchable, although we could allow them to be searchable if we insist that the search operator/function be type-compatible with the row and evaluate to NULL if it isn't.

The client-side row fetch API would need to be somewhat clever to handle this, but since the type of a given row is well-defined, this shouldn't be all that hard to implement.

No comments: