Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
AEsir777 committed Oct 4, 2023
1 parent af6bf97 commit 6f4a024
Show file tree
Hide file tree
Showing 19 changed files with 452 additions and 255 deletions.
8 changes: 6 additions & 2 deletions src/binder/bind/bind_graph_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "catalog/rel_table_schema.h"
#include "common/exception/binder.h"
#include "common/string_utils.h"
#include "function/cast/cast_utils.h"
#include "main/client_context.h"

using namespace kuzu::common;
Expand Down Expand Up @@ -424,10 +425,13 @@ std::shared_ptr<RelExpression> Binder::createRecursiveQueryRel(const parser::Rel
std::pair<uint64_t, uint64_t> Binder::bindVariableLengthRelBound(
const kuzu::parser::RelPattern& relPattern) {
auto recursiveInfo = relPattern.getRecursiveInfo();
auto lowerBound = TypeUtils::convertToUint32(recursiveInfo->lowerBound.c_str());
uint32_t lowerBound;
function::StringCastUtils::simpleIntegerCast(recursiveInfo->lowerBound.c_str(),
recursiveInfo->lowerBound.length(), lowerBound);
auto upperBound = clientContext->varLengthExtendMaxDepth;
if (!recursiveInfo->upperBound.empty()) {
upperBound = TypeUtils::convertToUint32(recursiveInfo->upperBound.c_str());
function::StringCastUtils::simpleIntegerCast(recursiveInfo->upperBound.c_str(),
recursiveInfo->upperBound.length(), upperBound);
}
if (lowerBound > upperBound) {
throw BinderException(
Expand Down
6 changes: 6 additions & 0 deletions src/common/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ std::vector<std::string> StringUtils::splitBySpace(const std::string& input) {
return result;
}

void StringUtils::skipWhitespace(const char*& input, const char* end) {
while (input != end && isspace(*input)) {
input++;
}
}

void StringUtils::removeCStringWhiteSpaces(const char*& input, uint64_t& len) {
// skip leading/trailing spaces
while (len > 0 && isspace(input[0])) {
Expand Down
10 changes: 0 additions & 10 deletions src/common/type_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
namespace kuzu {
namespace common {

uint32_t TypeUtils::convertToUint32(const char* data) {
std::istringstream iss(data);
uint32_t val;
if (!(iss >> val)) {
throw ConversionException(
StringUtils::string_format("Failed to convert {} to uint32_t", data));
}
return val;
}

std::string TypeUtils::castValueToString(
const LogicalType& dataType, uint8_t* value, void* vector) {
auto valueVector = reinterpret_cast<ValueVector*>(vector);
Expand Down
1 change: 1 addition & 0 deletions src/function/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(kuzu_function
built_in_aggregate_functions.cpp
built_in_vector_functions.cpp
built_in_table_functions.cpp
cast_utils.cpp
comparison_functions.cpp
find_function.cpp
scalar_macro_function.cpp
Expand Down
57 changes: 57 additions & 0 deletions src/function/cast_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "function/cast/cast_utils.h"

namespace kuzu {
namespace function {

bool StringCastUtils::tryCastToBool(const char* input, uint64_t len, bool& result) {
common::StringUtils::removeCStringWhiteSpaces(input, len);

switch (len) {
case 1: {
char c = std::tolower(*input);
if (c == 't' || c == '1') {
result = true;
return true;
} else if (c == 'f' || c == '0') {
result = false;
return true;
}
return false;
}
case 4: {
auto t = std::tolower(input[0]);
auto r = std::tolower(input[1]);
auto u = std::tolower(input[2]);
auto e = std::tolower(input[3]);
if (t == 't' && r == 'r' && u == 'u' && e == 'e') {
result = true;
return true;
}
return false;
}
case 5: {
auto f = std::tolower(input[0]);
auto a = std::tolower(input[1]);
auto l = std::tolower(input[2]);
auto s = std::tolower(input[3]);
auto e = std::tolower(input[4]);
if (f == 'f' && a == 'a' && l == 'l' && s == 's' && e == 'e') {
result = false;
return true;
}
return false;
}
default:
return false;
}
}

void StringCastUtils::castStringToBool(const char* input, uint64_t len, bool& result) {
if (!tryCastToBool(input, len, result)) {
throw common::ConversionException(
"Cast failed. " + std::string{input, len} + " is not in BOOL range.");
}
}

} // namespace function
} // namespace kuzu
8 changes: 8 additions & 0 deletions src/function/vector_cast_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ vector_function_definitions CastToBoolVectorFunction::getDefinitions() {
return result;
}

//vector_function_definitions CastToListVectorFunction::getDefinitions() {
// vector_function_definitions result;
// result.push_back(make_unique<VectorFunctionDefinition>(CAST_TO_LIST_FUNC_NAME,
// std::vector<LogicalTypeID>{LogicalTypeID::STRING}, LogicalTypeID::VAR_LIST,
// UnaryExecListStructFunction<ku_string_t, list_entry_t, CastToList>)); // TODO: modify later
// return result;
//}

vector_function_definitions CastToDoubleVectorFunction::getDefinitions() {
vector_function_definitions result;
for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) {
Expand Down
1 change: 1 addition & 0 deletions src/include/common/expression_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const std::string CAST_TO_UINT8_FUNC_NAME = "TO_UINT8";
const std::string CAST_BLOB_FUNC_NAME = "BLOB";
const std::string CAST_TO_BLOB_FUNC_NAME = "TO_BLOB";
const std::string CAST_TO_BOOL_FUNC_NAME = "TO_BOOL";
const std::string CAST_TO_LIST_FUNC_NAME = "TO_LIST";

// list
const std::string LIST_CREATION_FUNC_NAME = "LIST_CREATION";
Expand Down
2 changes: 2 additions & 0 deletions src/include/common/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class StringUtils {

static std::vector<std::string> splitBySpace(const std::string& input);

static void skipWhitespace(const char*& input, const char* end);

static void toUpper(std::string& input) {
std::transform(input.begin(), input.end(), input.begin(), ::toupper);
}
Expand Down
1 change: 0 additions & 1 deletion src/include/common/type_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace common {
class TypeUtils {

public:
static uint32_t convertToUint32(const char* data);
template<typename T>
static inline std::string toString(const T& val, void* valueVector = nullptr) {
static_assert(std::is_same<T, int64_t>::value || std::is_same<T, int32_t>::value ||
Expand Down
46 changes: 23 additions & 23 deletions src/include/function/cast/cast_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#include <cassert>

#include "cast_utils.h"
#include "common/exception/runtime.h"
#include "common/string_utils.h"
#include "common/type_utils.h"
#include "common/types/blob.h"
#include "common/vector/value_vector.h"
#include "numeric_cast.h"

namespace kuzu {
namespace function {
Expand Down Expand Up @@ -88,7 +88,7 @@ inline std::string CastToString::castToStringWithVector(

struct CastToBool {
static inline void operation(common::ku_string_t& input, bool& result) {
if (!tryCastToBool(reinterpret_cast<const char*>(input.getData()), input.len, result)) {
if (!StringCastUtils::tryCastToBool(reinterpret_cast<const char*>(input.getData()), input.len, result)) {
throw common::ConversionException{common::StringUtils::string_format(
"Value {} is not a valid boolean", input.getAsString())};
}
Expand All @@ -107,13 +107,13 @@ struct CastToDouble {

template<>
inline void CastToDouble::operation(char*& input, double_t& result) {
doubleCast<double_t>(
StringCastUtils::doubleCast<double_t>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::DOUBLE});
}

template<>
inline void CastToDouble::operation(common::ku_string_t& input, double_t& result) {
doubleCast<double_t>((char*)input.getData(), input.len, result,
StringCastUtils::doubleCast<double_t>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::DOUBLE});
}

Expand All @@ -129,13 +129,13 @@ struct CastToFloat {

template<>
inline void CastToFloat::operation(char*& input, float_t& result) {
doubleCast<float_t>(
StringCastUtils::doubleCast<float_t>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::FLOAT});
}

template<>
inline void CastToFloat::operation(common::ku_string_t& input, float_t& result) {
doubleCast<float_t>((char*)input.getData(), input.len, result,
StringCastUtils::doubleCast<float_t>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::FLOAT});
}

Expand All @@ -151,13 +151,13 @@ struct CastToInt64 {

template<>
inline void CastToInt64::operation(char*& input, int64_t& result) {
simpleIntegerCast<int64_t, true>(
StringCastUtils::simpleIntegerCast<int64_t, true>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::INT64});
}

template<>
inline void CastToInt64::operation(common::ku_string_t& input, int64_t& result) {
simpleIntegerCast<int64_t, true>((char*)input.getData(), input.len, result,
StringCastUtils::simpleIntegerCast<int64_t, true>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::INT64});
}

Expand All @@ -173,7 +173,7 @@ struct CastToSerial {

template<>
inline void CastToSerial::operation(common::ku_string_t& input, int64_t& result) {
simpleIntegerCast<int64_t>((char*)input.getData(), input.len, result,
StringCastUtils::simpleIntegerCast<int64_t>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::INT64});
}

Expand All @@ -189,13 +189,13 @@ struct CastToInt32 {

template<>
inline void CastToInt32::operation(char*& input, int32_t& result) {
simpleIntegerCast<int32_t, true>(
StringCastUtils::simpleIntegerCast<int32_t, true>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::INT32});
}

template<>
inline void CastToInt32::operation(common::ku_string_t& input, int32_t& result) {
simpleIntegerCast<int32_t, true>((char*)input.getData(), input.len, result,
StringCastUtils::simpleIntegerCast<int32_t, true>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::INT32});
}

Expand All @@ -211,13 +211,13 @@ struct CastToInt16 {

template<>
inline void CastToInt16::operation(common::ku_string_t& input, int16_t& result) {
simpleIntegerCast<int16_t, true>((char*)input.getData(), input.len, result,
StringCastUtils::simpleIntegerCast<int16_t, true>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::INT16});
}

template<>
inline void CastToInt16::operation(char*& input, int16_t& result) {
simpleIntegerCast<int16_t, true>(
StringCastUtils::simpleIntegerCast<int16_t, true>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::INT16});
}

Expand All @@ -233,13 +233,13 @@ struct CastToInt8 {

template<>
inline void CastToInt8::operation(common::ku_string_t& input, int8_t& result) {
simpleIntegerCast<int8_t, true>((char*)input.getData(), input.len, result,
StringCastUtils::simpleIntegerCast<int8_t, true>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::INT8});
}

template<>
inline void CastToInt8::operation(char*& input, int8_t& result) {
simpleIntegerCast<int8_t, true>(
StringCastUtils::simpleIntegerCast<int8_t, true>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::INT8});
}

Expand All @@ -255,13 +255,13 @@ struct CastToUInt64 {

template<>
inline void CastToUInt64::operation(common::ku_string_t& input, uint64_t& result) {
simpleIntegerCast<uint64_t, false>((char*)input.getData(), input.len, result,
StringCastUtils::simpleIntegerCast<uint64_t, false>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::UINT64});
}

template<>
inline void CastToUInt64::operation(char*& input, uint64_t& result) {
simpleIntegerCast<uint64_t, false>(
StringCastUtils::simpleIntegerCast<uint64_t, false>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::UINT64});
}

Expand All @@ -277,13 +277,13 @@ struct CastToUInt32 {

template<>
inline void CastToUInt32::operation(common::ku_string_t& input, uint32_t& result) {
simpleIntegerCast<uint32_t, false>((char*)input.getData(), input.len, result,
StringCastUtils::simpleIntegerCast<uint32_t, false>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::UINT32});
}

template<>
inline void CastToUInt32::operation(char*& input, uint32_t& result) {
simpleIntegerCast<uint32_t, false>(
StringCastUtils::simpleIntegerCast<uint32_t, false>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::UINT32});
}

Expand All @@ -299,13 +299,13 @@ struct CastToUInt16 {

template<>
inline void CastToUInt16::operation(common::ku_string_t& input, uint16_t& result) {
simpleIntegerCast<uint16_t, false>((char*)input.getData(), input.len, result,
StringCastUtils::simpleIntegerCast<uint16_t, false>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::UINT16});
}

template<>
inline void CastToUInt16::operation(char*& input, uint16_t& result) {
simpleIntegerCast<uint16_t, false>(
StringCastUtils::simpleIntegerCast<uint16_t, false>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::UINT16});
}

Expand All @@ -321,13 +321,13 @@ struct CastToUInt8 {

template<>
inline void CastToUInt8::operation(common::ku_string_t& input, uint8_t& result) {
simpleIntegerCast<uint8_t, false>((char*)input.getData(), input.len, result,
StringCastUtils::simpleIntegerCast<uint8_t, false>((char*)input.getData(), input.len, result,
common::LogicalType{common::LogicalTypeID::UINT8});
}

template<>
inline void CastToUInt8::operation(char*& input, uint8_t& result) {
simpleIntegerCast<uint8_t, false>(
StringCastUtils::simpleIntegerCast<uint8_t, false>(
input, strlen(input), result, common::LogicalType{common::LogicalTypeID::UINT8});
}

Expand Down
Loading

0 comments on commit 6f4a024

Please sign in to comment.