Skip to content

Commit

Permalink
SNOW-1657238: Fix incorrect row count for rows loaded (#1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-lf authored Oct 28, 2024
1 parent 8199563 commit fe8e848
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
90 changes: 90 additions & 0 deletions Snowflake.Data.Tests/IntegrationTests/SFDbCommandIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Threading;
using System.Threading.Tasks;
using Snowflake.Data.Core;
using System.Linq;
using System.IO;

namespace Snowflake.Data.Tests.IntegrationTests
{
Expand Down Expand Up @@ -1674,5 +1676,93 @@ public async Task TestCommandWithCommentEmbeddedAsync()
Assert.AreEqual("--", reader.GetString(0));
}
}

[Test]
public void TestExecuteNonQueryReturnsCorrectRowCountForUploadWithMultipleFiles()
{
const int NumberOfFiles = 5;
const int NumberOfRows = 3;
const int ExpectedRowCount = NumberOfFiles * NumberOfRows;

using (SnowflakeDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = ConnectionString + "poolingEnabled=false";
conn.Open();

using (SnowflakeDbCommand cmd = (SnowflakeDbCommand)conn.CreateCommand())
{
var tempFolder = $"{Path.GetTempPath()}Temp_{Guid.NewGuid()}";

try
{
// Arrange
Directory.CreateDirectory(tempFolder);
var data = string.Concat(Enumerable.Repeat(string.Join(",", "TestData") + "\n", NumberOfRows));
for (int i = 0; i < NumberOfFiles; i++)
{
File.WriteAllText(Path.Combine(tempFolder, $"{TestContext.CurrentContext.Test.Name}_{i}.csv"), data);
}
CreateOrReplaceTable(conn, TableName, new[] { "COL1 STRING" });
cmd.CommandText = $"PUT file://{Path.Combine(tempFolder, "*.csv")} @%{TableName} AUTO_COMPRESS=FALSE";
var reader = cmd.ExecuteReader();

// Act
cmd.CommandText = $"COPY INTO {TableName} FROM @%{TableName} PATTERN='.*.csv' FILE_FORMAT=(TYPE=CSV)";
int actualRowCount = cmd.ExecuteNonQuery();

// Assert
Assert.AreEqual(ExpectedRowCount, actualRowCount);
}
finally
{
Directory.Delete(tempFolder, true);
}
}
}
}

[Test]
public async Task TestExecuteNonQueryAsyncReturnsCorrectRowCountForUploadWithMultipleFiles()
{
const int NumberOfFiles = 5;
const int NumberOfRows = 3;
const int ExpectedRowCount = NumberOfFiles * NumberOfRows;

using (SnowflakeDbConnection conn = new SnowflakeDbConnection())
{
conn.ConnectionString = ConnectionString + "poolingEnabled=false";
conn.Open();

using (SnowflakeDbCommand cmd = (SnowflakeDbCommand)conn.CreateCommand())
{
var tempFolder = $"{Path.GetTempPath()}Temp_{Guid.NewGuid()}";

try
{
// Arrange
Directory.CreateDirectory(tempFolder);
var data = string.Concat(Enumerable.Repeat(string.Join(",", "TestData") + "\n", NumberOfRows));
for (int i = 0; i < NumberOfFiles; i++)
{
File.WriteAllText(Path.Combine(tempFolder, $"{TestContext.CurrentContext.Test.Name}_{i}.csv"), data);
}
CreateOrReplaceTable(conn, TableName, new[] { "COL1 STRING" });
cmd.CommandText = $"PUT file://{Path.Combine(tempFolder, "*.csv")} @%{TableName} AUTO_COMPRESS=FALSE";
var reader = cmd.ExecuteReader();

// Act
cmd.CommandText = $"COPY INTO {TableName} FROM @%{TableName} PATTERN='.*.csv' FILE_FORMAT=(TYPE=CSV)";
int actualRowCount = await cmd.ExecuteNonQueryAsync().ConfigureAwait(false);

// Assert
Assert.AreEqual(ExpectedRowCount, actualRowCount);
}
finally
{
Directory.Delete(tempFolder, true);
}
}
}
}
}
}
8 changes: 5 additions & 3 deletions Snowflake.Data/Core/ResultSetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ internal static int CalculateUpdateCount(this SFBaseResultSet resultSet)
var index = resultSet.sfResultSetMetaData.GetColumnIndexByName("rows_loaded");
if (index >= 0)
{
resultSet.Next();
updateCount = resultSet.GetInt64(index);
resultSet.Rewind();
while (resultSet.Next())
{
updateCount += resultSet.GetInt64(index);
}
while (resultSet.Rewind()) {}
}
break;
case SFStatementType.COPY_UNLOAD:
Expand Down

0 comments on commit fe8e848

Please sign in to comment.