Commit c16b0ae1 authored by Laura Schlimmer's avatar Laura Schlimmer
Browse files

fix svalue numeric conversion

parent ce0c23dd
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -1252,3 +1252,21 @@ TEST_CASE(SQLTest, TestSimpleGroupOverTimeWindow, [] () {
  EXPECT_EQ(result->getRow(15)[1], "NULL");
  EXPECT_EQ(result->getRow(28)[1], "28170");
});

TEST_CASE(SQLTest, TestNumericConversion, [] () {
  {
    SValue val("42");
    EXPECT_EQ(val.getType(), SValue::T_STRING);
    EXPECT(val.testType<fnordmetric::IntegerType>());
    EXPECT(val.tryNumericConversion());
    EXPECT_EQ(val.getInteger(), 42);
  }

  {
    SValue val("1415912541648");
    EXPECT_EQ(val.getType(), SValue::T_STRING);
    EXPECT(val.testType<fnordmetric::IntegerType>());
    EXPECT(val.tryNumericConversion());
    EXPECT_EQ(val.getInteger(), 1415912541648lu);
  }
});
+32 −10
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
#include <fnordmetric/sql/parser/token.h>
#include <fnordmetric/sql/svalue.h>
#include <fnordmetric/util/format.h>
#include <fnordmetric/util/inspect.h>

namespace fnordmetric {
namespace query {
@@ -44,22 +45,26 @@ SValue::SValue(const fnordmetric::StringType& string_value) {
      data_.u.t_string.len);
}

SValue::SValue(fnordmetric::IntegerType integer_value) : SValue() {
SValue::SValue(
    char const* string_value) :
    SValue(std::string(string_value)) {}

SValue::SValue(fnordmetric::IntegerType integer_value) {
  data_.type = T_INTEGER;
  data_.u.t_integer = integer_value;
}

SValue::SValue(fnordmetric::FloatType float_value) : SValue() {
SValue::SValue(fnordmetric::FloatType float_value) {
  data_.type = T_FLOAT;
  data_.u.t_float = float_value;
}

SValue::SValue(fnordmetric::BoolType bool_value) : SValue() {
SValue::SValue(fnordmetric::BoolType bool_value) {
  data_.type = T_BOOL;
  data_.u.t_bool = bool_value;
}

SValue::SValue(fnordmetric::TimeType time_value) : SValue() {
SValue::SValue(fnordmetric::TimeType time_value) {
  data_.type = T_TIMESTAMP;
  data_.u.t_timestamp = static_cast<uint64_t>(time_value);
}
@@ -147,7 +152,7 @@ fnordmetric::IntegerType SValue::getInteger() const {

    case T_STRING:
      try {
        return std::stoi(getString());
        return std::stol(getString());
      } catch (std::exception e) {
        /* fallthrough */
      }
@@ -373,14 +378,19 @@ template <> bool SValue::testType<fnordmetric::IntegerType>() const {
  }

  auto str = toString();
  const char* c = str.c_str();
  const char* cur = str.c_str();
  const char* end = cur + str.size();

  if (*c == '-') {
    ++c;
  if (*cur == '-') {
    ++cur;
  }

  for (; *c != 0; ++c) {
    if (*c < '0' || *c > '9') {
  if (cur == end) {
    return false;
  }

  for (; cur < end; ++cur) {
    if (*cur < '0' || *cur > '9') {
      return false;
    }
  }
@@ -462,5 +472,17 @@ bool SValue::tryTimeConversion() {
}


}
}

namespace fnord {
namespace util {

template <>
std::string inspect<fnordmetric::query::SValue::kSValueType>(
    const fnordmetric::query::SValue::kSValueType& type) {
  return fnordmetric::query::SValue::getTypeName(type);
}

}
}
+1 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ public:

  explicit SValue();
  explicit SValue(const fnordmetric::StringType& string_value);
  explicit SValue(char const* string_value); // FIXPAUL HACK!!!
  explicit SValue(fnordmetric::IntegerType integer_value);
  explicit SValue(fnordmetric::FloatType float_value);
  explicit SValue(fnordmetric::BoolType bool_value);
+6 −0
Original line number Diff line number Diff line
@@ -38,6 +38,12 @@ std::string inspect<unsigned long long>(
  return std::to_string(value);
}

template <>
std::string inspect<long long>(
    const long long& value) {
  return std::to_string(value);
}

template <>
std::string inspect<float>(const float& value) {
  return std::to_string(value);