Skip to content

Commit

Permalink
Support UInt32, UInt64, Int32, Int64 in new expression
Browse files Browse the repository at this point in the history
Signed-off-by: Enwei Jiao <jiaoew2011@gmail.com>
  • Loading branch information
jiaoew1991 committed Jul 16, 2022
1 parent f1c04a4 commit d77ca78
Show file tree
Hide file tree
Showing 10 changed files with 981 additions and 16 deletions.
12 changes: 12 additions & 0 deletions common/expression/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ impl<'a> Display for ScalarRef<'a> {
ScalarRef::EmptyArray => write!(f, "[]"),
ScalarRef::Int8(i) => write!(f, "{}", i),
ScalarRef::Int16(i) => write!(f, "{}", i),
ScalarRef::Int32(i) => write!(f, "{}", i),
ScalarRef::Int64(i) => write!(f, "{}", i),
ScalarRef::UInt8(i) => write!(f, "{}", i),
ScalarRef::UInt16(i) => write!(f, "{}", i),
ScalarRef::UInt32(i) => write!(f, "{}", i),
ScalarRef::UInt64(i) => write!(f, "{}", i),
ScalarRef::Boolean(b) => write!(f, "{}", b),
ScalarRef::String(s) => write!(f, "{}", String::from_utf8_lossy(s)),
ScalarRef::Array(col) => write!(f, "[{}]", col.iter().join(", ")),
Expand Down Expand Up @@ -135,8 +139,12 @@ impl Display for Literal {
Literal::Boolean(val) => write!(f, "{val}::Boolean"),
Literal::UInt8(val) => write!(f, "{val}::UInt8"),
Literal::UInt16(val) => write!(f, "{val}::UInt16"),
Literal::UInt32(val) => write!(f, "{val}::UInt32"),
Literal::UInt64(val) => write!(f, "{val}::UInt64"),
Literal::Int8(val) => write!(f, "{val}::Int8"),
Literal::Int16(val) => write!(f, "{val}::Int16"),
Literal::Int32(val) => write!(f, "{val}::Int32"),
Literal::Int64(val) => write!(f, "{val}::Int64"),
Literal::String(val) => write!(f, "{}::String", String::from_utf8_lossy(val)),
}
}
Expand All @@ -149,8 +157,12 @@ impl Display for DataType {
DataType::String => write!(f, "String"),
DataType::UInt8 => write!(f, "UInt8"),
DataType::UInt16 => write!(f, "UInt16"),
DataType::UInt32 => write!(f, "UInt32"),
DataType::UInt64 => write!(f, "UInt64"),
DataType::Int8 => write!(f, "Int8"),
DataType::Int16 => write!(f, "Int16"),
DataType::Int32 => write!(f, "Int32"),
DataType::Int64 => write!(f, "Int64"),
DataType::Null => write!(f, "Nullable(Nothing)"),
DataType::Nullable(inner) => write!(f, "Nullable({inner})"),
DataType::EmptyArray => write!(f, "Array(Nothing)"),
Expand Down
150 changes: 150 additions & 0 deletions common/expression/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,67 @@ impl Evaluator {
(Scalar::UInt8(val), DataType::UInt16) => {
Some(Value::Scalar(Scalar::UInt16(val as u16)))
}
(Scalar::UInt8(val), DataType::UInt32) => {
Some(Value::Scalar(Scalar::UInt32(val as u32)))
}
(Scalar::UInt16(val), DataType::UInt32) => {
Some(Value::Scalar(Scalar::UInt32(val as u32)))
}
(Scalar::UInt8(val), DataType::UInt64) => {
Some(Value::Scalar(Scalar::UInt64(val as u64)))
}
(Scalar::UInt16(val), DataType::UInt64) => {
Some(Value::Scalar(Scalar::UInt64(val as u64)))
}
(Scalar::UInt32(val), DataType::UInt64) => {
Some(Value::Scalar(Scalar::UInt64(val as u64)))
}
(Scalar::Int8(val), DataType::Int16) => {
Some(Value::Scalar(Scalar::Int16(val as i16)))
}
(Scalar::UInt8(val), DataType::Int16) => {
Some(Value::Scalar(Scalar::Int16(val as i16)))
}
(Scalar::Int8(val), DataType::Int32) => {
Some(Value::Scalar(Scalar::Int32(val as i32)))
}
(Scalar::Int16(val), DataType::Int32) => {
Some(Value::Scalar(Scalar::Int32(val as i32)))
}
(Scalar::UInt8(val), DataType::Int32) => {
Some(Value::Scalar(Scalar::Int32(val as i32)))
}
(Scalar::UInt16(val), DataType::Int32) => {
Some(Value::Scalar(Scalar::Int32(val as i32)))
}
(Scalar::Int8(val), DataType::Int64) => {
Some(Value::Scalar(Scalar::Int64(val as i64)))
}
(Scalar::Int16(val), DataType::Int64) => {
Some(Value::Scalar(Scalar::Int64(val as i64)))
}
(Scalar::Int32(val), DataType::Int64) => {
Some(Value::Scalar(Scalar::Int64(val as i64)))
}
(Scalar::UInt8(val), DataType::Int64) => {
Some(Value::Scalar(Scalar::Int64(val as i64)))
}
(Scalar::UInt16(val), DataType::Int64) => {
Some(Value::Scalar(Scalar::Int64(val as i64)))
}
(Scalar::UInt32(val), DataType::Int64) => {
Some(Value::Scalar(Scalar::Int64(val as i64)))
}
(scalar @ Scalar::Boolean(_), DataType::Boolean)
| (scalar @ Scalar::String(_), DataType::String)
| (scalar @ Scalar::UInt8(_), DataType::UInt8)
| (scalar @ Scalar::UInt16(_), DataType::UInt16)
| (scalar @ Scalar::UInt32(_), DataType::UInt32)
| (scalar @ Scalar::UInt64(_), DataType::UInt64)
| (scalar @ Scalar::Int8(_), DataType::Int8)
| (scalar @ Scalar::Int16(_), DataType::Int16)
| (scalar @ Scalar::Int32(_), DataType::Int32)
| (scalar @ Scalar::Int64(_), DataType::Int64)
| (scalar @ Scalar::Null, DataType::Null)
| (scalar @ Scalar::EmptyArray, DataType::EmptyArray) => {
Some(Value::Scalar(scalar))
Expand Down Expand Up @@ -159,17 +209,67 @@ impl Evaluator {
(Column::UInt8(column), DataType::UInt16) => Some(Value::Column(Column::UInt16(
column.iter().map(|v| *v as u16).collect(),
))),
(Column::UInt8(column), DataType::UInt32) => Some(Value::Column(Column::UInt32(
column.iter().map(|v| *v as u32).collect(),
))),
(Column::UInt16(column), DataType::UInt32) => Some(Value::Column(Column::UInt32(
column.iter().map(|v| *v as u32).collect(),
))),
(Column::UInt8(column), DataType::UInt64) => Some(Value::Column(Column::UInt64(
column.iter().map(|v| *v as u64).collect(),
))),
(Column::UInt16(column), DataType::UInt64) => Some(Value::Column(Column::UInt64(
column.iter().map(|v| *v as u64).collect(),
))),
(Column::UInt32(column), DataType::UInt64) => Some(Value::Column(Column::UInt64(
column.iter().map(|v| *v as u64).collect(),
))),
(Column::Int8(column), DataType::Int16) => Some(Value::Column(Column::Int16(
column.iter().map(|v| *v as i16).collect(),
))),
(Column::UInt8(column), DataType::Int16) => Some(Value::Column(Column::Int16(
column.iter().map(|v| *v as i16).collect(),
))),
(Column::Int8(column), DataType::Int32) => Some(Value::Column(Column::Int32(
column.iter().map(|v| *v as i32).collect(),
))),
(Column::Int16(column), DataType::Int32) => Some(Value::Column(Column::Int32(
column.iter().map(|v| *v as i32).collect(),
))),
(Column::UInt8(column), DataType::Int32) => Some(Value::Column(Column::Int32(
column.iter().map(|v| *v as i32).collect(),
))),
(Column::UInt16(column), DataType::Int32) => Some(Value::Column(Column::Int32(
column.iter().map(|v| *v as i32).collect(),
))),
(Column::Int8(column), DataType::Int64) => Some(Value::Column(Column::Int64(
column.iter().map(|v| *v as i64).collect(),
))),
(Column::Int16(column), DataType::Int64) => Some(Value::Column(Column::Int64(
column.iter().map(|v| *v as i64).collect(),
))),
(Column::Int32(column), DataType::Int64) => Some(Value::Column(Column::Int64(
column.iter().map(|v| *v as i64).collect(),
))),
(Column::UInt8(column), DataType::Int64) => Some(Value::Column(Column::Int64(
column.iter().map(|v| *v as i64).collect(),
))),
(Column::UInt16(column), DataType::Int64) => Some(Value::Column(Column::Int64(
column.iter().map(|v| *v as i64).collect(),
))),
(Column::UInt32(column), DataType::Int64) => Some(Value::Column(Column::Int64(
column.iter().map(|v| *v as i64).collect(),
))),
(col @ Column::Boolean(_), DataType::Boolean)
| (col @ Column::String { .. }, DataType::String)
| (col @ Column::UInt8(_), DataType::UInt8)
| (col @ Column::UInt16(_), DataType::UInt16)
| (col @ Column::UInt32(_), DataType::UInt32)
| (col @ Column::UInt64(_), DataType::UInt64)
| (col @ Column::Int8(_), DataType::Int8)
| (col @ Column::Int16(_), DataType::Int16)
| (col @ Column::Int32(_), DataType::Int32)
| (col @ Column::Int64(_), DataType::Int64)
| (col @ Column::Null { .. }, DataType::Null)
| (col @ Column::EmptyArray { .. }, DataType::EmptyArray) => {
Some(Value::Column(col))
Expand All @@ -184,8 +284,12 @@ impl Evaluator {
Literal::Null => Scalar::Null,
Literal::Int8(val) => Scalar::Int8(*val),
Literal::Int16(val) => Scalar::Int16(*val),
Literal::Int32(val) => Scalar::Int32(*val),
Literal::Int64(val) => Scalar::Int64(*val),
Literal::UInt8(val) => Scalar::UInt8(*val),
Literal::UInt16(val) => Scalar::UInt16(*val),
Literal::UInt32(val) => Scalar::UInt32(*val),
Literal::UInt64(val) => Scalar::UInt64(*val),
Literal::Boolean(val) => Scalar::Boolean(*val),
Literal::String(val) => Scalar::String(val.clone()),
}
Expand Down Expand Up @@ -235,6 +339,11 @@ impl DomainCalculator {
min: *i as i64,
max: *i as i64,
}),
Literal::Int32(i) => Domain::Int(IntDomain {
min: *i as i64,
max: *i as i64,
}),
Literal::Int64(i) => Domain::Int(IntDomain { min: *i, max: *i }),
Literal::UInt8(i) => Domain::UInt(UIntDomain {
min: *i as u64,
max: *i as u64,
Expand All @@ -243,6 +352,11 @@ impl DomainCalculator {
min: *i as u64,
max: *i as u64,
}),
Literal::UInt32(i) => Domain::UInt(UIntDomain {
min: *i as u64,
max: *i as u64,
}),
Literal::UInt64(i) => Domain::UInt(UIntDomain { min: *i, max: *i }),
Literal::Boolean(true) => Domain::Boolean(BooleanDomain {
has_false: false,
has_true: true,
Expand Down Expand Up @@ -300,6 +414,42 @@ impl DomainCalculator {
max: (*max).min(i16::MAX as u64) as i64,
}))
}
(Domain::UInt(UIntDomain { min, max }), DataType::UInt32) => {
Some(Domain::UInt(UIntDomain {
min: (*min).min(u32::MAX as u64),
max: (*max).min(u32::MAX as u64),
}))
}
(Domain::Int(IntDomain { min, max }), DataType::Int32) => {
Some(Domain::Int(IntDomain {
min: (*min).max(i32::MIN as i64).min(i32::MAX as i64),
max: (*max).max(i32::MIN as i64).min(i32::MAX as i64),
}))
}
(Domain::UInt(UIntDomain { min, max }), DataType::Int32) => {
Some(Domain::Int(IntDomain {
min: (*min).min(i32::MAX as u64) as i64,
max: (*max).min(i32::MAX as u64) as i64,
}))
}
(Domain::UInt(UIntDomain { min, max }), DataType::UInt64) => {
Some(Domain::UInt(UIntDomain {
min: (*min).min(u64::MAX),
max: (*max).min(u64::MAX),
}))
}
(Domain::Int(IntDomain { min, max }), DataType::Int64) => {
Some(Domain::Int(IntDomain {
min: (*min).max(i64::MIN).min(i64::MAX),
max: (*max).max(i64::MIN).min(i64::MAX),
}))
}
(Domain::UInt(UIntDomain { min, max }), DataType::Int64) => {
Some(Domain::Int(IntDomain {
min: (*min).min(i64::MAX as u64) as i64,
max: (*max).min(i64::MAX as u64) as i64,
}))
}
(Domain::Boolean(_), DataType::Boolean)
| (Domain::String(_), DataType::String)
| (Domain::UInt(_), DataType::UInt8)
Expand Down
4 changes: 4 additions & 0 deletions common/expression/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ pub enum Literal {
Null,
Int8(i8),
Int16(i16),
Int32(i32),
Int64(i64),
UInt8(u8),
UInt16(u16),
UInt32(u32),
UInt64(u64),
Boolean(bool),
String(Vec<u8>),
}
4 changes: 4 additions & 0 deletions common/expression/src/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ impl Domain {
DataType::EmptyArray => Domain::Array(None),
DataType::Int8 => Domain::Int(NumberType::<i8>::full_domain(generics)),
DataType::Int16 => Domain::Int(NumberType::<i16>::full_domain(generics)),
DataType::Int32 => Domain::Int(NumberType::<i32>::full_domain(generics)),
DataType::Int64 => Domain::Int(NumberType::<i64>::full_domain(generics)),
DataType::UInt8 => Domain::UInt(NumberType::<u8>::full_domain(generics)),
DataType::UInt16 => Domain::UInt(NumberType::<u16>::full_domain(generics)),
DataType::UInt32 => Domain::UInt(NumberType::<u32>::full_domain(generics)),
DataType::UInt64 => Domain::UInt(NumberType::<u64>::full_domain(generics)),
DataType::Boolean => Domain::Boolean(BooleanType::full_domain(generics)),
DataType::String => Domain::String(StringType::full_domain(generics)),
DataType::Nullable(ty) => Domain::Nullable(NullableDomain {
Expand Down
48 changes: 36 additions & 12 deletions common/expression/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@ pub fn check_literal(literal: &Literal) -> DataType {
Literal::Null => DataType::Null,
Literal::Int8(_) => DataType::Int8,
Literal::Int16(_) => DataType::Int16,
Literal::Int32(_) => DataType::Int32,
Literal::Int64(_) => DataType::Int64,
Literal::UInt8(_) => DataType::UInt8,
Literal::UInt16(_) => DataType::UInt16,
Literal::UInt32(_) => DataType::UInt32,
Literal::UInt64(_) => DataType::UInt64,
Literal::Boolean(_) => DataType::Boolean,
Literal::String(_) => DataType::String,
}
Expand Down Expand Up @@ -215,10 +219,15 @@ pub fn can_cast_to(src_ty: &DataType, dest_ty: &DataType) -> bool {
(DataType::Nullable(src_ty), DataType::Nullable(dest_ty)) => can_cast_to(src_ty, dest_ty),
(src_ty, DataType::Nullable(dest_ty)) => can_cast_to(src_ty, dest_ty),
(DataType::Array(src_ty), DataType::Array(dest_ty)) => can_cast_to(src_ty, dest_ty),
(DataType::UInt8, DataType::UInt16)
| (DataType::Int8, DataType::Int16)
| (DataType::UInt8, DataType::Int16) => true,
_ => false,
(src_ty, dest_ty) => {
let (info1, info2) = (number_type_info(src_ty), number_type_info(dest_ty));
match (info1, info2) {
(Some((size1, b1)), Some((size2, b2))) if b1 == b2 => size1 <= size2,
(Some((size1, true)), Some((size2, false))) if size2 > size1 => true,
(Some((size1, false)), Some((size2, true))) if size1 > size2 => true,
_ => false,
}
}
}
}

Expand All @@ -238,15 +247,30 @@ pub fn common_super_type(ty1: DataType, ty2: DataType) -> Option<DataType> {
(DataType::Array(box ty1), DataType::Array(box ty2)) => {
Some(DataType::Array(Box::new(common_super_type(ty1, ty2)?)))
}
(DataType::UInt8, DataType::UInt16) | (DataType::UInt16, DataType::UInt8) => {
Some(DataType::UInt16)
}
(DataType::Int8, DataType::Int16) | (DataType::Int16, DataType::Int8) => {
Some(DataType::Int16)
}
(DataType::Int16, DataType::UInt8) | (DataType::UInt8, DataType::Int16) => {
Some(DataType::Int16)
(ty1, ty2) => {
let (info1, info2) = (number_type_info(&ty1), number_type_info(&ty2));
match (info1, info2) {
(Some((size1, b1)), Some((size2, b2))) if b1 == b2 => {
(size1 >= size2).then(|| Some(ty1)).unwrap_or(Some(ty2))
}
(Some((size1, true)), Some((size2, false))) if size2 > size1 => Some(ty2),
(Some((size1, false)), Some((size2, true))) if size1 > size2 => Some(ty1),
_ => None,
}
}
}
}

fn number_type_info(ty: &DataType) -> Option<(i8, bool)> {
match ty {
DataType::UInt8 => Some((1, true)),
DataType::UInt16 => Some((2, true)),
DataType::UInt32 => Some((4, true)),
DataType::UInt64 => Some((8, true)),
DataType::Int8 => Some((1, false)),
DataType::Int16 => Some((2, false)),
DataType::Int32 => Some((4, false)),
DataType::Int64 => Some((8, false)),
_ => None,
}
}
8 changes: 4 additions & 4 deletions common/expression/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ pub enum DataType {
String,
UInt8,
UInt16,
UInt32,
UInt64,
Int8,
Int16,
Int32,
Int64,
// TODO: Implement them
// UInt32,
// Int32,
// UInt64,
// Int64,
// Float32,
// Float64,
// Timestamp
Expand Down
Loading

0 comments on commit d77ca78

Please sign in to comment.