Skip to content

Commit

Permalink
Support text column creation
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Oct 4, 2023
1 parent 4bf7385 commit 15f0deb
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cargo run
```
test command
```sql
create table t1 (a int, b int);
create table t1 (a int primary key, b int);

insert into t1 (a, b) values (1, 1), (5, 3), (5, 2);

Expand Down
2 changes: 1 addition & 1 deletion src/binder/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<S: Storage> Binder<S> {
&mut self,
name: &ObjectName,
columns: &[ColumnDef],
constraints: &[TableConstraint],
_constraints: &[TableConstraint],
) -> Result<LogicalPlan, BindError> {
let name = lower_case_name(&name);
let (_, name) = split_name(&name)?;
Expand Down
7 changes: 7 additions & 0 deletions src/catalog/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub struct ColumnCatalog {
pub nullable: bool,
pub desc: ColumnDesc,
pub ref_expr: Option<ScalarExpression>,
// absoulte file path, used to store text content
pub text_files: Vec<String>,
pub deleted_text_files: Vec<String>,
}

impl ColumnCatalog {
Expand All @@ -32,6 +35,8 @@ impl ColumnCatalog {
nullable,
desc: column_desc,
ref_expr,
text_files: Vec::new(),
deleted_text_files: Vec::new(),
}
}

Expand All @@ -43,6 +48,8 @@ impl ColumnCatalog {
nullable: false,
desc: ColumnDesc::new(LogicalType::Varchar(None), false, false),
ref_expr: None,
text_files: Vec::new(),
deleted_text_files: Vec::new(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/expression/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use lazy_static::lazy_static;
use std::sync::Arc;

lazy_static! {
static ref NULL_VALUE: ValueRef = { Arc::new(DataValue::Null) };
static ref NULL_VALUE: ValueRef = Arc::new(DataValue::Null);
}

impl ScalarExpression {
Expand Down
2 changes: 2 additions & 0 deletions src/expression/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,8 @@ mod test {
is_unique: false,
},
ref_expr: None,
text_files: Vec::new(),
deleted_text_files: Vec::new(),
});
let val_1 = Arc::new(DataValue::Int32(Some(1)));

Expand Down
4 changes: 4 additions & 0 deletions src/optimizer/rule/simplification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ mod test {
is_unique: false,
},
ref_expr: None,
text_files: Vec::new(),
deleted_text_files: Vec::new(),
};
let c2_col = ColumnCatalog {
id: Some(1),
Expand All @@ -159,6 +161,8 @@ mod test {
is_unique: true,
},
ref_expr: None,
text_files: Vec::new(),
deleted_text_files: Vec::new(),
};

// -(c1 + 1) > c2 => c1 < -c2 - 1
Expand Down
2 changes: 1 addition & 1 deletion src/storage/table_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use lazy_static::lazy_static;
const BOUND_MIN_TAG: u8 = 0;
const BOUND_MAX_TAG: u8 = 1;
lazy_static! {
static ref ROOT_BYTES: Vec<u8> = { b"Root".to_vec() };
static ref ROOT_BYTES: Vec<u8> = b"Root".to_vec();
}

#[derive(Clone)]
Expand Down
4 changes: 4 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum LogicalType {
Float,
Double,
Varchar(Option<u32>),
Text,
Date,
DateTime,
// decimal (precision, scale)
Expand Down Expand Up @@ -95,6 +96,7 @@ impl LogicalType {
LogicalType::Double => Some(8),
/// Note: The non-fixed length type's raw_len is None e.g. Varchar
LogicalType::Varchar(_) => None,
LogicalType::Text => None,
LogicalType::Decimal(_, _) => Some(16),
LogicalType::Date => Some(4),
LogicalType::DateTime => Some(8),
Expand Down Expand Up @@ -293,6 +295,7 @@ impl LogicalType {
LogicalType::Float => matches!(to, LogicalType::Double),
LogicalType::Double => false,
LogicalType::Varchar(_) => false,
LogicalType::Text => false,
LogicalType::Date => matches!(to, LogicalType::DateTime | LogicalType::Varchar(_)),
LogicalType::DateTime => matches!(to, LogicalType::Date | LogicalType::Varchar(_)),
LogicalType::Decimal(_, _) => false,
Expand All @@ -309,6 +312,7 @@ impl TryFrom<sqlparser::ast::DataType> for LogicalType {
sqlparser::ast::DataType::Char(len) | sqlparser::ast::DataType::Varchar(len) => {
Ok(LogicalType::Varchar(len.map(|len| len.length as u32)))
}
sqlparser::ast::DataType::Text => Ok(LogicalType::Text),
sqlparser::ast::DataType::Float(_) => Ok(LogicalType::Float),
sqlparser::ast::DataType::Double => Ok(LogicalType::Double),
sqlparser::ast::DataType::TinyInt(_) => Ok(LogicalType::Tinyint),
Expand Down
27 changes: 26 additions & 1 deletion src/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use serde::{Deserialize, Serialize};
use super::LogicalType;

lazy_static! {
static ref UNIX_DATETIME: NaiveDateTime = { NaiveDateTime::from_timestamp_opt(0, 0).unwrap() };
static ref UNIX_DATETIME: NaiveDateTime = NaiveDateTime::from_timestamp_opt(0, 0).unwrap();
}

pub const DATE_FMT: &str = "%Y-%m-%d";
Expand Down Expand Up @@ -329,6 +329,7 @@ impl DataValue {
LogicalType::Float => DataValue::Float32(None),
LogicalType::Double => DataValue::Float64(None),
LogicalType::Varchar(_) => DataValue::Utf8(None),
LogicalType::Text => DataValue::Utf8(None),
LogicalType::Date => DataValue::Date32(None),
LogicalType::DateTime => DataValue::Date64(None),
LogicalType::Decimal(_, _) => DataValue::Decimal(None),
Expand All @@ -351,6 +352,7 @@ impl DataValue {
LogicalType::Float => DataValue::Float32(Some(0.0)),
LogicalType::Double => DataValue::Float64(Some(0.0)),
LogicalType::Varchar(_) => DataValue::Utf8(Some("".to_string())),
LogicalType::Text => DataValue::Utf8(Some("".to_string())),
LogicalType::Date => DataValue::Date32(Some(UNIX_DATETIME.num_days_from_ce())),
LogicalType::DateTime => DataValue::Date64(Some(UNIX_DATETIME.timestamp())),
LogicalType::Decimal(_, _) => DataValue::Decimal(Some(Decimal::new(0, 0))),
Expand Down Expand Up @@ -421,6 +423,9 @@ impl DataValue {
LogicalType::Varchar(_) => DataValue::Utf8(
(!bytes.is_empty()).then(|| String::from_utf8(bytes.to_owned()).unwrap()),
),
LogicalType::Text => DataValue::Utf8(
(!bytes.is_empty()).then(|| String::from_utf8(bytes.to_owned()).unwrap()),
),
LogicalType::Date => {
DataValue::Date32((!bytes.is_empty()).then(|| i32::decode_fixed(bytes)))
}
Expand Down Expand Up @@ -594,6 +599,7 @@ impl DataValue {
LogicalType::Float => Ok(DataValue::Float32(None)),
LogicalType::Double => Ok(DataValue::Float64(None)),
LogicalType::Varchar(_) => Ok(DataValue::Utf8(None)),
LogicalType::Text => Ok(DataValue::Utf8(None)),
LogicalType::Date => Ok(DataValue::Date32(None)),
LogicalType::DateTime => Ok(DataValue::Date64(None)),
LogicalType::Decimal(_, _) => Ok(DataValue::Decimal(None)),
Expand All @@ -612,13 +618,15 @@ impl DataValue {
LogicalType::Float => Ok(DataValue::Float32(value.map(|v| v.into()))),
LogicalType::Double => Ok(DataValue::Float64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
_ => Err(TypeError::CastFail),
},
DataValue::Float32(value) => match to {
LogicalType::SqlNull => Ok(DataValue::Null),
LogicalType::Float => Ok(DataValue::Float32(value)),
LogicalType::Double => Ok(DataValue::Float64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(
value
.map(|v| {
Expand All @@ -635,6 +643,7 @@ impl DataValue {
LogicalType::SqlNull => Ok(DataValue::Null),
LogicalType::Double => Ok(DataValue::Float64(value)),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(
value
.map(|v| {
Expand Down Expand Up @@ -668,6 +677,7 @@ impl DataValue {
LogicalType::Float => Ok(DataValue::Float32(value.map(|v| v.into()))),
LogicalType::Double => Ok(DataValue::Float64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(value.map(|v| {
let mut decimal = Decimal::from(v);
Self::decimal_round_i(option, &mut decimal);
Expand Down Expand Up @@ -696,6 +706,7 @@ impl DataValue {
LogicalType::Float => Ok(DataValue::Float32(value.map(|v| v.into()))),
LogicalType::Double => Ok(DataValue::Float64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(value.map(|v| {
let mut decimal = Decimal::from(v);
Self::decimal_round_i(option, &mut decimal);
Expand All @@ -722,6 +733,7 @@ impl DataValue {
LogicalType::Bigint => Ok(DataValue::Int64(value.map(|v| v.into()))),
LogicalType::Double => Ok(DataValue::Float64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(value.map(|v| {
let mut decimal = Decimal::from(v);
Self::decimal_round_i(option, &mut decimal);
Expand All @@ -746,6 +758,7 @@ impl DataValue {
)),
LogicalType::Bigint => Ok(DataValue::Int64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(value.map(|v| {
let mut decimal = Decimal::from(v);
Self::decimal_round_i(option, &mut decimal);
Expand All @@ -766,6 +779,7 @@ impl DataValue {
LogicalType::Float => Ok(DataValue::Float32(value.map(|v| v.into()))),
LogicalType::Double => Ok(DataValue::Float64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(value.map(|v| {
let mut decimal = Decimal::from(v);
Self::decimal_round_i(option, &mut decimal);
Expand All @@ -784,6 +798,7 @@ impl DataValue {
LogicalType::Float => Ok(DataValue::Float32(value.map(|v| v.into()))),
LogicalType::Double => Ok(DataValue::Float64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(value.map(|v| {
let mut decimal = Decimal::from(v);
Self::decimal_round_i(option, &mut decimal);
Expand All @@ -799,6 +814,7 @@ impl DataValue {
LogicalType::UBigint => Ok(DataValue::UInt64(value.map(|v| v.into()))),
LogicalType::Double => Ok(DataValue::Float64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(value.map(|v| {
let mut decimal = Decimal::from(v);
Self::decimal_round_i(option, &mut decimal);
Expand All @@ -811,6 +827,7 @@ impl DataValue {
LogicalType::SqlNull => Ok(DataValue::Null),
LogicalType::UBigint => Ok(DataValue::UInt64(value.map(|v| v.into()))),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Decimal(_, option) => Ok(DataValue::Decimal(value.map(|v| {
let mut decimal = Decimal::from(v);
Self::decimal_round_i(option, &mut decimal);
Expand Down Expand Up @@ -856,6 +873,7 @@ impl DataValue {
value.map(|v| f64::from_str(&v)).transpose()?,
)),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
LogicalType::Date => {
let option = value
.map(|v| {
Expand Down Expand Up @@ -887,6 +905,9 @@ impl DataValue {
DataValue::Date32(value) => match to {
LogicalType::SqlNull => Ok(DataValue::Null),
LogicalType::Varchar(len) => varchar_cast!(Self::format_date(value), len),
LogicalType::Text => Ok(DataValue::Utf8(
Self::format_date(value).map(|v| format!("{v}")),
)),
LogicalType::Date => Ok(DataValue::Date32(value)),
LogicalType::DateTime => {
let option = value.and_then(|v| {
Expand All @@ -902,6 +923,9 @@ impl DataValue {
DataValue::Date64(value) => match to {
LogicalType::SqlNull => Ok(DataValue::Null),
LogicalType::Varchar(len) => varchar_cast!(Self::format_datetime(value), len),
LogicalType::Text => Ok(DataValue::Utf8(
Self::format_datetime(value).map(|v| format!("{v}")),
)),
LogicalType::Date => {
let option = value.and_then(|v| {
NaiveDateTime::from_timestamp_opt(v, 0)
Expand All @@ -917,6 +941,7 @@ impl DataValue {
LogicalType::SqlNull => Ok(DataValue::Null),
LogicalType::Decimal(_, _) => Ok(DataValue::Decimal(value)),
LogicalType::Varchar(len) => varchar_cast!(value, len),
LogicalType::Text => Ok(DataValue::Utf8(value.map(|v| format!("{v}")))),
_ => Err(TypeError::CastFail),
},
}
Expand Down
2 changes: 1 addition & 1 deletion tests/slt/create.slt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
statement ok
create table t(id int primary key, v1 int, v2 int, v3 int)
create table t(id int primary key, v1 int, v2 int, v3 text)

0 comments on commit 15f0deb

Please sign in to comment.