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

Unable to Scan with Multiple Conditions #2987

Closed
jbtdevgit opened this issue Jun 29, 2023 · 5 comments
Closed

Unable to Scan with Multiple Conditions #2987

jbtdevgit opened this issue Jun 29, 2023 · 5 comments
Assignees
Labels
bug This issue is a bug. dynamodb response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@jbtdevgit
Copy link

Describe the bug

Hi,

I was unable to scan objects with multiple conditions using ScanAsync below is the code snippet:

if(DynamoDBContext != null)
{
     List<ScanCondition> scanConditions = new List<ScanCondition>
     {
           new ScanCondition("SortKey", ScanOperator.Equal, "sk1"),
           new ScanCondition("TestBool", ScanOperator.Equal, new DynamoDBBool(true))
      };
     return await DynamoDBContext.ScanAsync<T>(scanConditions).GetRemainingAsync();
}

And below I have these in my table:
image

Below is the result of the debug:
image

Removing one of the conditions will give me results:
image

Expected Behavior

Return based on the scan conditions

Current Behavior

Did not return

Reproduction Steps

Add multiple conditions on scan conditions list

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

AWSSDK.AmazonDynamoDBv2 3.7.103.21

Targeted .NET Platform

.NET 7

Operating System and version

Windows 10

@jbtdevgit jbtdevgit added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 29, 2023
@ashishdhingra ashishdhingra added needs-reproduction This issue needs reproduction. dynamodb and removed needs-triage This issue or PR still needs to be triaged. labels Jun 29, 2023
@ashishdhingra ashishdhingra self-assigned this Jun 29, 2023
@ashishdhingra
Copy link
Contributor

ashishdhingra commented Jun 29, 2023

Hi @jbtamaresgit,

Good morning.

Thanks for reporting the issue.

Unfortunately, I'm unable to reproduce the issue. For Object Persistence model, you would need to use convertor to convert between bool and DynamoDBBool types, and also need to set DynamoDBContextConfig.Conversion for DynamoDBContext to DynamoDBEntryConversion.V2. I suspect there might be an issue with your convertor where it might only be handling bool data, not if the user passes DynamoDBBool in ScanCondition (you might try using true value directly in your code to see if it works).

For the example table schema shared by you, the below code works fine (2 results are returned; where I created 3 items with one of them having TestBool value as false):

using System;
using System.Collections.Generic;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DataModel;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Runtime;

using (AmazonDynamoDBClient client = new AmazonDynamoDBClient())
{
    DynamoDBContext context = new DynamoDBContext(client, new DynamoDBContextConfig() { Conversion = DynamoDBEntryConversion.V2 });

    if (context != null)
    {
        List<ScanCondition> scanConditions = new List<ScanCondition>
        {
           new ScanCondition("SortKey", ScanOperator.Equal, "sk1"),
           new ScanCondition("TestBool", ScanOperator.Equal, new DynamoDBBool(true))
        };
        var scanResponse = await context.ScanAsync<TestTable>(scanConditions).GetRemainingAsync();
    }
}

[DynamoDBTable("TestTable")]
public class TestTable
{
    [DynamoDBHashKey]
    public string Id { get; set; }

    [DynamoDBRangeKey]
    public string SortKey { get; set; }

    [DynamoDBProperty("TestBool", typeof(DynamoDBNativeBooleanConverter))]
    public bool TestBool { get; set; }
}

public class DynamoDBNativeBooleanConverter : IPropertyConverter
{
    public DynamoDBEntry ToEntry(object value)
    {
        DynamoDBEntry entry = DynamoDBNull.Null;

        if (value != null)
        {
            if (value is DynamoDBBool)
            {
                entry = (DynamoDBBool) value;
            }
            else
            {
                entry = new DynamoDBBool(bool.TryParse(value?.ToString(), out var val) && val);
            }
        }

        return entry;
    }

    public object FromEntry(DynamoDBEntry entry)
    {
        return entry.AsDynamoDBBool().Value;
    }
}

Thanks,
Ashish

@ashishdhingra ashishdhingra added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-reproduction This issue needs reproduction. labels Jun 29, 2023
@jbtdevgit
Copy link
Author

jbtdevgit commented Jun 30, 2023

Hi @ashishdhingra ,
I do not have the conversion set DynamoDBContextConfig.Conversion for DynamoDbContext but I do have the property converter

public class DynamoDBNativeBooleanConverter : IPropertyConverter
{
        public DynamoDBEntry ToEntry(object value) => new DynamoDBBool(bool.TryParse(value?.ToString(), out var val) && val);
        public object FromEntry(DynamoDBEntry entry) => entry.AsDynamoDBBool().Value;
}

And that should not be a problem since I can comment out the SortKey Condition and leave the TestBool and it will return those with TestBool as True but never did when adding both of the ScanCondition.

I suspect there might be an issue with your convertor where it might only be handling bool data, not if the user passes DynamoDBBool in ScanCondition (you might try using true value directly in your code to see if it works).

I also did try this, but it did not work as well.

Does the conversion for the DynamoDbContext needed to be added in order for multiple ScanConditions to work?
And by the way, what package version are you using? Are you you using the same package as I am, or is it a newer version?


Update:
Updated the package to v3.7.105
Added DynamoDBEntryConversion.V2
And it is still not working.

Code snippet for DynamoDbContext

DynamoDBContext = new DynamoDBContext(DynamoDBClient, new DynamoDBContextConfig()
{
    Conversion = DynamoDBEntryConversion.V2
});

Fetching of items is still the same

if(DynamoDBContext != null)
{
     List<ScanCondition> scanConditions = new List<ScanCondition>
     {
           new ScanCondition("SortKey", ScanOperator.Equal, "sk1"),
           new ScanCondition("TestBool", ScanOperator.Equal, new DynamoDBBool(true))
      };
     return await DynamoDBContext.ScanAsync<T>(scanConditions).GetRemainingAsync();
}

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 1, 2023
@ashishdhingra
Copy link
Contributor

Hi @ashishdhingra ,
I do not have the conversion set DynamoDBContextConfig.Conversion for DynamoDbContext but I do have the property converter

public class DynamoDBNativeBooleanConverter : IPropertyConverter
{
public DynamoDBEntry ToEntry(object value) => new DynamoDBBool(bool.TryParse(value?.ToString(), out var val) && val);
public object FromEntry(DynamoDBEntry entry) => entry.AsDynamoDBBool().Value;
}
And that should not be a problem since I can comment out the SortKey Condition and leave the TestBool and it will return those with TestBool as True but never did when adding both of the ScanCondition.

@jbtamaresgit Good morning. I was also using the same converter as you, but since you are using ScanCondition("TestBool", ScanOperator.Equal, new DynamoDBBool(true)), the property converter's ToEntry will fail since it is trying to use bool.TryParse() on DynamoDBBool object. Please modify your converter to the logic shared in #2987 (comment). Also make sure to decorate TestBool property with [DynamoDBProperty("TestBool", typeof(DynamoDBNativeBooleanConverter))] attribute. The code shared in #2987 (comment) works perfectly fine at my end.

Thanks,
Ashish

@ashishdhingra ashishdhingra added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 3, 2023
@jbtdevgit
Copy link
Author

Hi @ashishdhingra ,
I reworked our tables so I am using QueryAsync to query objects with a single ScanCondition and it works great, so I guess I am not going to dabble around the ScanAsync. Thanks for your help.

@github-actions
Copy link

github-actions bot commented Jul 4, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. dynamodb response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

2 participants