Skip to content

Commit 3cf7434

Browse files
craig[bot]normanchenn
andcommitted
Merge #140204
140204: sql: add jsonpath type r=normanchenn a=normanchenn As part of work to implement jsonpath queries, this PR implements the`jsonpath` type within CockroachDB. There is no parsing or evaluation added, and the `jsonpath` will currently accept any non-empty string. Indexes are not currently supported for `jsonpath` columns. Part of: #22513 Release note (sql change): Adding jsonpath type, without parsing or evaluation. Currently accepts any non-empty string. Co-authored-by: Norman Chen <[email protected]>
2 parents b9c6bb6 + 2dda5fd commit 3cf7434

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+835
-45
lines changed

docs/generated/sql/operators.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@
479479
<tr><td><a href="interval.html">interval</a> <code>IS NOT DISTINCT FROM</code> <a href="interval.html">interval</a></td><td><a href="bool.html">bool</a></td></tr>
480480
<tr><td><a href="interval.html">interval[]</a> <code>IS NOT DISTINCT FROM</code> <a href="interval.html">interval[]</a></td><td><a href="bool.html">bool</a></td></tr>
481481
<tr><td>jsonb <code>IS NOT DISTINCT FROM</code> jsonb</td><td><a href="bool.html">bool</a></td></tr>
482+
<tr><td>jsonpath <code>IS NOT DISTINCT FROM</code> jsonpath</td><td><a href="bool.html">bool</a></td></tr>
482483
<tr><td>oid <code>IS NOT DISTINCT FROM</code> <a href="int.html">int</a></td><td><a href="bool.html">bool</a></td></tr>
483484
<tr><td>oid <code>IS NOT DISTINCT FROM</code> oid</td><td><a href="bool.html">bool</a></td></tr>
484485
<tr><td>pg_lsn <code>IS NOT DISTINCT FROM</code> pg_lsn</td><td><a href="bool.html">bool</a></td></tr>

pkg/ccl/changefeedccl/avro/avro.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@ func typeToSchema(typ *types.T) (*SchemaField, error) {
644644
return tree.ParseDJSON(x.(string))
645645
},
646646
)
647+
// case types.JsonpathFamily:
648+
// We do not support Jsonpath yet, but should be able to support it easily.
647649
case types.TSQueryFamily:
648650
setNullable(
649651
SchemaTypeString,

pkg/ccl/changefeedccl/avro/avro_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ func TestAvroSchema(t *testing.T) {
318318
case types.AnyFamily, types.OidFamily, types.TupleFamily:
319319
// These aren't expected to be needed for changefeeds.
320320
return true
321-
case types.PGVectorFamily:
322-
// We don't support PGVector in Avro yet.
321+
case types.PGVectorFamily, types.JsonpathFamily:
322+
// We don't support PGVector and Jsonpath in Avro yet.
323323
return true
324324
case types.ArrayFamily:
325325
if !randgen.IsAllowedForArray(typ.ArrayContents()) {

pkg/ccl/logictestccl/tests/3node-tenant/generated_test.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ccl/logictestccl/tests/local-read-committed/generated_test.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ccl/logictestccl/tests/local-repeatable-read/generated_test.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql/catalog/colinfo/col_type_info.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,19 @@ func ValidateColumnDefType(ctx context.Context, st *cluster.Settings, t *types.T
129129
// column in a regular FORWARD index.
130130
func ColumnTypeIsIndexable(t *types.T) bool {
131131
// NB: .IsAmbiguous checks the content type of array types.
132-
if t.IsAmbiguous() || t.Family() == types.TupleFamily || t.Family() == types.RefCursorFamily {
132+
if t.IsAmbiguous() {
133+
return false
134+
}
135+
136+
switch t.Family() {
137+
case types.TupleFamily, types.RefCursorFamily, types.JsonpathFamily:
133138
return false
134139
}
135140

136141
// If the type is an array, check its content type as well.
137142
if unwrapped := t.ArrayContents(); unwrapped != nil {
138-
if unwrapped.Family() == types.TupleFamily || unwrapped.Family() == types.RefCursorFamily {
143+
switch unwrapped.Family() {
144+
case types.TupleFamily, types.RefCursorFamily, types.JsonpathFamily:
139145
return false
140146
}
141147
}
@@ -150,7 +156,12 @@ func ColumnTypeIsIndexable(t *types.T) bool {
150156
func ColumnTypeIsInvertedIndexable(t *types.T) bool {
151157
switch t.Family() {
152158
case types.ArrayFamily:
153-
return t.ArrayContents().Family() != types.RefCursorFamily
159+
switch t.ArrayContents().Family() {
160+
case types.RefCursorFamily, types.JsonpathFamily:
161+
return false
162+
default:
163+
return true
164+
}
154165
case types.JsonFamily, types.StringFamily:
155166
return true
156167
}

pkg/sql/catalog/colinfo/column_type_properties.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ func CanHaveCompositeKeyEncoding(typ *types.T) bool {
8282
types.VoidFamily,
8383
types.EncodedKeyFamily,
8484
types.TSQueryFamily,
85-
types.TSVectorFamily:
85+
types.TSVectorFamily,
86+
types.JsonpathFamily:
8687
return false
8788
case types.UnknownFamily,
8889
types.AnyFamily:

pkg/sql/distsql_physical_planner.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ func (v *distSQLExprCheckVisitor) VisitPre(expr tree.Expr) (recurse bool, newExp
357357
v.err = newQueryNotSupportedErrorf("tuple %s cannot be executed with distsql", t)
358358
return false, expr
359359
}
360+
case *tree.DJsonpath:
361+
// TODO(normanchenn): We currently do not have an encoding for jsonpath
362+
// thus do not support it within distsql
363+
v.err = newQueryNotSupportedErrorf("jsonpath %s cannot be executed with distsql", t)
364+
return false, expr
360365
}
361366
return true, expr
362367
}

pkg/sql/exec_util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,6 +2154,7 @@ func checkResultType(typ *types.T, fmtCode pgwirebase.FormatCode) error {
21542154
case types.TSVectorFamily:
21552155
case types.IntervalFamily:
21562156
case types.JsonFamily:
2157+
case types.JsonpathFamily:
21572158
case types.UuidFamily:
21582159
case types.INetFamily:
21592160
case types.OidFamily:

pkg/sql/logictest/testdata/logic_test/grant_table

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ test pg_catalog jsonb type
236236
test pg_catalog jsonb[] type admin ALL false
237237
test pg_catalog jsonb[] type public USAGE false
238238
test pg_catalog jsonb[] type root ALL false
239+
test pg_catalog jsonpath type admin ALL false
240+
test pg_catalog jsonpath type public USAGE false
241+
test pg_catalog jsonpath type root ALL false
242+
test pg_catalog jsonpath[] type admin ALL false
243+
test pg_catalog jsonpath[] type public USAGE false
244+
test pg_catalog jsonpath[] type root ALL false
239245
test pg_catalog name type admin ALL false
240246
test pg_catalog name type public USAGE false
241247
test pg_catalog name type root ALL false
@@ -607,6 +613,10 @@ test pg_catalog jsonb type admin ALL
607613
test pg_catalog jsonb type root ALL false
608614
test pg_catalog jsonb[] type admin ALL false
609615
test pg_catalog jsonb[] type root ALL false
616+
test pg_catalog jsonpath type admin ALL false
617+
test pg_catalog jsonpath type root ALL false
618+
test pg_catalog jsonpath[] type admin ALL false
619+
test pg_catalog jsonpath[] type root ALL false
610620
test pg_catalog name type admin ALL false
611621
test pg_catalog name type root ALL false
612622
test pg_catalog name[] type admin ALL false
@@ -853,6 +863,10 @@ a pg_catalog jsonb type admin ALL
853863
a pg_catalog jsonb type root ALL false
854864
a pg_catalog jsonb[] type admin ALL false
855865
a pg_catalog jsonb[] type root ALL false
866+
a pg_catalog jsonpath type admin ALL false
867+
a pg_catalog jsonpath type root ALL false
868+
a pg_catalog jsonpath[] type admin ALL false
869+
a pg_catalog jsonpath[] type root ALL false
856870
a pg_catalog name type admin ALL false
857871
a pg_catalog name type root ALL false
858872
a pg_catalog name[] type admin ALL false
@@ -1037,6 +1051,10 @@ defaultdb pg_catalog jsonb type admin ALL
10371051
defaultdb pg_catalog jsonb type root ALL false
10381052
defaultdb pg_catalog jsonb[] type admin ALL false
10391053
defaultdb pg_catalog jsonb[] type root ALL false
1054+
defaultdb pg_catalog jsonpath type admin ALL false
1055+
defaultdb pg_catalog jsonpath type root ALL false
1056+
defaultdb pg_catalog jsonpath[] type admin ALL false
1057+
defaultdb pg_catalog jsonpath[] type root ALL false
10401058
defaultdb pg_catalog name type admin ALL false
10411059
defaultdb pg_catalog name type root ALL false
10421060
defaultdb pg_catalog name[] type admin ALL false
@@ -1221,6 +1239,10 @@ postgres pg_catalog jsonb type admin ALL
12211239
postgres pg_catalog jsonb type root ALL false
12221240
postgres pg_catalog jsonb[] type admin ALL false
12231241
postgres pg_catalog jsonb[] type root ALL false
1242+
postgres pg_catalog jsonpath type admin ALL false
1243+
postgres pg_catalog jsonpath type root ALL false
1244+
postgres pg_catalog jsonpath[] type admin ALL false
1245+
postgres pg_catalog jsonpath[] type root ALL false
12241246
postgres pg_catalog name type admin ALL false
12251247
postgres pg_catalog name type root ALL false
12261248
postgres pg_catalog name[] type admin ALL false
@@ -1413,6 +1435,10 @@ test pg_catalog jsonb type admin ALL
14131435
test pg_catalog jsonb type root ALL false
14141436
test pg_catalog jsonb[] type admin ALL false
14151437
test pg_catalog jsonb[] type root ALL false
1438+
test pg_catalog jsonpath type admin ALL false
1439+
test pg_catalog jsonpath type root ALL false
1440+
test pg_catalog jsonpath[] type admin ALL false
1441+
test pg_catalog jsonpath[] type root ALL false
14161442
test pg_catalog name type admin ALL false
14171443
test pg_catalog name type root ALL false
14181444
test pg_catalog name[] type admin ALL false
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# LogicTest: !local-mixed-24.3 !local-mixed-25.1
2+
3+
query T
4+
SELECT pg_typeof(JSONPATH '$.a')
5+
----
6+
jsonpath
7+
8+
query T
9+
SELECT '$.a'::JSONPATH
10+
----
11+
$.a
12+
13+
statement error pq: value type jsonpath cannot be used for table columns
14+
CREATE TABLE a (j JSONPATH)
15+
16+
statement error pq: value type jsonpath cannot be used for table columns
17+
CREATE TABLE a (j JSONPATH[])
18+
19+
query T
20+
SELECT '$'::JSONPATH
21+
----
22+
$
23+
24+
query T
25+
SELECT '$.*'::JSONPATH
26+
----
27+
$.*
28+
29+
query T
30+
SELECT 'strict $'::JSONPATH
31+
----
32+
strict $
33+
34+
query T
35+
SELECT 'lax $'::JSONPATH
36+
----
37+
lax $
38+
39+
query T
40+
SELECT '$.1a[*]'::JSONPATH
41+
----
42+
$.1a[*]
43+
44+
query T
45+
SELECT '$.a1[*]'::JSONPATH
46+
----
47+
$.a1[*]
48+
49+
query T
50+
SELECT '$.a[*] ? (@.b == 1 && @.c != 1)'::JSONPATH
51+
----
52+
$.a[*] ? (@.b == 1 && @.c != 1)
53+
54+
query T
55+
SELECT '$.a[*] ? (@.b != 1)'::JSONPATH
56+
----
57+
$.a[*] ? (@.b != 1)
58+
59+
query T
60+
SELECT '$.a[*] ? (@.b < 1)'::JSONPATH
61+
----
62+
$.a[*] ? (@.b < 1)
63+
64+
query T
65+
SELECT '$.a[*] ? (@.b <= 1)'::JSONPATH
66+
----
67+
$.a[*] ? (@.b <= 1)
68+
69+
query T
70+
SELECT '$.a[*] ? (@.b > 1)'::JSONPATH
71+
----
72+
$.a[*] ? (@.b > 1)
73+
74+
query T
75+
SELECT '$.a[*] ? (@.b >= 1)'::JSONPATH
76+
----
77+
$.a[*] ? (@.b >= 1)
78+
79+
query T
80+
SELECT '$.a ? (@.b == 1).c ? (@.d == 2)'::JSONPATH
81+
----
82+
$.a ? (@.b == 1).c ? (@.d == 2)
83+
84+
query T
85+
SELECT '$.a?(@.b==1).c?(@.d==2)'::JSONPATH
86+
----
87+
$.a?(@.b==1).c?(@.d==2)
88+
89+
query T
90+
SELECT '$ . a ? ( @ . b == 1 ) . c ? ( @ . d == 2 ) '::JSONPATH
91+
----
92+
$ . a ? ( @ . b == 1 ) . c ? ( @ . d == 2 )
93+
94+
query T
95+
SELECT '$.a.type()'::JSONPATH
96+
----
97+
$.a.type()
98+
99+
query B
100+
SELECT '$'::JSONPATH IS NULL
101+
----
102+
false
103+
104+
query B
105+
SELECT '$'::JSONPATH IS NOT NULL
106+
----
107+
true
108+
109+
statement error invalid jsonpath
110+
SELECT ''::JSONPATH
111+
112+
statement error pq: unsupported comparison operator: jsonpath IS NOT DISTINCT FROM jsonpath
113+
SELECT '$'::JSONPATH IS NOT DISTINCT FROM '$'::JSONPATH
114+
115+
statement error pq: unsupported comparison operator: jsonpath IS DISTINCT FROM jsonpath
116+
SELECT '$'::JSONPATH IS DISTINCT FROM '$'::JSONPATH
117+
118+
query B
119+
SELECT '$'::JSONPATH IS NOT DISTINCT FROM NULL
120+
----
121+
false
122+
123+
query B
124+
SELECT '$'::JSONPATH IS DISTINCT FROM NULL
125+
----
126+
true
127+
128+
# statement ok
129+
# SELECT * FROM a WHERE j IS NULL
130+
131+
## Jsonpath checks that shouldn't work when the type is fully implemented
132+
133+
query T
134+
SELECT '$.'::JSONPATH
135+
----
136+
$.
137+
138+
query T
139+
SELECT 'word $'::JSONPATH
140+
----
141+
word $
142+
143+
query T
144+
SELECT '$a'::JSONPATH;
145+
----
146+
$a
147+
148+
# statement ok
149+
# INSERT INTO a VALUES ('$.something'), ('$.other.thing'), ('$.another.thing'), ('$.a[*].b.c[*]')
150+
151+
# query T rowsort
152+
# SELECT * FROM a;
153+
# ----
154+
# $.something
155+
# $.other.thing
156+
# $.another.thing
157+
# $.a[*].b.c[*]
158+
159+
# statement error pq: column j is of type jsonpath, which does not support forward indexing
160+
# CREATE TABLE b (j JSONPATH, PRIMARY KEY(j))
161+
162+
# statement error pq: column j is of type jsonpath\[\], which does not support forward indexing
163+
# CREATE TABLE c (j JSONPATH[], PRIMARY KEY(j))
164+
165+
# statement error unsupported comparison operator
166+
# SELECT * FROM a WHERE j = '$.a'

0 commit comments

Comments
 (0)