dolt icon indicating copy to clipboard operation
dolt copied to clipboard

"SELECT * FROM tbl" has incorrect schema when table has default values.

Open nicktobey opened this issue 2 years ago • 1 comments

Dolt often makes a query of the form "SELECT * from tbl" and examines the resulting schema in order to get a table schema. However, depending on specific factors, this resulting schema may be missing information about:

defaults
auto_increment
primary keys

The issue seems to be a mismatch between the Project node and the EraseProject optimization.

  • Project.Schema() only considers the name, type, and nullability of the underlying table. It does not report defaultness, primary key-ness, and extra metadata like auto_increment.
  • The EraseProject optimization removes Project nodes if it detects that they have no affect on the result. It does this by comparing the schema of the Project node with the schema of the underlying columns. However, this check does consider default values, but doesn't consider primary-keyness or auto_increment.

The main consequence of this mismatch is that if the underlying table has a column with a default value, then the Project node will NEVER have the same schema as the underlying table, and the Project node will never be removed from the plan tree. THEN, because Project nodes don't report the defaultnes, primary key-ness, or auto-increment, ALL of those attributes end up stripped from the final schema.

However, if the underlying table doesn't have a column with a default value, then those same values will be in the result table schema.

(Mirror of https://github.com/dolthub/go-mysql-server/issues/1753)

nicktobey avatar Jul 26 '23 22:07 nicktobey

It would be helpful to supplement this description with a specific query that fails and with expected/current behavior.

max-hoffman avatar Apr 16 '24 00:04 max-hoffman