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

implement timestamp <=> timestamp boolean operations

parent 7b325179
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -30,8 +30,10 @@ void eqExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {

  switch(lhs->testTypeWithNumericConversion()) {
    case SValue::T_INTEGER:
    case SValue::T_TIMESTAMP:
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getInteger() == rhs->getInteger());
          return;
        case SValue::T_FLOAT:
@@ -45,6 +47,7 @@ void eqExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_FLOAT:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getFloat() == rhs->getFloat());
          return;
        default:
@@ -167,8 +170,10 @@ void ltExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {

  switch(lhs->testTypeWithNumericConversion()) {
    case SValue::T_INTEGER:
    case SValue::T_TIMESTAMP:
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getInteger() < rhs->getInteger());
          return;
        case SValue::T_FLOAT:
@@ -182,6 +187,7 @@ void ltExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_FLOAT:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getFloat() < rhs->getFloat());
          return;
        default:
@@ -215,8 +221,10 @@ void lteExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {

  switch(lhs->testTypeWithNumericConversion()) {
    case SValue::T_INTEGER:
    case SValue::T_TIMESTAMP:
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getInteger() <= rhs->getInteger());
          return;
        case SValue::T_FLOAT:
@@ -230,6 +238,7 @@ void lteExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_FLOAT:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getFloat() <= rhs->getFloat());
          return;
        default:
@@ -263,8 +272,10 @@ void gtExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {

  switch(lhs->testTypeWithNumericConversion()) {
    case SValue::T_INTEGER:
    case SValue::T_TIMESTAMP:
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getInteger() > rhs->getInteger());
          return;
        case SValue::T_FLOAT:
@@ -278,6 +289,7 @@ void gtExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_FLOAT:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getFloat() > rhs->getFloat());
          return;
        default:
@@ -311,8 +323,10 @@ void gteExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {

  switch(lhs->testTypeWithNumericConversion()) {
    case SValue::T_INTEGER:
    case SValue::T_TIMESTAMP:
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getInteger() >= rhs->getInteger());
          return;
        case SValue::T_FLOAT:
@@ -326,6 +340,7 @@ void gteExpr(void* scratchpad, int argc, SValue* argv, SValue* out) {
      switch(rhs->testTypeWithNumericConversion()) {
        case SValue::T_INTEGER:
        case SValue::T_FLOAT:
        case SValue::T_TIMESTAMP:
          *out = SValue(lhs->getFloat() >= rhs->getFloat());
          return;
        default:
+4 −0
Original line number Diff line number Diff line
@@ -108,6 +108,10 @@ ASTNode* ASTNode::deepCopy() const {
  }

  for (const auto child : children_) {
    if (child == nullptr) {
      continue;
    }

    copy->appendChild(child->deepCopy());
  }

+40 −0
Original line number Diff line number Diff line
@@ -1270,3 +1270,43 @@ TEST_CASE(SQLTest, TestNumericConversion, [] () {
    EXPECT_EQ(val.getInteger(), 1415912541648lu);
  }
});

TEST_CASE(SQLTest, TestCompareTimestamps, [] () {
  auto result = executeTestQuery(
      "  SELECT"
      "  FROM_TIMESTAMP(1415916005281) > FROM_TIMESTAMP(1415916005281) as a,"
      "  FROM_TIMESTAMP(1415916005281) < FROM_TIMESTAMP(1415916005281) as b,"
      "  FROM_TIMESTAMP(1415916005282) > FROM_TIMESTAMP(1415916005281) as c,"
      "  FROM_TIMESTAMP(1415916005280) < FROM_TIMESTAMP(1415916005281) as d,"
      "  FROM_TIMESTAMP(1415916005280) > FROM_TIMESTAMP(1415916005281) as e,"
      "  FROM_TIMESTAMP(1415916005282) < FROM_TIMESTAMP(1415916005281) as f;");

  EXPECT_EQ(result->getNumRows(), 1);
  EXPECT_EQ(result->getNumColumns(), 6);
  EXPECT_EQ(result->getRow(0)[0], "false");
  EXPECT_EQ(result->getRow(0)[1], "false");
  EXPECT_EQ(result->getRow(0)[2], "true");
  EXPECT_EQ(result->getRow(0)[3], "true");
  EXPECT_EQ(result->getRow(0)[4], "false");
  EXPECT_EQ(result->getRow(0)[5], "false");
});

TEST_CASE(SQLTest, TestInvalidQueries, [] () {
  std::vector<std::string> queries;

  queries.push_back(
      "SELECT time AS x, count(value) as y FROM `http_status_codes`"
      "where GROUP OVER TIMEWINDOW(time, 1000, 1) BY test;");

  for (const auto& query : queries) {
    bool raised = false;

    try {
      executeTestQuery(query.c_str());
    } catch (std::exception& e) {
      raised = true;
    }

    EXPECT(raised);
  }
});
+4 −1
Original line number Diff line number Diff line
@@ -150,6 +150,9 @@ fnordmetric::IntegerType SValue::getInteger() const {
    case T_INTEGER:
      return data_.u.t_integer;

    case T_TIMESTAMP:
      return data_.u.t_timestamp;

    case T_STRING:
      try {
        return std::stol(getString());
@@ -160,7 +163,7 @@ fnordmetric::IntegerType SValue::getInteger() const {
    default:
      RAISE(
          kTypeError,
          "can't convert %s '%s' to Float",
          "can't convert %s '%s' to Integer",
          SValue::getTypeName(data_.type),
          toString().c_str());