chore: add optional for datatype, rename data_type to datatype (#258)

This commit is contained in:
fys
2022-09-14 18:07:22 +08:00
committed by GitHub
parent 0dce8946d4
commit ef40b12749
8 changed files with 48 additions and 37 deletions

View File

@@ -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;
}

View File

@@ -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<ObjectResult> for Output {
}
fn column_to_vector(column: &Column, rows: u32) -> Result<VectorRef> {
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),
}
}
}

View File

@@ -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<T> = std::result::Result<T, Error>;
@@ -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,

View File

@@ -140,7 +140,7 @@ fn create_table_schema(expr: &CreateExpr) -> Result<SchemaRef> {
fn create_column_schema(column_def: &ColumnDef) -> Result<ColumnSchema> {
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,
},
];

View File

@@ -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 {

View File

@@ -74,9 +74,11 @@ fn try_convert(record_batches: Vec<RecordBatch>) -> Result<SelectResult> {
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);

View File

@@ -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,
},
];

View File

@@ -211,7 +211,7 @@ fn columns_to_expr(column_defs: &[ColumnDef]) -> Result<Vec<GrpcColumnDef>> {
.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::<Vec<GrpcColumnDef>>())
@@ -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,
},
];