-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Lucene query parser with sql and dynamodb support #124
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5c006ff
feat: implement Lucene query parser with PostgreSQL and DynamoDB support
Lutherwaves 99c87e7
refactor: simplify parser
Lutherwaves a28e426
refactor: simplify Lucene parser API and architecture
Lutherwaves d5bc359
refactor: make Lucene parser SQL generation generic for PostgreSQL, M…
Lutherwaves 7c2f0af
feat: add SearchQuery schema with Lucene search examples to OpenAPI d…
Lutherwaves 405c008
chore: update grindlemire/go-lucene deps
Lutherwaves 6d32abe
chore: add unit tests for parser
Lutherwaves 8149826
fix: stricter validation for duplicates, escapes to prevent injection
Lutherwaves 730e3c5
fix: linting errors
Lutherwaves 3f9d731
fix(parser): fix OpenAPI validation, improve error handling with erro…
Lutherwaves 14424e9
fix(sqlstorage): fix cursor pagination with json tag lookup and place…
Lutherwaves a8c7575
fix(lucene): handle null keyword in grouped OR field expressions
Lutherwaves fc197cc
fix(lucene): renderGroupedFieldExpr preserves outer field name in gro…
Lutherwaves aad39d1
fix: propagate parser errors in DynamoDB Search, fix godoc and linting
Lutherwaves File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| package lucene | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
| "github.com/grindlemire/go-lucene/pkg/driver" | ||
| "github.com/grindlemire/go-lucene/pkg/lucene/expr" | ||
| ) | ||
|
|
||
| // DynamoDBPartiQLDriver converts Lucene queries to DynamoDB PartiQL. | ||
| type DynamoDBPartiQLDriver struct { | ||
| driver.Base | ||
| fields map[string]FieldInfo | ||
| } | ||
|
|
||
| func NewDynamoDBDriver(fields []FieldInfo) (*DynamoDBPartiQLDriver, error) { | ||
| fieldMap, err := buildFieldMap(fields) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| fns := map[expr.Operator]driver.RenderFN{ | ||
| expr.Literal: driver.Shared[expr.Literal], | ||
| expr.And: driver.Shared[expr.And], | ||
| expr.Or: driver.Shared[expr.Or], | ||
| expr.Not: driver.Shared[expr.Not], | ||
| expr.Equals: driver.Shared[expr.Equals], | ||
| expr.Range: driver.Shared[expr.Range], | ||
| expr.Must: driver.Shared[expr.Must], | ||
| expr.MustNot: driver.Shared[expr.MustNot], | ||
| expr.Wild: driver.Shared[expr.Wild], | ||
| expr.Regexp: driver.Shared[expr.Regexp], | ||
| expr.Like: dynamoDBLike, // Custom LIKE for DynamoDB functions | ||
| expr.Greater: driver.Shared[expr.Greater], | ||
| expr.GreaterEq: driver.Shared[expr.GreaterEq], | ||
| expr.Less: driver.Shared[expr.Less], | ||
| expr.LessEq: driver.Shared[expr.LessEq], | ||
| expr.In: driver.Shared[expr.In], | ||
| expr.List: driver.Shared[expr.List], | ||
| } | ||
|
|
||
| return &DynamoDBPartiQLDriver{ | ||
| Base: driver.Base{ | ||
| RenderFNs: fns, | ||
| }, | ||
| fields: fieldMap, | ||
| }, nil | ||
| } | ||
|
|
||
| // RenderPartiQL renders the expression to DynamoDB PartiQL with AttributeValue parameters. | ||
| func (d *DynamoDBPartiQLDriver) RenderPartiQL(e *expr.Expression) (string, []types.AttributeValue, error) { | ||
| // Use base rendering with ? placeholders | ||
| str, params, err := d.RenderParam(e) | ||
| if err != nil { | ||
| return "", nil, err | ||
| } | ||
|
|
||
| // Convert params to DynamoDB AttributeValues | ||
| attrValues := make([]types.AttributeValue, len(params)) | ||
| for i, param := range params { | ||
| attrValues[i] = &types.AttributeValueMemberS{Value: fmt.Sprintf("%v", param)} | ||
| } | ||
|
|
||
| return str, attrValues, nil | ||
| } | ||
|
|
||
| // escapePartiQLString escapes a string value for safe use in PartiQL string literals. | ||
| // Escapes single quotes by doubling them (PartiQL standard). | ||
| func escapePartiQLString(s string) string { | ||
| return strings.ReplaceAll(s, "'", "''") | ||
| } | ||
|
|
||
| var ( | ||
| // partiQLIdentifierPattern matches valid PartiQL identifiers (alphanumeric and underscore only) | ||
| partiQLIdentifierPattern = regexp.MustCompile(`^[a-zA-Z0-9_]+$`) | ||
| ) | ||
|
|
||
| // escapePartiQLIdentifier escapes a field name for safe use in PartiQL. | ||
| // Validates that the identifier contains only safe characters (alphanumeric, underscore). | ||
| // Returns error if identifier contains potentially dangerous characters. | ||
| func escapePartiQLIdentifier(identifier string) (string, error) { | ||
| if !partiQLIdentifierPattern.MatchString(identifier) { | ||
| return "", fmt.Errorf("invalid identifier: contains unsafe characters (only alphanumeric and underscore allowed)") | ||
| } | ||
| return identifier, nil | ||
| } | ||
|
|
||
| // unquotePartiQLString safely removes surrounding quotes from a PartiQL string literal. | ||
| // Handles already-escaped quotes correctly. | ||
| func unquotePartiQLString(s string) string { | ||
| if len(s) >= 2 && s[0] == '\'' && s[len(s)-1] == '\'' { | ||
| return s[1 : len(s)-1] | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // dynamoDBLike implements LIKE using DynamoDB's begins_with and contains functions. | ||
| func dynamoDBLike(left, right string) (string, error) { | ||
| // Validate and escape field name (left) | ||
| safeLeft, err := escapePartiQLIdentifier(left) | ||
| if err != nil { | ||
| return "", fmt.Errorf("invalid field name: %w", err) | ||
| } | ||
|
|
||
| // Extract the raw value from the right side (remove quotes if present) | ||
| rawValue := unquotePartiQLString(right) | ||
|
|
||
| // Analyze pattern for wildcards | ||
| hasPrefix := strings.HasPrefix(rawValue, "%") | ||
| hasSuffix := strings.HasSuffix(rawValue, "%") | ||
|
|
||
| if hasPrefix && hasSuffix { | ||
| // %value% -> contains(field, value) | ||
| value := strings.Trim(rawValue, "%") | ||
| escapedValue := escapePartiQLString(value) | ||
| return fmt.Sprintf("contains(%s, '%s')", safeLeft, escapedValue), nil | ||
| } | ||
| if !hasPrefix && hasSuffix { | ||
| // value% -> begins_with(field, value) | ||
| value := strings.TrimSuffix(rawValue, "%") | ||
| escapedValue := escapePartiQLString(value) | ||
| return fmt.Sprintf("begins_with(%s, '%s')", safeLeft, escapedValue), nil | ||
| } | ||
| if hasPrefix && !hasSuffix { | ||
| // %value -> contains(field, value) (DynamoDB doesn't have ends_with) | ||
| value := strings.TrimPrefix(rawValue, "%") | ||
| escapedValue := escapePartiQLString(value) | ||
| return fmt.Sprintf("contains(%s, '%s')", safeLeft, escapedValue), nil | ||
| } | ||
|
|
||
| // Exact match - escape the value and wrap in quotes | ||
| escapedValue := escapePartiQLString(rawValue) | ||
| return fmt.Sprintf("%s = '%s'", safeLeft, escapedValue), nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.