chore: add half-range indices (#3041)

Add indices in form of [:end] or [start:]
Also, enable jsonpathv2 by default

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
Roman Gershman 2024-05-13 10:30:58 +03:00 committed by GitHub
parent 3e0a9e16cc
commit 8721ca7fca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 16 additions and 2 deletions

View file

@ -88,6 +88,8 @@ identifier: UNQ_STR
bracket_index: WILDCARD { $$ = PathSegment{SegmentType::INDEX, IndexExpr::All()}; }
| INT { $$ = PathSegment(SegmentType::INDEX, IndexExpr($1, $1)); }
| INT COLON INT { $$ = PathSegment(SegmentType::INDEX, IndexExpr::HalfOpen($1, $3)); }
| INT COLON { $$ = PathSegment(SegmentType::INDEX, IndexExpr($1, INT_MAX)); }
| COLON INT { $$ = PathSegment(SegmentType::INDEX, IndexExpr::HalfOpen(0, $2)); }
function_expr: UNQ_STR { driver->AddFunction($1); } LPARENT ROOT relative_location RPARENT
%%

View file

@ -519,6 +519,18 @@ TYPED_TEST(JsonPathTest, SubRange) {
EvaluatePath(path, json, cb);
ASSERT_THAT(arr, ElementsAre());
arr.clear();
ASSERT_EQ(0, this->Parse("$.arr[:2]"));
path = this->driver_.TakePath();
EvaluatePath(path, json, cb);
ASSERT_THAT(arr, ElementsAre(1, 2));
arr.clear();
ASSERT_EQ(0, this->Parse("$.arr[2:]"));
path = this->driver_.TakePath();
EvaluatePath(path, json, cb);
ASSERT_THAT(arr, ElementsAre(3, 4, 5));
arr.clear();
}
} // namespace dfly::json

View file

@ -55,7 +55,7 @@ IndexExpr IndexExpr::Normalize(size_t array_len) const {
return IndexExpr(1, 0); // empty range.
IndexExpr res = *this;
auto wrap = [=](int negative) {
auto wrap = [array_len](int negative) {
unsigned positive = -negative;
return positive > array_len ? 0 : array_len - positive;
};

View file

@ -30,7 +30,7 @@
#include "server/tiered_storage.h"
#include "server/transaction.h"
ABSL_FLAG(bool, jsonpathv2, false,
ABSL_FLAG(bool, jsonpathv2, true,
"If true uses Dragonfly jsonpath implementation, "
"otherwise uses legacy jsoncons implementation.");
ABSL_FLAG(bool, experimental_flat_json, false, "If true uses flat json implementation.");