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

feat: filtering support type uint64 #255

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions filtering/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func (c *Checker) checkExpr(e *expr.Expr) error {
return c.checkDoubleLiteral(e)
case *expr.Constant_Int64Value:
return c.checkInt64Literal(e)
case *expr.Constant_Uint64Value:
return c.checkUint64Literal(e)
case *expr.Constant_StringValue:
return c.checkStringLiteral(e)
default:
Expand Down Expand Up @@ -155,6 +157,11 @@ func (c *Checker) resolveCallExprFunctionOverload(
if !ok {
return nil, c.errorf(callExpr.Args[i], "unknown type")
}

if argType.GetPrimitive() == expr.Type_UINT64 && param.GetPrimitive() == expr.Type_INT64 {
param = TypeUint
srstack marked this conversation as resolved.
Show resolved Hide resolved
}

if !proto.Equal(argType, param) {
allTypesMatch = false
break
Expand Down Expand Up @@ -215,6 +222,10 @@ func (c *Checker) checkInt64Literal(e *expr.Expr) error {
return c.setType(e, TypeInt)
}

func (c *Checker) checkUint64Literal(e *expr.Expr) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't alias Uint to Uint64 - let's use consistent terminology across the board

Copy link
Author

@srstack srstack Dec 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aip-go/filtering/checker.go

Lines 214 to 216 in 0041801

func (c *Checker) checkInt64Literal(e *expr.Expr) error {
return c.setType(e, TypeInt)
}

I was referenced here, also alias Int to Int64, ,need modifications too? @odsod

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@odsod PTAL ~

return c.setType(e, TypeUint)
}

func (c *Checker) checkStringLiteral(e *expr.Expr) error {
return c.setType(e, TypeString)
}
Expand Down
16 changes: 16 additions & 0 deletions filtering/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ func TestChecker(t *testing.T) {
},
},

{
filter: `foo = 10000000000000000001`,
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareIdent("foo", TypeUint),
},
},

{
filter: `foo = 10 OR foo = 10000000000000000001`,
declarations: []DeclarationOption{
DeclareStandardFunctions(),
DeclareIdent("foo", TypeUint),
},
},

{
filter: "NOT (a OR b)",
declarations: []DeclarationOption{
Expand Down
12 changes: 12 additions & 0 deletions filtering/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ func Int(value int64) *expr.Expr {
}
}

func Uint(value uint64) *expr.Expr {
return &expr.Expr{
ExprKind: &expr.Expr_ConstExpr{
ConstExpr: &expr.Constant{
ConstantKind: &expr.Constant_Uint64Value{
Uint64Value: value,
},
},
},
}
}

func Equals(lhs, rhs *expr.Expr) *expr.Expr {
return Function(FunctionEquals, lhs, rhs)
}
Expand Down
12 changes: 12 additions & 0 deletions filtering/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func StandardFunctionNot() *expr.Decl {
// LessThan overloads.
const (
FunctionOverloadLessThanInt = FunctionLessThan + "_int"
FunctionOverloadLessThanUint = FunctionLessThan + "_uint"
FunctionOverloadLessThanFloat = FunctionLessThan + "_float"
FunctionOverloadLessThanString = FunctionLessThan + "_string"
FunctionOverloadLessThanTimestamp = FunctionLessThan + "_timestamp"
Expand All @@ -135,6 +136,7 @@ func StandardFunctionLessThan() *expr.Decl {
return NewFunctionDeclaration(
FunctionLessThan,
NewFunctionOverload(FunctionOverloadLessThanInt, TypeBool, TypeInt, TypeInt),
NewFunctionOverload(FunctionOverloadLessThanUint, TypeBool, TypeUint, TypeUint),
NewFunctionOverload(FunctionOverloadLessThanFloat, TypeBool, TypeFloat, TypeFloat),
NewFunctionOverload(FunctionOverloadLessThanString, TypeBool, TypeString, TypeString),
NewFunctionOverload(FunctionOverloadLessThanTimestamp, TypeBool, TypeTimestamp, TypeTimestamp),
Expand All @@ -146,6 +148,7 @@ func StandardFunctionLessThan() *expr.Decl {
// GreaterThan overloads.
const (
FunctionOverloadGreaterThanInt = FunctionGreaterThan + "_int"
FunctionOverloadGreaterThanUint = FunctionGreaterThan + "_uint"
FunctionOverloadGreaterThanFloat = FunctionGreaterThan + "_float"
FunctionOverloadGreaterThanString = FunctionGreaterThan + "_string"
FunctionOverloadGreaterThanTimestamp = FunctionGreaterThan + "_timestamp"
Expand All @@ -158,6 +161,7 @@ func StandardFunctionGreaterThan() *expr.Decl {
return NewFunctionDeclaration(
FunctionGreaterThan,
NewFunctionOverload(FunctionOverloadGreaterThanInt, TypeBool, TypeInt, TypeInt),
NewFunctionOverload(FunctionOverloadGreaterThanUint, TypeBool, TypeUint, TypeUint),
NewFunctionOverload(FunctionOverloadGreaterThanFloat, TypeBool, TypeFloat, TypeFloat),
NewFunctionOverload(FunctionOverloadGreaterThanString, TypeBool, TypeString, TypeString),
NewFunctionOverload(FunctionOverloadGreaterThanTimestamp, TypeBool, TypeTimestamp, TypeTimestamp),
Expand All @@ -169,6 +173,7 @@ func StandardFunctionGreaterThan() *expr.Decl {
// LessEquals overloads.
const (
FunctionOverloadLessEqualsInt = FunctionLessEquals + "_int"
FunctionOverloadLessEqualsUint = FunctionLessEquals + "_uint"
FunctionOverloadLessEqualsFloat = FunctionLessEquals + "_float"
FunctionOverloadLessEqualsString = FunctionLessEquals + "_string"
FunctionOverloadLessEqualsTimestamp = FunctionLessEquals + "_timestamp"
Expand All @@ -181,6 +186,7 @@ func StandardFunctionLessEquals() *expr.Decl {
return NewFunctionDeclaration(
FunctionLessEquals,
NewFunctionOverload(FunctionOverloadLessEqualsInt, TypeBool, TypeInt, TypeInt),
NewFunctionOverload(FunctionOverloadLessEqualsUint, TypeBool, TypeUint, TypeUint),
NewFunctionOverload(FunctionOverloadLessEqualsFloat, TypeBool, TypeFloat, TypeFloat),
NewFunctionOverload(FunctionOverloadLessEqualsString, TypeBool, TypeString, TypeString),
NewFunctionOverload(FunctionOverloadLessEqualsTimestamp, TypeBool, TypeTimestamp, TypeTimestamp),
Expand All @@ -192,6 +198,7 @@ func StandardFunctionLessEquals() *expr.Decl {
// GreaterEquals overloads.
const (
FunctionOverloadGreaterEqualsInt = FunctionGreaterEquals + "_int"
FunctionOverloadGreaterEqualsUint = FunctionGreaterEquals + "_uint"
FunctionOverloadGreaterEqualsFloat = FunctionGreaterEquals + "_float"
FunctionOverloadGreaterEqualsString = FunctionGreaterEquals + "_string"
FunctionOverloadGreaterEqualsTimestamp = FunctionGreaterEquals + "_timestamp"
Expand All @@ -204,6 +211,7 @@ func StandardFunctionGreaterEquals() *expr.Decl {
return NewFunctionDeclaration(
FunctionGreaterEquals,
NewFunctionOverload(FunctionOverloadGreaterEqualsInt, TypeBool, TypeInt, TypeInt),
NewFunctionOverload(FunctionOverloadGreaterEqualsUint, TypeBool, TypeUint, TypeUint),
NewFunctionOverload(FunctionOverloadGreaterEqualsFloat, TypeBool, TypeFloat, TypeFloat),
NewFunctionOverload(FunctionOverloadGreaterEqualsString, TypeBool, TypeString, TypeString),
NewFunctionOverload(FunctionOverloadGreaterEqualsTimestamp, TypeBool, TypeTimestamp, TypeTimestamp),
Expand All @@ -216,6 +224,7 @@ func StandardFunctionGreaterEquals() *expr.Decl {
const (
FunctionOverloadEqualsBool = FunctionEquals + "_bool"
FunctionOverloadEqualsInt = FunctionEquals + "_int"
FunctionOverloadEqualsUint = FunctionEquals + "_uint"
FunctionOverloadEqualsFloat = FunctionEquals + "_float"
FunctionOverloadEqualsString = FunctionEquals + "_string"
FunctionOverloadEqualsTimestamp = FunctionEquals + "_timestamp"
Expand All @@ -229,6 +238,7 @@ func StandardFunctionEquals() *expr.Decl {
FunctionEquals,
NewFunctionOverload(FunctionOverloadEqualsBool, TypeBool, TypeBool, TypeBool),
NewFunctionOverload(FunctionOverloadEqualsInt, TypeBool, TypeInt, TypeInt),
NewFunctionOverload(FunctionOverloadEqualsUint, TypeBool, TypeUint, TypeUint),
NewFunctionOverload(FunctionOverloadEqualsFloat, TypeBool, TypeFloat, TypeFloat),
NewFunctionOverload(FunctionOverloadEqualsString, TypeBool, TypeString, TypeString),
NewFunctionOverload(FunctionOverloadEqualsTimestamp, TypeBool, TypeTimestamp, TypeTimestamp),
Expand All @@ -241,6 +251,7 @@ func StandardFunctionEquals() *expr.Decl {
const (
FunctionOverloadNotEqualsBool = FunctionNotEquals + "_bool"
FunctionOverloadNotEqualsInt = FunctionNotEquals + "_int"
FunctionOverloadNotEqualsUint = FunctionNotEquals + "_uint"
FunctionOverloadNotEqualsFloat = FunctionNotEquals + "_float"
FunctionOverloadNotEqualsString = FunctionNotEquals + "_string"
FunctionOverloadNotEqualsTimestamp = FunctionNotEquals + "_timestamp"
Expand All @@ -254,6 +265,7 @@ func StandardFunctionNotEquals() *expr.Decl {
FunctionNotEquals,
NewFunctionOverload(FunctionOverloadNotEqualsBool, TypeBool, TypeBool, TypeBool),
NewFunctionOverload(FunctionOverloadNotEqualsInt, TypeBool, TypeInt, TypeInt),
NewFunctionOverload(FunctionOverloadNotEqualsUint, TypeBool, TypeUint, TypeUint),
NewFunctionOverload(FunctionOverloadNotEqualsFloat, TypeBool, TypeFloat, TypeFloat),
NewFunctionOverload(FunctionOverloadNotEqualsString, TypeBool, TypeString, TypeString),
NewFunctionOverload(FunctionOverloadNotEqualsTimestamp, TypeBool, TypeTimestamp, TypeTimestamp),
Expand Down
6 changes: 6 additions & 0 deletions filtering/parsedexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ func parsedInt(id, value int64) *expr.Expr {
return result
}

func parsedUint(id int64, value uint64) *expr.Expr {
result := Uint(value)
result.Id = id
return result
}

func parsedText(id int64, s string) *expr.Expr {
result := Text(s)
result.Id = id
Expand Down
50 changes: 49 additions & 1 deletion filtering/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ func (p *Parser) ParseComposite() (_ *expr.Expr, err error) {
// number
// : float
// | int
// | uint
// ;
//
// float
Expand All @@ -462,17 +463,28 @@ func (p *Parser) ParseComposite() (_ *expr.Expr, err error) {
// : MINUS? NUMBER
// | MINUS? HEX
// ;
//
// uint
// : NUMBER
// | HEX
// ;
func (p *Parser) ParseNumber() (_ *expr.Expr, err error) {
start := p.lexer.Position()
defer func() {
if err != nil {
err = p.wrapf(err, start, "number")
}
}()

if float, ok := p.TryParseFloat(); ok {
return float, nil
}
return p.ParseInt()

if int, ok := p.TryParseInt(); ok {
return int, nil
}

return p.ParseUint()
}

func (p *Parser) TryParseNumber() (*expr.Expr, bool) {
Expand Down Expand Up @@ -554,6 +566,16 @@ func (p *Parser) TryParseFloat() (*expr.Expr, bool) {
return result, true
}

func (p *Parser) TryParseInt() (*expr.Expr, bool) {
start := *p
result, err := p.ParseInt()
if err != nil {
*p = start
return nil, false
}
return result, true
}

// ParseInt parses an int.
//
// EBNF
Expand Down Expand Up @@ -584,6 +606,32 @@ func (p *Parser) ParseInt() (_ *expr.Expr, err error) {
return parsedInt(p.nextID(start), intValue), nil
}

// ParseUint parses an uint.
//
// EBNF
//
// uint
// : NUMBER
// | HEX
// ;
func (p *Parser) ParseUint() (_ *expr.Expr, err error) {
start := p.lexer.Position()
defer func() {
if err != nil {
err = p.wrapf(err, start, "uint")
}
}()
token, err := p.parseToken(TokenTypeNumber.Test, TokenTypeHexNumber.Test)
if err != nil {
return nil, err
}
uintValue, err := strconv.ParseUint(token.Value, 0, 64)
if err != nil {
return nil, err
}
return parsedUint(p.nextID(start), uintValue), nil
}

// ParseArg parses an Arg.
//
// EBNF
Expand Down
9 changes: 9 additions & 0 deletions filtering/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ func TestParser(t *testing.T) {
GreaterEquals(Text("a"), Int(100)),
),
},

{
filter: " a < 10 OR a >= 10000000000000000001",
expected: Or(
LessThan(Text("a"), Int(10)),
GreaterEquals(Text("a"), Uint(10000000000000000001)),
),
},

{
filter: "a OR b OR c",
expected: Or(
Expand Down
1 change: 1 addition & 0 deletions filtering/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
//nolint:gochecknoglobals
var (
TypeInt = &expr.Type{TypeKind: &expr.Type_Primitive{Primitive: expr.Type_INT64}}
TypeUint = &expr.Type{TypeKind: &expr.Type_Primitive{Primitive: expr.Type_UINT64}}
TypeFloat = &expr.Type{TypeKind: &expr.Type_Primitive{Primitive: expr.Type_DOUBLE}}
TypeString = &expr.Type{TypeKind: &expr.Type_Primitive{Primitive: expr.Type_STRING}}
TypeBool = &expr.Type{TypeKind: &expr.Type_Primitive{Primitive: expr.Type_BOOL}}
Expand Down