Skip to content

Commit

Permalink
Add duration. (#3338)
Browse files Browse the repository at this point in the history
* Add duration.

* Add datetime/duration computation support.

* Remove debug code.

* Add operator override.

* Support syntax.

* Fix the compare behavior.

* Store property data in disk.

Co-authored-by: Yee <[email protected]>
Co-authored-by: Sophie <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2021
1 parent e6ddeb2 commit af74bab
Show file tree
Hide file tree
Showing 32 changed files with 1,754 additions and 28 deletions.
11 changes: 11 additions & 0 deletions src/codec/RowReaderV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ Value RowReaderV2::getValueByIndex(const int64_t index) const noexcept {
dt.microsec = microsec;
return dt;
}
case PropertyType::DURATION: {
Duration d;
memcpy(reinterpret_cast<void*>(&d.seconds), &data_[offset], sizeof(int64_t));
memcpy(reinterpret_cast<void*>(&d.microseconds),
&data_[offset + sizeof(int64_t)],
sizeof(int32_t));
memcpy(reinterpret_cast<void*>(&d.months),
&data_[offset + sizeof(int64_t) + sizeof(int32_t)],
sizeof(int32_t));
return d;
}
case PropertyType::GEOGRAPHY: {
int32_t strOffset;
int32_t strLen;
Expand Down
31 changes: 31 additions & 0 deletions src/codec/RowWriterV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ RowWriterV2::RowWriterV2(RowReader& reader) : RowWriterV2(reader.getSchema()) {
case Value::Type::GEOGRAPHY:
set(i, v.moveGeography());
break;
case Value::Type::DURATION:
set(i, v.moveDuration());
break;
default:
LOG(FATAL) << "Invalid data: " << v << ", type: " << v.typeName();
}
Expand Down Expand Up @@ -209,6 +212,8 @@ WriteResult RowWriterV2::setValue(ssize_t index, const Value& val) noexcept {
return write(index, val.getDateTime());
case Value::Type::GEOGRAPHY:
return write(index, val.getGeography());
case Value::Type::DURATION:
return write(index, val.getDuration());
default:
return WriteResult::TYPE_MISMATCH;
}
Expand Down Expand Up @@ -762,6 +767,29 @@ WriteResult RowWriterV2::write(ssize_t index, const DateTime& v) noexcept {
return WriteResult::SUCCEEDED;
}

WriteResult RowWriterV2::write(ssize_t index, const Duration& v) noexcept {
auto field = schema_->field(index);
auto offset = headerLen_ + numNullBytes_ + field->offset();
switch (field->type()) {
case PropertyType::DURATION:
memcpy(&buf_[offset], reinterpret_cast<const void*>(&v.seconds), sizeof(int64_t));
memcpy(&buf_[offset + sizeof(int64_t)],
reinterpret_cast<const void*>(&v.microseconds),
sizeof(int32_t));
memcpy(&buf_[offset + sizeof(int64_t) + sizeof(int32_t)],
reinterpret_cast<const void*>(&v.months),
sizeof(int32_t));
break;
default:
return WriteResult::TYPE_MISMATCH;
}
if (field->nullable()) {
clearNullBit(field->nullFlagPos());
}
isSet_[index] = true;
return WriteResult::SUCCEEDED;
}

WriteResult RowWriterV2::write(ssize_t index, const Geography& v) noexcept {
auto field = schema_->field(index);
auto geoShape = field->geoShape();
Expand Down Expand Up @@ -815,6 +843,9 @@ WriteResult RowWriterV2::checkUnsetFields() noexcept {
case Value::Type::GEOGRAPHY:
r = write(i, defVal.getGeography());
break;
case Value::Type::DURATION:
r = write(i, defVal.getDuration());
break;
default:
LOG(FATAL) << "Unsupported default value type: " << defVal.typeName()
<< ", default value: " << defVal
Expand Down
1 change: 1 addition & 0 deletions src/codec/RowWriterV2.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class RowWriterV2 {
WriteResult write(ssize_t index, const Date& v) noexcept;
WriteResult write(ssize_t index, const Time& v) noexcept;
WriteResult write(ssize_t index, const DateTime& v) noexcept;
WriteResult write(ssize_t index, const Duration& v) noexcept;

WriteResult write(ssize_t index, const Geography& v) noexcept;
};
Expand Down
11 changes: 11 additions & 0 deletions src/codec/test/RowWriterV2Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const Geography geogPolygon = Polygon(
Coordinate(-102.9, 37.6),
Coordinate(-96.8, 37.5),
Coordinate(-100.1, 41.4)}});
const Duration du = Duration(1, 2, 3);

TEST(RowWriterV2, NoDefaultValue) {
SchemaWriter schema(12 /*Schema version*/);
Expand All @@ -68,6 +69,7 @@ TEST(RowWriterV2, NoDefaultValue) {
schema.appendCol(
"Col18", PropertyType::GEOGRAPHY, 0, false, nullptr, meta::cpp2::GeoShape::POLYGON);
schema.appendCol("Col19", PropertyType::GEOGRAPHY, 0, true, nullptr, meta::cpp2::GeoShape::ANY);
schema.appendCol("Col20", PropertyType::DURATION);

ASSERT_EQ(Value::Type::STRING, sVal.type());
ASSERT_EQ(Value::Type::INT, iVal.type());
Expand All @@ -92,6 +94,7 @@ TEST(RowWriterV2, NoDefaultValue) {
EXPECT_EQ(WriteResult::SUCCEEDED, writer1.set(16, geogLineString));
EXPECT_EQ(WriteResult::SUCCEEDED, writer1.set(17, geogPolygon));
// Purposely skip the col19
EXPECT_EQ(WriteResult::SUCCEEDED, writer1.set(19, du));
ASSERT_EQ(WriteResult::SUCCEEDED, writer1.finish());

RowWriterV2 writer2(&schema);
Expand All @@ -114,6 +117,7 @@ TEST(RowWriterV2, NoDefaultValue) {
EXPECT_EQ(WriteResult::SUCCEEDED, writer2.set("Col17", geogLineString));
EXPECT_EQ(WriteResult::SUCCEEDED, writer2.set("Col18", geogPolygon));
// Purposely skip the col19
EXPECT_EQ(WriteResult::SUCCEEDED, writer2.set("Col20", du));
ASSERT_EQ(WriteResult::SUCCEEDED, writer2.finish());

std::string encoded1 = std::move(writer1).moveEncodedStr();
Expand Down Expand Up @@ -253,6 +257,13 @@ TEST(RowWriterV2, NoDefaultValue) {
v2 = reader2->getValueByIndex(18);
EXPECT_EQ(Value::Type::NULLVALUE, v1.type());
EXPECT_EQ(v1, v2);

// Col20
v1 = reader1->getValueByName("Col20");
v2 = reader2->getValueByIndex(19);
EXPECT_EQ(Value::Type::DURATION, v1.type());
EXPECT_EQ(du, v1.getDuration());
EXPECT_EQ(v1, v2);
}

TEST(RowWriterV2, WithDefaultValue) {
Expand Down
3 changes: 3 additions & 0 deletions src/codec/test/SchemaWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ SchemaWriter& SchemaWriter::appendCol(folly::StringPiece name,
case PropertyType::GEOGRAPHY:
size = 2 * sizeof(int32_t); // as same as STRING
break;
case PropertyType::DURATION:
size = sizeof(int64_t) + sizeof(int32_t) + sizeof(int32_t);
break;
default:
LOG(FATAL) << "Unknown column type";
}
Expand Down
1 change: 1 addition & 0 deletions src/common/datatypes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ nebula_add_library(
List.cpp
Set.cpp
Geography.cpp
Duration.cpp
)

nebula_add_subdirectory(test)
2 changes: 2 additions & 0 deletions src/common/datatypes/CommonCpp2Ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct Point;
struct LineString;
struct Polygon;
struct Geography;
struct Duration;
} // namespace nebula

namespace apache::thrift {
Expand All @@ -52,6 +53,7 @@ SPECIALIZE_CPP2OPS(nebula::Point);
SPECIALIZE_CPP2OPS(nebula::LineString);
SPECIALIZE_CPP2OPS(nebula::Polygon);
SPECIALIZE_CPP2OPS(nebula::Geography);
SPECIALIZE_CPP2OPS(nebula::Duration);

} // namespace apache::thrift

Expand Down
Loading

0 comments on commit af74bab

Please sign in to comment.