@@ -199,6 +199,29 @@ flattenJsonPathParseItem(StringInfo buf, JsonPathParseItem *item,
199199 case jpiDouble :
200200 case jpiKeyValue :
201201 break ;
202+ case jpiSequence :
203+ {
204+ int32 nelems = list_length (item -> value .sequence .elems );
205+ ListCell * lc ;
206+ int offset ;
207+
208+ appendBinaryStringInfo (buf , (char * ) & nelems , sizeof (nelems ));
209+
210+ offset = buf -> len ;
211+
212+ appendStringInfoSpaces (buf , sizeof (int32 ) * nelems );
213+
214+ foreach (lc , item -> value .sequence .elems )
215+ {
216+ int32 pos =
217+ flattenJsonPathParseItem (buf , lfirst (lc ),
218+ allowCurrent , insideArraySubscript );
219+
220+ * (int32 * ) & buf -> data [offset ] = pos ;
221+ offset += sizeof (int32 );
222+ }
223+ }
224+ break ;
202225 default :
203226 elog (ERROR , "Unknown jsonpath item type: %d" , item -> type );
204227 }
@@ -284,6 +307,8 @@ operationPriority(JsonPathItemType op)
284307{
285308 switch (op )
286309 {
310+ case jpiSequence :
311+ return -1 ;
287312 case jpiOr :
288313 return 0 ;
289314 case jpiAnd :
@@ -441,12 +466,12 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey, bool printBracket
441466 if (i )
442467 appendStringInfoChar (buf , ',' );
443468
444- printJsonPathItem (buf , & from , false, false );
469+ printJsonPathItem (buf , & from , false, from . type == jpiSequence );
445470
446471 if (range )
447472 {
448473 appendBinaryStringInfo (buf , " to " , 4 );
449- printJsonPathItem (buf , & to , false, false );
474+ printJsonPathItem (buf , & to , false, to . type == jpiSequence );
450475 }
451476 }
452477 appendStringInfoChar (buf , ']' );
@@ -504,6 +529,25 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey, bool printBracket
504529 printJsonPathItem (buf , & elem , false, false);
505530 appendStringInfoChar (buf , ')' );
506531 break ;
532+ case jpiSequence :
533+ if (printBracketes || jspHasNext (v ))
534+ appendStringInfoChar (buf , '(' );
535+
536+ for (i = 0 ; i < v -> content .sequence .nelems ; i ++ )
537+ {
538+ JsonPathItem elem ;
539+
540+ if (i )
541+ appendBinaryStringInfo (buf , ", " , 2 );
542+
543+ jspGetSequenceElement (v , i , & elem );
544+
545+ printJsonPathItem (buf , & elem , false, elem .type == jpiSequence );
546+ }
547+
548+ if (printBracketes || jspHasNext (v ))
549+ appendStringInfoChar (buf , ')' );
550+ break ;
507551 default :
508552 elog (ERROR , "Unknown jsonpath item type: %d" , v -> type );
509553 }
@@ -526,7 +570,7 @@ jsonpath_out(PG_FUNCTION_ARGS)
526570 appendBinaryStringInfo (& buf , "strict " , 7 );
527571
528572 jspInit (& v , in );
529- printJsonPathItem (& buf , & v , false, true );
573+ printJsonPathItem (& buf , & v , false, v . type != jpiSequence );
530574
531575 PG_RETURN_CSTRING (buf .data );
532576}
@@ -643,6 +687,11 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
643687 read_int32 (v -> content .anybounds .first , base , pos );
644688 read_int32 (v -> content .anybounds .last , base , pos );
645689 break ;
690+ case jpiSequence :
691+ read_int32 (v -> content .sequence .nelems , base , pos );
692+ read_int32_n (v -> content .sequence .elems , base , pos ,
693+ v -> content .sequence .nelems );
694+ break ;
646695 default :
647696 elog (ERROR , "Unknown jsonpath item type: %d" , v -> type );
648697 }
@@ -712,7 +761,8 @@ jspGetNext(JsonPathItem *v, JsonPathItem *a)
712761 v -> type == jpiDatetime ||
713762 v -> type == jpiKeyValue ||
714763 v -> type == jpiStartsWith ||
715- v -> type == jpiMap
764+ v -> type == jpiMap ||
765+ v -> type == jpiSequence
716766 );
717767
718768 if (a )
@@ -814,3 +864,11 @@ jspGetArraySubscript(JsonPathItem *v, JsonPathItem *from, JsonPathItem *to,
814864
815865 return true;
816866}
867+
868+ void
869+ jspGetSequenceElement (JsonPathItem * v , int i , JsonPathItem * elem )
870+ {
871+ Assert (v -> type == jpiSequence );
872+
873+ jspInitByBuffer (elem , v -> base , v -> content .sequence .elems [i ]);
874+ }
0 commit comments