chaisql / chai

Modern embedded SQL database
MIT License
1.56k stars 95 forks source link

Wrong index arity after reopen an existing DB(multiple index) #433

Closed abramlab closed 3 years ago

abramlab commented 3 years ago

What version of Genji are you using?

$ genji version

Genji v0.13.0
Genji CLI v0.13.0

Does this issue reproduce with the latest release?

Yes

What did you do?

genji test_index.db

CREATE TABLE cars (name TEXT NOT NULL, color TEXT NOT NULL, price VARCHAR(128));

CREATE UNIQUE INDEX idx_cars_name_color ON cars(name, color);

INSERT INTO cars (name, color, price) VALUES ("Tesla", "Red", "100000");
{
  "name": "Tesla",
  "color": "Red",
  "price": "100000"
}

UPDATE cars SET price = '500000' WHERE (name = 'Tesla' AND color = 'Red');

exit

genji test_index.db

UPDATE cars SET price = '777777' WHERE (name = 'Tesla' AND color = 'Red');
wrong index arity <- here I get an ERROR!

The same behavior is observed in the runnable program, not only in CLI. The problem only occurs when you run it again on an already created database.

What did you expect to see?

Correct record update, no mistake.

What did you see instead?

Error: wrong index arity

What Go version and environment are you using?

$ go version

go version go1.17 linux/amd64
go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/misha/.cache/go-build"
GOENV="/home/misha/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/misha/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/misha/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2235764675=/tmp/go-build -gno-record-gcc-switches"

Code research

In my research on the code, I came to the conclusion that this problem may be because when indexes loading from existing database https://github.com/genjidb/genji/blob/b9b312418718f6eb24f7fea5ce480aa133e526e3/internal/catalog/catalog.go#L73 their types are not filled https://github.com/genjidb/genji/blob/b9b312418718f6eb24f7fea5ce480aa133e526e3/internal/database/info.go#L90

In the case when the database is created for the first time, the types are successfully added when creating the index https://github.com/genjidb/genji/blob/b9b312418718f6eb24f7fea5ce480aa133e526e3/internal/catalog/catalog.go#L300

asdine commented 3 years ago

Thanks for reporting @abramlab ! @KudinovKV fixed this issue (#434), feel free to reopen if you still experience the same problem