1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- use std:: collections:: HashSet ;
15+ use std:: collections:: { BTreeMap , HashMap , HashSet } ;
1616use std:: sync:: Arc ;
1717
1818use common_decimal:: Decimal128 ;
1919use common_decimal:: decimal128:: { DECIMAL128_DEFAULT_SCALE , DECIMAL128_MAX_PRECISION } ;
2020use common_time:: time:: Time ;
2121use common_time:: timestamp:: TimeUnit ;
2222use common_time:: { Date , IntervalDayTime , IntervalMonthDayNano , IntervalYearMonth , Timestamp } ;
23+ use datatypes:: json:: value:: { JsonNumber , JsonValue , JsonValueRef , JsonVariant } ;
2324use datatypes:: prelude:: { ConcreteDataType , ValueRef } ;
2425use datatypes:: types:: {
2526 IntervalType , JsonFormat , StructField , StructType , TimeType , TimestampType ,
@@ -34,9 +35,9 @@ use greptime_proto::v1::greptime_request::Request;
3435use greptime_proto:: v1:: query_request:: Query ;
3536use greptime_proto:: v1:: value:: ValueData ;
3637use greptime_proto:: v1:: {
37- self , ColumnDataTypeExtension , DdlRequest , DecimalTypeExtension , JsonNativeTypeExtension ,
38- JsonTypeExtension , ListTypeExtension , QueryRequest , Row , SemanticType , StructTypeExtension ,
39- VectorTypeExtension ,
38+ self , ColumnDataTypeExtension , DdlRequest , DecimalTypeExtension , JsonList ,
39+ JsonNativeTypeExtension , JsonObject , JsonTypeExtension , ListTypeExtension , QueryRequest , Row ,
40+ SemanticType , StructTypeExtension , VectorTypeExtension , json_value ,
4041} ;
4142use paste:: paste;
4243use snafu:: prelude:: * ;
@@ -801,21 +802,8 @@ pub fn pb_value_to_value_ref<'a>(
801802 }
802803
803804 ValueData :: JsonValue ( inner_value) => {
804- let json_datatype_ext = datatype_ext
805- . as_ref ( )
806- . and_then ( |ext| {
807- if let Some ( TypeExt :: JsonNativeType ( l) ) = & ext. type_ext {
808- Some ( l)
809- } else {
810- None
811- }
812- } )
813- . expect ( "json value must contain datatype ext" ) ;
814-
815- ValueRef :: Json ( Box :: new ( pb_value_to_value_ref (
816- inner_value,
817- json_datatype_ext. datatype_extension . as_deref ( ) ,
818- ) ) )
805+ let value = decode_json_value ( inner_value) ;
806+ ValueRef :: Json ( Box :: new ( value) )
819807 }
820808 }
821809}
@@ -938,12 +926,64 @@ pub fn to_proto_value(value: Value) -> v1::Value {
938926 } ) ) ,
939927 } ,
940928 Value :: Json ( v) => v1:: Value {
941- value_data : Some ( ValueData :: JsonValue ( Box :: new ( to_proto_value ( * v ) ) ) ) ,
929+ value_data : Some ( ValueData :: JsonValue ( encode_json_value ( * v ) ) ) ,
942930 } ,
943931 Value :: Duration ( _) => v1:: Value { value_data : None } ,
944932 }
945933}
946934
935+ fn encode_json_value ( value : JsonValue ) -> v1:: JsonValue {
936+ fn helper ( json : JsonVariant ) -> v1:: JsonValue {
937+ let value = match json {
938+ JsonVariant :: Null => None ,
939+ JsonVariant :: Bool ( x) => Some ( json_value:: Value :: Boolean ( x) ) ,
940+ JsonVariant :: Number ( x) => Some ( match x {
941+ JsonNumber :: PosInt ( i) => json_value:: Value :: Uint ( i) ,
942+ JsonNumber :: NegInt ( i) => json_value:: Value :: Int ( i) ,
943+ JsonNumber :: Float ( f) => json_value:: Value :: Float ( f. 0 ) ,
944+ } ) ,
945+ JsonVariant :: String ( x) => Some ( json_value:: Value :: Str ( x) ) ,
946+ JsonVariant :: Array ( x) => Some ( json_value:: Value :: Array ( JsonList {
947+ items : x. into_iter ( ) . map ( helper) . collect :: < Vec < _ > > ( ) ,
948+ } ) ) ,
949+ JsonVariant :: Object ( x) => {
950+ let entries = x
951+ . into_iter ( )
952+ . map ( |( k, v) | ( k, helper ( v) ) )
953+ . collect :: < HashMap < _ , _ > > ( ) ;
954+ Some ( json_value:: Value :: Object ( JsonObject { entries } ) )
955+ }
956+ } ;
957+ v1:: JsonValue { value }
958+ }
959+ helper ( value. into_variant ( ) )
960+ }
961+
962+ fn decode_json_value ( value : & v1:: JsonValue ) -> JsonValueRef < ' _ > {
963+ let Some ( value) = & value. value else {
964+ return ( ) . into ( ) ;
965+ } ;
966+ match value {
967+ json_value:: Value :: Boolean ( x) => ( * x) . into ( ) ,
968+ json_value:: Value :: Int ( x) => ( * x) . into ( ) ,
969+ json_value:: Value :: Uint ( x) => ( * x) . into ( ) ,
970+ json_value:: Value :: Float ( x) => ( * x) . into ( ) ,
971+ json_value:: Value :: Str ( x) => ( x. as_str ( ) ) . into ( ) ,
972+ json_value:: Value :: Array ( array) => array
973+ . items
974+ . iter ( )
975+ . map ( |x| decode_json_value ( x) . into_variant ( ) )
976+ . collect :: < Vec < _ > > ( )
977+ . into ( ) ,
978+ json_value:: Value :: Object ( x) => x
979+ . entries
980+ . iter ( )
981+ . map ( |( k, v) | ( k. as_str ( ) , decode_json_value ( v) . into_variant ( ) ) )
982+ . collect :: < BTreeMap < _ , _ > > ( )
983+ . into ( ) ,
984+ }
985+ }
986+
947987fn convert_list_to_pb_values ( list_value : ListValue ) -> Vec < v1:: Value > {
948988 list_value
949989 . take_items ( )
@@ -1065,9 +1105,7 @@ pub fn value_to_grpc_value(value: Value) -> GrpcValue {
10651105 . collect ( ) ;
10661106 Some ( ValueData :: StructValue ( v1:: StructValue { items } ) )
10671107 }
1068- Value :: Json ( inner_value) => Some ( ValueData :: JsonValue ( Box :: new ( value_to_grpc_value (
1069- * inner_value,
1070- ) ) ) ) ,
1108+ Value :: Json ( v) => Some ( ValueData :: JsonValue ( encode_json_value ( * v) ) ) ,
10711109 Value :: Duration ( _) => unreachable ! ( ) ,
10721110 } ,
10731111 }
@@ -1778,4 +1816,202 @@ mod tests {
17781816 _ => panic ! ( "Unexpected value type" ) ,
17791817 }
17801818 }
1819+
1820+ #[ test]
1821+ fn test_encode_decode_json_value ( ) {
1822+ let json: JsonValue = ( ) . into ( ) ;
1823+ let proto = encode_json_value ( json. clone ( ) ) ;
1824+ assert ! ( proto. value. is_none( ) ) ;
1825+ let value = decode_json_value ( & proto) ;
1826+ assert_eq ! ( json. as_ref( ) , value) ;
1827+
1828+ let json: JsonValue = true . into ( ) ;
1829+ let proto = encode_json_value ( json. clone ( ) ) ;
1830+ assert_eq ! ( proto. value, Some ( json_value:: Value :: Boolean ( true ) ) ) ;
1831+ let value = decode_json_value ( & proto) ;
1832+ assert_eq ! ( json. as_ref( ) , value) ;
1833+
1834+ let json: JsonValue = ( -1i64 ) . into ( ) ;
1835+ let proto = encode_json_value ( json. clone ( ) ) ;
1836+ assert_eq ! ( proto. value, Some ( json_value:: Value :: Int ( -1 ) ) ) ;
1837+ let value = decode_json_value ( & proto) ;
1838+ assert_eq ! ( json. as_ref( ) , value) ;
1839+
1840+ let json: JsonValue = 1u64 . into ( ) ;
1841+ let proto = encode_json_value ( json. clone ( ) ) ;
1842+ assert_eq ! ( proto. value, Some ( json_value:: Value :: Uint ( 1 ) ) ) ;
1843+ let value = decode_json_value ( & proto) ;
1844+ assert_eq ! ( json. as_ref( ) , value) ;
1845+
1846+ let json: JsonValue = 1.0f64 . into ( ) ;
1847+ let proto = encode_json_value ( json. clone ( ) ) ;
1848+ assert_eq ! ( proto. value, Some ( json_value:: Value :: Float ( 1.0 ) ) ) ;
1849+ let value = decode_json_value ( & proto) ;
1850+ assert_eq ! ( json. as_ref( ) , value) ;
1851+
1852+ let json: JsonValue = "s" . into ( ) ;
1853+ let proto = encode_json_value ( json. clone ( ) ) ;
1854+ assert_eq ! ( proto. value, Some ( json_value:: Value :: Str ( "s" . to_string( ) ) ) ) ;
1855+ let value = decode_json_value ( & proto) ;
1856+ assert_eq ! ( json. as_ref( ) , value) ;
1857+
1858+ let json: JsonValue = [ 1i64 , 2 , 3 ] . into ( ) ;
1859+ let proto = encode_json_value ( json. clone ( ) ) ;
1860+ assert_eq ! (
1861+ proto. value,
1862+ Some ( json_value:: Value :: Array ( JsonList {
1863+ items: vec![
1864+ v1:: JsonValue {
1865+ value: Some ( json_value:: Value :: Int ( 1 ) )
1866+ } ,
1867+ v1:: JsonValue {
1868+ value: Some ( json_value:: Value :: Int ( 2 ) )
1869+ } ,
1870+ v1:: JsonValue {
1871+ value: Some ( json_value:: Value :: Int ( 3 ) )
1872+ }
1873+ ]
1874+ } ) )
1875+ ) ;
1876+ let value = decode_json_value ( & proto) ;
1877+ assert_eq ! ( json. as_ref( ) , value) ;
1878+
1879+ let json: JsonValue = [ ( ) ; 0 ] . into ( ) ;
1880+ let proto = encode_json_value ( json. clone ( ) ) ;
1881+ assert_eq ! (
1882+ proto. value,
1883+ Some ( json_value:: Value :: Array ( JsonList { items: vec![ ] } ) )
1884+ ) ;
1885+ let value = decode_json_value ( & proto) ;
1886+ assert_eq ! ( json. as_ref( ) , value) ;
1887+
1888+ let json: JsonValue = [ ( "k1" , 1i64 ) , ( "k2" , 2i64 ) , ( "k3" , 3i64 ) ] . into ( ) ;
1889+ let proto = encode_json_value ( json. clone ( ) ) ;
1890+ assert_eq ! (
1891+ proto. value,
1892+ Some ( json_value:: Value :: Object ( JsonObject {
1893+ entries: [
1894+ (
1895+ "k1" . to_string( ) ,
1896+ v1:: JsonValue {
1897+ value: Some ( json_value:: Value :: Int ( 1 ) )
1898+ }
1899+ ) ,
1900+ (
1901+ "k2" . to_string( ) ,
1902+ v1:: JsonValue {
1903+ value: Some ( json_value:: Value :: Int ( 2 ) )
1904+ }
1905+ ) ,
1906+ (
1907+ "k3" . to_string( ) ,
1908+ v1:: JsonValue {
1909+ value: Some ( json_value:: Value :: Int ( 3 ) )
1910+ }
1911+ )
1912+ ]
1913+ . into( ) ,
1914+ } ) )
1915+ ) ;
1916+ let value = decode_json_value ( & proto) ;
1917+ assert_eq ! ( json. as_ref( ) , value) ;
1918+
1919+ let json: JsonValue = [ ( "null" , ( ) ) ; 0 ] . into ( ) ;
1920+ let proto = encode_json_value ( json. clone ( ) ) ;
1921+ assert_eq ! (
1922+ proto. value,
1923+ Some ( json_value:: Value :: Object ( JsonObject {
1924+ entries: HashMap :: new( ) ,
1925+ } ) )
1926+ ) ;
1927+ let value = decode_json_value ( & proto) ;
1928+ assert_eq ! ( json. as_ref( ) , value) ;
1929+
1930+ let json: JsonValue = [
1931+ ( "null" , JsonVariant :: from ( ( ) ) ) ,
1932+ ( "bool" , false . into ( ) ) ,
1933+ ( "list" , [ "hello" , "world" ] . into ( ) ) ,
1934+ (
1935+ "object" ,
1936+ [
1937+ ( "positive_i" , JsonVariant :: from ( 42u64 ) ) ,
1938+ ( "negative_i" , ( -42i64 ) . into ( ) ) ,
1939+ ( "nested" , [ ( "what" , "blah" ) ] . into ( ) ) ,
1940+ ]
1941+ . into ( ) ,
1942+ ) ,
1943+ ]
1944+ . into ( ) ;
1945+ let proto = encode_json_value ( json. clone ( ) ) ;
1946+ assert_eq ! (
1947+ proto. value,
1948+ Some ( json_value:: Value :: Object ( JsonObject {
1949+ entries: [
1950+ ( "null" . to_string( ) , v1:: JsonValue { value: None } ) ,
1951+ (
1952+ "bool" . to_string( ) ,
1953+ v1:: JsonValue {
1954+ value: Some ( json_value:: Value :: Boolean ( false ) )
1955+ }
1956+ ) ,
1957+ (
1958+ "list" . to_string( ) ,
1959+ v1:: JsonValue {
1960+ value: Some ( json_value:: Value :: Array ( JsonList {
1961+ items: vec![
1962+ v1:: JsonValue {
1963+ value: Some ( json_value:: Value :: Str ( "hello" . to_string( ) ) )
1964+ } ,
1965+ v1:: JsonValue {
1966+ value: Some ( json_value:: Value :: Str ( "world" . to_string( ) ) )
1967+ } ,
1968+ ]
1969+ } ) )
1970+ }
1971+ ) ,
1972+ (
1973+ "object" . to_string( ) ,
1974+ v1:: JsonValue {
1975+ value: Some ( json_value:: Value :: Object ( JsonObject {
1976+ entries: [
1977+ (
1978+ "positive_i" . to_string( ) ,
1979+ v1:: JsonValue {
1980+ value: Some ( json_value:: Value :: Uint ( 42 ) )
1981+ }
1982+ ) ,
1983+ (
1984+ "negative_i" . to_string( ) ,
1985+ v1:: JsonValue {
1986+ value: Some ( json_value:: Value :: Int ( -42 ) )
1987+ }
1988+ ) ,
1989+ (
1990+ "nested" . to_string( ) ,
1991+ v1:: JsonValue {
1992+ value: Some ( json_value:: Value :: Object ( JsonObject {
1993+ entries: [ (
1994+ "what" . to_string( ) ,
1995+ v1:: JsonValue {
1996+ value: Some ( json_value:: Value :: Str (
1997+ "blah" . to_string( )
1998+ ) )
1999+ }
2000+ ) ]
2001+ . into( )
2002+ } ) )
2003+ }
2004+ )
2005+ ]
2006+ . into( ) ,
2007+ } ) )
2008+ }
2009+ ) ,
2010+ ]
2011+ . into( ) ,
2012+ } ) )
2013+ ) ;
2014+ let value = decode_json_value ( & proto) ;
2015+ assert_eq ! ( json. as_ref( ) , value) ;
2016+ }
17812017}
0 commit comments