Skip to content

Commit c3feb4b

Browse files
authored
feat: add logical operations && comparison operations to OwnedColumn (#87)
# Rationale for this change This PR is split out from #81 and continues #85. <!-- Why are you proposing this change? If this is already explained clearly in the linked Jira ticket then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> # What changes are included in this PR? - add `NOT`, `AND` and `OR` to `column_operation.rs` - add comparison operations to `OwnedColumn` <!-- There is no need to duplicate the description in the ticket here but it is sometimes worth providing a summary of the individual changes in this PR. --> # Are these changes tested? Yes <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? -->
1 parent 618b4bb commit c3feb4b

File tree

3 files changed

+842
-3
lines changed

3 files changed

+842
-3
lines changed

crates/proof-of-sql/src/base/database/column_operation.rs

+56
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,35 @@ pub fn try_divide_column_types(
154154
Ok(ColumnType::Decimal75(precision, scale))
155155
}
156156

157+
// Unary operations
158+
159+
/// Negate a slice of boolean values.
160+
pub(super) fn slice_not(input: &[bool]) -> Vec<bool> {
161+
input.iter().map(|l| -> bool { !*l }).collect::<Vec<_>>()
162+
}
163+
157164
// Binary operations on slices of the same type
158165

166+
/// Element-wise AND on two boolean slices of the same length.
167+
///
168+
/// We do not check for length equality here.
169+
pub(super) fn slice_and(lhs: &[bool], rhs: &[bool]) -> Vec<bool> {
170+
lhs.iter()
171+
.zip(rhs.iter())
172+
.map(|(l, r)| -> bool { *l && *r })
173+
.collect::<Vec<_>>()
174+
}
175+
176+
/// Element-wise OR on two boolean slices of the same length.
177+
///
178+
/// We do not check for length equality here.
179+
pub(super) fn slice_or(lhs: &[bool], rhs: &[bool]) -> Vec<bool> {
180+
lhs.iter()
181+
.zip(rhs.iter())
182+
.map(|(l, r)| -> bool { *l || *r })
183+
.collect::<Vec<_>>()
184+
}
185+
159186
/// Try to check whether two slices of the same length are equal element-wise.
160187
///
161188
/// We do not check for length equality here.
@@ -1298,6 +1325,35 @@ mod test {
12981325
));
12991326
}
13001327

1328+
// NOT
1329+
#[test]
1330+
fn we_can_negate_boolean_slices() {
1331+
let input = [true, false, true];
1332+
let actual = slice_not(&input);
1333+
let expected = vec![false, true, false];
1334+
assert_eq!(expected, actual);
1335+
}
1336+
1337+
// AND
1338+
#[test]
1339+
fn we_can_and_boolean_slices() {
1340+
let lhs = [true, false, true, false];
1341+
let rhs = [true, true, false, false];
1342+
let actual = slice_and(&lhs, &rhs);
1343+
let expected = vec![true, false, false, false];
1344+
assert_eq!(expected, actual);
1345+
}
1346+
1347+
// OR
1348+
#[test]
1349+
fn we_can_or_boolean_slices() {
1350+
let lhs = [true, false, true, false];
1351+
let rhs = [true, true, false, false];
1352+
let actual = slice_or(&lhs, &rhs);
1353+
let expected = vec![true, true, true, false];
1354+
assert_eq!(expected, actual);
1355+
}
1356+
13011357
// =
13021358
#[test]
13031359
fn we_can_eq_slices() {

crates/proof-of-sql/src/base/database/column_operation_error.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::base::{database::ColumnType, math::decimal::DecimalError};
2-
use proof_of_sql_parser::intermediate_ast::BinaryOperator;
2+
use proof_of_sql_parser::intermediate_ast::{BinaryOperator, UnaryOperator};
33
use thiserror::Error;
44

55
/// Errors from operations on columns.
@@ -20,6 +20,15 @@ pub enum ColumnOperationError {
2020
right_type: ColumnType,
2121
},
2222

23+
/// Incorrect `ColumnType` in unary operations
24+
#[error("{operator:?}(operand: {operand_type:?}) is not supported")]
25+
UnaryOperationInvalidColumnType {
26+
/// `UnaryOperator` that caused the error
27+
operator: UnaryOperator,
28+
/// `ColumnType` of the operand
29+
operand_type: ColumnType,
30+
},
31+
2332
/// Overflow in integer operations
2433
#[error("Overflow in integer operation: {0}")]
2534
IntegerOverflow(String),

0 commit comments

Comments
 (0)