Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory database with JSON column cannot be updated #361

Closed
firelizzard18 opened this issue Apr 7, 2021 · 3 comments · Fixed by #356
Closed

memory database with JSON column cannot be updated #361

firelizzard18 opened this issue Apr 7, 2021 · 3 comments · Fixed by #356

Comments

@firelizzard18
Copy link
Contributor

func TestJsonUpdate(t *testing.T) {
	table := memory.NewTable(t.Name(), sql.Schema{
		{Name: "json", Type: sql.JSON, Nullable: false, Source: t.Name()},
	})

	old := sql.NewRow(sql.JSONDocument{Val: []string{"foo", "bar"}})
	new := sql.NewRow(sql.JSONDocument{Val: []string{"foo"}})

	ctx := sql.NewEmptyContext()
	table.Insert(ctx, old)

	up := table.Updater(ctx)
	up.Update(ctx, old, new)
}

results in

--- FAIL: TestJsonUpdate (0.00s)
panic: runtime error: comparing uncomparable type []string [recovered]
        panic: runtime error: comparing uncomparable type []string

goroutine 22 [running]:
testing.tRunner.func1.2(0x16996e0, 0xc000289e70)
        GOPATH/src/testing/testing.go:1144 +0x332
testing.tRunner.func1(0xc000102a80)
        GOPATH/src/testing/testing.go:1147 +0x4b6
panic(0x16996e0, 0xc000289e70)
        GOPATH/src/runtime/panic.go:965 +0x1b9
github.com/dolthub/go-mysql-server/memory.(*tableEditor).Update(0xc000133f18, 0xc000204ab0, 0xc000289d90, 0x1, 0x1, 0xc000289dc0, 0x1, 0x1, 0x105a2a5, 0x10740c0)
        CODE/go-mysql-server/memory/table.go:621 +0x275
github.com/dolthub/go-mysql-server/memory_test.TestJsonUpdate(0xc000102a80)
        CODE/go-mysql-server/memory/regression_test.go:36 +0x405
testing.tRunner(0xc000102a80, 0x178a6a0)
        GOPATH/src/testing/testing.go:1194 +0xef
created by testing.(*T).Run
        GOPATH/src/testing/testing.go:1239 +0x2b3
FAIL    github.com/dolthub/go-mysql-server/memory       0.019s
FAIL
firelizzard18 added a commit to firelizzard18/go-mysql-server that referenced this issue Apr 7, 2021
firelizzard18 added a commit to firelizzard18/go-mysql-server that referenced this issue Apr 7, 2021
@firelizzard18
Copy link
Contributor Author

Also true for delete

firelizzard18 added a commit to firelizzard18/go-mysql-server that referenced this issue Apr 7, 2021
@andy-wm-arthur
Copy link
Contributor

@firelizzard18 thanks for the bug! I'll take a look.

At first glance, it might be cause by []string{ "foo", "bar"} vs []interface{"foo", "bar"}.

@firelizzard18
Copy link
Contributor Author

@andrew-wm-arthur The issue is that arrays are not comparable:

A comparison of two interface values with identical dynamic types causes a run-time panic if values of that type are not comparable. This behavior applies not only to direct interface value comparisons but also when comparing arrays of interface values or structs with interface-valued fields.

Slice, map, and function values are not comparable. However, as a special case, a slice, map, or function value may be compared to the predeclared identifier nil. Comparison of pointer, channel, and interface values to nil is also allowed and follows from the general rules above.

(*memory.Table).Insert is a bit weird, in that it circumvents the processing/normalization of values done by the sql package when an INSERT is executed. _example/main.go inserts values directly, without being wrapped in a sql.JSONDocument. However, even if the insert is done via SQL, or if you wrap the value with a JSONDocument, it still breaks, because then you're comparing two JSONDocuments that point to arrays, and types that contain an incomparable are themselves incomparable.

The solution is to modify (*memory.tableEditor).Update and .Delete: when comparing two rows, if a given column is sqltypes.TypeJSON, use sql.Type.Compare(a, b) == 0 instead of ==. I submitted a fix as part of #356, since the bug was breaking my tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants