Skip to content

Commit

Permalink
Add support for serializing null scalar values (#3303)
Browse files Browse the repository at this point in the history
  • Loading branch information
andygrove authored Aug 31, 2022
1 parent fcd2275 commit 70322e5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
4 changes: 1 addition & 3 deletions datafusion/proto/src/from_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,7 @@ impl TryFrom<&protobuf::PrimitiveScalarType> for ScalarValue {
use protobuf::PrimitiveScalarType;

Ok(match scalar {
PrimitiveScalarType::Null => {
return Err(proto_error("Untyped null is an invalid scalar value"));
}
PrimitiveScalarType::Null => Self::Null,
PrimitiveScalarType::Bool => Self::Boolean(None),
PrimitiveScalarType::Uint8 => Self::UInt8(None),
PrimitiveScalarType::Int8 => Self::Int8(None),
Expand Down
20 changes: 20 additions & 0 deletions datafusion/proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,26 @@ mod roundtrip_tests {
roundtrip_expr_test(test_expr, ctx);
}

#[test]
fn roundtrip_case_with_null() {
let test_expr = Expr::Case {
expr: Some(Box::new(lit(1.0_f32))),
when_then_expr: vec![(Box::new(lit(2.0_f32)), Box::new(lit(3.0_f32)))],
else_expr: Some(Box::new(Expr::Literal(ScalarValue::Null))),
};

let ctx = SessionContext::new();
roundtrip_expr_test(test_expr, ctx);
}

#[test]
fn roundtrip_null_literal() {
let test_expr = Expr::Literal(ScalarValue::Null);

let ctx = SessionContext::new();
roundtrip_expr_test(test_expr, ctx);
}

#[test]
fn roundtrip_cast() {
let test_expr = Expr::Cast {
Expand Down
3 changes: 3 additions & 0 deletions datafusion/proto/src/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,9 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
Value::IntervalDaytimeValue(*s)
})
}
ScalarValue::Null => protobuf::ScalarValue {
value: Some(Value::NullValue(PrimitiveScalarType::Null as i32)),
},
_ => {
return Err(Error::invalid_scalar_value(val));
}
Expand Down

0 comments on commit 70322e5

Please sign in to comment.