|
| 1 | +// Copyright 2026 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package _go |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/dolthub/go-mysql-server/sql" |
| 22 | + gmstypes "github.com/dolthub/go-mysql-server/sql/types" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + |
| 25 | + "github.com/dolthub/doltgresql/server/functions" |
| 26 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 27 | +) |
| 28 | + |
| 29 | +// testAnyWrapper simulates *val.ExtendedValueWrapper — a sql.AnyWrapper that wraps a |
| 30 | +// sql.JSONWrapper rather than a plain string. This is what Dolt returns when reading a |
| 31 | +// large JSONB value that was stored out-of-band with the old ExtendedAdaptiveEnc encoding |
| 32 | +// (used before JsonAdaptiveEnc was enabled for json/jsonb columns). UnwrapAny() on such a |
| 33 | +// wrapper calls the child handler's DeserializeValue, which for JSONB returns a |
| 34 | +// gmstypes.JSONDocument, not a string. |
| 35 | +type testAnyWrapper struct { |
| 36 | + inner interface{} |
| 37 | +} |
| 38 | + |
| 39 | +func (w *testAnyWrapper) UnwrapAny(_ context.Context) (interface{}, error) { return w.inner, nil } |
| 40 | +func (w *testAnyWrapper) IsExactLength() bool { return true } |
| 41 | +func (w *testAnyWrapper) MaxByteLength() int64 { return 1000 } |
| 42 | +func (w *testAnyWrapper) Compare(_ context.Context, _ interface{}) (int, bool, error) { |
| 43 | + return 0, false, nil |
| 44 | +} |
| 45 | +func (w *testAnyWrapper) Hash() interface{} { return nil } |
| 46 | + |
| 47 | +// TestJsonbOutCallableWithAnyWrapper verifies that jsonb_out_callable handles a |
| 48 | +// sql.AnyWrapper value. Large JSONB values in databases created before JsonAdaptiveEnc |
| 49 | +// was enabled (using ExtendedAdaptiveEnc instead) are returned from storage as a |
| 50 | +// *val.ExtendedValueWrapper, which implements sql.AnyWrapper. Its UnwrapAny() yields a |
| 51 | +// gmstypes.JSONDocument — so jsonb_out_callable must unwrap and then handle the document, |
| 52 | +// not panic or return an "unexpected type" error. |
| 53 | +func TestJsonbOutCallableWithAnyWrapper(t *testing.T) { |
| 54 | + ctx := sql.NewEmptyContext() |
| 55 | + |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + jsonStr string |
| 59 | + }{ |
| 60 | + {"object", `{"key": "value", "num": 42}`}, |
| 61 | + {"array", `[1, 2, 3]`}, |
| 62 | + {"nested", `{"a": {"b": [true, null]}}`}, |
| 63 | + {"string_scalar", `"hello"`}, |
| 64 | + {"number_scalar", `99`}, |
| 65 | + {"bool_scalar", `false`}, |
| 66 | + {"null_scalar", `null`}, |
| 67 | + // large_object exercises the out-of-band storage path: a document large |
| 68 | + // enough that the old ExtendedAdaptiveEnc encoding would store it off-page. |
| 69 | + {"large_object", makeLargeJSONObject(100)}, |
| 70 | + } |
| 71 | + |
| 72 | + for _, tt := range tests { |
| 73 | + t.Run(tt.name, func(t *testing.T) { |
| 74 | + jsonDoc := gmstypes.MustJSON(tt.jsonStr) |
| 75 | + |
| 76 | + result, err := functions.JsonbOutCallable(ctx, [2]*pgtypes.DoltgresType{}, &testAnyWrapper{inner: jsonDoc}) |
| 77 | + require.NoError(t, err, "jsonb_out_callable must handle sql.AnyWrapper wrapping a JSONDocument") |
| 78 | + require.NotEmpty(t, result) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// TestJsonOutCallableWithAnyWrapper is the json_out equivalent of TestJsonbOutCallableWithAnyWrapper. |
| 84 | +// json_out_callable has the same AnyWrapper gap as jsonb_out_callable. |
| 85 | +func TestJsonOutCallableWithAnyWrapper(t *testing.T) { |
| 86 | + ctx := sql.NewEmptyContext() |
| 87 | + |
| 88 | + tests := []struct { |
| 89 | + name string |
| 90 | + jsonStr string |
| 91 | + }{ |
| 92 | + {"object", `{"key": "value", "num": 42}`}, |
| 93 | + {"array", `[1, 2, 3]`}, |
| 94 | + {"nested", `{"a": {"b": [true, null]}}`}, |
| 95 | + {"string_scalar", `"hello"`}, |
| 96 | + {"number_scalar", `99`}, |
| 97 | + {"bool_scalar", `false`}, |
| 98 | + {"null_scalar", `null`}, |
| 99 | + {"large_object", makeLargeJSONObject(100)}, |
| 100 | + } |
| 101 | + |
| 102 | + for _, tt := range tests { |
| 103 | + t.Run(tt.name, func(t *testing.T) { |
| 104 | + jsonDoc := gmstypes.MustJSON(tt.jsonStr) |
| 105 | + |
| 106 | + result, err := functions.JsonOutCallable(ctx, [2]*pgtypes.DoltgresType{}, &testAnyWrapper{inner: jsonDoc}) |
| 107 | + require.NoError(t, err, "json_out_callable must handle sql.AnyWrapper wrapping a JSONDocument") |
| 108 | + require.NotEmpty(t, result) |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// TestJsonbSerializeWithAnyWrapper verifies that types.JsonB.SerializeValue handles a |
| 114 | +// sql.AnyWrapper that wraps a JSONDocument containing *apd.Decimal values. This |
| 115 | +// represents the combined Bug 1 + Bug 2 production failure path: |
| 116 | +// |
| 117 | +// 1. A large legacy JSONB value is read from Dolt → *val.ExtendedValueWrapper (AnyWrapper). |
| 118 | +// 2. The wrapper is passed to serializeTypeJsonB (e.g. during replication or export). |
| 119 | +// 3. sql.UnwrapAny extracts a gmstypes.JSONDocument. |
| 120 | +// 4. ToInterface() returns the .Val, which after the jsonValueToInterface bugfix |
| 121 | +// contains *apd.Decimal for numeric fields. |
| 122 | +// 5. ConvertToJsonDocument(*apd.Decimal) must handle that type — previously it did not. |
| 123 | +func TestJsonbSerializeWithAnyWrapper(t *testing.T) { |
| 124 | + ctx := sql.NewEmptyContext() |
| 125 | + |
| 126 | + tests := []struct { |
| 127 | + name string |
| 128 | + inner gmstypes.JSONDocument |
| 129 | + }{ |
| 130 | + { |
| 131 | + "scalar_decimal", |
| 132 | + gmstypes.JSONDocument{Val: mustDecimal("3.14")}, |
| 133 | + }, |
| 134 | + { |
| 135 | + "object_with_decimal", |
| 136 | + gmstypes.JSONDocument{Val: map[string]any{ |
| 137 | + "n": mustDecimal("42"), |
| 138 | + "s": "hello", |
| 139 | + }}, |
| 140 | + }, |
| 141 | + { |
| 142 | + "array_with_decimals", |
| 143 | + gmstypes.JSONDocument{Val: []any{mustDecimal("1"), mustDecimal("2"), mustDecimal("3")}}, |
| 144 | + }, |
| 145 | + { |
| 146 | + "nested_decimals", |
| 147 | + gmstypes.JSONDocument{Val: map[string]any{ |
| 148 | + "a": map[string]any{"b": mustDecimal("-9.9")}, |
| 149 | + }}, |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + for _, tt := range tests { |
| 154 | + t.Run(tt.name, func(t *testing.T) { |
| 155 | + wrapper := &testAnyWrapper{inner: tt.inner} |
| 156 | + _, err := pgtypes.JsonB.SerializeValue(ctx, wrapper) |
| 157 | + require.NoError(t, err, |
| 158 | + "SerializeValue must handle an AnyWrapper wrapping a JSONDocument with *apd.Decimal") |
| 159 | + }) |
| 160 | + } |
| 161 | +} |
0 commit comments