From 4a6bc8b42f45da5bebb306df0dc163bdae160834 Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 19 Dec 2023 09:27:41 -0500 Subject: [PATCH 1/5] fix (parse): queries with multiple columns need to properly handle right-side logic expressions when they are complete expressions (having both a left and right side). --- mql_test.go | 9 +++++++++ parser.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/mql_test.go b/mql_test.go index 9c80eeb..fc9fd2d 100644 --- a/mql_test.go +++ b/mql_test.go @@ -67,6 +67,15 @@ func TestParse(t *testing.T) { Args: []any{"alice", "eve@example.com", "1", 21, 1.5}, }, }, + { + name: "success-multi-columned", + query: "(name=`alice`) and (email=`eve@example.com`) and (member_number = 1)", + model: &testModel{}, + want: &mql.WhereClause{ + Condition: "(name=? and (email=? and member_number=?))", + Args: []any{"alice", "eve@example.com", "1"}, + }, + }, { name: "null-string", query: "name=\"null\"", diff --git a/parser.go b/parser.go index 8a9b8a0..c86c765 100644 --- a/parser.go +++ b/parser.go @@ -63,6 +63,14 @@ TkLoop: return nil, fmt.Errorf("%s: %w before right side expression in: %q", op, ErrMissingLogicalOp, p.raw) // finally, assign the right expr case logicExpr.rightExpr == nil: + if e.rightExpr != nil { + // if e.rightExpr isn't nil, then we've got a complete + // expr (left + op + right) and we need to assign this to + // our rightExpr + logicExpr.rightExpr = e + break TkLoop + } + // otherwise, we need to assign the left side of e to our logicExpr.rightExpr = e.leftExpr break TkLoop } From 1204431e19d3f38355608bcde54301115d57f442 Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 19 Dec 2023 09:48:33 -0500 Subject: [PATCH 2/5] chore: update CHANGELOG.md --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33fe45d..7775b81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,16 @@ Canonical reference for changes, improvements, and bugfixes for mql. ## Next +## 0.1.3 (2023/12/19) + +* chore(deps): bump golang.org/x/crypto from 0.7.0 to 0.17.0 in /tests/postgres ([PR](https://github.com/hashicorp/mql/pull/33)) +* fix (parse): queries with multiple columns need to properly handle right-side + logic expressions when they are complete expressions (having both a left and + right side). ([PR](https://github.com/hashicorp/mql/pull/34)) +* chore: add github action to check diffs on generated bits ([PR](https://github.com/hashicorp/mql/pull/32)) +* chore: add race checker to "go test" in github action ([PR](https://github.com/hashicorp/mql/pull/31)) * chore: add govulncheck to github actions ([PR](https://github.com/hashicorp/mql/pull/30)) -* update go matrix in CI: remove 1.18 and add 1.21 ([PR](https://github.com/hashicorp/mql/pull/30)) +* update go matrix in CI: remove 1.18 and add 1.21 ([PR](https://github.com/hashicorp/mql/pull/30)) ## 0.1.2 (2023/09/18) From 855fc89a218466502c7a35e900cea9e4655fa5a5 Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 19 Dec 2023 11:45:27 -0500 Subject: [PATCH 3/5] fixup! fix (parse): queries with multiple columns need to properly handle right-side logic expressions when they are complete expressions (having both a left and right side). --- parser.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser.go b/parser.go index c86c765..9628c33 100644 --- a/parser.go +++ b/parser.go @@ -70,7 +70,7 @@ TkLoop: logicExpr.rightExpr = e break TkLoop } - // otherwise, we need to assign the left side of e to our + // otherwise, we need to assign the left side of e logicExpr.rightExpr = e.leftExpr break TkLoop } From aef2e49899aec58bad95bf9c23a1738b08968ec1 Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 19 Dec 2023 12:31:57 -0500 Subject: [PATCH 4/5] fixup! fix (parse): queries with multiple columns need to properly handle right-side logic expressions when they are complete expressions (having both a left and right side). --- mql_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mql_test.go b/mql_test.go index fc9fd2d..f94036d 100644 --- a/mql_test.go +++ b/mql_test.go @@ -76,6 +76,15 @@ func TestParse(t *testing.T) { Args: []any{"alice", "eve@example.com", "1"}, }, }, + { + name: "success-multi-columned-with-an-or", + query: "(name=`alice`) and (email=`eve@example.com`) or (member_number = 1)", + model: &testModel{}, + want: &mql.WhereClause{ + Condition: "(name=? and (email=? or member_number=?))", + Args: []any{"alice", "eve@example.com", "1"}, + }, + }, { name: "null-string", query: "name=\"null\"", From ce127a092d344d83b1775d1f0157bed83c62a4ea Mon Sep 17 00:00:00 2001 From: Jim Date: Tue, 19 Dec 2023 16:25:59 -0500 Subject: [PATCH 5/5] fixup! fix (parse): queries with multiple columns need to properly handle right-side logic expressions when they are complete expressions (having both a left and right side). --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 971d0e8..33dc3d6 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ Example query: If your model contains a time.Time field, then we'll append `::date` to the column name when generating a where clause and the comparison value must be in -an `ISO-8601` format. +an `ISO-8601` format. Note: It's possible to compare date-time fields down to the millisecond using `::date` and a literal in `ISO-8601` format. @@ -98,6 +98,12 @@ Example date comparison down to the HH::MM using an ISO-8601 format: `name="alice" and created_at>"2023-12-01 14:01"` +Note: Expressions with the same level of precedence are evaluated right to left. +Example: +`name="alice" and age > 11 and region = +"Boston"` is evaluated as: `name="alice" and (age > 11 and region = +"Boston")` + ### Mapping column names @@ -194,11 +200,11 @@ if err != nil { ``` - ### Grammar See: [GRAMMAR.md](./GRAMMAR.md) + ## Security **Please note**: We take security and our users' trust very seriously. If you