Skip to content

Commit

Permalink
Merge branch 'main' into fix-like-expression-panic-on-parse
Browse files Browse the repository at this point in the history
  • Loading branch information
duglin authored May 14, 2024
2 parents d22deae + 0988325 commit ff47a1c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

- name: Go Lint on ./v2
if: steps.golangci_configuration.outputs.files_exists == 'true'
uses: golangci/golangci-lint-action@v5
uses: golangci/golangci-lint-action@v6
with:
version: v1.54
working-directory: v2
Expand Down
96 changes: 44 additions & 52 deletions sql/v2/expression/like_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
package expression

import (
"regexp"
"strings"

cesql "github.com/cloudevents/sdk-go/sql/v2"
"github.com/cloudevents/sdk-go/sql/v2/utils"
cloudevents "github.com/cloudevents/sdk-go/v2"
)

type likeExpression struct {
baseUnaryExpression
pattern *regexp.Regexp
pattern string
}

func (l likeExpression) Evaluate(event cloudevents.Event) (interface{}, error) {
Expand All @@ -30,70 +27,65 @@ func (l likeExpression) Evaluate(event cloudevents.Event) (interface{}, error) {
return nil, err
}

return l.pattern.MatchString(val.(string)), nil
return matchString(val.(string), l.pattern), nil

}

func NewLikeExpression(child cesql.Expression, pattern string) (cesql.Expression, error) {
// Converting to regex is not the most performant impl, but it works
p, err := convertLikePatternToRegex(pattern)
if err != nil {
return nil, err
}

return likeExpression{
baseUnaryExpression: baseUnaryExpression{
child: child,
},
pattern: p,
pattern: pattern,
}, nil
}

func convertLikePatternToRegex(pattern string) (*regexp.Regexp, error) {
var chunks []string
chunks = append(chunks, "^")
func matchString(text, pattern string) bool {
textLen := len(text)
patternLen := len(pattern)
textIdx := 0
patternIdx := 0
lastWildcardIdx := -1
lastMatchIdx := 0

var chunk strings.Builder
if patternLen == 0 {
return patternLen == textLen
}

for i := 0; i < len(pattern); i++ {
if pattern[i] == '\\' && i < len(pattern)-1 {
if pattern[i+1] == '%' {
// \% case
chunk.WriteRune('%')
chunks = append(chunks, "\\Q"+chunk.String()+"\\E")
chunk.Reset()
i++
continue
} else if pattern[i+1] == '_' {
// \_ case
chunk.WriteRune('_')
chunks = append(chunks, "\\Q"+chunk.String()+"\\E")
chunk.Reset()
i++
continue
} else {
// if there is an actual literal \ character, we need to include that in the string
chunk.WriteRune('\\')
}
} else if pattern[i] == '_' {
// replace with .
chunks = append(chunks, "\\Q"+chunk.String()+"\\E")
chunk.Reset()
chunks = append(chunks, ".")
} else if pattern[i] == '%' {
// replace with .*
chunks = append(chunks, "\\Q"+chunk.String()+"\\E")
chunk.Reset()
chunks = append(chunks, ".*")
for textIdx < textLen {
if patternIdx < patternLen-1 && pattern[patternIdx] == '\\' &&
((pattern[patternIdx+1] == '_' || pattern[patternIdx+1] == '%') &&
pattern[patternIdx+1] == text[textIdx]) {
// handle escaped characters -> pattern needs to increment two places here
patternIdx += 2
textIdx += 1
} else if patternIdx < patternLen && (pattern[patternIdx] == '_' || pattern[patternIdx] == text[textIdx]) {
// handle non escaped characters
textIdx += 1
patternIdx += 1
} else if patternIdx < patternLen && pattern[patternIdx] == '%' {
// handle wildcard characters
lastWildcardIdx = patternIdx
lastMatchIdx = textIdx
patternIdx += 1
} else if lastWildcardIdx != -1 {
// greedy match didn't work, try again from the last known match
patternIdx = lastWildcardIdx + 1
lastMatchIdx += 1
textIdx = lastMatchIdx
} else {
chunk.WriteByte(pattern[i])
return false
}
}

if chunk.Len() != 0 {
chunks = append(chunks, "\\Q"+chunk.String()+"\\E")
}
// consume remaining pattern characters as long as they are wildcards
for patternIdx < patternLen {
if pattern[patternIdx] != '%' {
return false
}

chunks = append(chunks, "$")
patternIdx += 1
}

return regexp.Compile(strings.Join(chunks, ""))
return true
}
62 changes: 60 additions & 2 deletions sql/v2/test/tck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package test

import (
"fmt"
"io"
"os"
"path"
Expand Down Expand Up @@ -70,7 +71,7 @@ type TckTestCase struct {
EventOverrides map[string]interface{} `json:"eventOverrides"`
}

func (tc TckTestCase) InputEvent(t *testing.T) cloudevents.Event {
func (tc TckTestCase) InputEvent(tb testing.TB) cloudevents.Event {
var inputEvent cloudevents.Event
if tc.Event != nil {
inputEvent = *tc.Event
Expand All @@ -82,7 +83,7 @@ func (tc TckTestCase) InputEvent(t *testing.T) cloudevents.Event {
inputEvent.SetSpecVersion(event.CloudEventsVersionV1)

for k, v := range tc.EventOverrides {
require.NoError(t, spec.V1.SetAttribute(inputEvent.Context, k, v))
require.NoError(tb, spec.V1.SetAttribute(inputEvent.Context, k, v))
}

return inputEvent
Expand Down Expand Up @@ -159,3 +160,60 @@ func TestTCK(t *testing.T) {
})
}
}

func BenchmarkTCK(b *testing.B) {
tckFiles := make([]TckFile, 0, len(TCKFileNames))

_, basePath, _, _ := runtime.Caller(0)
basePath, _ = path.Split(basePath)

for _, testFile := range TCKFileNames {
testFilePath := path.Join(basePath, "tck", testFile+".yaml")

b.Logf("Loading file %s", testFilePath)

file, err := os.Open(testFilePath)
require.NoError(b, err)

fileBytes, err := io.ReadAll(file)
require.NoError(b, err)

tckFileModel := TckFile{}
require.NoError(b, yaml.Unmarshal(fileBytes, &tckFileModel))

tckFiles = append(tckFiles, tckFileModel)
}

for i, file := range tckFiles {
i := i
b.Run(file.Name, func(b *testing.B) {
for j, testCase := range tckFiles[i].Tests {
j := j
testCase := testCase
b.Run(fmt.Sprintf("%v parse", testCase.Name), func(b *testing.B) {
testCase := tckFiles[i].Tests[j]
for k := 0; k < b.N; k++ {
_, _ = parser.Parse(testCase.Expression)
}
})

if testCase.Error == ParseError {
return
}

b.Run(fmt.Sprintf("%v evaluate", testCase.Name), func(b *testing.B) {
testCase := tckFiles[i].Tests[j]

expr, _ := parser.Parse(testCase.Expression)

inputEvent := testCase.InputEvent(b)

for k := 0; k < b.N; k++ {
_, _ = expr.Evaluate(inputEvent)
}

})
}
})
}
}

0 comments on commit ff47a1c

Please sign in to comment.