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

BED-4366 -- parse negative numbers in Cypher queries #661

Merged
merged 8 commits into from
Jul 22, 2024
7 changes: 7 additions & 0 deletions packages/go/cypher/backend/cypher/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,13 @@ func (s Emitter) WriteExpression(writer io.Writer, expression model.Expression)

return s.WriteExpression(writer, typedExpression.Right)

case *model.UnaryAddOrSubtractExpression:
if _, err := io.WriteString(writer, typedExpression.Operator.String()); err != nil {
return err
}

return s.WriteExpression(writer, typedExpression.Right)

default:
return fmt.Errorf("unexpected expression type for string formatting: %T", expression)
}
Expand Down
7 changes: 6 additions & 1 deletion packages/go/cypher/frontend/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,12 @@ func (s *ArithmeticExpressionVisitor) EnterOC_UnaryAddOrSubtractExpression(ctx *
}

func (s *ArithmeticExpressionVisitor) ExitOC_UnaryAddOrSubtractExpression(ctx *parser.OC_UnaryAddOrSubtractExpressionContext) {
s.exitSubArithmeticExpression(ctx)
if operators := newTokenLiteralIterator(ctx); operators.HasTokens() {
s.assignExpression(&model.UnaryAddOrSubtractExpression{
Operator: operators.NextOperator(),
Right: s.ctx.Exit().(*ArithmeticExpressionVisitor).Expression,
})
}
}

func (s *ArithmeticExpressionVisitor) EnterOC_NonArithmeticOperatorExpression(ctx *parser.OC_NonArithmeticOperatorExpressionContext) {
Expand Down
3 changes: 3 additions & 0 deletions packages/go/cypher/model/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func Copy[T any](value T, extensions ...CopyExtension[T]) T {
case *PartialArithmeticExpression:
return any(typedValue.copy()).(T)

case *UnaryAddOrSubtractExpression:
return any(typedValue.copy()).(T)

case *PartialComparison:
return any(typedValue.copy()).(T)

Expand Down
16 changes: 16 additions & 0 deletions packages/go/cypher/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,22 @@ func (s *ArithmeticExpression) AddArithmeticExpressionPart(part *PartialArithmet
s.Partials = append(s.Partials, part)
}

type UnaryAddOrSubtractExpression struct {
Operator Operator
Right Expression
}

func NewUnaryAddOrSubtractExpression() *UnaryAddOrSubtractExpression {
return &UnaryAddOrSubtractExpression{}
}

func (s *UnaryAddOrSubtractExpression) copy() *UnaryAddOrSubtractExpression {
return &UnaryAddOrSubtractExpression{
Operator: s.Operator,
Right: Copy(s.Right),
}
}

type Match struct {
Optional bool
Pattern []*PatternPart
Expand Down
3 changes: 3 additions & 0 deletions packages/go/cypher/model/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ func cypherModelCollect(nextCursor *WalkCursor, expression Expression) bool {
case *PartialArithmeticExpression:
CollectExpression(nextCursor, typedExpr.Right)

case *UnaryAddOrSubtractExpression:
CollectExpression(nextCursor, typedExpr.Right)

case *Merge:
Collect(nextCursor, typedExpr.PatternPart)
CollectSlice(nextCursor, typedExpr.MergeActions)
Expand Down
18 changes: 17 additions & 1 deletion packages/go/cypher/test/cases/positive_tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,22 @@
"complexity": 1
}
},
{
"name": "Support unary operators in property match",
"type": "string_match",
"details": {
"query": "match (n:Product) where n.prop_1 = -1 return n",
"complexity": 2
}
},
{
"name": "Support complex arithmetic expressions with unary operators in property match",
"type": "string_match",
"details": {
"query": "match (n:Product) where n.prop_1 = -(1 + 2) * -3 return n",
"complexity": 2
}
},
{
"name": "Find Kerberoastable Users with most privileges",
"type": "string_match",
Expand Down Expand Up @@ -906,4 +922,4 @@
}
}
]
}
}
Loading