Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.19.7
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.55.0
github.com/aws/aws-sdk-go-v2/service/sns v1.39.11
github.com/grindlemire/go-lucene v0.0.26
gopkg.in/yaml.v3 v3.0.1
gorm.io/gorm v1.31.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grindlemire/go-lucene v0.0.26 h1:81ttZkMvU3rFD0TfmjdIZT2U0Fd4TT7buDy+xq1x5EQ=
github.com/grindlemire/go-lucene v0.0.26/go.mod h1:INRJBdhkLjS4jc7XgkGPfzC5wuFg3BHDukXMTc+OTbc=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
Expand Down
2 changes: 1 addition & 1 deletion storage/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (s *DynamoDBAdapter) Search(dest any, sortKey string, query string, limit i
// Parse Lucene query
destType := reflect.TypeOf(dest).Elem().Elem()
model := reflect.New(destType).Elem().Interface()
parser, _ := lucene.NewParserFromType(model)
parser, _ := lucene.NewParser(model)
whereClause, params, _ := parser.ParseToDynamoDBPartiQL(query)

// Build query
Expand Down
137 changes: 137 additions & 0 deletions storage/search/lucene/dynamodb_driver.go
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
}
Loading