Skip to content

Commit

Permalink
Boolean for for
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <[email protected]>
  • Loading branch information
Licenser committed Sep 29, 2023
1 parent 02f9e35 commit 397336c
Show file tree
Hide file tree
Showing 43 changed files with 451 additions and 255 deletions.
397 changes: 208 additions & 189 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/codec/syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ where
if let Some(facility) = parsed.facility {
decoded.try_insert("facility", facility.as_str());
}
if let Some(timestamp) = parsed.timestamp {
decoded.try_insert("timestamp", timestamp.timestamp_nanos());
if let Some(timestamp) = parsed.timestamp.and_then(|t| t.timestamp_nanos_opt()) {
decoded.try_insert("timestamp", timestamp);
}
decoded.try_insert(
"protocol",
Expand Down Expand Up @@ -630,7 +630,7 @@ mod test {
"appname": "rsyslogd",
"msg": "[software=\"rsyslogd\" swVersion=\"8.32.0\"] message",
"protocol": "RFC3164",
"timestamp": timestamp.timestamp_nanos()
"timestamp": timestamp.timestamp_nanos_opt().unwrap_or_default()
});
assert_eq!(
tremor_script::utils::sorted_serialize(&expected)?,
Expand Down
4 changes: 4 additions & 0 deletions tests/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ test_cases!(
empty_array_pattern,
const_in_const_lookup,
// INSERT
fold_bool_or,
fold_bool_or_imut,
fold_bool_imut,
fold_bool,
fold_number_op,
fold_number,
fold_record,
Expand Down
2 changes: 1 addition & 1 deletion tests/script_errors/bad_fold/error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Error:
1 | for event of
2 | case (k, v) => v
3 | into [] use merge end;
| ^^^^^ Found the token `merge` but expected one of `%`, `&`, `*`, `+`, `-`, `/`, `^`
| ^^^^^ Found the token `merge` but expected one of `%`, `&`, `*`, `+`, `-`, `/`, `<<`, `>>`, `>>>`, `^`, `and`, `or`, `xor`
2 changes: 1 addition & 1 deletion tests/script_errors/bad_fold_imut/error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Error:
1 | (for event of
2 | case (k, v) => v
3 | into [] use merge end)
| ^^^^^ Found the token `merge` but expected one of `%`, `&`, `*`, `+`, `-`, `/`, `^`
| ^^^^^ Found the token `merge` but expected one of `%`, `&`, `*`, `+`, `-`, `/`, `<<`, `>>`, `>>>`, `^`, `and`, `or`, `xor`
4 changes: 4 additions & 0 deletions tests/script_runtime_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ test_cases!(
subslice_no_arr,
subslice_out_of_bounds,
// INSERT
fold_bool_or_imut,
fold_bool_or,
fold_bool_op_imut,
fold_bool_op,
bad_fold_op_record,
bad_fold_op,
bad_fold_op_record_imut,
Expand Down
2 changes: 1 addition & 1 deletion tests/script_runtime_errors/bad_fold_op/error.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Error:
1 | for event of
2 | case (k, v) => v
| ^ Invalid fold operation / for arrays
| ^ Invalid fold operation /
3 | into [] use / end
2 changes: 1 addition & 1 deletion tests/script_runtime_errors/bad_fold_op_imut/error.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Error:
1 | (for event of
2 | case (k, v) => v
| ^ Invalid fold operation / for arrays
| ^ Invalid fold operation /
3 | into [] use / end)
2 changes: 1 addition & 1 deletion tests/script_runtime_errors/bad_fold_op_record/error.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Error:
1 | for event of
2 | case (k, v) => {"#{k}": v}
| ^^^^^^^^^^^ Invalid fold operation / for records
| ^^^^^^^^^^^ Invalid fold operation /
3 | into {} use / end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Error:
1 | (for event of
2 | case (k, v) => {"#{k}": v}
| ^^^^^^^^^^^ Invalid fold operation / for records
| ^^^^^^^^^^^ Invalid fold operation /
3 | into {} use / end)
5 changes: 5 additions & 0 deletions tests/script_runtime_errors/fold_bool_op/error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Error:
1 | for event of
2 | case (k, v) => v
| ^ The binary operation `and` is not defined for the type `integer` and `<not executed>`
3 | into true use and end
1 change: 1 addition & 0 deletions tests/script_runtime_errors/fold_bool_op/in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[true, 42]
3 changes: 3 additions & 0 deletions tests/script_runtime_errors/fold_bool_op/script.tremor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for event of
case (k, v) => v
into true use and end
5 changes: 5 additions & 0 deletions tests/script_runtime_errors/fold_bool_op_imut/error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Error:
1 | (for event of
2 | case (k, v) => v
| ^ The binary operation `and` is not defined for the type `integer` and `<not executed>`
3 | into true use and end)
1 change: 1 addition & 0 deletions tests/script_runtime_errors/fold_bool_op_imut/in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[true, 42]
3 changes: 3 additions & 0 deletions tests/script_runtime_errors/fold_bool_op_imut/script.tremor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(for event of
case (k, v) => v
into true use and end)
5 changes: 5 additions & 0 deletions tests/script_runtime_errors/fold_bool_or/error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Error:
1 | for event of
2 | case (k, v) => v
| ^ The binary operation `or` is not defined for the type `integer` and `<not executed>`
3 | into false use or end
1 change: 1 addition & 0 deletions tests/script_runtime_errors/fold_bool_or/in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[false, 42]
3 changes: 3 additions & 0 deletions tests/script_runtime_errors/fold_bool_or/script.tremor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for event of
case (k, v) => v
into false use or end
5 changes: 5 additions & 0 deletions tests/script_runtime_errors/fold_bool_or_imut/error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Error:
1 | (for event of
2 | case (k, v) => v
| ^ The binary operation `or` is not defined for the type `integer` and `<not executed>`
3 | into false use or end)
1 change: 1 addition & 0 deletions tests/script_runtime_errors/fold_bool_or_imut/in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[false, 42]
3 changes: 3 additions & 0 deletions tests/script_runtime_errors/fold_bool_or_imut/script.tremor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(for event of
case (k, v) => v
into false use or end)
4 changes: 4 additions & 0 deletions tests/scripts/fold_bool/in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[true, true, true]
[]
[false]
[true, false, "invalid"]
4 changes: 4 additions & 0 deletions tests/scripts/fold_bool/out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
true
true
false
false
3 changes: 3 additions & 0 deletions tests/scripts/fold_bool/script.tremor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for event of
case (k, v) => v
into true use and end
4 changes: 4 additions & 0 deletions tests/scripts/fold_bool_imut/in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[true, true, true]
[]
[false]
[true, false, "invalid"]
4 changes: 4 additions & 0 deletions tests/scripts/fold_bool_imut/out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
true
true
false
false
3 changes: 3 additions & 0 deletions tests/scripts/fold_bool_imut/script.tremor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(for event of
case (k, v) => v
into true use and end)
4 changes: 4 additions & 0 deletions tests/scripts/fold_bool_or/in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[false, false, false]
[]
[true]
[true, false, "invalid"]
4 changes: 4 additions & 0 deletions tests/scripts/fold_bool_or/out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
false
false
true
true
3 changes: 3 additions & 0 deletions tests/scripts/fold_bool_or/script.tremor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for event of
case (k, v) => v
into false use or end
4 changes: 4 additions & 0 deletions tests/scripts/fold_bool_or_imut/in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[false, false, false]
[]
[true]
[true, false, "invalid"]
4 changes: 4 additions & 0 deletions tests/scripts/fold_bool_or_imut/out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
false
false
true
true
3 changes: 3 additions & 0 deletions tests/scripts/fold_bool_or_imut/script.tremor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(for event of
case (k, v) => v
into false use or end)
2 changes: 1 addition & 1 deletion tremor-cli/tests/stdlib/std/all.tremor
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ test::suite({
}),
test::test({
"name": "rfc3339",
"test": test::assert("datetime::rfc2282", datetime::rfc2822(0), "Thu, 01 Jan 1970 00:00:00 +0000")
"test": test::assert("datetime::rfc2282", datetime::rfc2822(0), "Thu, 1 Jan 1970 00:00:00 +0000")
}),
test::test({
"name": "rfc3339 Z",
Expand Down
17 changes: 15 additions & 2 deletions tremor-script/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1563,11 +1563,24 @@ impl_expr!(Merge);

/// Fold operation for for comprehensiosn
#[derive(Clone, Debug, PartialEq, Serialize, Copy)]
pub struct ComprehensionFoldOp(pub(crate) BinOpKind);
pub enum ComprehensionFoldOp {
/// Arith expressions
Arith(BinOpKind),
/// Logical expressions
Logic(BooleanBinOpKind),
}
impl std::fmt::Display for ComprehensionFoldOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Arith(op) => op.fmt(f),
Self::Logic(op) => op.fmt(f),
}
}
}

impl Default for ComprehensionFoldOp {
fn default() -> Self {
Self(BinOpKind::Add)
Self::Arith(BinOpKind::Add)
}
}

Expand Down
15 changes: 13 additions & 2 deletions tremor-script/src/ast/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,18 @@ impl<'script> Upable<'script> for MergeRaw<'script> {
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Copy)]
pub struct ComprehensionFoldOpRaw(pub BinOpKind);
pub enum ComprehensionFoldOpRaw {
Arith(BinOpKind),
Logic(BooleanBinOpKind),
}
impl From<ComprehensionFoldOpRaw> for ComprehensionFoldOp {
fn from(val: ComprehensionFoldOpRaw) -> Self {
match val {
ComprehensionFoldOpRaw::Arith(o) => ComprehensionFoldOp::Arith(o),
ComprehensionFoldOpRaw::Logic(o) => ComprehensionFoldOp::Logic(o),
}
}
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct ComprehensionRaw<'script, Ex>
Expand Down Expand Up @@ -1266,7 +1277,7 @@ where
}
let target = self.target.up(helper)?;
let initial = self.initial.up(helper)?;
let fold = self.fold.map(|f| ComprehensionFoldOp(f.0));
let fold = self.fold.map(Into::into);

// We know that each case will have a key and a value as a shadowed
// variable so we reserve two ahead of time so we know what id's those
Expand Down
6 changes: 4 additions & 2 deletions tremor-script/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ pub fn _parse(datetime: &str, input_fmt: &str, has_timezone: bool) -> Result<u64
if has_timezone {
Ok(DateTime::parse_from_str(datetime, input_fmt)
.map_err(|e| Error::from(format!("Datetime Parse Error: {e:?}")))?
.timestamp_nanos() as u64)
.timestamp_nanos_opt()
.ok_or("Datetime Parse Error")? as u64)
} else {
Ok(NaiveDateTime::parse_from_str(datetime, input_fmt)
.map_err(|e| Error::from(format!("Datetime Parse Error: {e:?}")))?
.timestamp_nanos() as u64)
.timestamp_nanos_opt()
.ok_or("Datetime Parse Error")? as u64)
}
}

Expand Down
20 changes: 18 additions & 2 deletions tremor-script/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
pub use crate::prelude::ValueType;
use crate::{
arena,
ast::{self, base_expr::Ranged, BaseExpr},
ast::{self, base_expr::Ranged, BaseExpr, BooleanBinOpKind},
errors, lexer,
pos::{self, Span},
prelude::*,
Expand All @@ -31,8 +31,8 @@ use lalrpop_util::ParseError as LalrpopError;
use simd_json::{ExtendedValueType, TryTypeError};

use crate::errors::ErrorKind::InvalidBinaryBoolean;
use std::num;
use std::ops::{Range as RangeExclusive, RangeInclusive};
use std::{fmt::Display, num};

#[derive(Debug)]
//// An error with a associated arena index
Expand Down Expand Up @@ -1097,6 +1097,22 @@ pub fn error_generic<O: Ranged, I: Ranged, S: ToString>(outer: &O, inner: &I, er
ErrorKind::Generic(outer.extent(), inner.extent(), error.to_string()).into()
}

pub(crate) fn err_invalid_fold<O: Ranged, I: Ranged, Op: Display, T>(
outer: &O,
inner: &I,
op: Op,
) -> Result<T> {
err_generic(outer, inner, &format!("Invalid fold operation {op}"))
}
pub(crate) fn error_invalid_bool_op<O: Ranged, I: Ranged>(
outer: &O,
inner: &I,
op: BooleanBinOpKind,
left: ValueType,
right: Option<ValueType>,
) -> Error {
ErrorKind::InvalidBinaryBoolean(outer.extent(), inner.extent(), op, left, right).into()
}
pub(crate) fn error_type_conflict_mult<T, O: Ranged, I: Ranged>(
outer: &O,
inner: &I,
Expand Down
16 changes: 8 additions & 8 deletions tremor-script/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -1272,14 +1272,14 @@ MaybeFoldOperator: Option<ComprehensionFoldOpRaw> = {
}

FoldOperator: ComprehensionFoldOpRaw = {
<BinOr> => ComprehensionFoldOpRaw(<>),
<BinXor> => ComprehensionFoldOpRaw(<>),
<BinAnd> => ComprehensionFoldOpRaw(<>),
<BinBitXor> => ComprehensionFoldOpRaw(<>),
<BinBitAnd> => ComprehensionFoldOpRaw(<>),
<BinBitShift> => ComprehensionFoldOpRaw(<>),
<BinAdd> => ComprehensionFoldOpRaw(<>),
<BinMul> => ComprehensionFoldOpRaw(<>),
<BinOr> => ComprehensionFoldOpRaw::Logic(<>),
<BinXor> => ComprehensionFoldOpRaw::Logic(<>),
<BinAnd> => ComprehensionFoldOpRaw::Logic(<>),
<BinBitXor> => ComprehensionFoldOpRaw::Arith(<>),
<BinBitAnd> => ComprehensionFoldOpRaw::Arith(<>),
<BinBitShift> => ComprehensionFoldOpRaw::Arith(<>),
<BinAdd> => ComprehensionFoldOpRaw::Arith(<>),
<BinMul> => ComprehensionFoldOpRaw::Arith(<>),
}

ForCaseClauses: ComprehensionCasesRaw<'input, ExprRaw<'input>> = {
Expand Down
Loading

0 comments on commit 397336c

Please sign in to comment.