hendry
hendry

Reputation: 10833

How to filter for empty values with the Golang filter builder?

Carrying on from How to filter on NULL? I am struggling to replicate the Go code to filter for null values of a particular column:

import (
        "github.com/apex/log"
        "github.com/aws/aws-sdk-go-v2/aws"
        "github.com/aws/aws-sdk-go-v2/aws/endpoints"
        "github.com/aws/aws-sdk-go-v2/aws/external"
        "github.com/aws/aws-sdk-go-v2/service/dynamodb"
        "github.com/aws/aws-sdk-go-v2/service/dynamodb/expression"
)

// aws --profile dd dynamodb scan --table-name plocal2 --filter-expression 'paymentmethod = :null' --expression-attribute-values '{":null" :{"NULL":true}}'

var table = "plocal2"

func main() {
        cfg, err := external.LoadDefaultAWSConfig(external.WithSharedConfigProfile("dd"))
        if err != nil {
                log.WithError(err).Fatal("setting up credentials")
                return
        }
        cfg.Region = endpoints.ApSoutheast1RegionID
        db := dynamodb.New(cfg)
        filt := expression.Name("paymentmethod").Equal(expression.Value(expression.Null))
        expr, err := expression.NewBuilder().
                WithFilter(filt).
                Build()
        if err != nil {
                log.WithError(err).Error("failed to build expression")
                return
        }
        scanReq := db.ScanRequest(&dynamodb.ScanInput{
                ExpressionAttributeValues: expr.Values(),
                FilterExpression:          expr.Filter(),
                TableName:                 aws.String(table),
        })
        result, err := scanReq.Send()
        if err != nil {
                log.WithError(err).Error("failed to query table")
                return
        }
        log.Infof("%d", len(result.Items))
}

The error is:

2019/04/30 16:59:54 error failed to query table     error=ValidationException: Invalid FilterExpression: An expression attribute name used in the document path
 is not defined; attribute name: #0
        status code: 400, request id: 8CFTOAGVB7M1IM3DC6BFPEVLUNVV4KQNSO5AEMVJF66Q9ASUAAJG

Any tips how to debug this or where I am going wrong using the expression builder?

Really wish there were examples, since filtering on NULL should be a pretty common use case to me if one is using MarshalMap since empty strings becomes NULLs!

Upvotes: 1

Views: 2449

Answers (2)

krzysbaranski
krzysbaranski

Reputation: 1

in aws-sdk-go (v1) https://github.com/aws/aws-sdk-go/blob/main/service/dynamodb/expression/condition.go#L71

expresion.Null

Example:

const endTsField = "end_ts"
filterNoEndTime := expression.Name(endTsField).AttributeNotExists()
filterNull := expression.Name(endTsField).NotEqual(expression.Value(expression.Null))
filterEndTime := expression.Name(endTsField).GreaterThanEqual(expression.Value(time.Now().UTC().UnixMilli()))
filter := filterNoEndTime.Or(filterEndTime).Or(filterNull)

Upvotes: 0

fedonev
fedonev

Reputation: 25709

#better-late-than-never, in hopes of helping others who, like me, struggled to discover the secret to filtering dynamoDB NULL values with the aws-sdk-go-v2 expression package:

filt := expression.Name("paymentmethod").
        Equal(expression.Value(&types.AttributeValueMemberNULL{Value: true})

Upvotes: 2

Related Questions