Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support $gt comparision operator #330

Merged
merged 11 commits into from
Apr 4, 2022
6 changes: 3 additions & 3 deletions internal/handlers/common/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ func compareScalars(a, b any) compareResult {

case time.Time:
b, ok := b.(time.Time)
if ok {
return compareOrdered(a.UnixNano(), b.UnixNano())
if !ok {
return notEqual
}
return notEqual
return compareOrdered(a.UnixMilli(), b.UnixMilli())

case types.NullType:
_, ok := b.(types.NullType)
Expand Down
12 changes: 3 additions & 9 deletions internal/handlers/common/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,8 @@ func filterFieldExpr(fieldValue any, expr *types.Document) (bool, error) {
switch exprKey {
case "$eq":
// {field: {$eq: exprValue}}
_, isValueDocument := fieldValue.(*types.Document)
_, isValueArray := fieldValue.(*types.Array)
switch {
case isValueDocument, isValueArray:
return compare(fieldValue, exprValue) == equal, nil

default:
return compareScalars(fieldValue, exprValue) == equal, nil
if compare(fieldValue, exprValue) != equal {
return false, nil
}

case "$ne":
Expand All @@ -181,7 +175,7 @@ func filterFieldExpr(fieldValue any, expr *types.Document) (bool, error) {

case "$gt":
// {field: {$gt: exprValue}}
if c := compareScalars(fieldValue, exprValue); c != greater {
if compare(fieldValue, exprValue) != greater {
return false, nil
}

Expand Down
251 changes: 251 additions & 0 deletions internal/handlers/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,257 @@ func TestFind(t *testing.T) {
resp: must.NotFail(types.NewArray()),
},

"GtString": {
schemas: []string{"values"},
req: must.NotFail(types.NewDocument(
"find", "values",
"filter", must.NotFail(types.NewDocument(
"value", must.NotFail(types.NewDocument(
"$gt", "boo",
)),
)),
)),
resp: must.NotFail(types.NewArray(
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x01},
"name", "string",
"value", "foo",
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x02, 0x03},
"name", "string-shorter",
"value", "z",
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x04, 0x04},
"name", "array-three",
"value", must.NotFail(types.NewArray(int32(42), "foo", types.Null)),
)),
)),
},
"GtDouble": {
schemas: []string{"values"},
req: must.NotFail(types.NewDocument(
"find", "values",
"filter", must.NotFail(types.NewDocument(
"value", must.NotFail(types.NewDocument(
"$gt", 42.12,
)),
)),
)),
resp: must.NotFail(types.NewArray(
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01},
"name", "double",
"value", 42.13,
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x01, 0x03},
"name", "double-max",
"value", math.MaxFloat64,
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x01, 0x05, 0x00, 0x00, 0x01, 0x05},
"name", "double-positive-infinity",
"value", math.Inf(+1),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00, 0x04, 0x03},
"name", "array-one",
"value", must.NotFail(types.NewArray(42.13)),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x10, 0x03, 0x00, 0x00, 0x10, 0x03},
"name", "int32-max",
"value", int32(2147483647),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x12, 0x03, 0x00, 0x00, 0x12, 0x03},
"name", "int64-max",
"value", int64(9223372036854775807),
)),
)),
},
"GtInt32": {
schemas: []string{"values"},
req: must.NotFail(types.NewDocument(
"find", "values",
"filter", must.NotFail(types.NewDocument(
"value", must.NotFail(types.NewDocument(
"$gt", int32(41),
)),
)),
)),
resp: must.NotFail(types.NewArray(
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01},
"name", "double",
"value", 42.13,
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x01, 0x03},
"name", "double-max",
"value", math.MaxFloat64,
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x01, 0x05, 0x00, 0x00, 0x01, 0x05},
"name", "double-positive-infinity",
"value", math.Inf(+1),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x04, 0x01},
"name", "array",
"value", must.NotFail(types.NewArray("array", int32(42))),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00, 0x04, 0x03},
"name", "array-one",
"value", must.NotFail(types.NewArray(42.13)),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x04, 0x04},
"name", "array-three",
"value", must.NotFail(types.NewArray(int32(42), "foo", types.Null)),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x10, 0x01},
"name", "int32",
"value", int32(42),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x10, 0x03, 0x00, 0x00, 0x10, 0x03},
"name", "int32-max",
"value", int32(2147483647),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x12, 0x01},
"name", "int64",
"value", int64(42),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x12, 0x03, 0x00, 0x00, 0x12, 0x03},
"name", "int64-max",
"value", int64(9223372036854775807),
)),
)),
},
"GtInt64": {
schemas: []string{"values"},
req: must.NotFail(types.NewDocument(
"find", "values",
"filter", must.NotFail(types.NewDocument(
"value", must.NotFail(types.NewDocument(
"$gt", int64(41),
)),
)),
)),
resp: must.NotFail(types.NewArray(
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x01},
"name", "double",
"value", 42.13,
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x01, 0x03, 0x00, 0x00, 0x01, 0x03},
"name", "double-max",
"value", math.MaxFloat64,
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x01, 0x05, 0x00, 0x00, 0x01, 0x05},
"name", "double-positive-infinity",
"value", math.Inf(+1),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x04, 0x01},
"name", "array",
"value", must.NotFail(types.NewArray("array", int32(42))),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x04, 0x03, 0x00, 0x00, 0x04, 0x03},
"name", "array-one",
"value", must.NotFail(types.NewArray(42.13)),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x04, 0x04, 0x00, 0x00, 0x04, 0x04},
"name", "array-three",
"value", must.NotFail(types.NewArray(int32(42), "foo", types.Null)),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x10, 0x01},
"name", "int32",
"value", int32(42),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x10, 0x03, 0x00, 0x00, 0x10, 0x03},
"name", "int32-max",
"value", int32(2147483647),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x12, 0x01, 0x00, 0x00, 0x12, 0x01},
"name", "int64",
"value", int64(42),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x12, 0x03, 0x00, 0x00, 0x12, 0x03},
"name", "int64-max",
"value", int64(9223372036854775807),
)),
)),
},
"GtDateTime": {
schemas: []string{"values"},
req: must.NotFail(types.NewDocument(
"find", "values",
"filter", must.NotFail(types.NewDocument(
"value", must.NotFail(types.NewDocument(
"$gt", time.Date(2021, 11, 1, 10, 18, 42, 121000000, time.UTC).Local(),
)),
)),
)),
resp: must.NotFail(types.NewArray(
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x9, 0x01, 0x00, 0x00, 0x09, 0x01},
"name", "datetime",
"value", time.Date(2021, 11, 1, 10, 18, 42, 123000000, time.UTC).Local(),
)),
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x9, 0x04, 0x00, 0x00, 0x09, 0x04},
"name", "datetime-year-max",
"value", time.Date(9999, 12, 31, 23, 59, 59, 999000000, time.UTC).Local(),
)),
)),
},
"GtTimestamp": {
schemas: []string{"values"},
req: must.NotFail(types.NewDocument(
"find", "values",
"filter", must.NotFail(types.NewDocument(
"value", must.NotFail(types.NewDocument(
"$gt", types.Timestamp(180388626444),
)),
)),
)),
resp: must.NotFail(types.NewArray(
must.NotFail(types.NewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x11, 0x01},
"name", "timestamp",
"value", types.Timestamp(180388626445),
)),
)),
},
"GtNil": {
schemas: []string{"values"},
req: must.NotFail(types.NewDocument(
"find", "values",
"filter", must.NotFail(types.NewDocument(
"value", must.NotFail(types.NewDocument(
"$gt", types.Null,
)),
)),
)),
resp: must.NotFail(types.NewArray()),
},

"BitsAllClear": {
schemas: []string{"values"},
req: must.NotFail(types.NewDocument(
Expand Down