mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-25 18:49:36 -06:00
Merge pull request #9513 from dolthub/reltuk-186222f1
[auto-bump] [no-release-notes] dependency by reltuk
This commit is contained in:
@@ -295,7 +295,7 @@ func getJsonAsString(sqlCtx *sql.Context, params interface{}) (string, error) {
|
||||
case string:
|
||||
return p, nil
|
||||
case sql.JSONWrapper:
|
||||
json, err := types.JsonToMySqlString(p)
|
||||
json, err := types.JsonToMySqlString(sqlCtx, p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ require (
|
||||
github.com/creasty/defaults v1.6.0
|
||||
github.com/dolthub/aws-sdk-go-ini-parser v0.0.0-20250305001723-2821c37f6c12
|
||||
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
|
||||
github.com/dolthub/go-mysql-server v0.20.1-0.20250711220615-836cad83ae20
|
||||
github.com/dolthub/go-mysql-server v0.20.1-0.20250714204345-186222f1c013
|
||||
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63
|
||||
github.com/esote/minmaxheap v1.0.0
|
||||
github.com/goccy/go-json v0.10.2
|
||||
|
||||
@@ -221,8 +221,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
|
||||
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
|
||||
github.com/dolthub/go-icu-regex v0.0.0-20250327004329-6799764f2dad h1:66ZPawHszNu37VPQckdhX1BPPVzREsGgNxQeefnlm3g=
|
||||
github.com/dolthub/go-icu-regex v0.0.0-20250327004329-6799764f2dad/go.mod h1:ylU4XjUpsMcvl/BKeRRMXSH7e7WBrPXdSLvnRJYrxEA=
|
||||
github.com/dolthub/go-mysql-server v0.20.1-0.20250711220615-836cad83ae20 h1:AH5Uo2YuyPr5duwy69DuGSwbGcmLWn0hot4aqy9uxK0=
|
||||
github.com/dolthub/go-mysql-server v0.20.1-0.20250711220615-836cad83ae20/go.mod h1:zuYoQ3keJHAvWUWMLzbP9anvR32b3sy1Fm8wB8ukNxQ=
|
||||
github.com/dolthub/go-mysql-server v0.20.1-0.20250714204345-186222f1c013 h1:0QJEo7h1MOR4eFitjtdC1DnfSZ2pzzI1VYxjsieKKCU=
|
||||
github.com/dolthub/go-mysql-server v0.20.1-0.20250714204345-186222f1c013/go.mod h1:6dT48ffh+d12GMuwuabrRaR9tK/XlNc21hiU7BVRZ5Q=
|
||||
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI=
|
||||
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q=
|
||||
github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE=
|
||||
|
||||
@@ -2070,15 +2070,15 @@ func mergeJSON(ctx context.Context, ns tree.NodeStore, base, left, right sql.JSO
|
||||
// First, deserialize each value into JSON.
|
||||
// We can only merge if the value at all three commits is a JSON object.
|
||||
|
||||
baseIsObject, err := tree.IsJsonObject(base)
|
||||
baseIsObject, err := tree.IsJsonObject(ctx, base)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
leftIsObject, err := tree.IsJsonObject(left)
|
||||
leftIsObject, err := tree.IsJsonObject(ctx, left)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
rightIsObject, err := tree.IsJsonObject(right)
|
||||
rightIsObject, err := tree.IsJsonObject(ctx, right)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
@@ -2087,7 +2087,7 @@ func mergeJSON(ctx context.Context, ns tree.NodeStore, base, left, right sql.JSO
|
||||
// At least one of the commits does not have a JSON object.
|
||||
// If both left and right have the same value, use that value.
|
||||
// But if they differ, this is an unresolvable merge conflict.
|
||||
cmp, err := types.CompareJSON(left, right)
|
||||
cmp, err := types.CompareJSON(ctx, left, right)
|
||||
if err != nil {
|
||||
return types.JSONDocument{}, true, err
|
||||
}
|
||||
@@ -2112,11 +2112,11 @@ func mergeJSON(ctx context.Context, ns tree.NodeStore, base, left, right sql.JSO
|
||||
return nil, true, err
|
||||
}
|
||||
} else {
|
||||
baseObject, err := base.ToInterface()
|
||||
baseObject, err := base.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
leftObject, err := left.ToInterface()
|
||||
leftObject, err := left.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
@@ -2130,11 +2130,11 @@ func mergeJSON(ctx context.Context, ns tree.NodeStore, base, left, right sql.JSO
|
||||
return nil, true, err
|
||||
}
|
||||
} else {
|
||||
baseObject, err := base.ToInterface()
|
||||
baseObject, err := base.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
rightObject, err := right.ToInterface()
|
||||
rightObject, err := right.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return nil, true, err
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ func (differ *ThreeWayJsonDiffer) Next(ctx context.Context) (ThreeWayJsonDiff, e
|
||||
if differ.leftCurrentDiff.From == nil {
|
||||
// Key did not exist at base, so both sides are inserts.
|
||||
// Check that they're inserting the same value.
|
||||
valueCmp, err := types.CompareJSON(differ.leftCurrentDiff.To, differ.rightCurrentDiff.To)
|
||||
valueCmp, err := types.CompareJSON(ctx, differ.leftCurrentDiff.To, differ.rightCurrentDiff.To)
|
||||
if err != nil {
|
||||
return ThreeWayJsonDiff{}, err
|
||||
}
|
||||
|
||||
@@ -453,7 +453,7 @@ func (m FkCVMeta) Clone(_ context.Context) sql.JSONWrapper {
|
||||
|
||||
var _ sql.JSONWrapper = FkCVMeta{}
|
||||
|
||||
func (m FkCVMeta) ToInterface() (interface{}, error) {
|
||||
func (m FkCVMeta) ToInterface(context.Context) (interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"Columns": m.Columns,
|
||||
"ForeignKey": m.ForeignKey,
|
||||
|
||||
@@ -58,7 +58,7 @@ func (m UniqCVMeta) Clone(_ context.Context) sql.JSONWrapper {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m UniqCVMeta) ToInterface() (interface{}, error) {
|
||||
func (m UniqCVMeta) ToInterface(context.Context) (interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"Columns": m.Columns,
|
||||
"Name": m.Name,
|
||||
@@ -157,7 +157,7 @@ func newNotNullViolationMeta(violations []string, value val.Tuple) (prolly.Const
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m NullViolationMeta) ToInterface() (interface{}, error) {
|
||||
func (m NullViolationMeta) ToInterface(context.Context) (interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"Columns": m.Columns,
|
||||
}, nil
|
||||
@@ -205,7 +205,7 @@ func (m CheckCVMeta) Unmarshall(_ *sql.Context) (val types.JSONDocument, err err
|
||||
return types.JSONDocument{Val: m}, nil
|
||||
}
|
||||
|
||||
func (m CheckCVMeta) ToInterface() (interface{}, error) {
|
||||
func (m CheckCVMeta) ToInterface(context.Context) (interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"Name": m.Name,
|
||||
"Expression": m.Expression,
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package binlogreplication
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math"
|
||||
@@ -54,8 +55,8 @@ const maxOffsetSize = uint32(65_535)
|
||||
//
|
||||
// And a third-party description is here:
|
||||
// https://lafengnan.gitbooks.io/blog/content/mysql/chapter2.html
|
||||
func encodeJsonDoc(jsonDoc sql.JSONWrapper) (buffer []byte, err error) {
|
||||
val, err := jsonDoc.ToInterface()
|
||||
func encodeJsonDoc(ctx context.Context, jsonDoc sql.JSONWrapper) (buffer []byte, err error) {
|
||||
val, err := jsonDoc.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package binlogreplication
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
@@ -125,7 +126,7 @@ func TestJsonSerialization_EncodedBytes(t *testing.T) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
var jsonDoc any
|
||||
require.NoError(t, json.Unmarshal([]byte(test.json), &jsonDoc))
|
||||
encoded, err := encodeJsonDoc(gmstypes.JSONDocument{Val: jsonDoc})
|
||||
encoded, err := encodeJsonDoc(context.Background(), gmstypes.JSONDocument{Val: jsonDoc})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, test.expected, encoded)
|
||||
})
|
||||
@@ -274,7 +275,7 @@ func TestJsonSerialization_VitessRoundTrip(t *testing.T) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
var jsonDoc any
|
||||
require.NoError(t, json.Unmarshal([]byte(test.json), &jsonDoc))
|
||||
encoded, err := encodeJsonDoc(gmstypes.JSONDocument{Val: jsonDoc})
|
||||
encoded, err := encodeJsonDoc(context.Background(), gmstypes.JSONDocument{Val: jsonDoc})
|
||||
|
||||
if test.expectedErr != "" {
|
||||
require.Equal(t, test.expectedErr, err.Error())
|
||||
|
||||
@@ -771,7 +771,7 @@ func (j jsonSerializer) serialize(ctx *sql.Context, typ sql.Type, descriptor val
|
||||
return nil, fmt.Errorf("unsupported JSON type: %T", json)
|
||||
}
|
||||
|
||||
jsonBuffer, err := encodeJsonDoc(jsonDoc)
|
||||
jsonBuffer, err := encodeJsonDoc(ctx, jsonDoc)
|
||||
jsonLengthBuffer := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(jsonLengthBuffer, uint32(len(jsonBuffer)))
|
||||
data = append(data, jsonLengthBuffer...)
|
||||
|
||||
@@ -150,7 +150,7 @@ func testJsonValue(t *testing.T, test jsonValueTest, setupCommon []testCommand)
|
||||
|
||||
// special logic for comparing JSONValues
|
||||
if js, ok := exp.(json.NomsJSON); ok {
|
||||
cmp, err := gmstypes.CompareJSON(js, act.(json.NomsJSON))
|
||||
cmp, err := gmstypes.CompareJSON(ctx, js, act.(json.NomsJSON))
|
||||
require.NoError(t, err)
|
||||
assert.Zero(t, cmp)
|
||||
} else {
|
||||
|
||||
@@ -46,7 +46,7 @@ func NomsJSONFromJSONValue(ctx context.Context, vrw types.ValueReadWriter, val s
|
||||
return noms, nil
|
||||
}
|
||||
|
||||
sqlVal, err := val.ToInterface()
|
||||
sqlVal, err := val.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return NomsJSON{}, err
|
||||
}
|
||||
@@ -140,13 +140,13 @@ func marshalJSONObject(ctx context.Context, vrw types.ValueReadWriter, obj map[s
|
||||
return types.NewMap(ctx, vrw, vals...)
|
||||
}
|
||||
|
||||
func (v NomsJSON) ToInterface() (interface{}, error) {
|
||||
func (v NomsJSON) ToInterface(ctx context.Context) (interface{}, error) {
|
||||
nomsVal, err := types.JSON(v).Inner()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
val, err := unmarshalJSON(context.Background(), nomsVal)
|
||||
val, err := unmarshalJSON(ctx, nomsVal)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ func TestJSONCompare(t *testing.T) {
|
||||
name := fmt.Sprintf("%v_%v__%d", test.left, test.right, test.cmp)
|
||||
t.Run(name, func(t *testing.T) {
|
||||
left, right := MustNomsJSON(test.left), MustNomsJSON(test.right)
|
||||
cmp, err := gmstypes.CompareJSON(left, right)
|
||||
cmp, err := gmstypes.CompareJSON(t.Context(), left, right)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, test.cmp, cmp)
|
||||
})
|
||||
|
||||
@@ -518,7 +518,7 @@ func loadDefaultSqlMode() (string, error) {
|
||||
}
|
||||
|
||||
func getCreatedTime(ctx *sql.Context, extraCol sql.JSONWrapper) (int64, error) {
|
||||
doc, err := extraCol.ToInterface()
|
||||
doc, err := extraCol.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -418,7 +418,7 @@ func testUpdateQuery(t *testing.T, test UpdateTest) {
|
||||
assert.Equal(t, len(expectedRows[i]), len(actualRows[i]))
|
||||
for j := 0; j < len(expectedRows[i]); j++ {
|
||||
if _, ok := actualRows[i][j].(json.NomsJSON); ok {
|
||||
cmp, err := gmstypes.CompareJSON(actualRows[i][j].(json.NomsJSON), expectedRows[i][j].(json.NomsJSON))
|
||||
cmp, err := gmstypes.CompareJSON(ctx, actualRows[i][j].(json.NomsJSON), expectedRows[i][j].(json.NomsJSON))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 0, cmp)
|
||||
} else {
|
||||
|
||||
@@ -152,7 +152,7 @@ func NewProximityMap(ns tree.NodeStore, node tree.Node, keyDesc val.TupleDesc, v
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
floats, err := sql.ConvertToVector(jsonWrapper)
|
||||
floats, err := sql.ConvertToVector(ctx, jsonWrapper)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -602,5 +602,5 @@ func getVectorFromHash(ctx context.Context, ns tree.NodeStore, h hash.Hash) ([]f
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sql.ConvertToVector(otherValue)
|
||||
return sql.ConvertToVector(ctx, otherValue)
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ func vectorFromKey(t *testing.T, ctx context.Context, ns tree.NodeStore, keyDesc
|
||||
vectorHash, _ := keyDesc.GetJSONAddr(0, key)
|
||||
jsonWrapper, err := getJsonValueFromHash(ctx, ns, vectorHash)
|
||||
require.NoError(t, err)
|
||||
floats, err := sql.ConvertToVector(jsonWrapper)
|
||||
floats, err := sql.ConvertToVector(ctx, jsonWrapper)
|
||||
require.NoError(t, err)
|
||||
return floats
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func (f ProximityFlusher) ApplyMutationsWithSerializer(
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
floats, err := sql.ConvertToVector(jsonWrapper)
|
||||
floats, err := sql.ConvertToVector(ctx, jsonWrapper)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func SerializeJsonToAddr(ctx context.Context, ns NodeStore, j sql.JSONWrapper) (
|
||||
if indexedJson, ok := j.(IndexedJsonDocument); ok {
|
||||
return indexedJson.m.Root, nil
|
||||
}
|
||||
jsonBytes, err := types.MarshallJson(j)
|
||||
jsonBytes, err := types.MarshallJson(ctx, j)
|
||||
if err != nil {
|
||||
return Node{}, err
|
||||
}
|
||||
|
||||
@@ -507,12 +507,12 @@ func runTest(t *testing.T, test jsonDiffTest) {
|
||||
if !bytes.Equal(expected.Key, actual.Key) {
|
||||
return false
|
||||
}
|
||||
cmp, err := types.CompareJSON(expected.From, actual.From)
|
||||
cmp, err := types.CompareJSON(ctx, expected.From, actual.From)
|
||||
require.NoError(t, err)
|
||||
if cmp != 0 {
|
||||
return false
|
||||
}
|
||||
cmp, err = types.CompareJSON(expected.To, actual.To)
|
||||
cmp, err = types.CompareJSON(ctx, expected.To, actual.To)
|
||||
require.NoError(t, err)
|
||||
|
||||
return cmp == 0
|
||||
|
||||
@@ -41,8 +41,7 @@ type StaticJsonMap = StaticMap[jsonLocationKey, address, *jsonLocationOrdering]
|
||||
// The |interfaceFunc| field caches the result of ToInterface in the event it gets called multiple times.
|
||||
type IndexedJsonDocument struct {
|
||||
m StaticJsonMap
|
||||
interfaceFunc func() (interface{}, error)
|
||||
ctx context.Context
|
||||
interfaceFunc func(ctx context.Context) (interface{}, error)
|
||||
}
|
||||
|
||||
var _ types.JSONBytes = IndexedJsonDocument{}
|
||||
@@ -50,6 +49,36 @@ var _ types.MutableJSON = IndexedJsonDocument{}
|
||||
var _ fmt.Stringer = IndexedJsonDocument{}
|
||||
var _ driver.Valuer = IndexedJsonDocument{}
|
||||
|
||||
func OnceContextValues[T1, T2 any](f func(ctx context.Context) (T1, T2)) func(context.Context) (T1, T2) {
|
||||
var (
|
||||
once sync.Once
|
||||
valid bool
|
||||
p any
|
||||
r1 T1
|
||||
r2 T2
|
||||
)
|
||||
g := func(ctx context.Context) {
|
||||
defer func() {
|
||||
p = recover()
|
||||
if !valid {
|
||||
panic(p)
|
||||
}
|
||||
}()
|
||||
r1, r2 = f(ctx)
|
||||
f = nil
|
||||
valid = true
|
||||
}
|
||||
return func(ctx context.Context) (T1, T2) {
|
||||
once.Do(func() {
|
||||
g(ctx)
|
||||
})
|
||||
if !valid {
|
||||
panic(p)
|
||||
}
|
||||
return r1, r2
|
||||
}
|
||||
}
|
||||
|
||||
func NewIndexedJsonDocument(ctx context.Context, root Node, ns NodeStore) IndexedJsonDocument {
|
||||
m := StaticMap[jsonLocationKey, address, *jsonLocationOrdering]{
|
||||
Root: root,
|
||||
@@ -58,10 +87,9 @@ func NewIndexedJsonDocument(ctx context.Context, root Node, ns NodeStore) Indexe
|
||||
}
|
||||
return IndexedJsonDocument{
|
||||
m: m,
|
||||
interfaceFunc: sync.OnceValues(func() (interface{}, error) {
|
||||
interfaceFunc: OnceContextValues(func(ctx context.Context) (interface{}, error) {
|
||||
return getInterfaceFromIndexedJsonMap(ctx, m)
|
||||
}),
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,16 +100,15 @@ func (i IndexedJsonDocument) Clone(ctx context.Context) sql.JSONWrapper {
|
||||
m := i.m
|
||||
return IndexedJsonDocument{
|
||||
m: m,
|
||||
interfaceFunc: sync.OnceValues(func() (interface{}, error) {
|
||||
interfaceFunc: OnceContextValues(func(ctx context.Context) (interface{}, error) {
|
||||
return getInterfaceFromIndexedJsonMap(ctx, m)
|
||||
}),
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
// ToInterface implements sql.JSONWrapper
|
||||
func (i IndexedJsonDocument) ToInterface() (interface{}, error) {
|
||||
return i.interfaceFunc()
|
||||
func (i IndexedJsonDocument) ToInterface(ctx context.Context) (interface{}, error) {
|
||||
return i.interfaceFunc(ctx)
|
||||
}
|
||||
|
||||
// getInterfaceFromIndexedJsonMap extracts the JSON document from a StaticJsonMap and converts it into an interface{}
|
||||
@@ -120,7 +147,7 @@ func tryWithFallback(
|
||||
sqlCtx.GetLogger().Warn(err)
|
||||
}
|
||||
}
|
||||
v, err := i.ToInterface()
|
||||
v, err := i.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -251,7 +278,7 @@ func (i IndexedJsonDocument) insertIntoCursor(ctx context.Context, keyPath jsonL
|
||||
return i, false, err
|
||||
}
|
||||
|
||||
insertedValueBytes, err := types.MarshallJson(val)
|
||||
insertedValueBytes, err := types.MarshallJson(ctx, val)
|
||||
if err != nil {
|
||||
return IndexedJsonDocument{}, false, err
|
||||
}
|
||||
@@ -291,7 +318,7 @@ func (i IndexedJsonDocument) insertIntoCursor(ctx context.Context, keyPath jsonL
|
||||
return i, false, nil
|
||||
}
|
||||
|
||||
insertedValueBytes, err := types.MarshallJson(val)
|
||||
insertedValueBytes, err := types.MarshallJson(ctx, val)
|
||||
if err != nil {
|
||||
return IndexedJsonDocument{}, false, err
|
||||
}
|
||||
@@ -528,7 +555,7 @@ func (i IndexedJsonDocument) replaceIntoCursor(ctx context.Context, keyPath json
|
||||
return IndexedJsonDocument{}, false, err
|
||||
}
|
||||
|
||||
insertedValueBytes, err := types.MarshallJson(val)
|
||||
insertedValueBytes, err := types.MarshallJson(ctx, val)
|
||||
if err != nil {
|
||||
return IndexedJsonDocument{}, false, err
|
||||
}
|
||||
@@ -548,31 +575,32 @@ func (i IndexedJsonDocument) replaceIntoCursor(ctx context.Context, keyPath json
|
||||
}
|
||||
|
||||
// ArrayInsert is not yet implemented, so we call it on a types.JSONDocument instead.
|
||||
func (i IndexedJsonDocument) ArrayInsert(path string, val sql.JSONWrapper) (types.MutableJSON, bool, error) {
|
||||
v, err := i.ToInterface()
|
||||
func (i IndexedJsonDocument) ArrayInsert(ctx context.Context, path string, val sql.JSONWrapper) (types.MutableJSON, bool, error) {
|
||||
v, err := i.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return types.JSONDocument{Val: v}.ArrayInsert(path, val)
|
||||
return types.JSONDocument{Val: v}.ArrayInsert(ctx, path, val)
|
||||
}
|
||||
|
||||
// ArrayAppend is not yet implemented, so we call it on a types.JSONDocument instead.
|
||||
func (i IndexedJsonDocument) ArrayAppend(path string, val sql.JSONWrapper) (types.MutableJSON, bool, error) {
|
||||
v, err := i.ToInterface()
|
||||
func (i IndexedJsonDocument) ArrayAppend(ctx context.Context, path string, val sql.JSONWrapper) (types.MutableJSON, bool, error) {
|
||||
v, err := i.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return types.JSONDocument{Val: v}.ArrayAppend(path, val)
|
||||
return types.JSONDocument{Val: v}.ArrayAppend(ctx, path, val)
|
||||
}
|
||||
|
||||
// Value implements driver.Valuer for interoperability with other go libraries
|
||||
func (i IndexedJsonDocument) Value() (driver.Value, error) {
|
||||
return types.JsonToMySqlString(i)
|
||||
// :-/.
|
||||
return types.JsonToMySqlString(context.TODO(), i)
|
||||
}
|
||||
|
||||
// String implements the fmt.Stringer interface.
|
||||
func (i IndexedJsonDocument) String() string {
|
||||
s, err := types.JsonToMySqlString(i)
|
||||
s, err := types.JsonToMySqlString(context.TODO(), i)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("error while stringifying JSON: %s", err.Error())
|
||||
}
|
||||
@@ -580,9 +608,8 @@ func (i IndexedJsonDocument) String() string {
|
||||
}
|
||||
|
||||
// GetBytes implements the JSONBytes interface.
|
||||
func (i IndexedJsonDocument) GetBytes() (bytes []byte, err error) {
|
||||
// TODO: Add context parameter to JSONBytes.GetBytes
|
||||
return getBytesFromIndexedJsonMap(i.ctx, i.m)
|
||||
func (i IndexedJsonDocument) GetBytes(ctx context.Context) (bytes []byte, err error) {
|
||||
return getBytesFromIndexedJsonMap(ctx, i.m)
|
||||
}
|
||||
|
||||
func (i IndexedJsonDocument) getFirstCharacter(ctx context.Context) (byte, error) {
|
||||
@@ -601,22 +628,22 @@ func (i IndexedJsonDocument) getFirstCharacter(ctx context.Context) (byte, error
|
||||
return firstCharacter, nil
|
||||
}
|
||||
|
||||
func (i IndexedJsonDocument) getTypeCategory() (jsonTypeCategory, error) {
|
||||
firstCharacter, err := i.getFirstCharacter(i.ctx)
|
||||
func (i IndexedJsonDocument) getTypeCategory(ctx context.Context) (jsonTypeCategory, error) {
|
||||
firstCharacter, err := i.getFirstCharacter(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return getTypeCategoryFromFirstCharacter(firstCharacter), nil
|
||||
}
|
||||
|
||||
func GetTypeCategory(wrapper sql.JSONWrapper) (jsonTypeCategory, error) {
|
||||
func GetTypeCategory(ctx context.Context, wrapper sql.JSONWrapper) (jsonTypeCategory, error) {
|
||||
switch doc := wrapper.(type) {
|
||||
case IndexedJsonDocument:
|
||||
return doc.getTypeCategory()
|
||||
return doc.getTypeCategory(ctx)
|
||||
case *types.LazyJSONDocument:
|
||||
return getTypeCategoryFromFirstCharacter(doc.Bytes[0]), nil
|
||||
default:
|
||||
val, err := doc.ToInterface()
|
||||
val, err := doc.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -638,7 +665,7 @@ func (i IndexedJsonDocument) Type(ctx context.Context) (string, error) {
|
||||
return "ARRAY", nil
|
||||
}
|
||||
// At this point the value must be a scalar, so it's okay to just load the whole thing.
|
||||
val, err := i.ToInterface()
|
||||
val, err := i.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -646,29 +673,29 @@ func (i IndexedJsonDocument) Type(ctx context.Context) (string, error) {
|
||||
}
|
||||
|
||||
// Compare implements types.ComparableJson
|
||||
func (i IndexedJsonDocument) Compare(other interface{}) (int, error) {
|
||||
thisTypeCategory, err := i.getTypeCategory()
|
||||
func (i IndexedJsonDocument) Compare(ctx context.Context, other interface{}) (int, error) {
|
||||
thisTypeCategory, err := i.getTypeCategory(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
otherIndexedDocument, ok := other.(IndexedJsonDocument)
|
||||
if !ok {
|
||||
val, err := i.ToInterface()
|
||||
val, err := i.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
otherVal := other
|
||||
if otherWrapper, ok := other.(sql.JSONWrapper); ok {
|
||||
otherVal, err = otherWrapper.ToInterface()
|
||||
otherVal, err = otherWrapper.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return types.CompareJSON(val, otherVal)
|
||||
return types.CompareJSON(ctx, val, otherVal)
|
||||
}
|
||||
|
||||
otherTypeCategory, err := otherIndexedDocument.getTypeCategory()
|
||||
otherTypeCategory, err := otherIndexedDocument.getTypeCategory(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -684,11 +711,11 @@ func (i IndexedJsonDocument) Compare(other interface{}) (int, error) {
|
||||
case jsonTypeArray, jsonTypeObject:
|
||||
// To compare two values that are both arrays or both objects, we must locate the first location
|
||||
// where they differ.
|
||||
jsonDiffer, err := NewIndexedJsonDiffer(i.ctx, i, otherIndexedDocument)
|
||||
jsonDiffer, err := NewIndexedJsonDiffer(ctx, i, otherIndexedDocument)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
firstDiff, err := jsonDiffer.Next(i.ctx)
|
||||
firstDiff, err := jsonDiffer.Next(ctx)
|
||||
if err == io.EOF {
|
||||
// The two documents have no differences.
|
||||
return 0, nil
|
||||
@@ -705,15 +732,15 @@ func (i IndexedJsonDocument) Compare(other interface{}) (int, error) {
|
||||
case ModifiedDiff:
|
||||
// Since both modified values have already been loaded into memory,
|
||||
// We can just compare them.
|
||||
return types.JSON.Compare(i.ctx, firstDiff.From, firstDiff.To)
|
||||
return types.JSON.Compare(ctx, firstDiff.From, firstDiff.To)
|
||||
default:
|
||||
panic("Impossible diff type")
|
||||
}
|
||||
default:
|
||||
val, err := i.ToInterface()
|
||||
val, err := i.ToInterface(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return types.CompareJSON(val, other)
|
||||
return types.CompareJSON(ctx, val, other)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ func TestIndexedJsonDocument_Insert(t *testing.T) {
|
||||
require.NotEqual(t, newBoundary, previousBoundary)
|
||||
|
||||
// test that new value is valid by converting it to interface{}
|
||||
v, err := newDoc.ToInterface()
|
||||
v, err := newDoc.ToInterface(ctx)
|
||||
require.NoError(t, err)
|
||||
newJsonDocument := types.JSONDocument{Val: v}
|
||||
|
||||
@@ -211,7 +211,7 @@ func TestIndexedJsonDocument_Remove(t *testing.T) {
|
||||
require.True(t, changed)
|
||||
|
||||
// test that new value is valid by calling ToInterface
|
||||
v, err := newDoc.ToInterface()
|
||||
v, err := newDoc.ToInterface(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
// If the removed value was an object key, check that the key no longer exists.
|
||||
@@ -225,7 +225,7 @@ func TestIndexedJsonDocument_Remove(t *testing.T) {
|
||||
getParentArray := func(doc IndexedJsonDocument) []interface{} {
|
||||
arrayWrapper, err := doc.lookupByLocation(ctx, parentLocation)
|
||||
require.NoError(t, err)
|
||||
arrayInterface, err := arrayWrapper.ToInterface()
|
||||
arrayInterface, err := arrayWrapper.ToInterface(ctx)
|
||||
require.NoError(t, err)
|
||||
require.IsType(t, []interface{}{}, arrayInterface)
|
||||
return arrayInterface.([]interface{})
|
||||
@@ -323,7 +323,7 @@ func TestJsonCompare(t *testing.T) {
|
||||
rightJSON, inRange, err := types.JSON.Convert(ctx, right)
|
||||
require.NoError(t, err)
|
||||
require.True(t, bool(inRange))
|
||||
rightInterface, err := rightJSON.(sql.JSONWrapper).ToInterface()
|
||||
rightInterface, err := rightJSON.(sql.JSONWrapper).ToInterface(ctx)
|
||||
require.NoError(t, err)
|
||||
right = types.JSONDocument{Val: rightInterface}
|
||||
}
|
||||
@@ -421,19 +421,19 @@ func TestJsonCompare(t *testing.T) {
|
||||
{
|
||||
Name: "inserting into end of array makes it greater",
|
||||
Left: largeArray,
|
||||
Right: noError(largeArray.ArrayAppend("$", types.MustJSON("1"))),
|
||||
Right: noError(largeArray.ArrayAppend(ctx, "$", types.MustJSON("1"))),
|
||||
Cmp: -1,
|
||||
},
|
||||
{
|
||||
Name: "inserting high value into beginning of array makes it greater",
|
||||
Left: largeArray,
|
||||
Right: noError(largeArray.ArrayInsert("$[0]", types.MustJSON("true"))),
|
||||
Right: noError(largeArray.ArrayInsert(ctx, "$[0]", types.MustJSON("true"))),
|
||||
Cmp: -1,
|
||||
},
|
||||
{
|
||||
Name: "inserting low value into beginning of array makes it less",
|
||||
Left: largeArray,
|
||||
Right: noError(largeArray.ArrayInsert("$[0]", types.MustJSON("1"))),
|
||||
Right: noError(largeArray.ArrayInsert(ctx, "$[0]", types.MustJSON("1"))),
|
||||
Cmp: 1,
|
||||
},
|
||||
{
|
||||
@@ -468,7 +468,7 @@ func TestIndexedJsonDocument_CreateLargeStringValues(t *testing.T) {
|
||||
root, err := SerializeJsonToAddr(ctx, ns, doc.(sql.JSONWrapper))
|
||||
require.NoError(t, err)
|
||||
indexedDoc, err := NewJSONDoc(root.HashOf(), ns).ToIndexedJSONDocument(ctx)
|
||||
lookup, err := types.LookupJSONValue(indexedDoc, "$.key")
|
||||
lookup, err := types.LookupJSONValue(ctx, indexedDoc, "$.key")
|
||||
require.NoError(t, err)
|
||||
extractedValue, _, err := types.LongText.Convert(ctx, lookup)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package tree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/dolthub/go-mysql-server/sql"
|
||||
@@ -69,8 +70,8 @@ func getTypeCategoryFromFirstCharacter(c byte) jsonTypeCategory {
|
||||
}
|
||||
}
|
||||
|
||||
func IsJsonObject(json sql.JSONWrapper) (bool, error) {
|
||||
valType, err := GetTypeCategory(json)
|
||||
func IsJsonObject(ctx context.Context, json sql.JSONWrapper) (bool, error) {
|
||||
valType, err := GetTypeCategory(ctx, json)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
@@ -436,7 +436,7 @@ func getJSONAddrHash(ctx context.Context, ns NodeStore, v interface{}) (hash.Has
|
||||
}
|
||||
|
||||
func serializeJsonToBlob(ctx context.Context, ns NodeStore, j sql.JSONWrapper) (Node, hash.Hash, error) {
|
||||
buf, err := types.MarshallJson(j)
|
||||
buf, err := types.MarshallJson(ctx, j)
|
||||
if err != nil {
|
||||
return Node{}, hash.Hash{}, err
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ func testRoundTripProllyFields(t *testing.T, test prollyFieldTest) {
|
||||
require.NoError(t, err)
|
||||
|
||||
if js, ok := v.(sql.JSONWrapper); ok {
|
||||
v, err = js.ToInterface()
|
||||
v, err = js.ToInterface(ctx)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -231,7 +231,7 @@ func testRoundTripProllyFields(t *testing.T, test prollyFieldTest) {
|
||||
assert.NoError(t, err)
|
||||
|
||||
if js, ok := expectedValue.(sql.JSONWrapper); ok {
|
||||
expectedValue, err = js.ToInterface()
|
||||
expectedValue, err = js.ToInterface(ctx)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -191,7 +191,7 @@ func (t ProximityMap[K, V, O]) GetClosest(ctx context.Context, query interface{}
|
||||
return nil
|
||||
}
|
||||
|
||||
queryVector, err := sql.ConvertToVector(query)
|
||||
queryVector, err := sql.ConvertToVector(ctx, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -275,5 +275,5 @@ func getVectorFromHash(ctx context.Context, ns NodeStore, h hash.Hash) ([]float6
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sql.ConvertToVector(otherValue)
|
||||
return sql.ConvertToVector(ctx, otherValue)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user