Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/sqllib/src/aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ impl UnsignedWrappers {
}

// Conversion from U to O
// functions is the same.
#[doc(hidden)]
pub fn to_signed<O, S, I, U>(value: U, ascending: bool, _nullsLast: bool) -> O
where
Expand Down
188 changes: 188 additions & 0 deletions crates/sqllib/src/boolean.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
//! Boolean operations

use crate::some_function1;

#[doc(hidden)]
#[inline(always)]
pub fn wrap_bool(b: Option<bool>) -> bool {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

many of these functions were moved from lib.rs

b.unwrap_or_default()
}

#[doc(hidden)]
#[inline(always)]
pub fn or_b_b<F>(left: bool, right: F) -> bool
where
F: Fn() -> bool,
{
left || right()
}

#[doc(hidden)]
#[inline(always)]
pub fn or_bN_b<F>(left: Option<bool>, right: F) -> Option<bool>
where
F: Fn() -> bool,
{
match left {
Some(l) => Some(l || right()),
None => match right() {
true => Some(true),
_ => None,
},
}
}

#[doc(hidden)]
#[inline(always)]
pub fn or_b_bN<F>(left: bool, right: F) -> Option<bool>
where
F: Fn() -> Option<bool>,
{
match left {
false => right(),
true => Some(true),
}
}

#[doc(hidden)]
#[inline(always)]
pub fn or_bN_bN<F>(left: Option<bool>, right: F) -> Option<bool>
where
F: Fn() -> Option<bool>,
{
match left {
None => match right() {
Some(true) => Some(true),
_ => None,
},
Some(false) => right(),
Some(true) => Some(true),
}
}

// OR and AND are special, they can't be generated by rules

#[doc(hidden)]
#[inline(always)]
pub fn and_b_b<F>(left: bool, right: F) -> bool
where
F: Fn() -> bool,
{
left && right()
}

#[doc(hidden)]
#[inline(always)]
pub fn and_bN_b<F>(left: Option<bool>, right: F) -> Option<bool>
where
F: Fn() -> bool,
{
match left {
Some(false) => Some(false),
Some(true) => Some(right()),
None => match right() {
false => Some(false),
_ => None,
},
}
}

#[doc(hidden)]
#[inline(always)]
pub fn and_b_bN<F>(left: bool, right: F) -> Option<bool>
where
F: Fn() -> Option<bool>,
{
match left {
false => Some(false),
true => right(),
}
}

#[doc(hidden)]
#[inline(always)]
pub fn and_bN_bN<F>(left: Option<bool>, right: F) -> Option<bool>
where
F: Fn() -> Option<bool>,
{
match left {
Some(false) => Some(false),
Some(true) => right(),
None => match right() {
Some(false) => Some(false),
_ => None,
},
}
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_true_b_(left: bool) -> bool {
left
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_true_bN_(left: Option<bool>) -> bool {
matches!(left, Some(true))
}

#[doc(hidden)]
#[inline(always)]
pub fn is_false_b_(left: bool) -> bool {
!left
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_false_bN_(left: Option<bool>) -> bool {
matches!(left, Some(false))
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_not_true_b_(left: bool) -> bool {
!left
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_not_true_bN_(left: Option<bool>) -> bool {
match left {
Some(true) => false,
Some(false) => true,
_ => true,
}
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_not_false_b_(left: bool) -> bool {
left
}

#[doc(hidden)]
#[inline(always)]
pub const fn is_not_false_bN_(left: Option<bool>) -> bool {
match left {
Some(true) => true,
Some(false) => false,
_ => true,
}
}

#[doc(hidden)]
#[inline(always)]
pub const fn bool_to_i8_(value: bool) -> i8 {
if value { 1 } else { 0 }
}

some_function1!(bool_to_i8, bool, i8);

#[doc(hidden)]
#[inline(always)]
pub const fn i8_to_bool_(value: i8) -> bool {
value != 0
}

some_function1!(i8_to_bool, i8, bool);
15 changes: 15 additions & 0 deletions crates/sqllib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ where
Err(e) => Err(SqlRuntimeError::from_string(e.to_string())),
}
}

#[doc(hidden)]
// If the data is Ok(None), convert it to Err, otherwise leave it unchanged
pub fn unwrap_sql_result<T>(data: SqlResult<Option<T>>) -> SqlResult<T> {
match data {
Err(e) => Err(e),
Ok(None) => Err(SqlRuntimeError::from_strng("NULL result produced")),
Comment thread
mihaibudiu marked this conversation as resolved.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-flagging from last round (not a blocker): from_strng is a typo for from_string, and "NULL result produced" is non-actionable for end users now that nullable BOOL/INTERVAL ordering routes through here. Worth tightening the message (which SQL op, or at least "NULL result in non-nullable context") while we're in the area.

Ok(Some(data)) => Ok(data),
}
}

#[doc(hidden)]
pub fn wrap_sql_result<T>(data: T) -> SqlResult<T> {
Ok(data)
}
26 changes: 22 additions & 4 deletions crates/sqllib/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
use crate::{
Date, SqlDecimal, finite_or_null,
operators::{eq, gt, gte, lt, lte, neq},
plus_Date_Date_LongInterval__, sign, some_existing_operator, some_function2, some_operator,
some_polymorphic_function1, some_polymorphic_function2,
plus_Date_Date_LongInterval__, sign, some_existing_operator, some_function1, some_function2,
some_operator, some_polymorphic_function1, some_polymorphic_function2,
timestamp::{extract_epoch_Date, extract_quarter_Date},
};
use dbsp::{algebra::F64, num_entries_scalar};
Expand Down Expand Up @@ -1121,18 +1121,36 @@ pub fn extract_hour_LongInterval(_value: LongInterval) -> i64 {
0
}

#[doc(hidden)]
pub fn long_interval_to_integer_(value: LongInterval) -> i32 {
value.months
}

some_function1!(long_interval_to_integer, LongInterval, i32);

#[doc(hidden)]
pub fn integer_to_long_interval_(value: i32) -> LongInterval {
LongInterval::from_months(value)
}

some_function1!(integer_to_long_interval, i32, LongInterval);

///////////

#[doc(hidden)]
pub fn short_interval_to_integer(value: ShortInterval) -> i64 {
pub fn short_interval_to_integer_(value: ShortInterval) -> i64 {
value.microseconds
}

some_function1!(short_interval_to_integer, ShortInterval, i64);

#[doc(hidden)]
pub fn integer_to_short_interval(value: i64) -> ShortInterval {
pub fn integer_to_short_interval_(value: i64) -> ShortInterval {
ShortInterval::from_microseconds(value)
}

some_function1!(integer_to_short_interval, i64, ShortInterval);

#[doc(hidden)]
pub fn extract_day_ShortInterval(value: ShortInterval) -> i64 {
value.microseconds() / 86_400_000_000_i64
Expand Down
Loading