@@ -222,6 +222,29 @@ flattenJsonPathParseItem(StringInfo buf, JsonPathParseItem *item,
222222 case jpiDouble :
223223 case jpiKeyValue :
224224 break ;
225+ case jpiSequence :
226+ {
227+ int32 nelems = list_length (item -> value .sequence .elems );
228+ ListCell * lc ;
229+ int offset ;
230+
231+ appendBinaryStringInfo (buf , (char * ) & nelems , sizeof (nelems ));
232+
233+ offset = buf -> len ;
234+
235+ appendStringInfoSpaces (buf , sizeof (int32 ) * nelems );
236+
237+ foreach (lc , item -> value .sequence .elems )
238+ {
239+ int32 pos =
240+ flattenJsonPathParseItem (buf , lfirst (lc ),
241+ allowCurrent , insideArraySubscript );
242+
243+ * (int32 * ) & buf -> data [offset ] = pos ;
244+ offset += sizeof (int32 );
245+ }
246+ }
247+ break ;
225248 default :
226249 elog (ERROR , "Unknown jsonpath item type: %d" , item -> type );
227250 }
@@ -307,6 +330,8 @@ operationPriority(JsonPathItemType op)
307330{
308331 switch (op )
309332 {
333+ case jpiSequence :
334+ return -1 ;
310335 case jpiOr :
311336 return 0 ;
312337 case jpiAnd :
@@ -496,12 +521,12 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey, bool printBracket
496521 if (i )
497522 appendStringInfoChar (buf , ',' );
498523
499- printJsonPathItem (buf , & from , false, false );
524+ printJsonPathItem (buf , & from , false, from . type == jpiSequence );
500525
501526 if (range )
502527 {
503528 appendBinaryStringInfo (buf , " to " , 4 );
504- printJsonPathItem (buf , & to , false, false );
529+ printJsonPathItem (buf , & to , false, to . type == jpiSequence );
505530 }
506531 }
507532 appendStringInfoChar (buf , ']' );
@@ -559,6 +584,25 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey, bool printBracket
559584 printJsonPathItem (buf , & elem , false, false);
560585 appendStringInfoChar (buf , ')' );
561586 break ;
587+ case jpiSequence :
588+ if (printBracketes || jspHasNext (v ))
589+ appendStringInfoChar (buf , '(' );
590+
591+ for (i = 0 ; i < v -> content .sequence .nelems ; i ++ )
592+ {
593+ JsonPathItem elem ;
594+
595+ if (i )
596+ appendBinaryStringInfo (buf , ", " , 2 );
597+
598+ jspGetSequenceElement (v , i , & elem );
599+
600+ printJsonPathItem (buf , & elem , false, elem .type == jpiSequence );
601+ }
602+
603+ if (printBracketes || jspHasNext (v ))
604+ appendStringInfoChar (buf , ')' );
605+ break ;
562606 default :
563607 elog (ERROR , "Unknown jsonpath item type: %d" , v -> type );
564608 }
@@ -581,7 +625,7 @@ jsonpath_out(PG_FUNCTION_ARGS)
581625 appendBinaryStringInfo (& buf , "strict " , 7 );
582626
583627 jspInit (& v , in );
584- printJsonPathItem (& buf , & v , false, true );
628+ printJsonPathItem (& buf , & v , false, v . type != jpiSequence );
585629
586630 PG_RETURN_CSTRING (buf .data );
587631}
@@ -704,6 +748,11 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
704748 read_int32 (v -> content .anybounds .first , base , pos );
705749 read_int32 (v -> content .anybounds .last , base , pos );
706750 break ;
751+ case jpiSequence :
752+ read_int32 (v -> content .sequence .nelems , base , pos );
753+ read_int32_n (v -> content .sequence .elems , base , pos ,
754+ v -> content .sequence .nelems );
755+ break ;
707756 default :
708757 elog (ERROR , "Unknown jsonpath item type: %d" , v -> type );
709758 }
@@ -773,7 +822,8 @@ jspGetNext(JsonPathItem *v, JsonPathItem *a)
773822 v -> type == jpiDatetime ||
774823 v -> type == jpiKeyValue ||
775824 v -> type == jpiStartsWith ||
776- v -> type == jpiMap
825+ v -> type == jpiMap ||
826+ v -> type == jpiSequence
777827 );
778828
779829 if (a )
@@ -875,3 +925,11 @@ jspGetArraySubscript(JsonPathItem *v, JsonPathItem *from, JsonPathItem *to,
875925
876926 return true;
877927}
928+
929+ void
930+ jspGetSequenceElement (JsonPathItem * v , int i , JsonPathItem * elem )
931+ {
932+ Assert (v -> type == jpiSequence );
933+
934+ jspInitByBuffer (elem , v -> base , v -> content .sequence .elems [i ]);
935+ }
0 commit comments