Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Aitozi committed Aug 3, 2024
1 parent d3c0f6b commit a74e9b5
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 41 deletions.
3 changes: 1 addition & 2 deletions crates/paimon/src/spec/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,5 @@ impl DataField {
typ,
description,
}

}
}
}
193 changes: 154 additions & 39 deletions crates/paimon/src/spec/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::error::Error;
use crate::spec::DataField;
use bitflags::bitflags;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, Formatter, write};
use std::fmt::{write, Debug, Display, Formatter};
use std::str::FromStr;

bitflags! {
Expand Down Expand Up @@ -154,51 +154,161 @@ impl Display for DataType {

#[test]
fn test_type_to_string() {
assert_eq!(DataType::Boolean(BooleanType::with_nullable(true)).to_string(), "BOOLEAN");
assert_eq!(DataType::Boolean(BooleanType::with_nullable(false)).to_string(), "BOOLEAN NOT NULL");
assert_eq!(DataType::TinyInt(TinyIntType::with_nullable(true)).to_string(), "TINYINT");
assert_eq!(DataType::TinyInt(TinyIntType::with_nullable(false)).to_string(), "TINYINT NOT NULL");
assert_eq!(DataType::SmallInt(SmallIntType::with_nullable(true)).to_string(), "SMALLINT");
assert_eq!(DataType::SmallInt(SmallIntType::with_nullable(false)).to_string(), "SMALLINT NOT NULL");
assert_eq!(DataType::Int(IntType::with_nullable(true)).to_string(), "INTEGER");
assert_eq!(DataType::Int(IntType::with_nullable(false)).to_string(), "INTEGER NOT NULL");
assert_eq!(DataType::BigInt(BigIntType::with_nullable(true)).to_string(), "BIGINT");
assert_eq!(DataType::BigInt(BigIntType::with_nullable(false)).to_string(), "BIGINT NOT NULL");
assert_eq!(DataType::Decimal(DecimalType::with_nullable(true, 10, 2).unwrap()).to_string(), "DECIMAL(10, 2)");
assert_eq!(DataType::Decimal(DecimalType::with_nullable(false, 10, 2).unwrap()).to_string(), "DECIMAL(10, 2) NOT NULL");
assert_eq!(DataType::Double(DoubleType::with_nullable(true)).to_string(), "DOUBLE");
assert_eq!(DataType::Double(DoubleType::with_nullable(false)).to_string(), "DOUBLE NOT NULL");
assert_eq!(DataType::Float(FloatType::with_nullable(true)).to_string(), "FLOAT");
assert_eq!(DataType::Float(FloatType::with_nullable(false)).to_string(), "FLOAT NOT NULL");
assert_eq!(DataType::Binary(BinaryType::with_nullable(true, 10).unwrap()).to_string(), "BINARY(10)");
assert_eq!(DataType::Binary(BinaryType::with_nullable(false, 10).unwrap()).to_string(), "BINARY(10) NOT NULL");
assert_eq!(DataType::VarBinary(VarBinaryType::try_new(true, 10).unwrap()).to_string(), "VARBINARY(10)");
assert_eq!(DataType::VarBinary(VarBinaryType::try_new(false, 10).unwrap()).to_string(), "VARBINARY(10) NOT NULL");
assert_eq!(DataType::Char(CharType::with_nullable(true, 10).unwrap()).to_string(), "CHAR(10)");
assert_eq!(DataType::Char(CharType::with_nullable(false, 10).unwrap()).to_string(), "CHAR(10) NOT NULL");
assert_eq!(DataType::VarChar(VarCharType::with_nullable(true, 10).unwrap()).to_string(), "VARCHAR(10)");
assert_eq!(DataType::VarChar(VarCharType::with_nullable(false, 10).unwrap()).to_string(), "VARCHAR(10) NOT NULL");
assert_eq!(DataType::Date(DateType::with_nullable(true)).to_string(), "DATE");
assert_eq!(DataType::Date(DateType::with_nullable(false)).to_string(), "DATE NOT NULL");
assert_eq!(DataType::LocalZonedTimestamp(LocalZonedTimestampType::with_nullable(true, 6).unwrap()).to_string(), "TIMESTAMP WITH LOCAL TIME ZONE(6)");
assert_eq!(DataType::LocalZonedTimestamp(LocalZonedTimestampType::with_nullable(false, 6).unwrap()).to_string(), "TIMESTAMP WITH LOCAL TIME ZONE(6) NOT NULL");
assert_eq!(DataType::Time(TimeType::with_nullable(true, 6).unwrap()).to_string(), "TIME(6)");
assert_eq!(DataType::Time(TimeType::with_nullable(false, 6).unwrap()).to_string(), "TIME(6) NOT NULL");
assert_eq!(DataType::Timestamp(TimestampType::with_nullable(false, 6).unwrap()).to_string(), "TIMESTAMP(6) NOT NULL");
assert_eq!(DataType::Timestamp(TimestampType::with_nullable(true, 6).unwrap()).to_string(), "TIMESTAMP(6)");
assert_eq!(
DataType::Boolean(BooleanType::with_nullable(true)).to_string(),
"BOOLEAN"
);
assert_eq!(
DataType::Boolean(BooleanType::with_nullable(false)).to_string(),
"BOOLEAN NOT NULL"
);
assert_eq!(
DataType::TinyInt(TinyIntType::with_nullable(true)).to_string(),
"TINYINT"
);
assert_eq!(
DataType::TinyInt(TinyIntType::with_nullable(false)).to_string(),
"TINYINT NOT NULL"
);
assert_eq!(
DataType::SmallInt(SmallIntType::with_nullable(true)).to_string(),
"SMALLINT"
);
assert_eq!(
DataType::SmallInt(SmallIntType::with_nullable(false)).to_string(),
"SMALLINT NOT NULL"
);
assert_eq!(
DataType::Int(IntType::with_nullable(true)).to_string(),
"INTEGER"
);
assert_eq!(
DataType::Int(IntType::with_nullable(false)).to_string(),
"INTEGER NOT NULL"
);
assert_eq!(
DataType::BigInt(BigIntType::with_nullable(true)).to_string(),
"BIGINT"
);
assert_eq!(
DataType::BigInt(BigIntType::with_nullable(false)).to_string(),
"BIGINT NOT NULL"
);
assert_eq!(
DataType::Decimal(DecimalType::with_nullable(true, 10, 2).unwrap()).to_string(),
"DECIMAL(10, 2)"
);
assert_eq!(
DataType::Decimal(DecimalType::with_nullable(false, 10, 2).unwrap()).to_string(),
"DECIMAL(10, 2) NOT NULL"
);
assert_eq!(
DataType::Double(DoubleType::with_nullable(true)).to_string(),
"DOUBLE"
);
assert_eq!(
DataType::Double(DoubleType::with_nullable(false)).to_string(),
"DOUBLE NOT NULL"
);
assert_eq!(
DataType::Float(FloatType::with_nullable(true)).to_string(),
"FLOAT"
);
assert_eq!(
DataType::Float(FloatType::with_nullable(false)).to_string(),
"FLOAT NOT NULL"
);
assert_eq!(
DataType::Binary(BinaryType::with_nullable(true, 10).unwrap()).to_string(),
"BINARY(10)"
);
assert_eq!(
DataType::Binary(BinaryType::with_nullable(false, 10).unwrap()).to_string(),
"BINARY(10) NOT NULL"
);
assert_eq!(
DataType::VarBinary(VarBinaryType::try_new(true, 10).unwrap()).to_string(),
"VARBINARY(10)"
);
assert_eq!(
DataType::VarBinary(VarBinaryType::try_new(false, 10).unwrap()).to_string(),
"VARBINARY(10) NOT NULL"
);
assert_eq!(
DataType::Char(CharType::with_nullable(true, 10).unwrap()).to_string(),
"CHAR(10)"
);
assert_eq!(
DataType::Char(CharType::with_nullable(false, 10).unwrap()).to_string(),
"CHAR(10) NOT NULL"
);
assert_eq!(
DataType::VarChar(VarCharType::with_nullable(true, 10).unwrap()).to_string(),
"VARCHAR(10)"
);
assert_eq!(
DataType::VarChar(VarCharType::with_nullable(false, 10).unwrap()).to_string(),
"VARCHAR(10) NOT NULL"
);
assert_eq!(
DataType::Date(DateType::with_nullable(true)).to_string(),
"DATE"
);
assert_eq!(
DataType::Date(DateType::with_nullable(false)).to_string(),
"DATE NOT NULL"
);
assert_eq!(
DataType::LocalZonedTimestamp(LocalZonedTimestampType::with_nullable(true, 6).unwrap())
.to_string(),
"TIMESTAMP WITH LOCAL TIME ZONE(6)"
);
assert_eq!(
DataType::LocalZonedTimestamp(LocalZonedTimestampType::with_nullable(false, 6).unwrap())
.to_string(),
"TIMESTAMP WITH LOCAL TIME ZONE(6) NOT NULL"
);
assert_eq!(
DataType::Time(TimeType::with_nullable(true, 6).unwrap()).to_string(),
"TIME(6)"
);
assert_eq!(
DataType::Time(TimeType::with_nullable(false, 6).unwrap()).to_string(),
"TIME(6) NOT NULL"
);
assert_eq!(
DataType::Timestamp(TimestampType::with_nullable(false, 6).unwrap()).to_string(),
"TIMESTAMP(6) NOT NULL"
);
assert_eq!(
DataType::Timestamp(TimestampType::with_nullable(true, 6).unwrap()).to_string(),
"TIMESTAMP(6)"
);
let int_type = DataType::Int(IntType::with_nullable(true));
let arr_type = DataType::Array(ArrayType::with_nullable(true, int_type.clone()));
assert_eq!(arr_type.to_string(), "ARRAY<INTEGER>");
assert_eq!(DataType::Array(ArrayType::with_nullable(true, arr_type.clone())).to_string(), "ARRAY<ARRAY<INTEGER>>");
let map_type = DataType::Map(MapType::with_nullable(true, int_type.clone(), arr_type.clone()));
assert_eq!(
DataType::Array(ArrayType::with_nullable(true, arr_type.clone())).to_string(),
"ARRAY<ARRAY<INTEGER>>"
);
let map_type = DataType::Map(MapType::with_nullable(
true,
int_type.clone(),
arr_type.clone(),
));
assert_eq!(map_type.to_string(), "MAP<INTEGER, ARRAY<INTEGER>>");
let multiset_type = DataType::Multiset(MultisetType::with_nullable(true, int_type.clone()));
assert_eq!(multiset_type.to_string(), "MULTISET<INTEGER>");
let row_type = DataType::Row(RowType::with_nullable(true, vec![DataField::new(1, "a".to_string(), int_type.clone(), None), DataField::new(2, "b".to_string(), arr_type.clone(), None)]));
let row_type = DataType::Row(RowType::with_nullable(
true,
vec![
DataField::new(1, "a".to_string(), int_type.clone(), None),
DataField::new(2, "b".to_string(), arr_type.clone(), None),
],
));
assert_eq!(row_type.to_string(), "ROW<INTEGER, ARRAY<INTEGER>>");
}


impl FromStr for DataType {
type Err = Error;

Expand Down Expand Up @@ -1166,8 +1276,13 @@ pub struct RowType {

impl Display for RowType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let inner = self.fields.iter().map(|field| field.to_string()).collect::<Vec<String>>().join(", ");
write!(f, "ROW<{}>", inner)?;
let fields = self
.fields
.iter()
.map(|field| field.to_string())
.collect::<Vec<String>>()
.join(", ");
write!(f, "ROW<{}>", fields)?;
if !self.nullable {
write!(f, " NOT NULL")?;
}
Expand Down

0 comments on commit a74e9b5

Please sign in to comment.