@@ -1670,47 +1670,28 @@ recursiveExecuteNoUnwrap(JsonPathExecContext *cxt, JsonPathItem *jsp,
1670
1670
{
1671
1671
JsonbValue jbvbuf ;
1672
1672
Datum value ;
1673
- text * datetime_txt ;
1674
1673
Oid typid ;
1675
1674
int32 typmod = -1 ;
1676
1675
bool hasNext ;
1677
1676
1678
1677
if (JsonbType (jb ) == jbvScalar )
1679
1678
jb = JsonbExtractScalar (jb -> val .binary .data , & jbvbuf );
1680
1679
1681
- if (jb -> type != jbvString )
1680
+ if (jb -> type == jbvNumeric && ! jsp -> content . arg )
1682
1681
{
1683
- res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1684
- break ;
1685
- }
1686
-
1687
- datetime_txt = cstring_to_text_with_len (jb -> val .string .val ,
1688
- jb -> val .string .len );
1689
-
1690
- res = jperOk ;
1691
-
1692
- if (jsp -> content .arg )
1693
- {
1694
- text * template_txt ;
1695
- char * template_str ;
1696
- int template_len ;
1682
+ /* Standard extension: unix epoch to timestamptz */
1697
1683
MemoryContext mcxt = CurrentMemoryContext ;
1698
1684
1699
- jspGetArg (jsp , & elem );
1700
-
1701
- if (elem .type != jpiString )
1702
- elog (ERROR , "invalid jsonpath item type for .datetime() argument" );
1703
-
1704
- template_str = jspGetString (& elem , & template_len );
1705
- template_txt = cstring_to_text_with_len (template_str ,
1706
- template_len );
1707
-
1708
1685
PG_TRY ();
1709
1686
{
1710
- value = to_datetime (datetime_txt ,
1711
- template_str , template_len ,
1712
- false,
1713
- & typid , & typmod );
1687
+ Datum unix_epoch =
1688
+ DirectFunctionCall1 (numeric_float8 ,
1689
+ NumericGetDatum (jb -> val .numeric ));
1690
+
1691
+ value = DirectFunctionCall1 (float8_timestamptz ,
1692
+ unix_epoch );
1693
+ typid = TIMESTAMPTZOID ;
1694
+ res = jperOk ;
1714
1695
}
1715
1696
PG_CATCH ();
1716
1697
{
@@ -1724,29 +1705,80 @@ recursiveExecuteNoUnwrap(JsonPathExecContext *cxt, JsonPathItem *jsp,
1724
1705
res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1725
1706
}
1726
1707
PG_END_TRY ();
1727
-
1728
- pfree (template_txt );
1729
1708
}
1730
- else
1709
+ else if ( jb -> type == jbvString )
1731
1710
{
1732
- if (!tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS TZH:TZM" ,
1711
+
1712
+ text * datetime_txt =
1713
+ cstring_to_text_with_len (jb -> val .string .val ,
1714
+ jb -> val .string .len );
1715
+
1716
+ res = jperOk ;
1717
+
1718
+ if (jsp -> content .arg )
1719
+ {
1720
+ text * template_txt ;
1721
+ char * template_str ;
1722
+ int template_len ;
1723
+ MemoryContext mcxt = CurrentMemoryContext ;
1724
+
1725
+ jspGetArg (jsp , & elem );
1726
+
1727
+ if (elem .type != jpiString )
1728
+ elog (ERROR , "invalid jsonpath item type for .datetime() argument" );
1729
+
1730
+ template_str = jspGetString (& elem , & template_len );
1731
+ template_txt = cstring_to_text_with_len (template_str ,
1732
+ template_len );
1733
+
1734
+ PG_TRY ();
1735
+ {
1736
+ value = to_datetime (datetime_txt ,
1737
+ template_str , template_len ,
1738
+ false,
1739
+ & typid , & typmod );
1740
+ }
1741
+ PG_CATCH ();
1742
+ {
1743
+ if (ERRCODE_TO_CATEGORY (geterrcode ()) !=
1744
+ ERRCODE_DATA_EXCEPTION )
1745
+ PG_RE_THROW ();
1746
+
1747
+ FlushErrorState ();
1748
+ MemoryContextSwitchTo (mcxt );
1749
+
1750
+ res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1751
+ }
1752
+ PG_END_TRY ();
1753
+
1754
+ pfree (template_txt );
1755
+ }
1756
+ else
1757
+ {
1758
+ if (!tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS TZH:TZM" ,
1733
1759
datetime_txt , & value , & typid , & typmod ) &&
1734
- !tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS TZH" ,
1760
+ !tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS TZH" ,
1735
1761
datetime_txt , & value , & typid , & typmod ) &&
1736
- !tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS" ,
1762
+ !tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS" ,
1737
1763
datetime_txt , & value , & typid , & typmod ) &&
1738
- !tryToParseDatetime ("yyyy-mm-dd" ,
1764
+ !tryToParseDatetime ("yyyy-mm-dd" ,
1739
1765
datetime_txt , & value , & typid , & typmod ) &&
1740
- !tryToParseDatetime ("HH24:MI:SS TZH:TZM" ,
1766
+ !tryToParseDatetime ("HH24:MI:SS TZH:TZM" ,
1741
1767
datetime_txt , & value , & typid , & typmod ) &&
1742
- !tryToParseDatetime ("HH24:MI:SS TZH" ,
1768
+ !tryToParseDatetime ("HH24:MI:SS TZH" ,
1743
1769
datetime_txt , & value , & typid , & typmod ) &&
1744
- !tryToParseDatetime ("HH24:MI:SS" ,
1770
+ !tryToParseDatetime ("HH24:MI:SS" ,
1745
1771
datetime_txt , & value , & typid , & typmod ))
1746
- res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1747
- }
1772
+ res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1773
+ }
1748
1774
1749
- pfree (datetime_txt );
1775
+ pfree (datetime_txt );
1776
+ }
1777
+ else
1778
+ {
1779
+ res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1780
+ break ;
1781
+ }
1750
1782
1751
1783
if (jperIsError (res ))
1752
1784
break ;
0 commit comments