Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4e00dd5

Browse files
author
Nikita Glukhov
committed
Improve immutability check for JsonCtorExpr
1 parent39d0a43 commit4e00dd5

File tree

5 files changed

+92
-7
lines changed

5 files changed

+92
-7
lines changed

‎src/backend/optimizer/util/clauses.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#include"utils/builtins.h"
4949
#include"utils/datum.h"
5050
#include"utils/fmgroids.h"
51+
#include"utils/json.h"
52+
#include"utils/jsonb.h"
5153
#include"utils/lsyscache.h"
5254
#include"utils/memutils.h"
5355
#include"utils/syscache.h"
@@ -665,6 +667,27 @@ contain_mutable_functions_walker(Node *node, void *context)
665667
context))
666668
return true;
667669

670+
if (IsA(node,JsonCtorExpr))
671+
{
672+
JsonCtorExpr*ctor= (JsonCtorExpr*)node;
673+
ListCell*lc;
674+
boolis_jsonb=
675+
ctor->returning->format->format==JS_FORMAT_JSONB;
676+
677+
/* Check argument_type => json[b] conversions */
678+
foreach(lc,ctor->args)
679+
{
680+
Oidtypid=exprType(lfirst(lc));
681+
682+
if (is_jsonb ?
683+
!to_jsonb_is_immutable(typid) :
684+
!to_json_is_immutable(typid))
685+
return true;
686+
}
687+
688+
/* Check all subnodes */
689+
}
690+
668691
if (IsA(node,SQLValueFunction))
669692
{
670693
/* all variants of SQLValueFunction are stable */
@@ -677,12 +700,6 @@ contain_mutable_functions_walker(Node *node, void *context)
677700
return true;
678701
}
679702

680-
if (IsA(node,JsonCtorExpr))
681-
{
682-
/* JsonCtorExpr is stable */
683-
return true;
684-
}
685-
686703
/*
687704
* It should be safe to treat MinMaxExpr as immutable, because it will
688705
* depend on a non-cross-type btree comparison function, and those should

‎src/backend/utils/adt/json.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
#include"postgres.h"
1515

16+
#include"catalog/pg_proc.h"
1617
#include"catalog/pg_type.h"
1718
#include"common/hashfn.h"
1819
#include"funcapi.h"
@@ -760,6 +761,38 @@ row_to_json_pretty(PG_FUNCTION_ARGS)
760761
PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data,result->len));
761762
}
762763

764+
bool
765+
to_json_is_immutable(Oidtypoid)
766+
{
767+
JsonTypeCategorytcategory;
768+
Oidoutfuncoid;
769+
770+
json_categorize_type(typoid,&tcategory,&outfuncoid);
771+
772+
switch (tcategory)
773+
{
774+
caseJSONTYPE_BOOL:
775+
caseJSONTYPE_JSON:
776+
return true;
777+
778+
caseJSONTYPE_DATE:
779+
caseJSONTYPE_TIMESTAMP:
780+
caseJSONTYPE_TIMESTAMPTZ:
781+
return false;
782+
783+
caseJSONTYPE_ARRAY:
784+
return false;/* TODO recurse into elements */
785+
786+
caseJSONTYPE_COMPOSITE:
787+
return false;/* TODO recurse into fields */
788+
789+
caseJSONTYPE_NUMERIC:
790+
caseJSONTYPE_CAST:
791+
default:
792+
returnfunc_volatile(outfuncoid)==PROVOLATILE_IMMUTABLE;
793+
}
794+
}
795+
763796
/*
764797
* SQL function to_json(anyvalue)
765798
*/

‎src/backend/utils/adt/jsonb.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include"access/htup_details.h"
1616
#include"access/transam.h"
17+
#include"catalog/pg_proc.h"
1718
#include"catalog/pg_type.h"
1819
#include"funcapi.h"
1920
#include"libpq/pqformat.h"
@@ -1126,6 +1127,39 @@ add_jsonb(Datum val, bool is_null, JsonbInState *result,
11261127
datum_to_jsonb(val,is_null,result,tcategory,outfuncoid,key_scalar);
11271128
}
11281129

1130+
bool
1131+
to_jsonb_is_immutable(Oidtypoid)
1132+
{
1133+
JsonbTypeCategorytcategory;
1134+
Oidoutfuncoid;
1135+
1136+
jsonb_categorize_type(typoid,&tcategory,&outfuncoid);
1137+
1138+
switch (tcategory)
1139+
{
1140+
caseJSONBTYPE_BOOL:
1141+
caseJSONBTYPE_JSON:
1142+
caseJSONBTYPE_JSONB:
1143+
return true;
1144+
1145+
caseJSONBTYPE_DATE:
1146+
caseJSONBTYPE_TIMESTAMP:
1147+
caseJSONBTYPE_TIMESTAMPTZ:
1148+
return false;
1149+
1150+
caseJSONBTYPE_ARRAY:
1151+
return false;/* TODO recurse into elements */
1152+
1153+
caseJSONBTYPE_COMPOSITE:
1154+
return false;/* TODO recurse into fields */
1155+
1156+
caseJSONBTYPE_NUMERIC:
1157+
caseJSONBTYPE_JSONCAST:
1158+
default:
1159+
returnfunc_volatile(outfuncoid)==PROVOLATILE_IMMUTABLE;
1160+
}
1161+
}
1162+
11291163
/*
11301164
* SQL function to_jsonb(anyvalue)
11311165
*/

‎src/include/utils/json.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
externvoidescape_json(StringInfobuf,constchar*str);
2121
externchar*JsonEncodeDateTime(char*buf,Datumvalue,Oidtypid,
2222
constint*tzp);
23+
externboolto_json_is_immutable(Oidtypoid);
2324
externDatumjson_build_object_worker(intnargs,Datum*args,bool*nulls,
2425
Oid*types,boolabsent_on_null,
2526
boolunique_keys);

‎src/include/utils/jsonb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ extern char *JsonbToCStringIndent(StringInfo out, JsonbContainer *in,
408408
intestimated_len);
409409
externboolJsonbExtractScalar(JsonbContainer*jbc,JsonbValue*res);
410410
externconstchar*JsonbTypeName(JsonbValue*jb);
411-
411+
externboolto_jsonb_is_immutable(Oidtypoid);
412412
externDatumjsonb_build_object_worker(intnargs,Datum*args,bool*nulls,
413413
Oid*types,boolabsent_on_null,
414414
boolunique_keys);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp