-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support more type for json parser in source
* add date support for json parser * format code & add ut for date typr in json parser * format code * fix clippy check * fix build fail * Result<T, E>::ok() to convert Result to Option Co-authored-by: tabVersion <[email protected]>
- Loading branch information
1 parent
8f200aa
commit a225fc8
Showing
3 changed files
with
74 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,72 @@ | ||
use risingwave_common::error::ErrorCode::InternalError; | ||
use risingwave_common::error::{Result, RwError}; | ||
use risingwave_common::types::{DataTypeKind, Decimal, ScalarImpl, ScalarRef}; | ||
use risingwave_common::vector_op::cast::str_to_date; | ||
use serde_json::Value; | ||
|
||
use crate::SourceColumnDesc; | ||
|
||
macro_rules! make_ScalarImpl { | ||
($x:expr, $y:expr) => { | ||
match $x { | ||
Some(v) => return Ok($y(v)), | ||
None => return Err(RwError::from(InternalError("json parse error".to_string()))), | ||
} | ||
}; | ||
} | ||
|
||
pub(crate) fn json_parse_value( | ||
column: &SourceColumnDesc, | ||
value: Option<&Value>, | ||
) -> Option<ScalarImpl> { | ||
) -> Result<ScalarImpl> { | ||
match column.data_type.data_type_kind() { | ||
DataTypeKind::Boolean => value | ||
.and_then(|v| v.as_bool()) | ||
.map(|v| ScalarImpl::Bool(v as bool)), | ||
DataTypeKind::Int16 => value | ||
.and_then(|v| v.as_i64()) | ||
.map(|v| ScalarImpl::Int16(v as i16)), | ||
DataTypeKind::Int32 => value | ||
.and_then(|v| v.as_i64()) | ||
.map(|v| ScalarImpl::Int32(v as i32)), | ||
DataTypeKind::Int64 => value | ||
.and_then(|v| v.as_i64()) | ||
.map(|v| ScalarImpl::Int64(v as i64)), | ||
DataTypeKind::Float32 => value | ||
.and_then(|v| v.as_f64()) | ||
.map(|v| ScalarImpl::Float32((v as f32).into())), | ||
DataTypeKind::Float64 => value | ||
.and_then(|v| v.as_f64()) | ||
.map(|v| ScalarImpl::Float64(v.into())), | ||
DataTypeKind::Decimal => value | ||
.and_then(|v| v.as_u64()) | ||
.map(|v| ScalarImpl::Decimal(Decimal::from(v))), | ||
DataTypeKind::Char | DataTypeKind::Varchar => value | ||
.and_then(|v| v.as_str()) | ||
.map(|v| ScalarImpl::Utf8(v.to_owned_scalar())), | ||
DataTypeKind::Boolean => { | ||
make_ScalarImpl!(value.and_then(|v| v.as_bool()), |x| ScalarImpl::Bool( | ||
x as bool | ||
)) | ||
} | ||
DataTypeKind::Int16 => { | ||
make_ScalarImpl!(value.and_then(|v| v.as_i64()), |x| ScalarImpl::Int16( | ||
x as i16 | ||
)) | ||
} | ||
DataTypeKind::Int32 => { | ||
make_ScalarImpl!(value.and_then(|v| v.as_i64()), |x| ScalarImpl::Int32( | ||
x as i32 | ||
)) | ||
} | ||
DataTypeKind::Int64 => { | ||
make_ScalarImpl!(value.and_then(|v| v.as_i64()), |x| ScalarImpl::Int64( | ||
x as i64 | ||
)) | ||
} | ||
DataTypeKind::Float32 => { | ||
make_ScalarImpl!(value.and_then(|v| v.as_f64()), |v| ScalarImpl::Float32( | ||
(v as f32).into() | ||
)) | ||
} | ||
DataTypeKind::Float64 => { | ||
make_ScalarImpl!( | ||
value.and_then(|v| v.as_f64()), | ||
|v: f64| ScalarImpl::Float64(v.into()) | ||
) | ||
} | ||
DataTypeKind::Decimal => { | ||
make_ScalarImpl!(value.and_then(|v| v.as_u64()), |v| ScalarImpl::Decimal( | ||
Decimal::from(v) | ||
)) | ||
} | ||
DataTypeKind::Char | DataTypeKind::Varchar => make_ScalarImpl!( | ||
value.and_then(|v| v.as_str()), | ||
|v: &str| ScalarImpl::Utf8(v.to_owned_scalar()) | ||
), | ||
DataTypeKind::Date => match value.and_then(|v| v.as_str()) { | ||
None => Err(RwError::from(InternalError("parse error".to_string()))), | ||
Some(date_str) => match str_to_date(date_str) { | ||
Ok(date) => Ok(ScalarImpl::Int32(date as i32)), | ||
Err(e) => Err(e), | ||
}, | ||
}, | ||
_ => unimplemented!(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters