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

release-19.1: enable the decoding of single-column family columns of both json and array #36626

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/array
Original file line number Diff line number Diff line change
Expand Up @@ -1224,3 +1224,52 @@ query B
SELECT ARRAY[''] = ARRAY[] FROM (VALUES (1)) WHERE ARRAY[B''] != ARRAY[]
----
false

subtest 36477

statement ok
CREATE TABLE array_single_family (a INT PRIMARY KEY, b INT[], FAMILY fam0(a), FAMILY fam1(b))

statement ok
INSERT INTO array_single_family VALUES(0,ARRAY[])

statement ok
INSERT INTO array_single_family VALUES(1,ARRAY[1])

statement ok
INSERT INTO array_single_family VALUES(2,ARRAY[1,2])

statement ok
INSERT INTO array_single_family VALUES(3,ARRAY[1,2,NULL])

statement ok
INSERT INTO array_single_family VALUES(4,ARRAY[NULL,2,3])

statement ok
INSERT INTO array_single_family VALUES(5,ARRAY[1,NULL,3])

statement ok
INSERT INTO array_single_family VALUES(6,ARRAY[NULL::INT])

statement ok
INSERT INTO array_single_family VALUES(7,ARRAY[NULL::INT,NULL::INT])

statement ok
INSERT INTO array_single_family VALUES(8,ARRAY[NULL::INT,NULL::INT,NULL::INT])

query IT colnames
SELECT a, b FROM array_single_family ORDER BY a
----
a b
0 {}
1 {1}
2 {1,2}
3 {1,2,NULL}
4 {NULL,2,3}
5 {1,NULL,3}
6 {NULL}
7 {NULL,NULL}
8 {NULL,NULL,NULL}

statement ok
DROP TABLE array_single_family
21 changes: 21 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/json
Original file line number Diff line number Diff line change
Expand Up @@ -700,3 +700,24 @@ SELECT '{"b": [], "c": {"a": "b"}}'::JSONB - array['foo', NULL]

statement error pgcode 22P02 a path element is not an integer: foo
SELECT '{"a": {"b": ["foo"]}}'::JSONB #- ARRAY['a', 'b', 'foo']

subtest single_family_jsonb

statement ok
CREATE TABLE json_family (a INT PRIMARY KEY, b JSONB, FAMILY fam0(a), FAMILY fam1(b))

statement ok
INSERT INTO json_family VALUES(0,'{}')

statement ok
INSERT INTO json_family VALUES(1,'{"a":123,"c":"asdf"}')

query IT colnames
SELECT a, b FROM json_family ORDER BY a
----
a b
0 {}
1 {"a": 123, "c": "asdf"}

statement ok
DROP TABLE json_family
26 changes: 26 additions & 0 deletions pkg/sql/sqlbase/column_type_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,24 @@ func UnmarshalColumnValue(a *DatumAlloc, typ ColumnType, value roachpb.Value) (t
return nil, err
}
return a.NewDOid(tree.MakeDOid(tree.DInt(v))), nil
case ColumnType_ARRAY:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
elementType := columnSemanticTypeToDatumType(&ColumnType{}, *typ.ArrayContents)
datum, _, err := decodeArrayNoMarshalColumnValue(a, elementType, v)
return datum, err
case ColumnType_JSONB:
v, err := value.GetBytes()
if err != nil {
return nil, err
}
_, jsonDatum, err := json.DecodeJSON(v)
if err != nil {
return nil, err
}
return tree.NewDJSON(jsonDatum), nil
default:
return nil, errors.Errorf("unsupported column type: %s", typ.SemanticType)
}
Expand Down Expand Up @@ -922,6 +940,14 @@ func decodeArray(a *DatumAlloc, elementType types.T, b []byte) (tree.Datum, []by
if err != nil {
return nil, b, err
}
return decodeArrayNoMarshalColumnValue(a, elementType, b)
}

// decodeArrayNoMarshalColumnValue skips the step where the MarshalColumnValue
// is stripped from the bytes. This is required for single-column family arrays.
func decodeArrayNoMarshalColumnValue(
a *DatumAlloc, elementType types.T, b []byte,
) (tree.Datum, []byte, error) {
header, b, err := decodeArrayHeader(b)
if err != nil {
return nil, b, err
Expand Down