jackc / jackc/pgx

Error when using COPY FROM and array enum

Open
#1,031 1 comment 5 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

defines the following enum type and table in postgresql(simplified from my actual code for brevity):
```
CREATE TYPE events.EVENT_TYPES AS ENUM ('user','access','admin','allowed','change','connection','creation','deletion',
'denied','end','error','group','info','installation','protocol','start');

CREATE TABLE IF NOT EXISTS events.events (
key_time BIGINT NOT NULL,
key_roller INT NOT NULL,
key_stream INT NOT NULL,

ecs_event_type events.EVENT_TYPES[]);
```
and then define a eventtype_slice.go as:
```
type EventTypes []EventType

func (et EventTypes) EncodeText(ci *pgtype.ConnInfo, buf []byte) (newBuf []byte, err error) {
texts := make([]pgtype.GenericText, len(et))
for i := 0; i < len(et); i++ {
err = texts[i].Set(et[i].String())
if err != nil {
return nil, err
}
}
enums := pgtype.EnumArray{}
err = enums.Set(texts)
if err != nil {
return nil, err
}
return enums.EncodeText(ci, buf)
}
```
with eventype.go as:
```
type EventType uint8

const (
EventTypeUser EventType = iota
EventTypeAccess
EventTypeAdmin
EventTypeAllowed
EventTypeChange
EventTypeConnection
EventTypeCreation
EventTypeDeletion
EventTypeDenied
EventTypeEnd
EventTypeError
EventTypeGroup
EventTypeInfo
EventTypeInstallation
EventTypeProtocol
EventTypeStart
)

func (i EventType) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) (newBuf []byte, err error) {
return append(buf, i.String()...), nil
}

// generated code

const _EventTypeName = "useraccessadminallowedchangeconnectioncreationdeletiondeniedenderrorgroupinfoinstallationprotocolstart"

var _EventTypeIndex = [...]uint8{0, 4, 10, 15, 22, 28, 38, 46, 54, 60, 63, 68, 73, 77, 89, 97, 102}

func (i EventType) String() string {
if i >= EventType(len(_EventTypeIndex)-1) {
return fmt.Sprintf("EventType(%d)", i)
}
return _EventTypeName[_EventTypeIndex[i]:_EventTypeIndex[i+1]]
}

var _EventTypeValues = []EventType{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}

var _EventTypeNameToValueMap = map[string]EventType{
_EventTypeName[0:4]: 0,
_EventTypeName[4:10]: 1,
_EventTypeName[10:15]: 2,
_EventTypeName[15:22]: 3,
_EventTypeName[22:28]: 4,
_EventTypeName[28:38]: 5,
_EventTypeName[38:46]: 6,
_EventTypeName[46:54]: 7,
_EventTypeName[54:60]: 8,
_EventTypeName[60:63]: 9,
_EventTypeName[63:68]: 10,
_EventTypeName[68:73]: 11,
_EventTypeName[73:77]: 12,
_EventTypeName[77:89]: 13,
_EventTypeName[89:97]: 14,
_EventTypeName[97:102]: 15,
}

func EventTypeString(s string) (EventType, error) {
if val, ok := _EventTypeNameToValueMap[s]; ok {
return val, nil
}
return 0, fmt.Errorf("%s does not belong to EventType values", s)
}

// EventTypeValues returns all values of the enum
func EventTypeValues() []EventType {
return _EventTypeValues
}
```
When trying to use COPY FROM it throws an error:
```
ERROR: number of array dimensions (2071294821) exceeds the maximum allowed (6) (SQLSTATE 54000)
```
The EncodeText method is called for EventTypes.
When I replace EventTypes[] на smallint, smallint[] everything works fine.

Contributor guide

Open the contributing guide

Research direction

Start at EventTypes.EncodeText in eventtype_slice.go and trace the COPY FROM path that encodes the PostgreSQL enum array. Reproduce the reported dimension error with the supplied enum and table, then compare it with the working smallint[] case. Done means COPY FROM accepts EventTypes[] without the array-dimension error.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.