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

SNOW-945857: put with GCP to skip existing file #595

Merged
1 commit merged into from
Oct 26, 2023
Merged
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
31 changes: 26 additions & 5 deletions cpp/SnowflakeGCSClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ RemoteStorageRequestOutcome SnowflakeGCSClient::upload(FileMetadata *fileMetadat
if (!m_gcsAccessToken.empty())
{
std::string filePathFull = m_stageInfo->location + fileMetadata->destFileName;

//check if file exists if overwrite is not specified.
if (!fileMetadata->overWrite)
sfc-gh-igarish marked this conversation as resolved.
Show resolved Hide resolved
{
RemoteStorageRequestOutcome outcome = GetRemoteFileMetadata(&filePathFull, fileMetadata);
if (RemoteStorageRequestOutcome::SUCCESS == outcome)
{
CXX_LOG_DEBUG("File already exists skipping the file upload %s",
fileMetadata->srcFileToUpload.c_str());
return RemoteStorageRequestOutcome::SKIP_UPLOAD_FILE;
}
}
buildGcsRequest(filePathFull, url, reqHeaders);
}
else
Expand Down Expand Up @@ -134,12 +146,21 @@ RemoteStorageRequestOutcome SnowflakeGCSClient::GetRemoteFileMetadata(
url = fileMetadata->presignedUrl;
}

if (!m_statement->http_get(url,
reqHeaders,
NULL,
headerString,
true))
try
{
if (!m_statement->http_get(url,
reqHeaders,
NULL,
headerString,
true))
{
return RemoteStorageRequestOutcome::FAILED;
This conversation was marked as resolved.
Show resolved Hide resolved
}
}
catch (...)
{
// It's expected to fail when file doesn't exist, while http_get()
// could throw excpetion on that. Catch that and return FAILED
return RemoteStorageRequestOutcome::FAILED;
}

Expand Down
49 changes: 46 additions & 3 deletions tests/test_unit_put_get_gcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class MockedGCSStatement : public Snowflake::Client::IStatementPutGet
{
public:
MockedGCSStatement(bool useGcsToken)
: m_useGcsToken(useGcsToken), Snowflake::Client::IStatementPutGet()
: m_useGcsToken(useGcsToken),
m_firstTime(true),
m_isPut(false),
Snowflake::Client::IStatementPutGet()
{
m_stageInfo.stageType = Snowflake::Client::StageType::GCS;
if (m_useGcsToken)
Expand Down Expand Up @@ -63,9 +66,18 @@ class MockedGCSStatement : public Snowflake::Client::IStatementPutGet
}
else
{
m_isPut = true;
putGetParseResponse->command = CommandType::UPLOAD;
putGetParseResponse->srcLocations = m_srcLocationsPut;
putGetParseResponse->threshold = DEFAULT_UPLOAD_DATA_SIZE_THRESHOLD;
if (sql->find("OVERWRITE=true") != std::string::npos)
{
putGetParseResponse->overwrite = true;
}
else
{
putGetParseResponse->overwrite = false;
}
}
if (!m_useGcsToken)
{
Expand Down Expand Up @@ -106,8 +118,17 @@ class MockedGCSStatement : public Snowflake::Client::IStatementPutGet
std::string& responseHeaders,
bool headerOnly) override
{
if (headerOnly)
if (headerOnly) // try getting metadata
{
if (m_isPut)
{
if ((strcasecmp(url.c_str(), m_expectedUrl.c_str()) == 0) && m_firstTime)
{
// first time retry false to mock the case of file doesn't exist
m_firstTime = false;
return false;
}
}
responseHeaders = "HTTP/1.1 200 OK\n"
"x-goog-meta-encryptiondata: {\"WrappedContentKey\":{\"KeyId\":\"symmKey1\",\"EncryptedKey\":\"MyZBZNLcndKTeR+xC8Msle5IcSYKsx/nYNn93OONSqs=\",\"Algorithm\":\"AES_CBC_256\"},\"ContentEncryptionIV\":\"30fmhKrf1aKyWidrv06NNA==\"}\n"
"Content-Type: application/octet-stream\n"
Expand Down Expand Up @@ -143,12 +164,16 @@ class MockedGCSStatement : public Snowflake::Client::IStatementPutGet
bool m_useGcsToken;

std::string m_expectedUrl;

bool m_firstTime;

bool m_isPut;
};

// test helper for put
void put_gcs_test_core(bool useGcsToken)
{
std::string cmd = std::string("put file://small_file.csv.gz @odbctestStage AUTO_COMPRESS=false OVERWRITE=true");
std::string cmd = std::string("put file://small_file.csv.gz @odbctestStage AUTO_COMPRESS=false");

MockedGCSStatement mockedStatementPut(useGcsToken);

Expand All @@ -162,6 +187,24 @@ void put_gcs_test_core(bool useGcsToken)
result->getColumnAsString(6, put_status);
assert_string_equal("UPLOADED", put_status.c_str());
}

if (useGcsToken)
{
result = agent.execute(&cmd);
while (result->next())
{
result->getColumnAsString(6, put_status);
assert_string_equal("SKIPPED", put_status.c_str());
}
}

cmd += " OVERWRITE=true";
result = agent.execute(&cmd);
while (result->next())
{
result->getColumnAsString(6, put_status);
assert_string_equal("UPLOADED", put_status.c_str());
}
}

// test helper for get
Expand Down
Loading