AVRO-4026: [c++] Add new methods to CustomAttributes to allow non-string custom attributes in schemas #3266
+148
−47
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds new methods to
CustomAttributes
to allow setting non-string values. These other methods work with JSON-encoded strings.Unlike #3064 and #3069, this change attempts to be backwards compatible. However, from reading more comments in pull requests, it looks like the "fix" I added (to escape the keys and values in custom attributes when printing to JSON) may actually be a compatibility issue since it seems that users were expected to have to escape string values if they contained any characters that would be escaped in JSON (including quotes). That seems like a really terrible API, and it also meant that the values would not round-trip correctly: reading a data file would not create custom attributes with these strings properly escaped, so later writing out data with the same schema would generate an invalid schema JSON document.
In any event, this uses strings as the values even though it would be ideal if we could pass some sort of structured data as the value type. The ideal types (
json::Entity
and its accompanyingjson::Object
andjson::Array
types) are defined injson/JsonDom.hh
. But that header files is not part of the Avro include files distribution, which means we cannot#include
it fromCustomAttributes.hh
, so it's a no-go. From a little history spelunking, I see that they indeed used to use a structured form which was simplified to strings in #1821, purely because these JSON header files aren't available to users in the Avro distribution.Alternatives that I considered for using JSON-encoded strings:
lang/c++/impl
and intolang/c++/include/avro
. But then we expand the public API of too much. This approach was already tried and rejected in AVRO-3601: C++ API header contains breaking include #1820.std::any
as the value type. This can be#include
d inCustomAttributes.hh
but eliminates type safety in the signature. The only concrete accepted value would likely bejson::Entity
-- though we could make it more sophisticated and also allow the various value types sans wrapper:std::string
,bool
,int64_t
,double
,json::Array
(akastd::vector<json::Entity>
), andjson::Object
(akastd::map<std::string, json::Entity>
). But this isn't really usable by external/user code, at least not for any composite values, since they aren't able to include the JSON headers and then produce valid values ofjson::Entity
.json::Entity
to some other structured type that is defined inCustomAttributes.hh
. This could possibly be a concretestd::variant
that allows the various options (and could usestd::monostate
to represent JSON null values). This introduces non-trivial conversion code. From a performance perspective, it could likely be better than converting to/from strings, but it's a non-trivial amount of new-new code to maintain, which didn't feel right.What is the purpose of the change
Fixes exception from C++ library when compiling schemas with non-string custom attributes.
Verifying this change
This change added tests and can be verified as follows:
Documentation