dolthub/doltgresql

Column default value expression error

Closed this issue · 1 comments

After adding a column default value expression, Doltgres is unable to insert data into a table or add a primary key to the table because it can't analyze the column default value expression correctly.

CREATE TABLE public."Testing_Table_1" (
	"String_Data" text,
	id integer NOT NULL
);

CREATE SEQUENCE public."Testing_Table_1_id_seq"
      AS integer
      START WITH 1
      INCREMENT BY 1
      NO MINVALUE
      NO MAXVALUE
      CACHE 1;

ALTER TABLE ONLY public."Testing_Table_1" ALTER COLUMN id SET DEFAULT nextval('public."Testing_Table_1_id_seq"'::regclass);

insert into "Testing_Table_1" ("String_Data") VALUES ('bar');

Doltgres (i.e. GMS) seems to get confused about the value passed into the nextval function. It treats 'public."Testing_Table_1_id_seq"' as a column identify, and then errors out when it doesn't know about a table named "public". GMS should not assume this is a column identity.

Here's the stack trace from the error:

unable to prepare query: table not found: public

github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).buildScalar
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/scalar.go:125
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).buildScalar
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/scalar.go:262
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).selectExprToExpression
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/project.go:178
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).buildScalar
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/scalar.go:158
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).buildScalar
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/scalar.go:195
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).convertDefaultExpression
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/ddl.go:1547
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).resolveColumnDefaultExpression
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/ddl.go:1540
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).resolveSchemaDefaults
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/ddl.go:1461
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).buildInsert
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/dml.go:86
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).buildSubquery
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/builder.go:234
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).build
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/builder.go:198
github.com/dolthub/go-mysql-server/sql/planbuilder.(*Builder).BindOnly
	/Users/jason/Projects/secondary-go-mysql-server/sql/planbuilder/parse.go:101
github.com/dolthub/go-mysql-server.(*Engine).PrepareParsedQuery
	/Users/jason/Projects/secondary-go-mysql-server/engine.go:241
github.com/dolthub/doltgresql/server.(*DoltgresHandler).ComPrepareParsed
	/Users/jason/Projects/secondary-doltgres/server/doltgres_handler.go:121
github.com/dolthub/doltgresql/server.(*ConnectionHandler).handleParse
	/Users/jason/Projects/secondary-doltgres/server/connection_handler.go:465
github.com/dolthub/doltgresql/server.(*ConnectionHandler).handleMessage
	/Users/jason/Projects/secondary-doltgres/server/connection_handler.go:363
github.com/dolthub/doltgresql/server.(*ConnectionHandler).receiveMessage
	/Users/jason/Projects/secondary-doltgres/server/connection_handler.go:333
github.com/dolthub/doltgresql/server.(*ConnectionHandler).HandleConnection
	/Users/jason/Projects/secondary-doltgres/server/connection_handler.go:157

Originally reported in #843

This only seems to happen with the value is cast to regclass. Without the ::regclass suffix, the queries above work correctly.

This seems to be happening in the nodeSelectExpr function:

return &vitess.AliasedExpr{

We're returning an AliasedExpr where the column default value is aliased as a column identifier, but that doesn't seem correct for a column default value expression.