Skip to content

Commit

Permalink
try send null type
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Apr 3, 2022
1 parent 9fad323 commit 4f12192
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ydb/src/client_table_test_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::transaction::Mode;
use crate::transaction::Mode::SerializableReadWrite;
use crate::transaction::Transaction;
use crate::types::{Value, ValueList, ValueStruct};
use crate::ydb_params;
use async_once::AsyncOnce;
use lazy_static::lazy_static;
use std::collections::HashMap;
Expand Down Expand Up @@ -335,6 +336,40 @@ SELECT $test AS test;
return Ok(());
}

#[tokio::test]
#[traced_test]
// #[ignore]
async fn send_null_value_as_nullable_param() -> YdbResult<()> {
let client = create_client().await?;
let table_client = client.table_client();
let res: Option<i32> = table_client
.retry_transaction(|tx| async {
let mut tx = tx;
let param = Value::Null;

let res = tx
.query(
Query::from(
"
DECLARE $res AS Int32?;
SELECT $res AS res;
",
)
.with_params(ydb_params!("$res" => param)),
)
.await?;
let mut row = res.into_only_row()?;
return Ok(row.remove_field_by_name("res")?);
})
.await?
.try_into()?;

assert_eq!(None, res);

return Ok(());
}

#[tokio::test]
#[traced_test]
#[ignore] // need YDB access
Expand Down
2 changes: 2 additions & 0 deletions ydb/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl Session {
return grpc_read_void_operation_result(resp);
}

#[tracing::instrument(skip(self))]
pub(crate) async fn execute_data_query(
&mut self,
mut req: ExecuteDataQueryRequest,
Expand All @@ -110,6 +111,7 @@ impl Session {
req.operation_params = operation_params(self.timeouts.operation_timeout)
}
let mut channel = self.get_channel().await?;
// trace!("execute data query: '{:?}'", req);
let response = channel.execute_data_query(req).await?;
let operation_result: ExecuteQueryResult = self.handle_operation_result(response)?;
return QueryResult::from_proto(operation_result, error_on_truncated);
Expand Down
19 changes: 19 additions & 0 deletions ydb/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const SECONDS_PER_DAY: u64 = 60 * 60 * 24;
#[non_exhaustive]
pub enum Value {
Void,
Null,
Bool(bool),
Int8(i8),
Uint8(u8),
Expand Down Expand Up @@ -280,6 +281,7 @@ impl Value {
_ => unimplemented!("{:?} ({})", P::from_i32(*t_id), *t_id),
},
T::VoidType(_) => Value::Void,
T::NullType(_) => Value::Null,
T::OptionalType(val) => {
let t = if let Some(item) = &val.item {
Some(*item.clone())
Expand Down Expand Up @@ -319,6 +321,7 @@ impl Value {
pub(crate) fn from_proto(t: &Value, proto_value: ydb_proto::Value) -> YdbResult<Self> {
let res = match (t, proto_value) {
(Value::Void, _) => Value::Void,
(Value::Null, _) => Value::Null,
(
t,
ydb_proto::Value {
Expand Down Expand Up @@ -461,6 +464,21 @@ impl Value {
..ydb_proto::Value::default()
}),
},
Self::Null => ydb_proto::TypedValue {
r#type: Some(ydb_proto::Type {
r#type: Some(ydb_proto::r#type::Type::NullType(
prost_types::NullValue::NullValue.into(),
)),
..ydb_proto::Type::default()
}),
value: Some(ydb_proto::Value {
value: Some(ydb_proto::value::Value::NullFlagValue(
prost_types::NullValue::NullValue.into(),
)),
..ydb_proto::Value::default()
}),
..ydb_proto::TypedValue::default()
},
Self::Bool(val) => proto_typed_value(pt::Bool, pv::BoolValue(val)),
Self::Int8(val) => proto_typed_value(pt::Int8, pv::Int32Value(val.into())),
Self::Uint8(val) => proto_typed_value(pt::Uint8, pv::Uint32Value(val.into())),
Expand Down Expand Up @@ -627,6 +645,7 @@ mod test {
num_tests!(values, Value::Double, f64);

values.push(Value::Void);
values.push(Value::Null);

values.push(Value::Date(std::time::Duration::from_secs(1633996800))); //Tue Oct 12 00:00:00 UTC 2021
values.push(Value::DateTime(std::time::Duration::from_secs(1634000523))); //Tue Oct 12 01:02:03 UTC 2021
Expand Down

0 comments on commit 4f12192

Please sign in to comment.