Skip to content

Commit

Permalink
round the float/double when insert it to integer column
Browse files Browse the repository at this point in the history
  • Loading branch information
jievince committed Dec 27, 2021
1 parent 19f2bb7 commit b9b8098
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
18 changes: 10 additions & 8 deletions src/codec/RowWriterV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "codec/RowWriterV2.h"

#include <cmath>

#include "common/time/TimeUtils.h"
#include "common/time/WallClock.h"
#include "common/utils/DefaultValueContext.h"
Expand Down Expand Up @@ -286,15 +288,15 @@ WriteResult RowWriterV2::write(ssize_t index, float v) noexcept {
if (v > std::numeric_limits<int8_t>::max() || v < std::numeric_limits<int8_t>::min()) {
return WriteResult::OUT_OF_RANGE;
}
int8_t iv = v;
int8_t iv = std::round(v);
buf_[offset] = iv;
break;
}
case PropertyType::INT16: {
if (v > std::numeric_limits<int16_t>::max() || v < std::numeric_limits<int16_t>::min()) {
return WriteResult::OUT_OF_RANGE;
}
int16_t iv = v;
int16_t iv = std::round(v);
memcpy(&buf_[offset], reinterpret_cast<void*>(&iv), sizeof(int16_t));
break;
}
Expand All @@ -303,7 +305,7 @@ WriteResult RowWriterV2::write(ssize_t index, float v) noexcept {
v < static_cast<float>(std::numeric_limits<int32_t>::min())) {
return WriteResult::OUT_OF_RANGE;
}
int32_t iv = v;
int32_t iv = std::round(v);
memcpy(&buf_[offset], reinterpret_cast<void*>(&iv), sizeof(int32_t));
break;
}
Expand All @@ -312,7 +314,7 @@ WriteResult RowWriterV2::write(ssize_t index, float v) noexcept {
v < static_cast<float>(std::numeric_limits<int64_t>::min())) {
return WriteResult::OUT_OF_RANGE;
}
int64_t iv = v;
int64_t iv = std::round(v);
memcpy(&buf_[offset], reinterpret_cast<void*>(&iv), sizeof(int64_t));
break;
}
Expand Down Expand Up @@ -343,23 +345,23 @@ WriteResult RowWriterV2::write(ssize_t index, double v) noexcept {
if (v > std::numeric_limits<int8_t>::max() || v < std::numeric_limits<int8_t>::min()) {
return WriteResult::OUT_OF_RANGE;
}
int8_t iv = v;
int8_t iv = std::round(v);
buf_[offset] = iv;
break;
}
case PropertyType::INT16: {
if (v > std::numeric_limits<int16_t>::max() || v < std::numeric_limits<int16_t>::min()) {
return WriteResult::OUT_OF_RANGE;
}
int16_t iv = v;
int16_t iv = std::round(v);
memcpy(&buf_[offset], reinterpret_cast<void*>(&iv), sizeof(int16_t));
break;
}
case PropertyType::INT32: {
if (v > std::numeric_limits<int32_t>::max() || v < std::numeric_limits<int32_t>::min()) {
return WriteResult::OUT_OF_RANGE;
}
int32_t iv = v;
int32_t iv = std::round(v);
memcpy(&buf_[offset], reinterpret_cast<void*>(&iv), sizeof(int32_t));
break;
}
Expand All @@ -368,7 +370,7 @@ WriteResult RowWriterV2::write(ssize_t index, double v) noexcept {
v < static_cast<double>(std::numeric_limits<int64_t>::min())) {
return WriteResult::OUT_OF_RANGE;
}
int64_t iv = v;
int64_t iv = std::round(v);
memcpy(&buf_[offset], reinterpret_cast<void*>(&iv), sizeof(int64_t));
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/graph/executor/admin/SpaceExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ folly::Future<Status> DropSpaceExecutor::execute() {
}

void DropSpaceExecutor::unRegisterSpaceLevelMetrics(const std::string &spaceName) {
if (FLAGS_enable_space_level_metrics) {
if (FLAGS_enable_space_level_metrics && spaceName != "") {
stats::StatsManager::removeCounterWithLabels(kNumQueries, {{"space", spaceName}});
stats::StatsManager::removeCounterWithLabels(kNumSlowQueries, {{"space", spaceName}});
stats::StatsManager::removeCounterWithLabels(kNumQueryErrors, {{"space", spaceName}});
Expand Down
6 changes: 4 additions & 2 deletions src/graph/service/GraphService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ folly::Future<ExecutionResponse> GraphService::future_executeWithParameter(
return ctx->finish();
}
stats::StatsManager::addValue(kNumQueries);
stats::StatsManager::addValue(
stats::StatsManager::counterWithLabels(kNumQueries, {{"space", sessionPtr->space().name}}));
if (FLAGS_enable_space_level_metrics && sessionPtr->space().name != "") {
stats::StatsManager::addValue(stats::StatsManager::counterWithLabels(
kNumQueries, {{"space", sessionPtr->space().name}}));
}
ctx->setSession(std::move(sessionPtr));
ctx->setParameterMap(parameterMap);
queryEngine_->execute(std::move(ctx));
Expand Down
10 changes: 6 additions & 4 deletions src/graph/service/QueryInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,10 @@ void QueryInstance::onError(Status status) {
auto latency = rctx->duration().elapsedInUSec();
rctx->resp().latencyInUs = latency;
stats::StatsManager::addValue(kNumQueryErrors);
stats::StatsManager::addValue(
stats::StatsManager::counterWithLabels(kNumQueryErrors, {{"space", spaceName}}));
if (FLAGS_enable_space_level_metrics && spaceName != "") {
stats::StatsManager::addValue(
stats::StatsManager::counterWithLabels(kNumQueryErrors, {{"space", spaceName}}));
}
addSlowQueryStats(latency, spaceName);
rctx->session()->deleteQuery(qctx_.get());
rctx->finish();
Expand All @@ -173,14 +175,14 @@ void QueryInstance::onError(Status status) {

void QueryInstance::addSlowQueryStats(uint64_t latency, const std::string &spaceName) const {
stats::StatsManager::addValue(kQueryLatencyUs, latency);
if (FLAGS_enable_space_level_metrics) {
if (FLAGS_enable_space_level_metrics && spaceName != "") {
stats::StatsManager::addValue(
stats::StatsManager::histoWithLabels(kQueryLatencyUs, {{"space", spaceName}}), latency);
}
if (latency > static_cast<uint64_t>(FLAGS_slow_query_threshold_us)) {
stats::StatsManager::addValue(kNumSlowQueries);
stats::StatsManager::addValue(kSlowQueryLatencyUs, latency);
if (FLAGS_enable_space_level_metrics) {
if (FLAGS_enable_space_level_metrics && spaceName != "") {
stats::StatsManager::addValue(
stats::StatsManager::counterWithLabels(kNumSlowQueries, {{"space", spaceName}}));
stats::StatsManager::addValue(
Expand Down
45 changes: 45 additions & 0 deletions tests/tck/features/bugfix/RoundFloat.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright (c) 2021 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License.
Feature: Datetime insert mismatched type

# issue https://github.com/vesoft-inc/nebula/issues/3473
Scenario: Insert float/double into a integer column
Given an empty graph
And create a space with following options:
| partition_num | 9 |
| replica_factor | 1 |
| vid_type | FIXED_STRING(30) |
| charset | utf8 |
| collate | utf8_bin |
When executing query:
"""
create tag test(a int32);
"""
Then the execution should be successful
When try to execute query:
"""
INSERT VERTEX test(a) VALUES '101':(3.2);
"""
Then the execution should be successful
When executing query:
"""
INSERT VERTEX test(a) VALUES '102':(3.8);
"""
Then the execution should be successful
When executing query:
"""
INSERT VERTEX test(a) VALUES '103':(-3.2);
"""
Then the execution should be successful
When executing query:
"""
INSERT VERTEX test(a) VALUES '104':(-3.8);
"""
Then the execution should be successful
When executing query:
"""
INSERT VERTEX test(a) VALUES '104':(2147483647.1));
"""
Then an ExecutionError should be raised at runtime:Storage Error: Out of range value.
Then drop the used space

0 comments on commit b9b8098

Please sign in to comment.