Skip to content

Commit 5d7a5de

Browse files
peterepull[bot]
authored andcommitted
Reorganise jsonpath operators and methods
Various jsonpath operators and methods add various keywords, switch cases, and documentation entries in some order. However, they are not consistent; reorder them for better maintainability or readability. Author: Jeevan Chalke <jeevan.chalke@enterprisedb.com> Discussion: https://www.postgresql.org/message-id/flat/CAM2+6=XjTyqrrqHAOj80r0wVQxJSxc0iyib9bPC55uFO9VKatg@mail.gmail.com
1 parent 5fa0651 commit 5d7a5de

File tree

5 files changed

+119
-112
lines changed

5 files changed

+119
-112
lines changed

doc/src/sgml/func.sgml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17691,43 +17691,43 @@ strict $.**.HR
1769117691

1769217692
<row>
1769317693
<entry role="func_table_entry"><para role="func_signature">
17694-
<replaceable>number</replaceable> <literal>.</literal> <literal>ceiling()</literal>
17694+
<replaceable>number</replaceable> <literal>.</literal> <literal>abs()</literal>
1769517695
<returnvalue><replaceable>number</replaceable></returnvalue>
1769617696
</para>
1769717697
<para>
17698-
Nearest integer greater than or equal to the given number
17698+
Absolute value of the given number
1769917699
</para>
1770017700
<para>
17701-
<literal>jsonb_path_query('{"h": 1.3}', '$.h.ceiling()')</literal>
17702-
<returnvalue>2</returnvalue>
17701+
<literal>jsonb_path_query('{"z": -0.3}', '$.z.abs()')</literal>
17702+
<returnvalue>0.3</returnvalue>
1770317703
</para></entry>
1770417704
</row>
1770517705

1770617706
<row>
1770717707
<entry role="func_table_entry"><para role="func_signature">
17708-
<replaceable>number</replaceable> <literal>.</literal> <literal>floor()</literal>
17708+
<replaceable>number</replaceable> <literal>.</literal> <literal>ceiling()</literal>
1770917709
<returnvalue><replaceable>number</replaceable></returnvalue>
1771017710
</para>
1771117711
<para>
17712-
Nearest integer less than or equal to the given number
17712+
Nearest integer greater than or equal to the given number
1771317713
</para>
1771417714
<para>
17715-
<literal>jsonb_path_query('{"h": 1.7}', '$.h.floor()')</literal>
17716-
<returnvalue>1</returnvalue>
17715+
<literal>jsonb_path_query('{"h": 1.3}', '$.h.ceiling()')</literal>
17716+
<returnvalue>2</returnvalue>
1771717717
</para></entry>
1771817718
</row>
1771917719

1772017720
<row>
1772117721
<entry role="func_table_entry"><para role="func_signature">
17722-
<replaceable>number</replaceable> <literal>.</literal> <literal>abs()</literal>
17722+
<replaceable>number</replaceable> <literal>.</literal> <literal>floor()</literal>
1772317723
<returnvalue><replaceable>number</replaceable></returnvalue>
1772417724
</para>
1772517725
<para>
17726-
Absolute value of the given number
17726+
Nearest integer less than or equal to the given number
1772717727
</para>
1772817728
<para>
17729-
<literal>jsonb_path_query('{"z": -0.3}', '$.z.abs()')</literal>
17730-
<returnvalue>0.3</returnvalue>
17729+
<literal>jsonb_path_query('{"h": 1.7}', '$.h.floor()')</literal>
17730+
<returnvalue>1</returnvalue>
1773117731
</para></entry>
1773217732
</row>
1773317733

src/backend/utils/adt/jsonpath.c

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,10 @@ flattenJsonPathParseItem(StringInfo buf, int *result, struct Node *escontext,
439439
break;
440440
case jpiType:
441441
case jpiSize:
442+
case jpiDouble:
442443
case jpiAbs:
443-
case jpiFloor:
444444
case jpiCeiling:
445-
case jpiDouble:
445+
case jpiFloor:
446446
case jpiKeyValue:
447447
break;
448448
default:
@@ -610,18 +610,6 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey,
610610
if (printBracketes)
611611
appendStringInfoChar(buf, ')');
612612
break;
613-
case jpiPlus:
614-
case jpiMinus:
615-
if (printBracketes)
616-
appendStringInfoChar(buf, '(');
617-
appendStringInfoChar(buf, v->type == jpiPlus ? '+' : '-');
618-
jspGetArg(v, &elem);
619-
printJsonPathItem(buf, &elem, false,
620-
operationPriority(elem.type) <=
621-
operationPriority(v->type));
622-
if (printBracketes)
623-
appendStringInfoChar(buf, ')');
624-
break;
625613
case jpiFilter:
626614
appendStringInfoString(buf, "?(");
627615
jspGetArg(v, &elem);
@@ -712,23 +700,35 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey,
712700
v->content.anybounds.first,
713701
v->content.anybounds.last);
714702
break;
703+
case jpiPlus:
704+
case jpiMinus:
705+
if (printBracketes)
706+
appendStringInfoChar(buf, '(');
707+
appendStringInfoChar(buf, v->type == jpiPlus ? '+' : '-');
708+
jspGetArg(v, &elem);
709+
printJsonPathItem(buf, &elem, false,
710+
operationPriority(elem.type) <=
711+
operationPriority(v->type));
712+
if (printBracketes)
713+
appendStringInfoChar(buf, ')');
714+
break;
715715
case jpiType:
716716
appendStringInfoString(buf, ".type()");
717717
break;
718718
case jpiSize:
719719
appendStringInfoString(buf, ".size()");
720720
break;
721+
case jpiDouble:
722+
appendStringInfoString(buf, ".double()");
723+
break;
721724
case jpiAbs:
722725
appendStringInfoString(buf, ".abs()");
723726
break;
724-
case jpiFloor:
725-
appendStringInfoString(buf, ".floor()");
726-
break;
727727
case jpiCeiling:
728728
appendStringInfoString(buf, ".ceiling()");
729729
break;
730-
case jpiDouble:
731-
appendStringInfoString(buf, ".double()");
730+
case jpiFloor:
731+
appendStringInfoString(buf, ".floor()");
732732
break;
733733
case jpiDatetime:
734734
appendStringInfoString(buf, ".datetime(");
@@ -771,38 +771,38 @@ jspOperationName(JsonPathItemType type)
771771
return "<=";
772772
case jpiGreaterOrEqual:
773773
return ">=";
774-
case jpiPlus:
775774
case jpiAdd:
775+
case jpiPlus:
776776
return "+";
777-
case jpiMinus:
778777
case jpiSub:
778+
case jpiMinus:
779779
return "-";
780780
case jpiMul:
781781
return "*";
782782
case jpiDiv:
783783
return "/";
784784
case jpiMod:
785785
return "%";
786-
case jpiStartsWith:
787-
return "starts with";
788-
case jpiLikeRegex:
789-
return "like_regex";
790786
case jpiType:
791787
return "type";
792788
case jpiSize:
793789
return "size";
794-
case jpiKeyValue:
795-
return "keyvalue";
796790
case jpiDouble:
797791
return "double";
798792
case jpiAbs:
799793
return "abs";
800-
case jpiFloor:
801-
return "floor";
802794
case jpiCeiling:
803795
return "ceiling";
796+
case jpiFloor:
797+
return "floor";
804798
case jpiDatetime:
805799
return "datetime";
800+
case jpiKeyValue:
801+
return "keyvalue";
802+
case jpiStartsWith:
803+
return "starts with";
804+
case jpiLikeRegex:
805+
return "like_regex";
806806
default:
807807
elog(ERROR, "unrecognized jsonpath item type: %d", type);
808808
return NULL;
@@ -893,10 +893,10 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
893893
case jpiAnyKey:
894894
case jpiType:
895895
case jpiSize:
896+
case jpiDouble:
896897
case jpiAbs:
897-
case jpiFloor:
898898
case jpiCeiling:
899-
case jpiDouble:
899+
case jpiFloor:
900900
case jpiKeyValue:
901901
case jpiLast:
902902
break;
@@ -935,9 +935,9 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
935935
case jpiNot:
936936
case jpiExists:
937937
case jpiIsUnknown:
938+
case jpiFilter:
938939
case jpiPlus:
939940
case jpiMinus:
940-
case jpiFilter:
941941
case jpiDatetime:
942942
read_int32(v->content.arg, base, pos);
943943
break;
@@ -989,13 +989,6 @@ jspGetNext(JsonPathItem *v, JsonPathItem *a)
989989
v->type == jpiRoot ||
990990
v->type == jpiVariable ||
991991
v->type == jpiLast ||
992-
v->type == jpiAdd ||
993-
v->type == jpiSub ||
994-
v->type == jpiMul ||
995-
v->type == jpiDiv ||
996-
v->type == jpiMod ||
997-
v->type == jpiPlus ||
998-
v->type == jpiMinus ||
999992
v->type == jpiEqual ||
1000993
v->type == jpiNotEqual ||
1001994
v->type == jpiGreater ||
@@ -1006,12 +999,19 @@ jspGetNext(JsonPathItem *v, JsonPathItem *a)
1006999
v->type == jpiOr ||
10071000
v->type == jpiNot ||
10081001
v->type == jpiIsUnknown ||
1002+
v->type == jpiAdd ||
1003+
v->type == jpiPlus ||
1004+
v->type == jpiSub ||
1005+
v->type == jpiMinus ||
1006+
v->type == jpiMul ||
1007+
v->type == jpiDiv ||
1008+
v->type == jpiMod ||
10091009
v->type == jpiType ||
10101010
v->type == jpiSize ||
1011+
v->type == jpiDouble ||
10111012
v->type == jpiAbs ||
1012-
v->type == jpiFloor ||
10131013
v->type == jpiCeiling ||
1014-
v->type == jpiDouble ||
1014+
v->type == jpiFloor ||
10151015
v->type == jpiDatetime ||
10161016
v->type == jpiKeyValue ||
10171017
v->type == jpiStartsWith ||

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -874,33 +874,6 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
874874
}
875875
break;
876876

877-
case jpiAdd:
878-
return executeBinaryArithmExpr(cxt, jsp, jb,
879-
numeric_add_opt_error, found);
880-
881-
case jpiSub:
882-
return executeBinaryArithmExpr(cxt, jsp, jb,
883-
numeric_sub_opt_error, found);
884-
885-
case jpiMul:
886-
return executeBinaryArithmExpr(cxt, jsp, jb,
887-
numeric_mul_opt_error, found);
888-
889-
case jpiDiv:
890-
return executeBinaryArithmExpr(cxt, jsp, jb,
891-
numeric_div_opt_error, found);
892-
893-
case jpiMod:
894-
return executeBinaryArithmExpr(cxt, jsp, jb,
895-
numeric_mod_opt_error, found);
896-
897-
case jpiPlus:
898-
return executeUnaryArithmExpr(cxt, jsp, jb, NULL, found);
899-
900-
case jpiMinus:
901-
return executeUnaryArithmExpr(cxt, jsp, jb, numeric_uminus,
902-
found);
903-
904877
case jpiFilter:
905878
{
906879
JsonPathBool st;
@@ -980,6 +953,33 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
980953
}
981954
break;
982955

956+
case jpiAdd:
957+
return executeBinaryArithmExpr(cxt, jsp, jb,
958+
numeric_add_opt_error, found);
959+
960+
case jpiPlus:
961+
return executeUnaryArithmExpr(cxt, jsp, jb, NULL, found);
962+
963+
case jpiSub:
964+
return executeBinaryArithmExpr(cxt, jsp, jb,
965+
numeric_sub_opt_error, found);
966+
967+
case jpiMinus:
968+
return executeUnaryArithmExpr(cxt, jsp, jb, numeric_uminus,
969+
found);
970+
971+
case jpiMul:
972+
return executeBinaryArithmExpr(cxt, jsp, jb,
973+
numeric_mul_opt_error, found);
974+
975+
case jpiDiv:
976+
return executeBinaryArithmExpr(cxt, jsp, jb,
977+
numeric_div_opt_error, found);
978+
979+
case jpiMod:
980+
return executeBinaryArithmExpr(cxt, jsp, jb,
981+
numeric_mod_opt_error, found);
982+
983983
case jpiType:
984984
{
985985
JsonbValue *jbv = palloc(sizeof(*jbv));
@@ -1021,18 +1021,6 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
10211021
}
10221022
break;
10231023

1024-
case jpiAbs:
1025-
return executeNumericItemMethod(cxt, jsp, jb, unwrap, numeric_abs,
1026-
found);
1027-
1028-
case jpiFloor:
1029-
return executeNumericItemMethod(cxt, jsp, jb, unwrap, numeric_floor,
1030-
found);
1031-
1032-
case jpiCeiling:
1033-
return executeNumericItemMethod(cxt, jsp, jb, unwrap, numeric_ceil,
1034-
found);
1035-
10361024
case jpiDouble:
10371025
{
10381026
JsonbValue jbv;
@@ -1098,6 +1086,18 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
10981086
}
10991087
break;
11001088

1089+
case jpiAbs:
1090+
return executeNumericItemMethod(cxt, jsp, jb, unwrap, numeric_abs,
1091+
found);
1092+
1093+
case jpiCeiling:
1094+
return executeNumericItemMethod(cxt, jsp, jb, unwrap, numeric_ceil,
1095+
found);
1096+
1097+
case jpiFloor:
1098+
return executeNumericItemMethod(cxt, jsp, jb, unwrap, numeric_floor,
1099+
found);
1100+
11011101
case jpiDatetime:
11021102
if (unwrap && JsonbType(jb) == jbvArray)
11031103
return executeItemUnwrapTargetArray(cxt, jsp, jb, found, false);

0 commit comments

Comments
 (0)