Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
type constraints validate ScalarValue and return valid Datum (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-dukhno authored Sep 13, 2020
1 parent cce51ad commit dd6d413
Show file tree
Hide file tree
Showing 16 changed files with 738 additions and 866 deletions.
55 changes: 13 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"src/ast",
"src/binary",
"src/constraints",
"src/data_manager",
"src/expr_eval",
"src/kernel",
Expand Down
101 changes: 2 additions & 99 deletions src/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ impl<'a> Datum<'a> {
}

pub fn from_f32(val: f32) -> Datum<'static> {
Datum::Float32(val.into())
Datum::Float32(OrderedFloat(val))
}

pub fn from_f64(val: f64) -> Datum<'static> {
Datum::Float64(val.into())
Datum::Float64(OrderedFloat(val))
}

#[allow(clippy::should_implement_trait)]
Expand All @@ -161,123 +161,26 @@ impl<'a> Datum<'a> {
Datum::SqlType(val)
}

// @TODO: Add accessor helper functions.
pub fn as_i16(&self) -> i16 {
match self {
Self::Int16(val) => *val,
_ => panic!("invalid use of Datum::as_i16"),
}
}

pub fn as_i32(&self) -> i32 {
match self {
Self::Int32(val) => *val,
_ => panic!("invalid use of Datum::as_i32"),
}
}

pub fn as_i64(&self) -> i64 {
match self {
Self::Int64(val) => *val,
_ => panic!("invalid use of Datum::as_i64"),
}
}

pub fn as_u64(&self) -> u64 {
match self {
Self::UInt64(val) => *val,
_ => panic!("invalid use of Datum::as_u64"),
}
}

pub fn as_f32(&self) -> f32 {
match self {
Self::Float32(val) => **val,
_ => panic!("invalid use of Datum::as_f32"),
}
}

pub fn as_f64(&self) -> f64 {
match self {
Self::Float64(val) => **val,
_ => panic!("invalid use of Datum::as_f64"),
}
}

pub fn as_bool(&self) -> bool {
match self {
Self::True => true,
Self::False => false,
_ => panic!("invalid use of Datum::as_bool"),
}
}

pub fn as_str(&self) -> &str {
match self {
Self::String(s) => s,
_ => panic!("invalid use of Datum::as_str"),
}
}

pub fn as_string(&self) -> &str {
match self {
Self::OwnedString(s) => s,
_ => panic!("invalid use of Datum::as_string"),
}
}

pub fn as_sql_type(&self) -> SqlType {
match self {
Self::SqlType(sql_type) => *sql_type,
_ => panic!("invalid use of Datum::as_sql_type"),
}
}

pub fn is_integer(&self) -> bool {
match self {
Self::Int16(_) | Self::Int32(_) | Self::Int64(_) => true,
_ => false,
}
}

pub fn is_float(&self) -> bool {
match self {
Self::Float32(_) | Self::Float64(_) => true,
_ => false,
}
}

pub fn is_string(&self) -> bool {
match self {
Self::String(_) | Self::OwnedString(_) => true,
_ => false,
}
}

pub fn is_boolean(&self) -> bool {
match self {
Self::True | Self::False => true,
_ => false,
}
}

pub fn is_null(&self) -> bool {
if let Self::Null = self {
true
} else {
false
}
}

pub fn is_type(&self) -> bool {
if let Self::SqlType(_) = self {
true
} else {
false
}
}

// arithmetic operations
}

#[derive(Debug, Clone)]
Expand Down
15 changes: 15 additions & 0 deletions src/constraints/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "constraints"
version = "0.1.0"
authors = ["Alex Dukhno <alex.dukhno@icloud.com>"]
edition = "2018"
publish = false

[dependencies]
ast = { path = "../ast" }
sql_model = { path = "../sql_model" }
bigdecimal = "0.1.2"
num-bigint = "0.2"

[dev-dependencies]
rstest = "0.6.4"
Loading

0 comments on commit dd6d413

Please sign in to comment.