@@ -51,20 +51,23 @@ jsonGetField(Datum obj, const char *field)
51
51
*
52
52
* This expects the JSONB value to be a numeric, because that's how we store
53
53
* floats in JSONB, and we cast it to float4.
54
- *
55
- * XXX Not sure assert is a sufficient protection against different types of
56
- * JSONB values to be passed in.
57
54
*/
58
- static inline Datum
59
- jsonGetFloat4 (Datum jsonb )
55
+ static inline float4
56
+ jsonGetFloat4 (Datum jsonb , float4 default_val )
60
57
{
61
- Jsonb * jb = DatumGetJsonbP ( jsonb ) ;
58
+ Jsonb * jb ;
62
59
JsonbValue jv ;
63
60
64
- JsonbExtractScalar (& jb -> root , & jv );
65
- Assert (jv .type == jbvNumeric );
61
+ if (!DatumGetPointer (jsonb ))
62
+ return default_val ;
63
+
64
+ jb = DatumGetJsonbP (jsonb );
66
65
67
- return DirectFunctionCall1 (numeric_float4 , NumericGetDatum (jv .val .numeric ));
66
+ if (!JsonbExtractScalar (& jb -> root , & jv ) || jv .type != jbvNumeric )
67
+ return default_val ;
68
+
69
+ return DatumGetFloat4 (DirectFunctionCall1 (numeric_float4 ,
70
+ NumericGetDatum (jv .val .numeric )));
68
71
}
69
72
70
73
/*
@@ -663,12 +666,9 @@ jsonPathStatsExtractData(JsonPathStats pstats, JsonStatType stattype,
663
666
hst = jsonGetField (data , "histogram" );
664
667
corr = jsonGetField (data , "correlation" );
665
668
666
- statdata -> nullfrac = DatumGetPointer (nullf ) ?
667
- DatumGetFloat4 (jsonGetFloat4 (nullf )) : 0.0 ;
668
- statdata -> distinct = DatumGetPointer (dist ) ?
669
- DatumGetFloat4 (jsonGetFloat4 (dist )) : 0.0 ;
670
- statdata -> width = DatumGetPointer (width ) ?
671
- (int32 ) DatumGetFloat4 (jsonGetFloat4 (width )) : 0 ;
669
+ statdata -> nullfrac = jsonGetFloat4 (nullf , 0 );
670
+ statdata -> distinct = jsonGetFloat4 (dist , 0 );
671
+ statdata -> width = (int32 ) jsonGetFloat4 (width , 0 );
672
672
673
673
statdata -> nullfrac += (1.0 - statdata -> nullfrac ) * nullfrac ;
674
674
@@ -697,7 +697,8 @@ jsonPathStatsExtractData(JsonPathStats pstats, JsonStatType stattype,
697
697
698
698
if (DatumGetPointer (corr ))
699
699
{
700
- Datum correlation = jsonGetFloat4 (corr );
700
+ Datum correlation = Float4GetDatum (jsonGetFloat4 (corr , 0 ));
701
+
701
702
slot -> kind = STATISTIC_KIND_CORRELATION ;
702
703
slot -> opid = ltop ;
703
704
slot -> numbers = PointerGetDatum (construct_array (& correlation , 1 ,
@@ -726,15 +727,12 @@ jsonPathStatsExtractData(JsonPathStats pstats, JsonStatType stattype,
726
727
}
727
728
728
729
static float4
729
- jsonPathStatsGetFloat (JsonPathStats pstats , const char * key ,
730
- float4 defaultval )
730
+ jsonPathStatsGetFloat (JsonPathStats pstats , const char * key , float4 defaultval )
731
731
{
732
- Datum freq ;
733
-
734
- if (!pstats || !(freq = jsonGetField (* pstats -> datum , key )))
732
+ if (!pstats )
735
733
return defaultval ;
736
734
737
- return DatumGetFloat4 ( jsonGetFloat4 (freq ) );
735
+ return jsonGetFloat4 (jsonGetField ( * pstats -> datum , key ), defaultval );
738
736
}
739
737
740
738
float4
0 commit comments