Skip to content

Commit

Permalink
Merge pull request #201 from SpringQL/feat/unsigned
Browse files Browse the repository at this point in the history
feat: UNSIGNED INTEGER
  • Loading branch information
laysakura authored Jun 20, 2022
2 parents 931eb2d + 7c65411 commit ac7f9b9
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ All other sections are for end-users.

- Implicit `ptime` column (processing time) for streams without `ROWTIME` keyword (event time) ([#195](https://github.com/SpringQL/SpringQL/pull/195))
- `BLOB` type ([#187](https://github.com/SpringQL/SpringQL/pull/187))
- `UNSIGNED INTEGER` type ([#201](https://github.com/SpringQL/SpringQL/pull/201))

### For developers

Expand Down
2 changes: 1 addition & 1 deletion springql-core/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use pump_model::{
};
pub use relation::{
ColumnConstraint, ColumnDataType, ColumnDefinition, F32LooseType, I64LooseType,
NumericComparableType, SqlType, StringComparableLoseType,
NumericComparableType, SqlType, StringComparableLoseType, U64LooseType,
};
pub use sink_writer_model::{SinkWriterModel, SinkWriterType};
pub use source_reader_model::{SourceReaderModel, SourceReaderType};
Expand Down
1 change: 1 addition & 0 deletions springql-core/src/pipeline/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ mod sql_type;
pub use column::{ColumnConstraint, ColumnDataType, ColumnDefinition};
pub use sql_type::{
F32LooseType, I64LooseType, NumericComparableType, SqlType, StringComparableLoseType,
U64LooseType,
};
27 changes: 27 additions & 0 deletions springql-core/src/pipeline/relation/sql_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ impl SqlType {
SqlType::NumericComparable(NumericComparableType::I64Loose(I64LooseType::BigInt))
}

/// Constructor of Integer
pub fn unsigned_integer() -> SqlType {
SqlType::NumericComparable(NumericComparableType::U64Loose(
U64LooseType::UnsignedInteger,
))
}
/// Constructor of unsigned BigInt
pub fn unsigned_big_int() -> SqlType {
SqlType::NumericComparable(NumericComparableType::U64Loose(
U64LooseType::UnsignedBigInt,
))
}

/// Constructor of Float
pub fn float() -> SqlType {
SqlType::NumericComparable(NumericComparableType::F32Loose(F32LooseType::Float))
Expand Down Expand Up @@ -74,11 +87,15 @@ impl SqlType {
}

/// Numeric types (comparable).
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum NumericComparableType {
/// Loosely typed as i64
I64Loose(I64LooseType),

/// Loosely typed as u64
U64Loose(U64LooseType),

/// Loosely typed as f32
F32Loose(F32LooseType),
}
Expand All @@ -96,6 +113,16 @@ pub enum I64LooseType {
BigInt,
}

/// Unsigned integer types (loosely typed as u64).
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum U64LooseType {
/// 4-byte unsigned integer.
UnsignedInteger,

/// 8-byte unsigned integer.
UnsignedBigInt,
}

/// Float types (loosely typed as f64).
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum F32LooseType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ keyword = {
| ^"TIMESTAMP"
| ^"TRUE"
| ^"TYPE"
| ^"UNSIGNED"
| ^"WINDOW"
| ^"WRITER"
}
Expand Down Expand Up @@ -320,9 +321,11 @@ data_type = {
*/

integer_type = {
^"SMALLINT"
| ^"INTEGER"
| ^"BIGINT"
^"UNSIGNED"? ~ (
^"SMALLINT"
| ^"INTEGER"
| ^"BIGINT"
)
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ impl PestParserImpl {
let s = self_as_str(&mut params);
match s.to_ascii_uppercase().as_str() {
"INTEGER" => Ok(SqlType::integer()),
"UNSIGNED INTEGER" => Ok(SqlType::unsigned_integer()),
x => {
eprintln!("Unexpected data type parsed: {}", x);
unreachable!();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ pub trait SpringValue: Sized {
Self::default_err("i64")
}

/// # Failures
///
/// - `SpringError::Sql` when:
/// - the type implementing SqlConvertible is not convertible from u16
fn try_from_u16(_: &u16) -> Result<Self> {
Self::default_err("u16")
}

/// # Failures
///
/// - `SpringError::Sql` when:
/// - the type implementing SqlConvertible is not convertible from u32
fn try_from_u32(_: &u32) -> Result<Self> {
Self::default_err("u32")
}

/// # Failures
///
/// - `SpringError::Sql` when:
/// - the type implementing SqlConvertible is not convertible from u64
fn try_from_u64(_: &u64) -> Result<Self> {
Self::default_err("u64")
}

/// # Failures
///
/// - `SpringError::Sql` when:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of https://github.com/SpringQL/SpringQL which is licensed under MIT OR Apache-2.0. See file LICENSE-MIT or LICENSE-APACHE for full license details.

use anyhow::Context;
use anyhow::{anyhow, Context};

use crate::{
api::error::{Result, SpringError},
Expand Down Expand Up @@ -84,6 +84,112 @@ impl ToNnSqlValue for i64 {
}
}

impl SpringValue for u32 {
fn try_from_i16(v: &i16) -> Result<Self> {
if 0 <= *v {
Ok(*v as u32)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i16 value ({}) into u32",
v
)))
}
}

fn try_from_i32(v: &i32) -> Result<Self> {
if 0 <= *v {
Ok(*v as u32)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i32 value ({}) into u32",
v
)))
}
}

fn try_from_i64(v: &i64) -> Result<Self> {
if 0 <= *v && *v <= u32::MAX as i64 {
Ok(*v as u32)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i64 value ({}) into u32",
v
)))
}
}

fn try_from_u16(v: &u16) -> Result<Self> {
Ok(*v as u32)
}

fn try_from_u32(v: &u32) -> Result<Self> {
Ok(*v)
}

fn try_from_u64(v: &u64) -> Result<Self> {
u32::try_from(*v)
.with_context(|| format!("cannot convert u64 value ({}) into u32", v))
.map_err(SpringError::Sql)
}
}
impl ToNnSqlValue for u32 {
fn into_sql_value(self) -> NnSqlValue {
NnSqlValue::UnsignedInteger(self)
}
}

impl SpringValue for u64 {
fn try_from_i16(v: &i16) -> Result<Self> {
if 0 <= *v {
Ok(*v as u64)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i16 value ({}) into u64",
v
)))
}
}

fn try_from_i32(v: &i32) -> Result<Self> {
if 0 <= *v {
Ok(*v as u64)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i32 value ({}) into u64",
v
)))
}
}

fn try_from_i64(v: &i64) -> Result<Self> {
if 0 <= *v {
Ok(*v as u64)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i32 value ({}) into u64",
v
)))
}
}

fn try_from_u16(v: &u16) -> Result<Self> {
Ok(*v as u64)
}

fn try_from_u32(v: &u32) -> Result<Self> {
Ok(*v as u64)
}

fn try_from_u64(v: &u64) -> Result<Self> {
Ok(*v)
}
}
impl ToNnSqlValue for u64 {
fn into_sql_value(self) -> NnSqlValue {
NnSqlValue::UnsignedBigInt(self)
}
}

#[cfg(test)]
mod tests_i32 {
use crate::{
Expand Down
Loading

0 comments on commit ac7f9b9

Please sign in to comment.