aws-sdk-net: [S3] Select is broken on large-ish files

Reproducible example; https://github.com/aws/aws-sdk-net/issues/1296#issuecomment-493769504

Expected Behavior

The count should be correct:-

Expected...
Key: [BCD, 20933]
Key: [DEF, 13528]
Key: [FGH, 12188]
Key: [HIJ, 16105]
Key: [JKL, 21117]
Key: [LMN, 13560]
Key: [NOP, 18547]

Current Behavior

The count is wrong for result set:-

Actual...
Key: [BCD, 20933]
Key: [DEF, 13527]
Key: [FGH, 12187]
Key: [HIJ, 16105]
Key: [JKL, 21117]
Key: [LMN, 13560]
Key: [NOP, 18545]

I also get malformed data returned, every line should be in the s._1, s._2 format according to the query:-

bad line: DE
bad key: F
bad line: 3
bad line: NO
bad key: P
bad line: 3
bad line: NO
bad key: P
bad line: 3
bad line: FG
bad key: H

Possible Solution

Haven’t investigated further, just can consistently reproduce broken behaviour

Steps to Reproduce (for bugs)

See above

Context

Your Environment

  • AWSSDK.Core version used: 3.3.101.9
  • Service assembly and version used: 3.3.101.16
  • Operating System and version: Windows 10 Build 1809
  • Visual Studio version: 15.9.11
  • Targeted .NET platform: 4.5.2

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (15 by maintainers)

Most upvoted comments

The problem is we have a usability issue with the S3 select API in the SDK that we need to address.

When you are reading the RecordsEvent objects from the Payload, S3 makes no guarantees that it contains only full csv lines. Sometimes a csv line will be split over multiple RecordEvents. So you need to make sure you are reading from the RecordsEvent stream till you have a full csv line before doing any processing on it. I updated your reader code below to keep reading through multiple RecordsEvent till a newline was read and then do your processing. Once I did that I was able to successfully do the 100 iterations in your test code.

The SDK should have done a better job of abstracting this but I think when we originally designed the API we didn’t take into account getting partial lines in a RecordsEvent. I’ll talk with the rest of the team to see how we can improve this feature in the SDK.

using (var stream = response.Payload)
{
    StringBuilder fullCsvRecord = new StringBuilder();
    foreach (var ev in stream)
    {
        if (ev is RecordsEvent records)
        {
            Console.WriteLine("Got record event");
            using (var reader = new StreamReader(records.Payload, Encoding.UTF8))
            {
                while (reader.EndOfStream == false)
                {
                    var c = (char)reader.Read();
                    if(c == '\n')
                    {
                        var line = fullCsvRecord.ToString();
                        fullCsvRecord.Clear();

                        if (line.Contains(","))
                        {
                            var tokens = line.Split(',');
                            var key = tokens[0];

                            if (actual.ContainsKey(key))
                            {
                                actual[key]++;
                            }
                            else
                            {
                                Console.WriteLine("bad key: " + key);
                            }
                        }
                        else
                        {
                            Console.WriteLine("bad line: " + line);
                        }
                    }
                    else
                    {
                        fullCsvRecord.Append(c);
                    }
                }
            }
        }
    }
}