Skip to content

Commit

Permalink
refactoring for resultset C wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-hx committed May 31, 2024
1 parent b8b5845 commit 0c5b00a
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 1,637 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ set(SOURCE_FILES_CPP_WRAPPER
cpp/lib/ClientQueryContextCache.cpp
cpp/lib/ClientQueryContextCache.hpp
cpp/lib/result_set.cpp
cpp/lib/result_set_arrow.cpp
cpp/lib/result_set_json.cpp
cpp/lib/ResultSet.cpp
cpp/lib/ResultSet.hpp
cpp/lib/ResultSetArrow.cpp
Expand All @@ -246,8 +244,6 @@ set(SOURCE_FILES_CPP_WRAPPER
cpp/util/CurlDesc.cpp
cpp/util/CurlDescPool.cpp
lib/result_set.h
lib/result_set_arrow.h
lib/result_set_json.h
lib/query_context_cache.h
lib/curl_desc_pool.h
lib/authenticator.h)
Expand Down
24 changes: 14 additions & 10 deletions cpp/lib/ResultSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,13 @@
#include "cJSON.h"
#include "snowflake/basic_types.h"
#include "snowflake/client.h"
#include "result_set.h"

namespace Snowflake
{
namespace Client
{

/**
* Enumeration over valid query result formats.
*/
enum QueryResultFormat
{
ARROW,
JSON
};

/**
* The implementation of a base result set.
*
Expand Down Expand Up @@ -194,6 +186,13 @@ class ResultSet
*/
virtual SF_STATUS STDCALL isCellNull(size_t idx, sf_bool * out_data) = 0;

/**
* Gets the total number of rows in the current chunk being processed.
*
* @return the number of rows in the current chunk.
*/
virtual size_t getRowCountInChunk() = 0;

// Other member getters ========================================================================

SF_STATUS getError()
Expand All @@ -215,6 +214,11 @@ class ResultSet
}
}

QueryResultFormat_t getResultFormat()
{
return m_queryResultFormat;
}

protected:

// Protected members ===========================================================================
Expand Down Expand Up @@ -303,7 +307,7 @@ class ResultSet
/**
* The format of the result set.
*/
QueryResultFormat m_queryResultFormat;
QueryResultFormat_t m_queryResultFormat;

/**
* The tz string to use when dealing with time or timestamp values.
Expand Down
35 changes: 32 additions & 3 deletions cpp/lib/ResultSetArrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "../logger/SFLogger.hpp"
#include "ResultSetArrow.hpp"
#include "result_set_arrow.h"
#include "DataConversion.hpp"
#include "results.h"

Expand All @@ -24,7 +23,7 @@ namespace Client
ResultSetArrow::ResultSetArrow() :
Snowflake::Client::ResultSet()
{
m_queryResultFormat = QueryResultFormat::ARROW;
m_queryResultFormat = SF_ARROW_FORMAT;
}

ResultSetArrow::ResultSetArrow(
Expand All @@ -34,7 +33,7 @@ ResultSetArrow::ResultSetArrow(
) :
ResultSet(metadata, tzString)
{
m_queryResultFormat = QueryResultFormat::ARROW;
m_queryResultFormat = SF_ARROW_FORMAT;

this->appendChunk(initialChunk);

Expand All @@ -44,6 +43,36 @@ ResultSetArrow::ResultSetArrow(
m_currRowIdx = 0;
}

ResultSetArrow::ResultSetArrow(
cJSON * jsonRowset64,
SF_COLUMN_DESC * metadata,
std::string tzString
) :
ResultSet(metadata, tzString)
{
m_queryResultFormat = SF_ARROW_FORMAT;

arrow::BufferBuilder* bufferBuilder = NULL;
if (jsonRowset64)
{
const char* base64RowsetStr = snowflake_cJSON_GetStringValue(jsonRowset64);
if (base64RowsetStr && strlen(base64RowsetStr) > 0)
{
// Decode Base64-encoded Arrow-format rowset of the chunk and build a buffer builder from it.
std::string decodedRowsetStr = arrow::util::base64_decode(std::string(base64RowsetStr));
bufferBuilder = new arrow::BufferBuilder();
(void)bufferBuilder->Append((void*)decodedRowsetStr.c_str(), decodedRowsetStr.length());
}
}

this->appendChunk(bufferBuilder);

// Reset row indices so that they can be re-used by public API.
m_currChunkIdx = 0;
m_currChunkRowIdx = 0;
m_currRowIdx = 0;
}

ResultSetArrow::~ResultSetArrow()
{
; // Do nothing.
Expand Down
14 changes: 14 additions & 0 deletions cpp/lib/ResultSetArrow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ class ResultSetArrow : public Snowflake::Client::ResultSet
*/
ResultSetArrow(arrow::BufferBuilder * initialChunk, SF_COLUMN_DESC * metadata, std::string tzString);


/**
* Parameterized constructor.
*
* This constructor will initialize m_records with the (partial) results
* contained in the initial chunk. It will also initialize m_metadata with
* the metadata in "metadata".
*
* @param jsonRowset64 A pointer to the rowset64 data in json result set.
* @param metadata An array of metadata objects for each column.
* @param tzString The time zone.
*/
ResultSetArrow(cJSON* jsonRowset64, SF_COLUMN_DESC* metadata, std::string tzString);

/**
* Destructor.
*/
Expand Down
4 changes: 2 additions & 2 deletions cpp/lib/ResultSetJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Client
ResultSetJson::ResultSetJson() :
ResultSet()
{
m_queryResultFormat = QueryResultFormat::JSON;
m_queryResultFormat = SF_JSON_FORMAT;
}

ResultSetJson::ResultSetJson(
Expand All @@ -30,7 +30,7 @@ ResultSetJson::ResultSetJson(
) :
ResultSet(metadata, tzString)
{
m_queryResultFormat = QueryResultFormat::JSON;
m_queryResultFormat = SF_JSON_FORMAT;
m_chunk = nullptr;
appendChunk(rowset);
}
Expand Down
Loading

0 comments on commit 0c5b00a

Please sign in to comment.