Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default interpretation of real literals from DOUBLE to DECIMAL #3952

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/common/types/value/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,26 @@ Value::Value(double val_) : isNull_{false}, childrenSize{0} {
val.doubleVal = val_;
}

Value::Value(decimal_t val_) : isNull_{false}, childrenSize{0} {
dataType = LogicalType::DECIMAL(val_.precision, val_.scale);
switch (dataType.getPhysicalType()) {
case PhysicalTypeID::INT16:
val.int16Val = (int16_t)(val_.val);
break;
case PhysicalTypeID::INT32:
val.int32Val = (int32_t)(val_.val);
break;
case PhysicalTypeID::INT64:
val.int64Val = (int64_t)(val_.val);
break;
case PhysicalTypeID::INT128:
val.int128Val = val_.val;
break;
default:
KU_UNREACHABLE;
}
}

Value::Value(date_t val_) : isNull_{false}, childrenSize{0} {
dataType = LogicalType::DATE();
val.int32Val = val_.days;
Expand Down
20 changes: 20 additions & 0 deletions src/include/common/types/decimal_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "int128_t.h"

namespace kuzu {
namespace common {

struct KUZU_API decimal_t {

int128_t val = 0;
uint32_t precision = 18;
uint32_t scale = 3;

decimal_t() {}
decimal_t(int128_t val, uint32_t prec, uint32_t scale)
: val(val), precision(prec), scale(scale) {}
};

} // namespace common
} // namespace kuzu
5 changes: 5 additions & 0 deletions src/include/common/types/value/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "common/api.h"
#include "common/types/date_t.h"
#include "common/types/decimal_t.h"
#include "common/types/int128_t.h"
#include "common/types/internal_id_t.h"
#include "common/types/interval_t.h"
Expand Down Expand Up @@ -99,6 +100,10 @@ class Value {
* @param val_ the float value to set.
*/
KUZU_API explicit Value(float val_);
/**
* @param val_ the decimal_t value to set
*/
KUZU_API explicit Value(decimal_t val_);
/**
* @param val_ the date value to set.
*/
Expand Down
26 changes: 22 additions & 4 deletions src/parser/transform/transform_expression.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "function/aggregate/count_star.h"
#include "function/arithmetic/vector_arithmetic_functions.h"
#include "function/cast/functions/cast_from_string_functions.h"
#include "function/cast/functions/cast_string_non_nested_functions.h"
#include "function/list/vector_list_functions.h"
#include "function/string/vector_string_functions.h"
#include "function/struct/vector_struct_functions.h"
Expand Down Expand Up @@ -641,10 +642,27 @@ std::unique_ptr<ParsedExpression> Transformer::transformIntegerLiteral(
std::unique_ptr<ParsedExpression> Transformer::transformDoubleLiteral(
CypherParser::OC_DoubleLiteralContext& ctx) {
auto text = ctx.RegularDecimalReal()->getText();
ku_string_t literal{text.c_str(), text.length()};
double result;
function::CastString::operation(literal, result);
return std::make_unique<ParsedLiteralExpression>(Value(result), ctx.getText());
if (text[0] == '-') {
text.erase(text.begin());
}
auto type = LogicalType::DOUBLE();
if (text.size() - 1 <= DECIMAL_PRECISION_LIMIT) {
auto decimalPoint = text.find('.');
KU_ASSERT(decimalPoint != std::string::npos);
type = LogicalType::DECIMAL(text.size() - 1, text.size() - decimalPoint - 1);
}
text = ctx.RegularDecimalReal()->getText(); // undo changes
if (type.getLogicalTypeID() == LogicalTypeID::DECIMAL) {
int128_t val;
decimalCast(text.c_str(), text.length(), val, type);
decimal_t result(val, DecimalType::getPrecision(type), DecimalType::getScale(type));
return std::make_unique<ParsedLiteralExpression>(Value(result), ctx.getText());
} else {
ku_string_t literal{text.c_str(), text.length()};
double result;
function::CastString::operation(literal, result);
return std::make_unique<ParsedLiteralExpression>(Value(result), ctx.getText());
}
}

} // namespace parser
Expand Down
Loading