Skip to content

Commit

Permalink
[AVRO-4044][C++] Add time-nanos logical type
Browse files Browse the repository at this point in the history
  • Loading branch information
glywk committed Sep 3, 2024
1 parent 8979c37 commit 9af3ff3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lang/c++/impl/Compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ static LogicalType makeLogicalType(const Entity &e, const Object &m) {
t = LogicalType::TIME_MILLIS;
else if (typeField == "time-micros")
t = LogicalType::TIME_MICROS;
else if (typeField == "time-nanos")
t = LogicalType::TIME_NANOS;
else if (typeField == "timestamp-millis")
t = LogicalType::TIMESTAMP_MILLIS;
else if (typeField == "timestamp-micros")
Expand Down
3 changes: 3 additions & 0 deletions lang/c++/impl/LogicalType.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ void LogicalType::printJson(std::ostream &os) const {
case TIME_MICROS:
os << R"("logicalType": "time-micros")";
break;
case TIME_NANOS:
os << R"("logicalType": "time-nanos")";
break;
case TIMESTAMP_MILLIS:
os << R"("logicalType": "timestamp-millis")";
break;
Expand Down
6 changes: 6 additions & 0 deletions lang/c++/impl/Node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ void Node::setLogicalType(LogicalType logicalType) {
"LONG type");
}
break;
case LogicalType::TIME_NANOS:
if (type_ != AVRO_LONG) {
throw Exception("TIME-NANOS logical type can only annotate "
"LONG type");
}
break;
case LogicalType::TIMESTAMP_MILLIS:
if (type_ != AVRO_LONG) {
throw Exception("TIMESTAMP-MILLIS logical type can only annotate "
Expand Down
1 change: 1 addition & 0 deletions lang/c++/include/avro/LogicalType.hh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public:
DATE,
TIME_MILLIS,
TIME_MICROS,
TIME_NANOS,
TIMESTAMP_MILLIS,
TIMESTAMP_MICROS,
TIMESTAMP_NANOS,
Expand Down
14 changes: 14 additions & 0 deletions lang/c++/test/SchemaTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ const char *roundTripSchemas[] = {
R"({"type":"int","logicalType":"date"})",
R"({"type":"int","logicalType":"time-millis"})",
R"({"type":"long","logicalType":"time-micros"})",
R"({"type":"long","logicalType":"time-nanos"})",
R"({"type":"long","logicalType":"timestamp-millis"})",
R"({"type":"long","logicalType":"timestamp-micros"})",
R"({"type":"long","logicalType":"timestamp-nanos"})",
Expand Down Expand Up @@ -244,6 +245,7 @@ const char *malformedLogicalTypes[] = {
R"({"type":"string","logicalType":"date"})",
R"({"type":"string","logicalType":"time-millis"})",
R"({"type":"string","logicalType":"time-micros"})",
R"({"type":"string","logicalType":"time-nanos"})",
R"({"type":"string","logicalType":"timestamp-millis"})",
R"({"type":"string","logicalType":"timestamp-micros"})",
R"({"type":"string","logicalType":"timestamp-nanos"})",
Expand Down Expand Up @@ -358,6 +360,9 @@ static void testLogicalTypes() {
const char *timeMicrosType = "{\n\
\"type\": \"long\", \"logicalType\": \"time-micros\"\n\
}";
const char *timeNanosType = "{\n\
\"type\": \"long\", \"logicalType\": \"time-nanos\"\n\
}";
const char *timestampMillisType = "{\n\
\"type\": \"long\", \"logicalType\": \"timestamp-millis\"\n\
}";
Expand Down Expand Up @@ -438,6 +443,15 @@ static void testLogicalTypes() {
GenericDatum datum(schema);
BOOST_CHECK(datum.logicalType().type() == LogicalType::TIME_MICROS);
}
{
BOOST_TEST_CHECKPOINT(timeNanosType);
ValidSchema schema = compileJsonSchemaFromString(timeNanosType);
BOOST_CHECK(schema.root()->type() == AVRO_LONG);
LogicalType logicalType = schema.root()->logicalType();
BOOST_CHECK(logicalType.type() == LogicalType::TIME_NANOS);
GenericDatum datum(schema);
BOOST_CHECK(datum.logicalType().type() == LogicalType::TIME_NANOS);
}
{
BOOST_TEST_CHECKPOINT(timestampMillisType);
ValidSchema schema = compileJsonSchemaFromString(timestampMillisType);
Expand Down

0 comments on commit 9af3ff3

Please sign in to comment.