@@ -1839,47 +1839,28 @@ recursiveExecuteNoUnwrap(JsonPathExecContext *cxt, JsonPathItem *jsp,
1839
1839
{
1840
1840
JsonbValue jbvbuf ;
1841
1841
Datum value ;
1842
- text * datetime_txt ;
1843
1842
Oid typid ;
1844
1843
int32 typmod = -1 ;
1845
1844
bool hasNext ;
1846
1845
1847
1846
if (JsonbType (jb ) == jbvScalar )
1848
1847
jb = JsonbExtractScalar (jb -> val .binary .data , & jbvbuf );
1849
1848
1850
- if (jb -> type != jbvString )
1849
+ if (jb -> type == jbvNumeric && ! jsp -> content . arg )
1851
1850
{
1852
- res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1853
- break ;
1854
- }
1855
-
1856
- datetime_txt = cstring_to_text_with_len (jb -> val .string .val ,
1857
- jb -> val .string .len );
1858
-
1859
- res = jperOk ;
1860
-
1861
- if (jsp -> content .arg )
1862
- {
1863
- text * template_txt ;
1864
- char * template_str ;
1865
- int template_len ;
1851
+ /* Standard extension: unix epoch to timestamptz */
1866
1852
MemoryContext mcxt = CurrentMemoryContext ;
1867
1853
1868
- jspGetArg (jsp , & elem );
1869
-
1870
- if (elem .type != jpiString )
1871
- elog (ERROR , "invalid jsonpath item type for .datetime() argument" );
1872
-
1873
- template_str = jspGetString (& elem , & template_len );
1874
- template_txt = cstring_to_text_with_len (template_str ,
1875
- template_len );
1876
-
1877
1854
PG_TRY ();
1878
1855
{
1879
- value = to_datetime (datetime_txt ,
1880
- template_str , template_len ,
1881
- false,
1882
- & typid , & typmod );
1856
+ Datum unix_epoch =
1857
+ DirectFunctionCall1 (numeric_float8 ,
1858
+ NumericGetDatum (jb -> val .numeric ));
1859
+
1860
+ value = DirectFunctionCall1 (float8_timestamptz ,
1861
+ unix_epoch );
1862
+ typid = TIMESTAMPTZOID ;
1863
+ res = jperOk ;
1883
1864
}
1884
1865
PG_CATCH ();
1885
1866
{
@@ -1893,29 +1874,80 @@ recursiveExecuteNoUnwrap(JsonPathExecContext *cxt, JsonPathItem *jsp,
1893
1874
res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1894
1875
}
1895
1876
PG_END_TRY ();
1896
-
1897
- pfree (template_txt );
1898
1877
}
1899
- else
1878
+ else if ( jb -> type == jbvString )
1900
1879
{
1901
- if (!tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS TZH:TZM" ,
1880
+
1881
+ text * datetime_txt =
1882
+ cstring_to_text_with_len (jb -> val .string .val ,
1883
+ jb -> val .string .len );
1884
+
1885
+ res = jperOk ;
1886
+
1887
+ if (jsp -> content .arg )
1888
+ {
1889
+ text * template_txt ;
1890
+ char * template_str ;
1891
+ int template_len ;
1892
+ MemoryContext mcxt = CurrentMemoryContext ;
1893
+
1894
+ jspGetArg (jsp , & elem );
1895
+
1896
+ if (elem .type != jpiString )
1897
+ elog (ERROR , "invalid jsonpath item type for .datetime() argument" );
1898
+
1899
+ template_str = jspGetString (& elem , & template_len );
1900
+ template_txt = cstring_to_text_with_len (template_str ,
1901
+ template_len );
1902
+
1903
+ PG_TRY ();
1904
+ {
1905
+ value = to_datetime (datetime_txt ,
1906
+ template_str , template_len ,
1907
+ false,
1908
+ & typid , & typmod );
1909
+ }
1910
+ PG_CATCH ();
1911
+ {
1912
+ if (ERRCODE_TO_CATEGORY (geterrcode ()) !=
1913
+ ERRCODE_DATA_EXCEPTION )
1914
+ PG_RE_THROW ();
1915
+
1916
+ FlushErrorState ();
1917
+ MemoryContextSwitchTo (mcxt );
1918
+
1919
+ res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1920
+ }
1921
+ PG_END_TRY ();
1922
+
1923
+ pfree (template_txt );
1924
+ }
1925
+ else
1926
+ {
1927
+ if (!tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS TZH:TZM" ,
1902
1928
datetime_txt , & value , & typid , & typmod ) &&
1903
- !tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS TZH" ,
1929
+ !tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS TZH" ,
1904
1930
datetime_txt , & value , & typid , & typmod ) &&
1905
- !tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS" ,
1931
+ !tryToParseDatetime ("yyyy-mm-dd HH24:MI:SS" ,
1906
1932
datetime_txt , & value , & typid , & typmod ) &&
1907
- !tryToParseDatetime ("yyyy-mm-dd" ,
1933
+ !tryToParseDatetime ("yyyy-mm-dd" ,
1908
1934
datetime_txt , & value , & typid , & typmod ) &&
1909
- !tryToParseDatetime ("HH24:MI:SS TZH:TZM" ,
1935
+ !tryToParseDatetime ("HH24:MI:SS TZH:TZM" ,
1910
1936
datetime_txt , & value , & typid , & typmod ) &&
1911
- !tryToParseDatetime ("HH24:MI:SS TZH" ,
1937
+ !tryToParseDatetime ("HH24:MI:SS TZH" ,
1912
1938
datetime_txt , & value , & typid , & typmod ) &&
1913
- !tryToParseDatetime ("HH24:MI:SS" ,
1939
+ !tryToParseDatetime ("HH24:MI:SS" ,
1914
1940
datetime_txt , & value , & typid , & typmod ))
1915
- res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1916
- }
1941
+ res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1942
+ }
1917
1943
1918
- pfree (datetime_txt );
1944
+ pfree (datetime_txt );
1945
+ }
1946
+ else
1947
+ {
1948
+ res = jperMakeError (ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION );
1949
+ break ;
1950
+ }
1919
1951
1920
1952
if (jperIsError (res ))
1921
1953
break ;
0 commit comments