From 72ca000fa4971da867859d72bc11e296668306b8 Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Thu, 25 May 2023 09:14:00 -0700 Subject: [PATCH] fix AND & OR (#14) --- CHANGELOG.md | 7 ++++++- Cargo.toml | 16 ++++++++-------- src/parser.rs | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d3cd9a..3ac691b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.9.1] - 2023-05-25 +### Fixed +- Fixed AND & OR not early exiting evaluation. + ## [0.9.0] - 2023-01-05 ### Added - Added new `_uppercase_` & `_title_` COERCE identifiers. @@ -95,7 +99,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Initial release. -[Unreleased]: https://github.com/rust-playground/ksql/compare/v0.9.0...HEAD +[Unreleased]: https://github.com/rust-playground/ksql/compare/v0.9.1...HEAD +[0.9.1]: https://github.com/rust-playground/ksql/compare/v0.9.0...v0.9.1 [0.9.0]: https://github.com/rust-playground/ksql/compare/v0.8.0...v0.9.0 [0.8.0]: https://github.com/rust-playground/ksql/compare/v0.7.0...v0.8.0 [0.7.0]: https://github.com/rust-playground/ksql/compare/v0.6.2...v0.7.0 diff --git a/Cargo.toml b/Cargo.toml index 0f33872..06da129 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ksql" description = "A JSON data expression lexer, parser, cli and library" -version = "0.9.0" +version = "0.9.1" edition = "2021" license = "MIT OR Apache-2.0" readme = "README.md" @@ -17,16 +17,16 @@ keywords = [ [dependencies] anydate = "0.3.0" -anyhow = "1.0.69" -chrono = { version = "0.4.23", features = ["serde"] } -clap = { version = "4.1.4", features = ["derive"] } +anyhow = "1.0.71" +chrono = { version = "0.4.24", features = ["serde"] } +clap = { version = "4.3.0", features = ["derive"] } gjson = "0.8.1" -serde = { version = "1.0.152", features = ["derive"] } -serde_json = "1.0.92" -thiserror = "1.0.38" +serde = { version = "1.0.163", features = ["derive"] } +serde_json = "1.0.96" +thiserror = "1.0.40" [dev-dependencies] -criterion = { version = "0.4.0", features = ["html_reports"] } +criterion = { version = "0.5.0", features = ["html_reports"] } [lib] bench = false diff --git a/src/parser.rs b/src/parser.rs index da6cf31..a8029fa 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -841,6 +841,13 @@ struct Or { impl Expression for Or { fn calculate(&self, json: &[u8]) -> Result { let left = self.left.calculate(json)?; + + if let Value::Bool(is_true) = left { + if is_true { + return Ok(left); + } + } + let right = self.right.calculate(json)?; match (left, right) { @@ -859,6 +866,13 @@ struct And { impl Expression for And { fn calculate(&self, json: &[u8]) -> Result { let left = self.left.calculate(json)?; + + if let Value::Bool(is_true) = left { + if !is_true { + return Ok(left); + } + } + let right = self.right.calculate(json)?; match (left, right) { @@ -1807,6 +1821,11 @@ mod tests { let result = ex.calculate(src)?; assert_eq!(Value::Bool(true), result); + let expression = r#".MyValue != NULL && .MyValue > 19"#; + let ex = Parser::parse(expression)?; + let result = ex.calculate(src)?; + assert_eq!(Value::Bool(false), result); + Ok(()) } }