diff --git a/src/api/greptime/v1/column.proto b/src/api/greptime/v1/column.proto index 76c100387b..3abcbd54f7 100644 --- a/src/api/greptime/v1/column.proto +++ b/src/api/greptime/v1/column.proto @@ -49,12 +49,12 @@ message Column { bytes null_mask = 4; // Helpful in creating vector from column. - ColumnDataType datatype = 5; + optional ColumnDataType datatype = 5; } message ColumnDef { string name = 1; - ColumnDataType data_type = 2; + ColumnDataType datatype = 2; bool is_nullable = 3; } diff --git a/src/client/src/database.rs b/src/client/src/database.rs index 12ffd127be..997e3dc46d 100644 --- a/src/client/src/database.rs +++ b/src/client/src/database.rs @@ -23,7 +23,8 @@ use snafu::{ensure, OptionExt, ResultExt}; use crate::error; use crate::{ - error::DatanodeSnafu, error::DecodeSelectSnafu, error::EncodePhysicalSnafu, Client, Result, + error::DatanodeSnafu, error::DecodeSelectSnafu, error::EncodePhysicalSnafu, + error::MissingFieldSnafu, Client, Result, }; pub const PROTOCOL_VERSION: u32 = 1; @@ -214,8 +215,12 @@ impl TryFrom for Output { } fn column_to_vector(column: &Column, rows: u32) -> Result { - let wrapper = - ColumnDataTypeWrapper::try_new(column.datatype).context(error::ColumnDataTypeSnafu)?; + let wrapper = ColumnDataTypeWrapper::try_new( + column + .datatype + .context(MissingFieldSnafu { field: "datatype" })?, + ) + .context(error::ColumnDataTypeSnafu)?; let column_datatype = wrapper.datatype(); let rows = rows as usize; @@ -318,7 +323,7 @@ mod tests { #[test] fn test_column_to_vector() { let mut column = create_test_column(Arc::new(BooleanVector::from(vec![true]))); - column.datatype = -100; + column.datatype = Some(-100); let result = column_to_vector(&column, 1); assert!(result.is_err()); assert_eq!( @@ -396,7 +401,7 @@ mod tests { semantic_type: 1, values: Some(values(&[array.clone()]).unwrap()), null_mask: null_mask(&vec![array], vector.len()), - datatype: wrapper.datatype() as i32, + datatype: Some(wrapper.datatype() as i32), } } } diff --git a/src/client/src/error.rs b/src/client/src/error.rs index 7528fbdf69..29bbd20b7b 100644 --- a/src/client/src/error.rs +++ b/src/client/src/error.rs @@ -76,6 +76,9 @@ pub enum Error { err_msg: String, backtrace: Backtrace, }, + + #[snafu(display("Missing required field in protobuf, field: {}", field))] + MissingField { field: String, backtrace: Backtrace }, } pub type Result = std::result::Result; @@ -92,7 +95,8 @@ impl ErrorExt for Error { | Error::EncodePhysical { .. } | Error::MutateFailure { .. } | Error::InvalidColumnProto { .. } - | Error::ColumnDataType { .. } => StatusCode::Internal, + | Error::ColumnDataType { .. } + | Error::MissingField { .. } => StatusCode::Internal, Error::CreateVector { source } => source.status_code(), Error::CreateRecordBatches { source } => source.status_code(), Error::IllegalGrpcClientState { .. } => StatusCode::Unexpected, diff --git a/src/datanode/src/server/grpc/ddl.rs b/src/datanode/src/server/grpc/ddl.rs index 5e6b275fe9..94a7dce026 100644 --- a/src/datanode/src/server/grpc/ddl.rs +++ b/src/datanode/src/server/grpc/ddl.rs @@ -140,7 +140,7 @@ fn create_table_schema(expr: &CreateExpr) -> Result { fn create_column_schema(column_def: &ColumnDef) -> Result { let data_type = - ColumnDataTypeWrapper::try_new(column_def.data_type).context(error::ColumnDataTypeSnafu)?; + ColumnDataTypeWrapper::try_new(column_def.datatype).context(error::ColumnDataTypeSnafu)?; Ok(ColumnSchema { name: column_def.name.clone(), data_type: data_type.into(), @@ -204,7 +204,7 @@ mod tests { fn test_create_column_schema() { let column_def = ColumnDef { name: "a".to_string(), - data_type: 1024, + datatype: 1024, is_nullable: true, }; let result = create_column_schema(&column_def); @@ -216,7 +216,7 @@ mod tests { let column_def = ColumnDef { name: "a".to_string(), - data_type: 12, // string + datatype: 12, // string is_nullable: true, }; let column_schema = create_column_schema(&column_def).unwrap(); @@ -229,22 +229,22 @@ mod tests { let column_defs = vec![ ColumnDef { name: "host".to_string(), - data_type: 12, // string + datatype: 12, // string is_nullable: false, }, ColumnDef { name: "ts".to_string(), - data_type: 15, // timestamp + datatype: 15, // timestamp is_nullable: false, }, ColumnDef { name: "cpu".to_string(), - data_type: 9, // float32 + datatype: 9, // float32 is_nullable: true, }, ColumnDef { name: "memory".to_string(), - data_type: 10, // float64 + datatype: 10, // float64 is_nullable: true, }, ]; diff --git a/src/datanode/src/server/grpc/insert.rs b/src/datanode/src/server/grpc/insert.rs index 2aab556ff8..9b539b5eb1 100644 --- a/src/datanode/src/server/grpc/insert.rs +++ b/src/datanode/src/server/grpc/insert.rs @@ -365,7 +365,7 @@ mod tests { semantic_type: SEMANTIC_TS, values: Some(ts_vals), null_mask: vec![0], - datatype: 15, + datatype: Some(15), }; let insert_batch = InsertBatch { diff --git a/src/datanode/src/server/grpc/select.rs b/src/datanode/src/server/grpc/select.rs index 9088524679..d5a1975b9d 100644 --- a/src/datanode/src/server/grpc/select.rs +++ b/src/datanode/src/server/grpc/select.rs @@ -74,9 +74,11 @@ fn try_convert(record_batches: Vec) -> Result { column_name, values: Some(values(&arrays)?), null_mask: null_mask(&arrays, row_count), - datatype: ColumnDataTypeWrapper::try_from(schema.data_type.clone()) - .context(error::ColumnDataTypeSnafu)? - .datatype() as i32, + datatype: Some( + ColumnDataTypeWrapper::try_from(schema.data_type.clone()) + .context(error::ColumnDataTypeSnafu)? + .datatype() as i32, + ), ..Default::default() }; columns.push(column); diff --git a/src/datanode/src/tests/grpc_test.rs b/src/datanode/src/tests/grpc_test.rs index b54f28bcdb..215ce5d61a 100644 --- a/src/datanode/src/tests/grpc_test.rs +++ b/src/datanode/src/tests/grpc_test.rs @@ -48,7 +48,7 @@ async fn test_insert_and_select() { .collect(), ..Default::default() }), - datatype: 12, // string + datatype: Some(12), // string ..Default::default() }; let expected_cpu_col = Column { @@ -58,7 +58,7 @@ async fn test_insert_and_select() { ..Default::default() }), null_mask: vec![2], - datatype: 10, // float64 + datatype: Some(10), // float64 ..Default::default() }; let expected_mem_col = Column { @@ -68,7 +68,7 @@ async fn test_insert_and_select() { ..Default::default() }), null_mask: vec![4], - datatype: 10, // float64 + datatype: Some(10), // float64 ..Default::default() }; let expected_ts_col = Column { @@ -77,7 +77,7 @@ async fn test_insert_and_select() { ts_millis_values: vec![100, 101, 102, 103], ..Default::default() }), - datatype: 15, // timestamp + datatype: Some(15), // timestamp ..Default::default() }; @@ -95,7 +95,7 @@ async fn test_insert_and_select() { //alter let add_column = ColumnDef { name: "test_column".to_string(), - data_type: ColumnDataType::Int64.into(), + datatype: ColumnDataType::Int64.into(), is_nullable: true, }; let kind = Kind::AddColumn(AddColumn { @@ -160,22 +160,22 @@ fn testing_create_expr() -> CreateExpr { let column_defs = vec![ ColumnDef { name: "host".to_string(), - data_type: 12, // string + datatype: 12, // string is_nullable: false, }, ColumnDef { name: "cpu".to_string(), - data_type: 10, // float64 + datatype: 10, // float64 is_nullable: true, }, ColumnDef { name: "memory".to_string(), - data_type: 10, // float64 + datatype: 10, // float64 is_nullable: true, }, ColumnDef { name: "ts".to_string(), - data_type: 15, // timestamp + datatype: 15, // timestamp is_nullable: true, }, ]; diff --git a/src/frontend/src/instance.rs b/src/frontend/src/instance.rs index 60aa822813..254190fb74 100644 --- a/src/frontend/src/instance.rs +++ b/src/frontend/src/instance.rs @@ -211,7 +211,7 @@ fn columns_to_expr(column_defs: &[ColumnDef]) -> Result> { .zip(column_datatypes.into_iter()) .map(|(schema, datatype)| GrpcColumnDef { name: schema.name.clone(), - data_type: datatype as i32, + datatype: datatype as i32, is_nullable: schema.is_nullable, }) .collect::>()) @@ -345,7 +345,7 @@ mod tests { .collect(), ..Default::default() }), - datatype: 12, // string + datatype: Some(12), // string ..Default::default() }; let expected_cpu_col = Column { @@ -355,7 +355,7 @@ mod tests { ..Default::default() }), null_mask: vec![2], - datatype: 10, // float64 + datatype: Some(10), // float64 ..Default::default() }; let expected_mem_col = Column { @@ -365,7 +365,7 @@ mod tests { ..Default::default() }), null_mask: vec![4], - datatype: 10, // float64 + datatype: Some(10), // float64 ..Default::default() }; let expected_ts_col = Column { @@ -374,7 +374,7 @@ mod tests { ts_millis_values: vec![1000, 2000, 3000, 4000], ..Default::default() }), - datatype: 15, // timestamp + datatype: Some(15), // timestamp ..Default::default() }; @@ -520,22 +520,22 @@ mod tests { let column_defs = vec![ GrpcColumnDef { name: "host".to_string(), - data_type: 12, // string + datatype: 12, // string is_nullable: false, }, GrpcColumnDef { name: "cpu".to_string(), - data_type: 10, // float64 + datatype: 10, // float64 is_nullable: true, }, GrpcColumnDef { name: "memory".to_string(), - data_type: 10, // float64 + datatype: 10, // float64 is_nullable: true, }, GrpcColumnDef { name: "ts".to_string(), - data_type: 15, // timestamp + datatype: 15, // timestamp is_nullable: true, }, ];