pub fn to_jsonb(ecx: &ExprContext, expr: HirScalarExpr) -> HirScalarExpr {
    use ScalarType::*;

    match ecx.scalar_type(&expr) {
        Bool | Jsonb | Numeric { .. } => expr.call_unary(UnaryFunc::CastJsonbOrNullToJsonb),
        Int16 | Int32 | Float32 | Float64 => plan_cast(
            "to_jsonb",
            ecx,
            CastContext::Explicit,
            expr,
            &Numeric { scale: None },
        )
        .expect("cast known to exist")
        .call_unary(UnaryFunc::CastJsonbOrNullToJsonb),
        Record { fields, .. } => {
            let mut exprs = vec![];
            for (i, (name, _ty)) in fields.iter().enumerate() {
                exprs.push(HirScalarExpr::literal(
                    Datum::String(name.as_str()),
                    ScalarType::String,
                ));
                exprs.push(to_jsonb(
                    ecx,
                    expr.clone().call_unary(UnaryFunc::RecordGet(i)),
                ));
            }
            HirScalarExpr::CallVariadic {
                func: VariadicFunc::JsonbBuildObject,
                exprs,
            }
        }
        _ => to_string(ecx, expr).call_unary(UnaryFunc::CastJsonbOrNullToJsonb),
    }
}
/// Converts an expression to `ScalarType::Jsonb`.
///
/// The rules are as follows:
///   * `ScalarType::Boolean`s become JSON booleans.
///   * All numeric types are converted to `Float64`s, then become JSON numbers.
///   * Records are converted to a JSON object where the record's field names
///     are the keys of the object, and the record's fields are recursively
///     converted to JSON by `to_jsonb`.
///   * Other types are converted to strings by their usual cast function an
//      become JSON strings.
///   * A `Datum::Null` of any type becomes a JSON null.
Converts an expression to `ScalarType::Jsonb`.
The rules are as follows:
`ScalarType::Boolean`s become JSON booleans.
All numeric types are converted to `Float64`s, then become JSON numbers.
Records are converted to a JSON object where the record's field names
are the keys of the object, and the record's fields are recursively
converted to JSON by `to_jsonb`.
Other types are converted to strings by their usual cast function an
become JSON strings.
A `Datum::Null` of any type becomes a JSON null.
Converts an expression to ` ScalarType :: Jsonb ` . The rules are as follows : ` ScalarType :: Boolean ` s become JSON booleans . All numeric types are converted to ` Float64 ` s then become JSON numbers . Records are converted to a JSON object where the record ' s field names are the keys of the object and the record ' s fields are recursively converted to JSON by ` to_jsonb ` . Other types are converted to strings by their usual cast function an become JSON strings . A ` Datum :: Null ` of any type becomes a JSON null .
pub fn to_jsonb(ecx: &ExprContext, expr: HirScalarExpr) -> HirScalarExpr {
    use ScalarType::*;
    match ecx.scalar_type(&expr) {
        Bool | Jsonb | Numeric { .. } => expr.call_unary(UnaryFunc::CastJsonbOrNullToJsonb),
        Int16 | Int32 | Float32 | Float64 => plan_cast(
            "to_jsonb",
            ecx,
            CastContext::Explicit,
            expr,
            &Numeric { scale: None },
        )
        .expect("cast known to exist")
        .call_unary(UnaryFunc::CastJsonbOrNullToJsonb),
        Record { fields, .. } => {
            let mut exprs = vec![];
            for (i, (name, _ty)) in fields.iter().enumerate() {
                exprs.push(HirScalarExpr::literal(
                    Datum::String(name.as_str()),
                    ScalarType::String,
                ));
                exprs.push(to_jsonb(
                    ecx,
                    expr.clone().call_unary(UnaryFunc::RecordGet(i)),
                ));
            }
            HirScalarExpr::CallVariadic {
                func: VariadicFunc::JsonbBuildObject,
                exprs,
            }
        }
        _ => to_string(ecx, expr).call_unary(UnaryFunc::CastJsonbOrNullToJsonb),
    }
}
pub fn to_jsonb ( ecx : & ExprContext , expr : HirScalarExpr ) -> HirScalarExpr { use ScalarType :: * ; match ecx . scalar_type ( & expr ) { Bool | Jsonb | Numeric { .. } => expr . call_unary ( UnaryFunc :: CastJsonbOrNullToJsonb ) , Int16 | Int32 | Float32 | Float64 => plan_cast ( " " , ecx , CastContext :: Explicit , expr , & Numeric { scale : None } , ) . expect ( " " ) . call_unary ( UnaryFunc :: CastJsonbOrNullToJsonb ) , Record { fields , .. } => { let mut exprs = vec ! [ ] ; for ( i , ( name , _ty ) ) in fields . iter ( ) . enumerate ( ) { exprs . push ( HirScalarExpr :: literal ( Datum :: String ( name . as_str ( ) ) , ScalarType :: String , ) ) ; exprs . push ( to_jsonb ( ecx , expr . clone ( ) . call_unary ( UnaryFunc :: RecordGet ( i ) ) , ) ) ; } HirScalarExpr :: CallVariadic { func : VariadicFunc :: JsonbBuildObject , exprs , } } _ => to_string ( ecx , expr ) . call_unary ( UnaryFunc :: CastJsonbOrNullToJsonb ) , } }
Converts an expression to `ScalarType::Jsonb`.
Converts an expression to ` ScalarType :: Jsonb ` .
ecx
&ExprContext
expr
HirScalarExpr
ecx
&ExprContext
expr
HirScalarExpr