Skip to content

Commit 994f5d2

Browse files
committed
builtins: change json_build_objects/array() to accept types.Any
Previously, json_build_objects() and json_build_array() (and their binary variants) required that all arguments have the same type. This patch removes that requirement so that arguments can be of any type. Epic: none Release note (sql change): json_build_objects(), jsonb_build_objects(), json_build_array(), jsonb_build_array() no longer require that all arguments have the same type.
1 parent 91ce49a commit 994f5d2

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

docs/generated/sql/functions.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,9 +1180,9 @@ available replica will error.</p>
11801180
</span></td><td>Immutable</td></tr>
11811181
<tr><td><a name="json_array_length"></a><code>json_array_length(json: jsonb) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Returns the number of elements in the outermost JSON or JSONB array.</p>
11821182
</span></td><td>Immutable</td></tr>
1183-
<tr><td><a name="json_build_array"></a><code>json_build_array(anyelement...) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Builds a possibly-heterogeneously-typed JSON or JSONB array out of a variadic argument list.</p>
1183+
<tr><td><a name="json_build_array"></a><code>json_build_array(any...) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Builds a possibly-heterogeneously-typed JSON or JSONB array out of a variadic argument list.</p>
11841184
</span></td><td>Stable</td></tr>
1185-
<tr><td><a name="json_build_object"></a><code>json_build_object(anyelement...) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Builds a JSON object out of a variadic argument list.</p>
1185+
<tr><td><a name="json_build_object"></a><code>json_build_object(any...) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Builds a JSON object out of a variadic argument list.</p>
11861186
</span></td><td>Stable</td></tr>
11871187
<tr><td><a name="json_each"></a><code>json_each(input: jsonb) &rarr; tuple{string AS key, jsonb AS value}</code></td><td><span class="funcdesc"><p>Expands the outermost JSON or JSONB object into a set of key/value pairs.</p>
11881188
</span></td><td>Immutable</td></tr>
@@ -1218,9 +1218,9 @@ available replica will error.</p>
12181218
</span></td><td>Immutable</td></tr>
12191219
<tr><td><a name="jsonb_array_length"></a><code>jsonb_array_length(json: jsonb) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Returns the number of elements in the outermost JSON or JSONB array.</p>
12201220
</span></td><td>Immutable</td></tr>
1221-
<tr><td><a name="jsonb_build_array"></a><code>jsonb_build_array(anyelement...) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Builds a possibly-heterogeneously-typed JSON or JSONB array out of a variadic argument list.</p>
1221+
<tr><td><a name="jsonb_build_array"></a><code>jsonb_build_array(any...) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Builds a possibly-heterogeneously-typed JSON or JSONB array out of a variadic argument list.</p>
12221222
</span></td><td>Stable</td></tr>
1223-
<tr><td><a name="jsonb_build_object"></a><code>jsonb_build_object(anyelement...) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Builds a JSON object out of a variadic argument list.</p>
1223+
<tr><td><a name="jsonb_build_object"></a><code>jsonb_build_object(any...) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Builds a JSON object out of a variadic argument list.</p>
12241224
</span></td><td>Stable</td></tr>
12251225
<tr><td><a name="jsonb_each"></a><code>jsonb_each(input: jsonb) &rarr; tuple{string AS key, jsonb AS value}</code></td><td><span class="funcdesc"><p>Expands the outermost JSON or JSONB object into a set of key/value pairs.</p>
12261226
</span></td><td>Immutable</td></tr>

pkg/sql/logictest/testdata/logic_test/json_builtins

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,20 @@ SELECT json_build_object('a', '0100110'::varbit)
562562
----
563563
{"a": "0100110"}
564564

565+
statement ok
566+
CREATE TABLE foo (a INT);
567+
568+
statement ok
569+
INSERT INTO foo VALUES (42);
570+
571+
statement ok
572+
PREPARE jbo_stmt AS SELECT json_build_object('a', a, 'b', $1::STRING) FROM foo;
573+
574+
query T
575+
EXECUTE jbo_stmt(':');
576+
----
577+
{"a": 42, "b": ":"}
578+
565579
# Regression for an internal error when using an enum and void in the key.
566580
statement ok
567581
CREATE TYPE e AS ENUM ('e');
@@ -713,6 +727,14 @@ SELECT json_build_array(1, '1'::JSON, 1.2, NULL, ARRAY['x', 'y'])
713727
----
714728
[1, 1, 1.2, null, ["x", "y"]]
715729

730+
statement ok
731+
PREPARE jba_stmt AS SELECT json_build_array(a, $1::STRING) FROM foo;
732+
733+
query T
734+
EXECUTE jba_stmt(':');
735+
----
736+
[42, ":"]
737+
716738
query T
717739
SELECT jsonb_build_array()
718740
----

pkg/sql/sem/builtins/builtins.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5300,7 +5300,7 @@ value if you rely on the HLC for accuracy.`,
53005300
{Name: "id", Typ: types.Int},
53015301
},
53025302
ReturnType: tree.FixedReturnType(types.Int),
5303-
Body: `SELECT crdb_internal.create_tenant(json_build_object('id', $1, 'service_mode',
5303+
Body: `SELECT crdb_internal.create_tenant(json_build_object('id', $1::INT, 'service_mode',
53045304
'external'))`,
53055305
Info: `create_tenant(id) is an alias for create_tenant('{"id": id, "service_mode": "external"}'::jsonb)`,
53065306
Volatility: volatility.Volatile,
@@ -5313,7 +5313,7 @@ value if you rely on the HLC for accuracy.`,
53135313
{Name: "name", Typ: types.String},
53145314
},
53155315
ReturnType: tree.FixedReturnType(types.Int),
5316-
Body: `SELECT crdb_internal.create_tenant(json_build_object('id', $1, 'name', $2))`,
5316+
Body: `SELECT crdb_internal.create_tenant(json_build_object('id', $1::INT, 'name', $2::STRING))`,
53175317
Info: `create_tenant(id, name) is an alias for create_tenant('{"id": id, "name": name}'::jsonb)`,
53185318
Volatility: volatility.Volatile,
53195319
Language: tree.RoutineLangSQL,
@@ -5324,7 +5324,7 @@ value if you rely on the HLC for accuracy.`,
53245324
{Name: "name", Typ: types.String},
53255325
},
53265326
ReturnType: tree.FixedReturnType(types.Int),
5327-
Body: `SELECT crdb_internal.create_tenant(json_build_object('name', $1))`,
5327+
Body: `SELECT crdb_internal.create_tenant(json_build_object('name', $1::STRING))`,
53285328
Info: `create_tenant(name) is an alias for create_tenant('{"name": name}'::jsonb).
53295329
DO NOT USE -- USE 'CREATE VIRTUAL CLUSTER' INSTEAD`,
53305330
Volatility: volatility.Volatile,
@@ -7048,7 +7048,7 @@ SELECT
70487048
},
70497049
ReturnType: tree.FixedReturnType(types.Jsonb),
70507050
Body: `SELECT crdb_internal.generate_test_objects(
7051-
json_build_object('names', $1, 'counts', array[$2]))`,
7051+
json_build_object('names', $1::STRING, 'counts', array[$2::INT]))`,
70527052
Info: `Generates a number of objects whose name follow the provided pattern.
70537053
70547054
generate_test_objects(pat, num) is an alias for
@@ -7064,7 +7064,7 @@ generate_test_objects('{"names":pat, "counts":[num]}'::jsonb)
70647064
},
70657065
ReturnType: tree.FixedReturnType(types.Jsonb),
70667066
Body: `SELECT crdb_internal.generate_test_objects(
7067-
json_build_object('names', $1, 'counts', $2))`,
7067+
json_build_object('names', $1::STRING, 'counts', $2::INT[]))`,
70687068
Info: `Generates a number of objects whose name follow the provided pattern.
70697069
70707070
generate_test_objects(pat, counts) is an alias for
@@ -10097,7 +10097,7 @@ func jsonProps() tree.FunctionProperties {
1009710097
}
1009810098

1009910099
var jsonBuildObjectImpl = tree.Overload{
10100-
Types: tree.VariadicType{VarType: types.AnyElement},
10100+
Types: tree.VariadicType{VarType: types.Any},
1010110101
ReturnType: tree.FixedReturnType(types.Jsonb),
1010210102
Fn: func(ctx context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
1010310103
if len(args)%2 != 0 {
@@ -10175,7 +10175,7 @@ var arrayToJSONImpls = makeBuiltin(jsonProps(),
1017510175
)
1017610176

1017710177
var jsonBuildArrayImpl = tree.Overload{
10178-
Types: tree.VariadicType{VarType: types.AnyElement},
10178+
Types: tree.VariadicType{VarType: types.Any},
1017910179
ReturnType: tree.FixedReturnType(types.Jsonb),
1018010180
Fn: func(ctx context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
1018110181
builder := json.NewArrayBuilder(len(args))

pkg/sql/sem/builtins/fixed_oids.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,10 +1229,10 @@ var builtinOidsArray = []string{
12291229
1253: `jsonb_typeof(val: jsonb) -> string`,
12301230
1254: `to_json(val: anyelement) -> jsonb`,
12311231
1255: `to_jsonb(val: anyelement) -> jsonb`,
1232-
1256: `json_build_array(anyelement...) -> jsonb`,
1233-
1257: `jsonb_build_array(anyelement...) -> jsonb`,
1234-
1258: `json_build_object(anyelement...) -> jsonb`,
1235-
1259: `jsonb_build_object(anyelement...) -> jsonb`,
1232+
1256: `json_build_array(any...) -> jsonb`,
1233+
1257: `jsonb_build_array(any...) -> jsonb`,
1234+
1258: `json_build_object(any...) -> jsonb`,
1235+
1259: `jsonb_build_object(any...) -> jsonb`,
12361236
1260: `json_object(texts: string[]) -> jsonb`,
12371237
1261: `json_object(keys: string[], values: string[]) -> jsonb`,
12381238
1262: `jsonb_object(texts: string[]) -> jsonb`,

0 commit comments

Comments
 (0)