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

feat: Add schema construction DSL #569

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
134 changes: 134 additions & 0 deletions src/nanoarrow/nanoarrow_testing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2916,6 +2916,140 @@ class TestingJSONComparison {

/// @}

namespace dsl {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe factory?

/// \defgroup nanoarrow_testing-schema Schema factory DSL
///
/// @{

/// \brief An alias to express a sequence of key value pairs.
///
/// Each pair is a string formatted like "key=value".
using metadata = std::vector<std::string>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would std::vector<std::pair<std::string, std::string>> work? I'm not sure the world is clamouring to create metadata keys that contain the = symbol but it seems like it would be nice to avoid string parsing if we can.


/// \brief A wrapper around UniqueSchema with easy constructors
class schema;

/// \brief Alias schema to readably tag a vector of schemas
using children = std::vector<schema>;

/// \brief A wrapper around schema to readably tag a dictionary
struct dictionary : UniqueSchema {
template <typename... Args>
explicit dictionary(Args... args);
};

class schema {
public:
friend struct dictionary;

schema(schema const& other) : unique_schema{std::move(other.unique_schema)} {}
schema(schema&& other) = default;

schema(UniqueSchema unique_schema) : unique_schema{std::move(unique_schema)} {}

template <typename... Args>
schema(Args... args) {
ArrowSchemaInit(get());
get()->private_data = new Private;
get()->release = [](struct ArrowSchema* schema) {
delete static_cast<Private*>(schema->private_data);
};
set(std::move(args)...);
}

private:
mutable UniqueSchema unique_schema;

struct Private {
std::string format;
std::string name;
std::string metadata;

std::vector<schema> schemas;
std::vector<struct ArrowSchema*> children;
UniqueSchema dictionary;
};

Private* get_private() { return static_cast<Private*>(get()->private_data); }
ArrowSchema* get() { return unique_schema.get(); }

void set() {
// set string pointers in the actual ArrowSchema
// if format was not set, then set it to struct now
get()->format = get_private()->format.empty() ? "+s" : get_private()->format.c_str();
get()->name = get_private()->name.c_str();
get()->metadata = get_private()->metadata.c_str();
}

template <typename... Args>
void set(std::string format_or_name, Args... args) {
(get_private()->format.empty() // assign to format if it's empty
? get_private()->format
: get_private()->name) = std::move(format_or_name);
}

template <typename... Args>
void set(int64_t flags, Args... args) {
get()->flags = flags;
set(std::move(args)...);
}

template <typename... Args>
void set(dictionary d, Args... args) {
get_private()->dictionary = std::move(d);
set(std::move(args)...);
}

template <typename... Args>
void set(children c, Args... args) {
get()->n_children = c.size();
get_private()->children.resize(c.size());
get()->children = get_private()->children.data();
get_private()->schemas = std::move(c);

int i = 0;
for (auto& child : get_private()->children) {
child = get_private()->schemas[i++].get();
}
set(std::move(args)...);
}

template <typename... Args>
void set(metadata m, Args... args) {
std::string metadata_buf;
auto append_int32 = [&](size_t i) {
auto i32 = static_cast<int32_t>(i);
char chars[sizeof(int32_t)];
memcpy(&chars, &i32, sizeof(int32_t));
metadata_buf.append(chars, sizeof(int32_t));
};
append_int32(m.size());
for (const std::string& kv : m) {
size_t key_size = kv.find_first_of('=');
if (key_size == std::string::npos) {
continue;
}
append_int32(key_size);
metadata_buf.append(kv.data(), key_size);

size_t value_size = kv.size() - key_size - 1;
append_int32(value_size);
metadata_buf.append(kv.data() + key_size + 1, value_size);
}
get_private()->metadata = std::move(metadata_buf);
get()->metadata = get_private()->metadata.c_str();

set(std::move(args)...);
}
};

template <typename... Args>
dictionary::dictionary(Args... args)
: UniqueSchema{std::move(schema{std::move(args)...}.unique_schema)} {}

/// @}
} // namespace dsl

} // namespace testing
} // namespace nanoarrow

Expand Down
16 changes: 16 additions & 0 deletions src/nanoarrow/testing/testing_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1908,3 +1908,19 @@ TEST(NanoarrowTestingTest, NanoarrowTestingTestArrayStreamComparison) {

)");
}

TEST(SchemaDsl, Basic) {
using namespace nanoarrow::testing::dsl;

schema{children{
{"i", "int32 field name",
metadata{
"some_key=some_value",
}},
{"i", dictionary{"u"}, "dictionary field name",
metadata{
"some_key=some_value",
},
ARROW_FLAG_NULLABLE},
}};
Comment on lines +1915 to +1925
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In tests I think I would still prefer to see the C calls (but perhaps this is not where you were headed with this)

}
Loading