Skip to content

Commit 25a6c16

Browse files
committed
sql: prohibit jsonpath as element in composite type
This commit ties a few loose ends around the jsonpath data type: - it prohibits creation of a composite type with jsonpath as an element (it should be fine, but we're doing this out of caution until we have encoding for jsonpath) - it adds a validation for each element of a composite type for whether it can be used as a column type (under an assumption that if a type cannot be used as a column type, then any composite types consisting of that type cannot either) - it also adds the issue number to the errors. Release note: None
1 parent 50027f4 commit 25a6c16

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

pkg/sql/catalog/colinfo/col_type_info.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func ValidateColumnDefType(ctx context.Context, st *cluster.Settings, t *types.T
9797
return unimplemented.NewWithIssueDetailf(23468, t.String(),
9898
"arrays of JSON unsupported as column type")
9999
}
100+
if t.ArrayContents().Family() == types.JsonpathFamily {
101+
return unimplemented.NewWithIssueDetailf(144910, t.String(),
102+
"arrays of jsonpath unsupported as column type")
103+
}
100104
if err := types.CheckArrayElementType(t.ArrayContents()); err != nil {
101105
return err
102106
}
@@ -109,13 +113,22 @@ func ValidateColumnDefType(ctx context.Context, st *cluster.Settings, t *types.T
109113
types.TSQueryFamily, types.TSVectorFamily, types.PGLSNFamily, types.PGVectorFamily, types.RefCursorFamily:
110114
// These types are OK.
111115

116+
case types.JsonpathFamily:
117+
return unimplemented.NewWithIssueDetailf(144910, t.String(),
118+
"jsonpath unsupported as column type")
119+
112120
case types.TupleFamily:
113121
if !t.UserDefined() {
114122
return pgerror.New(pgcode.InvalidTableDefinition, "cannot use anonymous record type as table column")
115123
}
116124
if t.TypeMeta.ImplicitRecordType {
117125
return unimplemented.NewWithIssue(70099, "cannot use table record type as table column")
118126
}
127+
for _, typ := range t.TupleContents() {
128+
if err := ValidateColumnDefType(ctx, st, typ); err != nil {
129+
return err
130+
}
131+
}
119132

120133
default:
121134
return pgerror.Newf(pgcode.InvalidTableDefinition,

pkg/sql/create_type.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
2020
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
2121
"github.com/cockroachdb/cockroach/pkg/sql/enum"
22+
"github.com/cockroachdb/cockroach/pkg/sql/oidext"
2223
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
2324
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
2425
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice"
@@ -400,6 +401,10 @@ func CreateEnumTypeDesc(
400401
}).BuildCreatedMutableType(), nil
401402
}
402403

404+
var jsonpathInCompositeErr = unimplemented.NewWithIssueDetailf(
405+
144910, "", "jsonpath cannot be used in a composite type",
406+
)
407+
403408
// createCompositeTypeDesc creates a new composite type descriptor.
404409
func createCompositeTypeDesc(
405410
params runParams,
@@ -426,6 +431,10 @@ func createCompositeTypeDesc(
426431
if typ.Identical(types.Trigger) {
427432
return nil, tree.CannotAcceptTriggerErr
428433
}
434+
if typ.Oid() == oidext.T_jsonpath || typ.Oid() == oidext.T__jsonpath {
435+
// TODO(#144910): this is unsupported for now, out of caution.
436+
return nil, jsonpathInCompositeErr
437+
}
429438
if err = tree.CheckUnsupportedType(params.ctx, &params.p.semaCtx, typ); err != nil {
430439
return nil, err
431440
}

pkg/sql/logictest/testdata/logic_test/jsonpath

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ SELECT '$.a'::JSONPATH
1010
----
1111
$."a"
1212

13-
statement error pq: value type jsonpath cannot be used for table columns
13+
statement error pq: unimplemented: jsonpath unsupported as column type
1414
CREATE TABLE a (j JSONPATH)
1515

16-
statement error pq: value type jsonpath cannot be used for table columns
16+
statement error pq: unimplemented: arrays of jsonpath unsupported as column type
1717
CREATE TABLE a (j JSONPATH[])
1818

19+
statement error pq: unimplemented: jsonpath cannot be used in a composite type
20+
CREATE TYPE typ AS (j JSONPATH);
21+
1922
query T
2023
SELECT '$'::JSONPATH
2124
----

pkg/sql/sem/tree/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ type CreateType struct {
370370
Variety CreateTypeVariety
371371
// EnumLabels is set when this represents a CREATE TYPE ... AS ENUM statement.
372372
EnumLabels EnumValueList
373-
// CompositeTypeList is set when this repesnets a CREATE TYPE ... AS ( )
373+
// CompositeTypeList is set when this represents a CREATE TYPE ... AS ( )
374374
// statement.
375375
CompositeTypeList []CompositeTypeElem
376376
// IfNotExists is true if IF NOT EXISTS was requested.

0 commit comments

Comments
 (0)