Skip to content

Commit

Permalink
refactor(c/driver): Use non-objects framework components in Postgres …
Browse files Browse the repository at this point in the history
…driver (#2166)

This PR formalizes the names of documents the helpers in
`framework/objects.h` and `frameworks/utility.h`. It also uses them in
the PostgreSQL driver and removes duplicate functionality where possible
(GetObjects in the Postgres driver is a bit of a bigger project for
another PR).
  • Loading branch information
paleolimbot authored Sep 23, 2024
1 parent 776726f commit a2a39a5
Show file tree
Hide file tree
Showing 24 changed files with 597 additions and 720 deletions.
66 changes: 0 additions & 66 deletions c/driver/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,72 +235,6 @@ struct AdbcErrorDetail CommonErrorGetDetail(const struct AdbcError* error, int i
};
}

struct SingleBatchArrayStream {
struct ArrowSchema schema;
struct ArrowArray batch;
};
static const char* SingleBatchArrayStreamGetLastError(struct ArrowArrayStream* stream) {
(void)stream;
return NULL;
}
static int SingleBatchArrayStreamGetNext(struct ArrowArrayStream* stream,
struct ArrowArray* batch) {
if (!stream || !stream->private_data) return EINVAL;
struct SingleBatchArrayStream* impl =
(struct SingleBatchArrayStream*)stream->private_data;

memcpy(batch, &impl->batch, sizeof(*batch));
memset(&impl->batch, 0, sizeof(*batch));
return 0;
}
static int SingleBatchArrayStreamGetSchema(struct ArrowArrayStream* stream,
struct ArrowSchema* schema) {
if (!stream || !stream->private_data) return EINVAL;
struct SingleBatchArrayStream* impl =
(struct SingleBatchArrayStream*)stream->private_data;

return ArrowSchemaDeepCopy(&impl->schema, schema);
}
static void SingleBatchArrayStreamRelease(struct ArrowArrayStream* stream) {
if (!stream || !stream->private_data) return;
struct SingleBatchArrayStream* impl =
(struct SingleBatchArrayStream*)stream->private_data;
impl->schema.release(&impl->schema);
if (impl->batch.release) impl->batch.release(&impl->batch);
free(impl);

memset(stream, 0, sizeof(*stream));
}

AdbcStatusCode BatchToArrayStream(struct ArrowArray* values, struct ArrowSchema* schema,
struct ArrowArrayStream* stream,
struct AdbcError* error) {
if (!values->release) {
SetError(error, "ArrowArray is not initialized");
return ADBC_STATUS_INTERNAL;
} else if (!schema->release) {
SetError(error, "ArrowSchema is not initialized");
return ADBC_STATUS_INTERNAL;
} else if (stream->release) {
SetError(error, "ArrowArrayStream is already initialized");
return ADBC_STATUS_INTERNAL;
}

struct SingleBatchArrayStream* impl =
(struct SingleBatchArrayStream*)malloc(sizeof(*impl));
memcpy(&impl->schema, schema, sizeof(*schema));
memcpy(&impl->batch, values, sizeof(*values));
memset(schema, 0, sizeof(*schema));
memset(values, 0, sizeof(*values));
stream->private_data = impl;
stream->get_last_error = SingleBatchArrayStreamGetLastError;
stream->get_next = SingleBatchArrayStreamGetNext;
stream->get_schema = SingleBatchArrayStreamGetSchema;
stream->release = SingleBatchArrayStreamRelease;

return ADBC_STATUS_OK;
}

int StringBuilderInit(struct StringBuilder* builder, size_t initial_size) {
builder->buffer = (char*)malloc(initial_size);
if (builder->buffer == NULL) return errno;
Expand Down
5 changes: 0 additions & 5 deletions c/driver/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ void StringBuilderReset(struct StringBuilder* builder);

#undef ADBC_CHECK_PRINTF_ATTRIBUTE

/// Wrap a single batch as a stream.
AdbcStatusCode BatchToArrayStream(struct ArrowArray* values, struct ArrowSchema* schema,
struct ArrowArrayStream* stream,
struct AdbcError* error);

/// Check an NanoArrow status code.
#define CHECK_NA(CODE, EXPR, ERROR) \
do { \
Expand Down
2 changes: 1 addition & 1 deletion c/driver/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

include(FetchContent)

add_library(adbc_driver_framework STATIC catalog.cc objects.cc)
add_library(adbc_driver_framework STATIC objects.cc utility.cc)
adbc_configure_target(adbc_driver_framework)
set_target_properties(adbc_driver_framework PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_include_directories(adbc_driver_framework
Expand Down
328 changes: 0 additions & 328 deletions c/driver/framework/catalog.cc

This file was deleted.

162 changes: 0 additions & 162 deletions c/driver/framework/catalog.h

This file was deleted.

6 changes: 3 additions & 3 deletions c/driver/framework/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include <arrow-adbc/adbc.h>

#include "driver/framework/base_driver.h"
#include "driver/framework/catalog.h"
#include "driver/framework/objects.h"
#include "driver/framework/utility.h"

namespace adbc::driver {
/// \brief The CRTP base implementation of an AdbcConnection.
Expand Down Expand Up @@ -86,7 +86,7 @@ class Connection : public ObjectBase {

std::vector<uint32_t> codes(info_codes, info_codes + info_codes_length);
RAISE_RESULT(error, auto infos, impl().InfoImpl(codes));
RAISE_STATUS(error, AdbcGetInfo(infos, out));
RAISE_STATUS(error, MakeGetInfoStream(infos, out));
return ADBC_STATUS_OK;
}

Expand Down Expand Up @@ -204,7 +204,7 @@ class Connection : public ObjectBase {
}

RAISE_RESULT(error, std::vector<std::string> table_types, impl().GetTableTypesImpl());
RAISE_STATUS(error, AdbcGetTableTypes(table_types, out));
RAISE_STATUS(error, MakeTableTypesStream(table_types, out));
return ADBC_STATUS_OK;
}

Expand Down
2 changes: 1 addition & 1 deletion c/driver/framework/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
adbc_framework_lib = library(
'adbc_driver_framework',
sources: [
'catalog.cc',
'objects.cc',
'utility.cc',
],
include_directories: [include_dir, c_dir],
link_with: [adbc_common_lib],
Expand Down
Loading

0 comments on commit a2a39a5

Please sign in to comment.