diff --git a/dataset/load-from-test/union/union_correct1.csv b/dataset/load-from-test/union/union_correct1.csv new file mode 100644 index 00000000000..51f0e550d66 --- /dev/null +++ b/dataset/load-from-test/union/union_correct1.csv @@ -0,0 +1,9 @@ +"false","34","255","18446744073709551615",fsdfa +" true ","432","0","-1.43241543","543fasf" +" 34234 ","4294967295","65535",-128,432 +" -42342345 ","-1","-1","-129",fasf +" T ","2022-06-06","4324.123","-32768",fds +"TRUE","2019-03-19","-12.3432","32768","" +"1","-2147483648","1970-01-01 00:00:00.004666-10","-32769",fsdxcv +"0","0","2014-05-12 12:11:59",4324254534123134324321.4343252435,"fsaf" +" F","-4325"," 14 ",18446744073709551616," dfsa" \ No newline at end of file diff --git a/src/binder/bind/bind_graph_pattern.cpp b/src/binder/bind/bind_graph_pattern.cpp index 49a87abae17..7746a701e7e 100644 --- a/src/binder/bind/bind_graph_pattern.cpp +++ b/src/binder/bind/bind_graph_pattern.cpp @@ -401,11 +401,13 @@ std::pair Binder::bindVariableLengthRelBound( auto recursiveInfo = relPattern.getRecursiveInfo(); uint32_t lowerBound; function::CastStringToTypes::operation( - recursiveInfo->lowerBound.c_str(), recursiveInfo->lowerBound.length(), lowerBound); + ku_string_t{recursiveInfo->lowerBound.c_str(), recursiveInfo->lowerBound.length()}, + lowerBound); auto upperBound = clientContext->varLengthExtendMaxDepth; if (!recursiveInfo->upperBound.empty()) { function::CastStringToTypes::operation( - recursiveInfo->upperBound.c_str(), recursiveInfo->upperBound.length(), upperBound); + ku_string_t{recursiveInfo->upperBound.c_str(), recursiveInfo->upperBound.length()}, + upperBound); } if (lowerBound > upperBound) { throw BinderException( diff --git a/src/binder/bind_expression/bind_function_expression.cpp b/src/binder/bind_expression/bind_function_expression.cpp index 9362ac1024e..36d4f124e2d 100644 --- a/src/binder/bind_expression/bind_function_expression.cpp +++ b/src/binder/bind_expression/bind_function_expression.cpp @@ -42,6 +42,18 @@ std::shared_ptr ExpressionBinder::bindFunctionExpression( std::shared_ptr ExpressionBinder::bindScalarFunctionExpression( const ParsedExpression& parsedExpression, const std::string& functionName) { expression_vector children; + + if (functionName == "CAST") { + if (parsedExpression.getNumChildren() != 2) { + throw BinderException("Cannot match a built-in function for given function CAST"); + } + auto str = parsedExpression.getChild(1)->toString(); + auto type = binder->bindDataType(str.substr(1, str.length() - 2)); // trim off "" + children.push_back(bindExpression(*parsedExpression.getChild(0))); + + return bindScalarFunctionExpression(children, functionName, type.get()); + } + for (auto i = 0u; i < parsedExpression.getNumChildren(); ++i) { auto child = bindExpression(*parsedExpression.getChild(i)); children.push_back(std::move(child)); @@ -50,14 +62,21 @@ std::shared_ptr ExpressionBinder::bindScalarFunctionExpression( } std::shared_ptr ExpressionBinder::bindScalarFunctionExpression( - const expression_vector& children, const std::string& functionName) { + const expression_vector& children, const std::string& functionName, const LogicalType* type) { auto builtInFunctions = binder->catalog.getBuiltInFunctions(); std::vector childrenTypes; for (auto& child : children) { childrenTypes.push_back(&child->dataType); } - auto function = reinterpret_cast( - builtInFunctions->matchScalarFunction(functionName, childrenTypes)); + + function::ScalarFunction* function; + if (type) { + function = reinterpret_cast( + builtInFunctions->matchCastScalarFunction(functionName, childrenTypes, type)); + } else { + function = reinterpret_cast( + builtInFunctions->matchScalarFunction(functionName, childrenTypes)); + } expression_vector childrenAfterCast; for (auto i = 0u; i < children.size(); ++i) { auto targetType = @@ -68,14 +87,18 @@ std::shared_ptr ExpressionBinder::bindScalarFunctionExpression( if (function->bindFunc) { bindData = function->bindFunc(childrenAfterCast, function); } else { - bindData = - std::make_unique(LogicalType(function->returnTypeID)); + if (type) { + bindData = std::make_unique(*type); + } else { + bindData = + std::make_unique(LogicalType(function->returnTypeID)); + } } auto uniqueExpressionName = ScalarFunctionExpression::getUniqueName(function->name, childrenAfterCast); return make_shared(functionName, FUNCTION, std::move(bindData), std::move(childrenAfterCast), function->execFunc, function->selectFunc, - function->compileFunc, uniqueExpressionName); + function->compileFunc, function->castFunc, uniqueExpressionName); } std::shared_ptr ExpressionBinder::bindAggregateFunctionExpression( diff --git a/src/binder/expression/function_expression.cpp b/src/binder/expression/function_expression.cpp index f3f4dd21f50..d0f4e4e09a9 100644 --- a/src/binder/expression/function_expression.cpp +++ b/src/binder/expression/function_expression.cpp @@ -18,6 +18,10 @@ std::string ScalarFunctionExpression::getUniqueName( std::string ScalarFunctionExpression::toStringInternal() const { auto result = functionName + "("; result += ExpressionUtil::toString(children); + if (functionName == "CAST") { + result += ", "; + result += common::LogicalTypeUtils::dataTypeToString(bindData->resultType); + } result += ")"; return result; } diff --git a/src/c_api/value.cpp b/src/c_api/value.cpp index 21f73c6260b..5f84f044cf9 100644 --- a/src/c_api/value.cpp +++ b/src/c_api/value.cpp @@ -269,7 +269,7 @@ kuzu_int128_t kuzu_int128_t_from_string(const char* str) { int128_t int128_val = 0; kuzu_int128_t c_int128; try { - kuzu::function::CastStringToTypes::operation(str, strlen(str), int128_val); + kuzu::function::CastStringToTypes::operation(ku_string_t{str, strlen(str)}, int128_val); c_int128.low = int128_val.low; c_int128.high = int128_val.high; } catch (ConversionException& e) { diff --git a/src/common/types/ku_string.cpp b/src/common/types/ku_string.cpp index 1f81183426b..f1db589b9c0 100644 --- a/src/common/types/ku_string.cpp +++ b/src/common/types/ku_string.cpp @@ -3,6 +3,15 @@ namespace kuzu { namespace common { +ku_string_t::ku_string_t(const char* value, uint64_t length) : len(length) { + if (isShortString(length)) { + memcpy(prefix, value, length); + return; + } + overflowPtr = (uint64_t)(value); + memcpy(prefix, value, PREFIX_LENGTH); +} + void ku_string_t::set(const std::string& value) { set(value.data(), value.length()); } diff --git a/src/expression_evaluator/function_evaluator.cpp b/src/expression_evaluator/function_evaluator.cpp index 46b37062e27..5320ab87410 100644 --- a/src/expression_evaluator/function_evaluator.cpp +++ b/src/expression_evaluator/function_evaluator.cpp @@ -11,9 +11,11 @@ namespace evaluator { void FunctionExpressionEvaluator::init(const ResultSet& resultSet, MemoryManager* memoryManager) { ExpressionEvaluator::init(resultSet, memoryManager); - execFunc = ((binder::ScalarFunctionExpression&)*expression).execFunc; + auto expr = reinterpret_cast(expression.get()); + execFunc = expr->execFunc; + castFunc = expr->castFunc; if (expression->dataType.getLogicalTypeID() == LogicalTypeID::BOOL) { - selectFunc = ((binder::ScalarFunctionExpression&)*expression).selectFunc; + selectFunc = expr->selectFunc; } } @@ -21,6 +23,13 @@ void FunctionExpressionEvaluator::evaluate() { for (auto& child : children) { child->evaluate(); } + if (castFunc != nullptr) { + auto expr = reinterpret_cast(expression.get()); + castFunc(parameters, *resultVector, + &reinterpret_cast(expr->getBindData()) + ->csvConfig); + return; + } if (execFunc != nullptr) { execFunc(parameters, *resultVector); } diff --git a/src/function/built_in_functions.cpp b/src/function/built_in_functions.cpp index 0ab534eb4e8..60752859231 100644 --- a/src/function/built_in_functions.cpp +++ b/src/function/built_in_functions.cpp @@ -87,6 +87,30 @@ Function* BuiltInFunctions::matchScalarFunction( return candidateFunctions[0]; } +Function* BuiltInFunctions::matchCastScalarFunction( + const std::string& name, const std::vector& inputTypes, const LogicalType* type) { + auto& functionSet = functions.at(name); + std::vector candidateFunctions; + uint32_t minCost = UINT32_MAX; + for (auto& function : functionSet) { + auto func = reinterpret_cast(function.get()); + if (func->returnTypeID == type->getLogicalTypeID() ) { + if (func->parameterTypeIDs[0] == inputTypes[0]->getLogicalTypeID()) { + candidateFunctions.push_back(function.get()); + break; + } + if (inputTypes[0]->getLogicalTypeID() == LogicalTypeID::ANY) { + if (func->parameterTypeIDs[0] == LogicalTypeID::STRING) { + candidateFunctions.push_back(function.get()); + break; + } + } + } + } + validateNonEmptyCandidateFunctions(candidateFunctions, name, inputTypes); + return candidateFunctions[0]; +} + AggregateFunction* BuiltInFunctions::matchAggregateFunction( const std::string& name, const std::vector& inputTypes, bool isDistinct) { auto& functionSet = functions.at(name); @@ -597,6 +621,7 @@ void BuiltInFunctions::registerCastFunctions() { functions.insert({CAST_TO_UINT8_FUNC_NAME, CastToUInt8Function::getFunctionSet()}); functions.insert({CAST_TO_INT128_FUNC_NAME, CastToInt128Function::getFunctionSet()}); functions.insert({CAST_TO_BOOL_FUNC_NAME, CastToBoolFunction::getFunctionSet()}); + functions.insert({CAST_FUNC_NAME, CastAnyFunction::getFunctionSet()}); } void BuiltInFunctions::registerListFunctions() { diff --git a/src/function/cast_string_to_functions.cpp b/src/function/cast_string_to_functions.cpp index ded1a86e31b..fc601ac8a3b 100644 --- a/src/function/cast_string_to_functions.cpp +++ b/src/function/cast_string_to_functions.cpp @@ -13,26 +13,118 @@ using namespace kuzu::common; namespace kuzu { namespace function { +// ---------------------- cast String to Types Helper ------------------------------ // +struct CastStringToTypesHelper { + template + static void cast(const char* input, uint64_t len, T& result, ValueVector* /*vector*/ = nullptr, + uint64_t /*rowToAdd*/ = 0, const CSVReaderConfig* /*csvReaderConfig*/ = nullptr) { + simpleIntegerCast(input, len, result, LogicalType{LogicalTypeID::INT64}); + } +}; + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, int128_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + simpleInt128Cast(input, len, result); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, int32_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + simpleIntegerCast(input, len, result, LogicalType{LogicalTypeID::INT32}); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, int16_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + simpleIntegerCast(input, len, result, LogicalType{LogicalTypeID::INT16}); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, int8_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + simpleIntegerCast(input, len, result, LogicalType{LogicalTypeID::INT8}); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, uint64_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + simpleIntegerCast(input, len, result, LogicalType{LogicalTypeID::UINT64}); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, uint32_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + simpleIntegerCast(input, len, result, LogicalType{LogicalTypeID::UINT32}); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, uint16_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + simpleIntegerCast(input, len, result, LogicalType{LogicalTypeID::UINT16}); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, uint8_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + simpleIntegerCast(input, len, result, LogicalType{LogicalTypeID::UINT8}); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, float_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + doubleCast(input, len, result, LogicalType{LogicalTypeID::FLOAT}); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, double_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + doubleCast(input, len, result, LogicalType{LogicalTypeID::DOUBLE}); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, bool& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + castStringToBool(input, len, result); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, date_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + result = Date::fromCString(input, len); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, timestamp_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + result = Timestamp::fromCString(input, len); +} + +template<> +inline void CastStringToTypesHelper::cast(const char* input, uint64_t len, interval_t& result, + ValueVector* /*vector*/, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*csvReaderConfig*/) { + result = Interval::fromCString(input, len); +} + // ---------------------- cast String to Blob ------------------------------ // template<> -void CastStringToTypes::operation( - common::ku_string_t& input, common::blob_t& result, common::ValueVector& resultVector) { - result.value.len = common::Blob::getBlobSize(input); - if (!common::ku_string_t::isShortString(result.value.len)) { - auto overflowBuffer = common::StringVector::getInMemOverflowBuffer(&resultVector); +void CastStringToTypes::operation(const ku_string_t& input, blob_t& result, + ValueVector* resultVector, uint64_t /*rowToAdd*/, const CSVReaderConfig* /*CSVReaderConfig*/) { + result.value.len = Blob::getBlobSize(input); + if (!ku_string_t::isShortString(result.value.len)) { + auto overflowBuffer = StringVector::getInMemOverflowBuffer(resultVector); auto overflowPtr = overflowBuffer->allocateSpace(result.value.len); result.value.overflowPtr = reinterpret_cast(overflowPtr); - common::Blob::fromString( - reinterpret_cast(input.getData()), input.len, overflowPtr); - memcpy(result.value.prefix, overflowPtr, common::ku_string_t::PREFIX_LENGTH); + Blob::fromString(reinterpret_cast(input.getData()), input.len, overflowPtr); + memcpy(result.value.prefix, overflowPtr, ku_string_t::PREFIX_LENGTH); } else { - common::Blob::fromString( + Blob::fromString( reinterpret_cast(input.getData()), input.len, result.value.prefix); } } template<> -void CastStringToTypes::operation(const char* input, uint64_t len, common::blob_t& /*result*/, +void CastStringToTypesHelper::cast(const char* input, uint64_t len, blob_t& /*result*/, ValueVector* vector, uint64_t rowToAdd, const CSVReaderConfig* /*CSVReaderConfig*/) { // base case: blob storage::TableCopyUtils::validateStrLen(len); @@ -200,7 +292,7 @@ static inline void startListCast(const char* input, uint64_t len, T split, } template<> -void CastStringToTypes::operation(const char* input, uint64_t len, list_entry_t& /*result*/, +void CastStringToTypesHelper::cast(const char* input, uint64_t len, list_entry_t& /*result*/, ValueVector* vector, uint64_t rowToAdd, const CSVReaderConfig* csvReaderConfig) { // calculate the number of elements in array CountPartOperation state; @@ -214,6 +306,13 @@ void CastStringToTypes::operation(const char* input, uint64_t len, list_entry_t& startListCast(input, len, split, csvReaderConfig, vector); } +template<> +void CastStringToTypes::operation(const ku_string_t& input, list_entry_t& result, + ValueVector* resultVector, uint64_t rowToAdd, const CSVReaderConfig* CSVReaderConfig) { + CastStringToTypesHelper::cast(reinterpret_cast(input.getData()), input.len, result, + resultVector, rowToAdd, CSVReaderConfig); +} + // ---------------------- cast String to FixedList ------------------------------ // template struct SplitStringFixedListOperation { @@ -231,7 +330,7 @@ struct SplitStringFixedListOperation { throw ConversionException("Cast failed. NULL is not allowed for FIXEDLIST."); } auto type = FixedListType::getChildType(&resultVector->dataType); - function::CastStringToTypes::operation(start, str.length(), value); + CastStringToTypesHelper::cast(start, str.length(), value); resultVector->setValue(offset, value); offset++; } @@ -379,7 +478,7 @@ static bool splitCStringMap( } template<> -void CastStringToTypes::operation(const char* input, uint64_t len, common::map_entry_t& /*result*/, +void CastStringToTypesHelper::cast(const char* input, uint64_t len, map_entry_t& /*result*/, ValueVector* vector, uint64_t rowToAdd, const CSVReaderConfig* csvReaderConfig) { // count the number of maps in map CountPartOperation state; @@ -396,6 +495,13 @@ void CastStringToTypes::operation(const char* input, uint64_t len, common::map_e } } +template<> +void CastStringToTypes::operation(const ku_string_t& input, map_entry_t& result, + ValueVector* resultVector, uint64_t rowToAdd, const CSVReaderConfig* CSVReaderConfig) { + CastStringToTypesHelper::cast(reinterpret_cast(input.getData()), input.len, result, + resultVector, rowToAdd, CSVReaderConfig); +} + // ---------------------- cast String to Struct ------------------------------ // static bool parseStructFieldName(const char*& input, const char* end) { while (input < end) { @@ -486,7 +592,7 @@ static bool tryCastStringToStruct(const char* input, uint64_t len, ValueVector* } template<> -void CastStringToTypes::operation(const char* input, uint64_t len, struct_entry_t& /*result*/, +void CastStringToTypesHelper::cast(const char* input, uint64_t len, struct_entry_t& /*result*/, ValueVector* vector, uint64_t rowToAdd, const CSVReaderConfig* csvReaderConfig) { if (!tryCastStringToStruct(input, len, vector, rowToAdd, csvReaderConfig)) { throw ConversionException("Cast failed. " + std::string{input, len} + " is not in " + @@ -494,6 +600,13 @@ void CastStringToTypes::operation(const char* input, uint64_t len, struct_entry_ } } +template<> +void CastStringToTypes::operation(const ku_string_t& input, struct_entry_t& result, + ValueVector* resultVector, uint64_t rowToAdd, const CSVReaderConfig* CSVReaderConfig) { + CastStringToTypesHelper::cast(reinterpret_cast(input.getData()), input.len, result, + resultVector, rowToAdd, CSVReaderConfig); +} + // ---------------------- cast String to Union ------------------------------ // template static inline void testAndSetValue(ValueVector* vector, uint64_t rowToAdd, T result, bool success) { @@ -512,6 +625,11 @@ static bool tryCastUnionField( success = function::tryCastToBool(input, len, result); testAndSetValue(vector, rowToAdd, result, success); } break; + case LogicalTypeID::INT128: { + int128_t result; + success = function::trySimpleInt128Cast(input, len, result); + testAndSetValue(vector, rowToAdd, result, success); + } break; case LogicalTypeID::INT64: { int64_t result; success = function::trySimpleIntegerCast(input, len, result); @@ -589,7 +707,7 @@ static bool tryCastUnionField( } template<> -void CastStringToTypes::operation(const char* input, uint64_t len, union_entry_t& /*result*/, +void CastStringToTypesHelper::cast(const char* input, uint64_t len, union_entry_t& /*result*/, ValueVector* vector, uint64_t rowToAdd, const CSVReaderConfig* /*CSVReaderConfig**/) { auto& type = vector->dataType; union_field_idx_t selectedFieldIdx = INVALID_STRUCT_FIELD_IDX; @@ -616,6 +734,13 @@ void CastStringToTypes::operation(const char* input, uint64_t len, union_entry_t ->setNull(rowToAdd, false /* isNull */); } +template<> +void CastStringToTypes::operation(const ku_string_t& input, union_entry_t& result, + ValueVector* resultVector, uint64_t rowToAdd, const CSVReaderConfig* CSVReaderConfig) { + CastStringToTypesHelper::cast(reinterpret_cast(input.getData()), input.len, result, + resultVector, rowToAdd, CSVReaderConfig); +} + void CastStringToTypes::copyStringToVector(ValueVector* vector, uint64_t rowToAdd, std::string_view strVal, const CSVReaderConfig* csvReaderConfig) { auto& type = vector->dataType; @@ -629,67 +754,68 @@ void CastStringToTypes::copyStringToVector(ValueVector* vector, uint64_t rowToAd switch (type.getLogicalTypeID()) { case LogicalTypeID::INT128: { int128_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::INT64: { int64_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::INT32: { int32_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::INT16: { int16_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::INT8: { int8_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::UINT64: { uint64_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::UINT32: { uint32_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::UINT16: { uint16_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::UINT8: { uint8_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::FLOAT: { float_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::DOUBLE: { double_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::BOOL: { bool val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::BLOB: { blob_t val; - operation(strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); + CastStringToTypesHelper::cast( + strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); } break; case LogicalTypeID::STRING: { storage::TableCopyUtils::validateStrLen(strVal.length()); @@ -700,26 +826,28 @@ void CastStringToTypes::copyStringToVector(ValueVector* vector, uint64_t rowToAd } break; case LogicalTypeID::DATE: { date_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::TIMESTAMP: { timestamp_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::INTERVAL: { interval_t val; - operation(strVal.data(), strVal.length(), val); + CastStringToTypesHelper::cast(strVal.data(), strVal.length(), val); vector->setValue(rowToAdd, val); } break; case LogicalTypeID::MAP: { map_entry_t val; - operation(strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); + CastStringToTypesHelper::cast( + strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); } break; case LogicalTypeID::VAR_LIST: { list_entry_t val; - operation(strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); + CastStringToTypesHelper::cast( + strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); } break; case LogicalTypeID::FIXED_LIST: { // TODO: add fix list function wrapper @@ -727,11 +855,13 @@ void CastStringToTypes::copyStringToVector(ValueVector* vector, uint64_t rowToAd } break; case LogicalTypeID::STRUCT: { struct_entry_t val; - operation(strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); + CastStringToTypesHelper::cast( + strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); } break; case LogicalTypeID::UNION: { union_entry_t val; - operation(strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); + CastStringToTypesHelper::cast( + strVal.data(), strVal.length(), val, vector, rowToAdd, csvReaderConfig); } break; default: { // LCOV_EXCL_START throw NotImplementedException("CastStringToTypes::operation"); diff --git a/src/function/vector_cast_functions.cpp b/src/function/vector_cast_functions.cpp index 7049e106b7b..81ce6147d98 100644 --- a/src/function/vector_cast_functions.cpp +++ b/src/function/vector_cast_functions.cpp @@ -125,20 +125,19 @@ void CastFunction::bindImplicitCastFunc( } case LogicalTypeID::DATE: { assert(sourceTypeID == LogicalTypeID::STRING); - func = &ScalarFunction::UnaryStringExecFunction; + func = &UnaryStringCastExecFunction; return; } case LogicalTypeID::TIMESTAMP: { assert(sourceTypeID == LogicalTypeID::STRING || sourceTypeID == LogicalTypeID::DATE); func = sourceTypeID == LogicalTypeID::STRING ? - &ScalarFunction::UnaryStringExecFunction : + &UnaryStringCastExecFunction : &ScalarFunction::UnaryExecFunction; return; } case LogicalTypeID::INTERVAL: { assert(sourceTypeID == LogicalTypeID::STRING); - func = &ScalarFunction::UnaryStringExecFunction; + func = &UnaryStringCastExecFunction; return; } default: @@ -371,8 +370,12 @@ function_set CastToInt64Function::getFunctionSet() { result.push_back(CastFunction::bindNumericCastFunction( CAST_TO_INT64_FUNC_NAME, typeID, LogicalTypeID::INT64)); } - result.push_back(CastFunction::bindCastStringToFunction( - CAST_TO_INT64_FUNC_NAME, LogicalTypeID::INT64)); + result.push_back( + std::make_unique(CAST_TO_INT64_FUNC_NAME, + std::vector{LogicalTypeID::STRING}, LogicalTypeID::INT64, + ScalarFunction::UnaryExecFunction)); + // result.push_back(CastFunction::bindCastStringToFunction( + // CAST_TO_INT64_FUNC_NAME, LogicalTypeID::INT64)); return result; } @@ -453,5 +456,109 @@ function_set CastToUInt8Function::getFunctionSet() { return result; } +function_set CastAnyFunction::getFunctionSet() { + function_set result; + // cast string to + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::DATE)); + result.push_back(CastFunction::bindCastStringToFunction( + CAST_FUNC_NAME, LogicalTypeID::TIMESTAMP)); + result.push_back(CastFunction::bindCastStringToFunction( + CAST_FUNC_NAME, LogicalTypeID::INTERVAL)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::BLOB)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::BOOL)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::DOUBLE)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::FLOAT)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::INT128)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::INT64)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::SERIAL)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::INT32)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::INT16)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::INT8)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::UINT64)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::UINT32)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::UINT16)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::UINT8)); + result.push_back(CastFunction::bindCastStringToFunction( + CAST_FUNC_NAME, LogicalTypeID::VAR_LIST)); + result.push_back( + CastFunction::bindCastStringToFunction(CAST_FUNC_NAME, LogicalTypeID::MAP)); + result.push_back(CastFunction::bindCastStringToFunction( + CAST_FUNC_NAME, LogicalTypeID::STRUCT)); + result.push_back(CastFunction::bindCastStringToFunction( + CAST_FUNC_NAME, LogicalTypeID::UNION)); + // cast to other types + for (auto& type : LogicalTypeUtils::getAllValidLogicTypes()) { + scalar_exec_func execFunc; + CastToStringFunction::getUnaryCastToStringExecFunction(type.getLogicalTypeID(), execFunc); + auto func = std::make_unique(CAST_FUNC_NAME, + std::vector{type.getLogicalTypeID()}, LogicalTypeID::STRING, execFunc); + result.push_back(std::move(func)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_FUNC_NAME, typeID, LogicalTypeID::DOUBLE)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_FLOAT_FUNC_NAME, typeID, LogicalTypeID::FLOAT)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_INT128_FUNC_NAME, typeID, LogicalTypeID::INT128)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_SERIAL_FUNC_NAME, typeID, LogicalTypeID::SERIAL)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_INT64_FUNC_NAME, typeID, LogicalTypeID::INT64)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_INT32_FUNC_NAME, typeID, LogicalTypeID::INT32)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_INT16_FUNC_NAME, typeID, LogicalTypeID::INT16)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_INT8_FUNC_NAME, typeID, LogicalTypeID::INT8)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_UINT64_FUNC_NAME, typeID, LogicalTypeID::UINT64)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_UINT32_FUNC_NAME, typeID, LogicalTypeID::UINT32)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_UINT16_FUNC_NAME, typeID, LogicalTypeID::UINT16)); + } + for (auto typeID : LogicalTypeUtils::getNumericalLogicalTypeIDs()) { + result.push_back(CastFunction::bindNumericCastFunction( + CAST_TO_UINT8_FUNC_NAME, typeID, LogicalTypeID::UINT8)); + } + return result; +} + } // namespace function } // namespace kuzu diff --git a/src/include/binder/expression/function_expression.h b/src/include/binder/expression/function_expression.h index d4357b425c7..1c5a9eb842e 100644 --- a/src/include/binder/expression/function_expression.h +++ b/src/include/binder/expression/function_expression.h @@ -45,17 +45,18 @@ class ScalarFunctionExpression : public FunctionExpression { function::scalar_exec_func execFunc, function::scalar_select_func selectFunc, const std::string& uniqueName) : ScalarFunctionExpression{std::move(functionName), expressionType, std::move(bindData), - std::move(children), std::move(execFunc), std::move(selectFunc), nullptr, + std::move(children), std::move(execFunc), std::move(selectFunc), nullptr, nullptr, uniqueName} {} ScalarFunctionExpression(std::string functionName, common::ExpressionType expressionType, std::unique_ptr bindData, expression_vector children, function::scalar_exec_func execFunc, function::scalar_select_func selectFunc, - function::scalar_compile_func compileFunc, const std::string& uniqueName) + function::scalar_compile_func compileFunc, function::string_cast_func castFunc, + const std::string& uniqueName) : FunctionExpression{std::move(functionName), expressionType, std::move(bindData), std::move(children), uniqueName}, - execFunc{std::move(execFunc)}, selectFunc{std::move(selectFunc)}, compileFunc{std::move( - compileFunc)} {} + execFunc{std::move(execFunc)}, selectFunc{std::move(selectFunc)}, castFunc{castFunc}, + compileFunc{std::move(compileFunc)} {} static std::string getUniqueName(const std::string& functionName, expression_vector& children); @@ -65,6 +66,7 @@ class ScalarFunctionExpression : public FunctionExpression { function::scalar_exec_func execFunc; function::scalar_select_func selectFunc; function::scalar_compile_func compileFunc; + function::string_cast_func castFunc; }; class AggregateFunctionExpression : public FunctionExpression { diff --git a/src/include/binder/expression_binder.h b/src/include/binder/expression_binder.h index 4a1956fe6fb..d96ef200962 100644 --- a/src/include/binder/expression_binder.h +++ b/src/include/binder/expression_binder.h @@ -62,8 +62,8 @@ class ExpressionBinder { std::shared_ptr bindScalarFunctionExpression( const parser::ParsedExpression& parsedExpression, const std::string& functionName); - std::shared_ptr bindScalarFunctionExpression( - const expression_vector& children, const std::string& functionName); + std::shared_ptr bindScalarFunctionExpression(const expression_vector& children, + const std::string& functionName, const common::LogicalType* type = nullptr); std::shared_ptr bindAggregateFunctionExpression( const parser::ParsedExpression& parsedExpression, const std::string& functionName, bool isDistinct); diff --git a/src/include/common/enums/expression_type.h b/src/include/common/enums/expression_type.h index 9e2a1cfd076..87abf22c255 100644 --- a/src/include/common/enums/expression_type.h +++ b/src/include/common/enums/expression_type.h @@ -20,6 +20,7 @@ const std::string MAX_FUNC_NAME = "MAX"; const std::string COLLECT_FUNC_NAME = "COLLECT"; // cast +const std::string CAST_FUNC_NAME = "CAST"; const std::string CAST_DATE_FUNC_NAME = "DATE"; const std::string CAST_TO_DATE_FUNC_NAME = "TO_DATE"; const std::string CAST_TO_TIMESTAMP_FUNC_NAME = "TIMESTAMP"; diff --git a/src/include/common/types/ku_string.h b/src/include/common/types/ku_string.h index 273a04ae3ae..73d27d3b064 100644 --- a/src/include/common/types/ku_string.h +++ b/src/include/common/types/ku_string.h @@ -21,6 +21,7 @@ struct ku_string_t { }; ku_string_t() : len{0}, overflowPtr{0} {} + ku_string_t(const char* value, uint64_t length); static bool isShortString(uint32_t len) { return len <= SHORT_STR_LENGTH; } diff --git a/src/include/expression_evaluator/function_evaluator.h b/src/include/expression_evaluator/function_evaluator.h index f49197ff150..688bfedf275 100644 --- a/src/include/expression_evaluator/function_evaluator.h +++ b/src/include/expression_evaluator/function_evaluator.h @@ -10,8 +10,8 @@ class FunctionExpressionEvaluator : public ExpressionEvaluator { public: FunctionExpressionEvaluator(std::shared_ptr expression, std::vector> children) - : ExpressionEvaluator{std::move(children)}, - expression{std::move(expression)}, execFunc{nullptr}, selectFunc{nullptr} {} + : ExpressionEvaluator{std::move(children)}, expression{std::move(expression)}, + execFunc{nullptr}, selectFunc{nullptr}, castFunc{nullptr} {} void init( const processor::ResultSet& resultSet, storage::MemoryManager* memoryManager) override; @@ -30,6 +30,7 @@ class FunctionExpressionEvaluator : public ExpressionEvaluator { std::shared_ptr expression; function::scalar_exec_func execFunc; function::scalar_select_func selectFunc; + function::string_cast_func castFunc; std::vector> parameters; }; diff --git a/src/include/function/built_in_function.h b/src/include/function/built_in_function.h index e4c2a5091db..69313d87e35 100644 --- a/src/include/function/built_in_function.h +++ b/src/include/function/built_in_function.h @@ -25,6 +25,8 @@ class BuiltInFunctions { // functions. Function* matchScalarFunction( const std::string& name, const std::vector& inputTypes); + Function* matchCastScalarFunction(const std::string& name, + const std::vector& inputTypes, const common::LogicalType* type); AggregateFunction* matchAggregateFunction(const std::string& name, const std::vector& inputTypes, bool isDistinct); diff --git a/src/include/function/cast/functions/cast_string_to_functions.h b/src/include/function/cast/functions/cast_string_to_functions.h index 3dcac7841d8..10c352ee1e4 100644 --- a/src/include/function/cast/functions/cast_string_to_functions.h +++ b/src/include/function/cast/functions/cast_string_to_functions.h @@ -10,276 +10,158 @@ #include "common/types/blob.h" #include "common/vector/value_vector.h" +using namespace kuzu::common; + namespace kuzu { namespace function { struct CastStringToTypes { - static void copyStringToVector(common::ValueVector* vector, uint64_t rowToAdd, - std::string_view strVal, const common::CSVReaderConfig* csvReaderConfig); + static void copyStringToVector(ValueVector* vector, uint64_t rowToAdd, std::string_view strVal, + const CSVReaderConfig* csvReaderConfig); template - static inline bool tryCast(const char* input, uint64_t len, T& result) { + static inline bool tryCast(const ku_string_t& input, T& result) { // try cast for signed integer types - return trySimpleIntegerCast(input, len, result); + return trySimpleIntegerCast( + reinterpret_cast(input.getData()), input.len, result); } template - static inline void operation( - common::ku_string_t& input, T& result, common::ValueVector& /*resultVector*/) { + static inline void operation(const ku_string_t& input, T& result, + ValueVector* /*resultVector*/ = nullptr, uint64_t /*rowToAdd*/ = 0, + const CSVReaderConfig* /*CSVReaderConfig*/ = nullptr) { // base case: int64 - simpleIntegerCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::INT64}); - } - - template - static void operation(const char* input, uint64_t len, T& result, - common::ValueVector* /*vector*/ = nullptr, uint64_t /*rowToAdd*/ = 0, - const common::CSVReaderConfig* /*csvReaderConfig*/ = nullptr) { - simpleIntegerCast( - input, len, result, common::LogicalType{common::LogicalTypeID::INT64}); + simpleIntegerCast(reinterpret_cast(input.getData()), input.len, + result, LogicalType{LogicalTypeID::INT64}); } }; template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, common::int128_t& result, common::ValueVector& /*resultVector*/) { - simpleInt128Cast((char*)input.getData(), input.len, result); +inline void CastStringToTypes::operation(const ku_string_t& input, int128_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + simpleInt128Cast(reinterpret_cast(input.getData()), input.len, result); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, int32_t& result, common::ValueVector& /*resultVector*/) { - simpleIntegerCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::INT32}); +inline void CastStringToTypes::operation(const ku_string_t& input, int32_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + simpleIntegerCast(reinterpret_cast(input.getData()), input.len, result, + LogicalType{LogicalTypeID::INT32}); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, int16_t& result, common::ValueVector& /*resultVector*/) { - simpleIntegerCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::INT16}); +inline void CastStringToTypes::operation(const ku_string_t& input, int16_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + simpleIntegerCast(reinterpret_cast(input.getData()), input.len, result, + LogicalType{LogicalTypeID::INT16}); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, int8_t& result, common::ValueVector& /*resultVector*/) { - simpleIntegerCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::INT8}); +inline void CastStringToTypes::operation(const ku_string_t& input, int8_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + simpleIntegerCast(reinterpret_cast(input.getData()), input.len, result, + LogicalType{LogicalTypeID::INT8}); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, uint64_t& result, common::ValueVector& /*resultVector*/) { - simpleIntegerCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::UINT64}); +inline void CastStringToTypes::operation(const ku_string_t& input, uint64_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + simpleIntegerCast(reinterpret_cast(input.getData()), input.len, + result, LogicalType{LogicalTypeID::UINT64}); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, uint32_t& result, common::ValueVector& /*resultVector*/) { - simpleIntegerCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::UINT32}); +inline void CastStringToTypes::operation(const ku_string_t& input, uint32_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + simpleIntegerCast(reinterpret_cast(input.getData()), input.len, + result, LogicalType{LogicalTypeID::UINT32}); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, uint16_t& result, common::ValueVector& /*resultVector*/) { - simpleIntegerCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::UINT16}); +inline void CastStringToTypes::operation(const ku_string_t& input, uint16_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + simpleIntegerCast(reinterpret_cast(input.getData()), input.len, + result, LogicalType{LogicalTypeID::UINT16}); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, uint8_t& result, common::ValueVector& /*resultVector*/) { - simpleIntegerCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::UINT8}); +inline void CastStringToTypes::operation(const ku_string_t& input, uint8_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + simpleIntegerCast(reinterpret_cast(input.getData()), input.len, + result, LogicalType{LogicalTypeID::UINT8}); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, float_t& result, common::ValueVector& /*resultVector*/) { - doubleCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::FLOAT}); +inline void CastStringToTypes::operation(const ku_string_t& input, float_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + doubleCast(reinterpret_cast(input.getData()), input.len, result, + LogicalType{LogicalTypeID::FLOAT}); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, double_t& result, common::ValueVector& /*resultVector*/) { - doubleCast((char*)input.getData(), input.len, result, - common::LogicalType{common::LogicalTypeID::DOUBLE}); +inline void CastStringToTypes::operation(const ku_string_t& input, double_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + doubleCast(reinterpret_cast(input.getData()), input.len, result, + LogicalType{LogicalTypeID::DOUBLE}); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, common::date_t& result, common::ValueVector& /*resultVector*/) { - result = common::Date::fromCString((const char*)input.getData(), input.len); +inline void CastStringToTypes::operation(const ku_string_t& input, date_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + result = Date::fromCString((const char*)input.getData(), input.len); } template<> -inline void CastStringToTypes::operation(common::ku_string_t& input, common::timestamp_t& result, - common::ValueVector& /*resultVector*/) { - result = common::Timestamp::fromCString((const char*)input.getData(), input.len); +inline void CastStringToTypes::operation(const ku_string_t& input, timestamp_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + result = Timestamp::fromCString((const char*)input.getData(), input.len); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, common::interval_t& result, common::ValueVector& /*resultVector*/) { - result = common::Interval::fromCString((const char*)input.getData(), input.len); +inline void CastStringToTypes::operation(const ku_string_t& input, interval_t& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { + result = Interval::fromCString((const char*)input.getData(), input.len); } template<> -inline void CastStringToTypes::operation( - common::ku_string_t& input, bool& result, common::ValueVector& /*resultVector*/) { +inline void CastStringToTypes::operation(const ku_string_t& input, bool& result, + ValueVector* /*resultVector*/, uint64_t /*rowToAdd*/, + const CSVReaderConfig* /*CSVReaderConfig*/) { castStringToBool(reinterpret_cast(input.getData()), input.len, result); } template<> -void CastStringToTypes::operation( - common::ku_string_t& input, common::blob_t& result, common::ValueVector& resultVector); - -template<> -void CastStringToTypes::operation( - common::ku_string_t& input, common::list_entry_t& result, common::ValueVector& resultVector); - -template<> -void CastStringToTypes::operation( - common::ku_string_t& input, common::map_entry_t& result, common::ValueVector& resultVector); - -template<> -void CastStringToTypes::operation( - common::ku_string_t& input, common::union_entry_t& result, common::ValueVector& resultVector); - -template<> -void CastStringToTypes::operation( - common::ku_string_t& input, common::struct_entry_t& result, common::ValueVector& resultVector); - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, common::int128_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - simpleInt128Cast(input, len, result); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, int32_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - simpleIntegerCast( - input, len, result, common::LogicalType{common::LogicalTypeID::INT32}); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, int16_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - simpleIntegerCast( - input, len, result, common::LogicalType{common::LogicalTypeID::INT16}); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, int8_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - simpleIntegerCast(input, len, result, common::LogicalType{common::LogicalTypeID::INT8}); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, uint64_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - simpleIntegerCast( - input, len, result, common::LogicalType{common::LogicalTypeID::UINT64}); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, uint32_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - simpleIntegerCast( - input, len, result, common::LogicalType{common::LogicalTypeID::UINT32}); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, uint16_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - simpleIntegerCast( - input, len, result, common::LogicalType{common::LogicalTypeID::UINT16}); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, uint8_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - simpleIntegerCast( - input, len, result, common::LogicalType{common::LogicalTypeID::UINT8}); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, float_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - doubleCast(input, len, result, common::LogicalType{common::LogicalTypeID::FLOAT}); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, double_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - doubleCast(input, len, result, common::LogicalType{common::LogicalTypeID::DOUBLE}); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, bool& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - castStringToBool(input, len, result); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, common::date_t& result, - common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - result = common::Date::fromCString(input, len); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, - common::timestamp_t& result, common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - result = common::Timestamp::fromCString(input, len); -} - -template<> -inline void CastStringToTypes::operation(const char* input, uint64_t len, - common::interval_t& result, common::ValueVector* /*vector*/, uint64_t /*rowToAdd*/, - const common::CSVReaderConfig* /*csvReaderConfig*/) { - result = common::Interval::fromCString(input, len); -} - -template<> -void CastStringToTypes::operation(const char* input, uint64_t len, common::blob_t& result, - common::ValueVector* vector, uint64_t rowToAdd, const common::CSVReaderConfig* csvReaderConfig); +void CastStringToTypes::operation(const ku_string_t& input, blob_t& result, + ValueVector* resultVector, uint64_t rowToAdd, const CSVReaderConfig* CSVReaderConfig); template<> -void CastStringToTypes::operation(const char* input, uint64_t len, - common::list_entry_t& result, common::ValueVector* vector, uint64_t rowToAdd, - const common::CSVReaderConfig* csvReaderConfig); +void CastStringToTypes::operation(const ku_string_t& input, list_entry_t& result, + ValueVector* resultVector, uint64_t rowToAdd, const CSVReaderConfig* CSVReaderConfig); template<> -void CastStringToTypes::operation(const char* input, uint64_t len, - common::map_entry_t& result, common::ValueVector* vector, uint64_t rowToAdd, - const common::CSVReaderConfig* csvReaderConfig); +void CastStringToTypes::operation(const ku_string_t& input, map_entry_t& result, + ValueVector* resultVector, uint64_t rowToAdd, const CSVReaderConfig* CSVReaderConfig); template<> -void CastStringToTypes::operation(const char* input, uint64_t len, - common::struct_entry_t& result, common::ValueVector* vector, uint64_t rowToAdd, - const common::CSVReaderConfig* csvReaderConfig); +void CastStringToTypes::operation(const ku_string_t& input, struct_entry_t& result, + ValueVector* resultVector, uint64_t rowToAdd, const CSVReaderConfig* CSVReaderConfig); template<> -void CastStringToTypes::operation(const char* input, uint64_t len, - common::union_entry_t& result, common::ValueVector* vector, uint64_t rowToAdd, - const common::CSVReaderConfig* csvReaderConfig); +void CastStringToTypes::operation(const ku_string_t& input, union_entry_t& result, + ValueVector* resultVector, uint64_t rowToAdd, const CSVReaderConfig* CSVReaderConfig); } // namespace function } // namespace kuzu diff --git a/src/include/function/cast/vector_cast_functions.h b/src/include/function/cast/vector_cast_functions.h index 394a5911ff1..24dcb040b52 100644 --- a/src/include/function/cast/vector_cast_functions.h +++ b/src/include/function/cast/vector_cast_functions.h @@ -87,13 +87,39 @@ struct CastFunction { } } + template + static void UnaryStringCastFunction( + const std::vector>& params, + common::ValueVector& result, const common::CSVReaderConfig* csvConfig) { + assert(params.size() == 1); + result.resetAuxiliaryBuffer(); + auto resultValues = (RESULT_TYPE*)result.getData(); + auto operand = params[0].get(); + + for (auto selectedPos = 0u; selectedPos < operand->state->selVector->selectedSize; + ++selectedPos) { + auto pos = result.state->selVector->selectedPositions[selectedPos]; + result.setNull(pos, operand->isNull(pos)); + if (!result.isNull(pos)) { + FUNC::template operation(((OPERAND_TYPE*)operand->getData())[pos], + resultValues[pos], &result, pos, csvConfig); + } + } + } + + template + static inline void UnaryStringCastExecFunction( + const std::vector>& params, + common::ValueVector& result) { + UnaryStringCastFunction(params, result, nullptr); + } + template inline static std::unique_ptr bindCastStringToFunction( const std::string& funcName, common::LogicalTypeID targetTypeID) { return std::make_unique(funcName, std::vector{common::LogicalTypeID::STRING}, targetTypeID, - ScalarFunction::UnaryStringExecFunction); + UnaryStringCastFunction); } }; @@ -171,5 +197,9 @@ struct CastToUInt8Function { static function_set getFunctionSet(); }; +struct CastAnyFunction { + static function_set getFunctionSet(); +}; + } // namespace function } // namespace kuzu diff --git a/src/include/function/function.h b/src/include/function/function.h index 62e0ba5cb4b..d16ac31532d 100644 --- a/src/include/function/function.h +++ b/src/include/function/function.h @@ -1,6 +1,7 @@ #pragma once #include "binder/expression/expression.h" +#include "common/copier_config/copier_config.h" #include "common/enums/expression_type.h" namespace kuzu { @@ -14,6 +15,13 @@ struct FunctionBindData { virtual ~FunctionBindData() = default; }; +struct StringCastFunctionBindData : public FunctionBindData { + common::CSVReaderConfig csvConfig; + + StringCastFunctionBindData(common::LogicalType dataType) + : FunctionBindData{std::move(dataType)} {} +}; + struct Function; using scalar_bind_func = std::function( const binder::expression_vector&, Function* definition)>; diff --git a/src/include/function/scalar_function.h b/src/include/function/scalar_function.h index afb98d8615e..6c0167cdbf2 100644 --- a/src/include/function/scalar_function.h +++ b/src/include/function/scalar_function.h @@ -20,35 +20,52 @@ using scalar_exec_func = std::function>&, common::SelectionVector&)>; using function_set = std::vector>; +using string_cast_func = + std::function>&, + common::ValueVector&, const common::CSVReaderConfig*)>; struct ScalarFunction : public BaseScalarFunction { ScalarFunction(std::string name, std::vector parameterTypeIDs, common::LogicalTypeID returnTypeID, scalar_exec_func execFunc, bool isVarLength = false) : ScalarFunction{std::move(name), std::move(parameterTypeIDs), returnTypeID, - std::move(execFunc), nullptr, nullptr, nullptr, isVarLength} {} + std::move(execFunc), nullptr, nullptr, nullptr, nullptr, isVarLength} {} + + ScalarFunction(std::string name, std::vector parameterTypeIDs, + common::LogicalTypeID returnTypeID, string_cast_func castFunc, bool isVarLength = false) + : ScalarFunction{std::move(name), std::move(parameterTypeIDs), returnTypeID, nullptr, + nullptr, nullptr, std::move(castFunc), nullptr, isVarLength} {} ScalarFunction(std::string name, std::vector parameterTypeIDs, common::LogicalTypeID returnTypeID, scalar_exec_func execFunc, scalar_select_func selectFunc, bool isVarLength = false) : ScalarFunction{std::move(name), std::move(parameterTypeIDs), returnTypeID, - std::move(execFunc), std::move(selectFunc), nullptr, nullptr, isVarLength} {} + std::move(execFunc), std::move(selectFunc), nullptr, nullptr, nullptr, isVarLength} {} ScalarFunction(std::string name, std::vector parameterTypeIDs, common::LogicalTypeID returnTypeID, scalar_exec_func execFunc, scalar_select_func selectFunc, scalar_bind_func bindFunc, bool isVarLength = false) : ScalarFunction{std::move(name), std::move(parameterTypeIDs), returnTypeID, - std::move(execFunc), std::move(selectFunc), nullptr, std::move(bindFunc), + std::move(execFunc), std::move(selectFunc), nullptr, nullptr, std::move(bindFunc), isVarLength} {} ScalarFunction(std::string name, std::vector parameterTypeIDs, common::LogicalTypeID returnTypeID, scalar_exec_func execFunc, scalar_select_func selectFunc, scalar_compile_func compileFunc, scalar_bind_func bindFunc, bool isVarLength = false) + : ScalarFunction{std::move(name), std::move(parameterTypeIDs), returnTypeID, + std::move(execFunc), std::move(selectFunc), std::move(compileFunc), nullptr, + std::move(bindFunc), isVarLength} {} + + ScalarFunction(std::string name, std::vector parameterTypeIDs, + common::LogicalTypeID returnTypeID, scalar_exec_func execFunc, + scalar_select_func selectFunc, scalar_compile_func compileFunc, string_cast_func castFunc, + scalar_bind_func bindFunc, bool isVarLength = false) : BaseScalarFunction{FunctionType::SCALAR, std::move(name), std::move(parameterTypeIDs), returnTypeID, std::move(bindFunc)}, execFunc{std::move(execFunc)}, selectFunc(std::move(selectFunc)), - compileFunc{std::move(compileFunc)}, isVarLength{isVarLength} {} + compileFunc{std::move(compileFunc)}, castFunc{std::move(castFunc)}, isVarLength{ + isVarLength} {} template static void TernaryExecFunction(const std::vector>& params, @@ -136,6 +153,7 @@ struct ScalarFunction : public BaseScalarFunction { scalar_exec_func execFunc; scalar_select_func selectFunc; scalar_compile_func compileFunc; + string_cast_func castFunc; // Currently we only one variable-length function which is list creation. The expectation is // that all parameters must have the same type as parameterTypes[0]. bool isVarLength; diff --git a/src/include/storage/in_mem_storage_structure/in_mem_column_chunk.h b/src/include/storage/in_mem_storage_structure/in_mem_column_chunk.h index 0700a2135a7..7b6017afc2a 100644 --- a/src/include/storage/in_mem_storage_structure/in_mem_column_chunk.h +++ b/src/include/storage/in_mem_storage_structure/in_mem_column_chunk.h @@ -53,7 +53,7 @@ class InMemColumnChunk { template void setValueFromString(const char* value, uint64_t length, common::offset_t pos) { T val; - function::CastStringToTypes::operation(value, length, val); + function::CastStringToTypes::operation(ku_string_t{value, length}, val); setValue(val, pos); } diff --git a/src/include/storage/in_mem_storage_structure/in_mem_lists.h b/src/include/storage/in_mem_storage_structure/in_mem_lists.h index c21f3b70621..c90f9fa7007 100644 --- a/src/include/storage/in_mem_storage_structure/in_mem_lists.h +++ b/src/include/storage/in_mem_storage_structure/in_mem_lists.h @@ -51,7 +51,7 @@ class InMemLists { void setValueFromString( common::offset_t nodeOffset, uint64_t pos, const char* val, uint64_t length) { T result; - function::CastStringToTypes::operation(val, length, result); + function::CastStringToTypes::operation(ku_string_t{val, length}, result); setValue(nodeOffset, pos, (uint8_t*)&result); } diff --git a/src/parser/transform/transform_expression.cpp b/src/parser/transform/transform_expression.cpp index 9c1ad4cac50..f45829c6bcc 100644 --- a/src/parser/transform/transform_expression.cpp +++ b/src/parser/transform/transform_expression.cpp @@ -579,13 +579,14 @@ std::string Transformer::transformPropertyKeyName(CypherParser::OC_PropertyKeyNa std::unique_ptr Transformer::transformIntegerLiteral( CypherParser::OC_IntegerLiteralContext& ctx) { auto text = ctx.DecimalInteger()->getText(); + ku_string_t literal{text.c_str(), text.length()}; int64_t result; - if (function::CastStringToTypes::tryCast(text.c_str(), text.length(), result)) { + if (function::CastStringToTypes::tryCast(literal, result)) { return std::make_unique( std::make_unique(result), ctx.getText()); } int128_t result128; - function::CastStringToTypes::operation(text.c_str(), text.length(), result128); + function::CastStringToTypes::operation(literal, result128); return std::make_unique( std::make_unique(result128), ctx.getText()); } @@ -593,8 +594,9 @@ std::unique_ptr Transformer::transformIntegerLiteral( std::unique_ptr Transformer::transformDoubleLiteral( CypherParser::OC_DoubleLiteralContext& ctx) { auto text = ctx.RegularDecimalReal()->getText(); + ku_string_t literal{text.c_str(), text.length()}; double_t result; - function::CastStringToTypes::operation(text.c_str(), text.length(), result); + function::CastStringToTypes::operation(literal, result); return std::make_unique( std::make_unique(result), ctx.getText()); } diff --git a/test/test_files/demo_db/demo_db_create.test b/test/test_files/demo_db/demo_db_create.test index 4f99d047b37..1db5f6f0e15 100644 --- a/test/test_files/demo_db/demo_db_create.test +++ b/test/test_files/demo_db/demo_db_create.test @@ -6,6 +6,12 @@ -CASE CreateNodeFromFile -STATEMENT CREATE NODE TABLE User2(name STRING, age INT64, PRIMARY KEY (name)); ---- ok +-STATEMENT LOAD FROM "${KUZU_ROOT_DIRECTORY}/dataset/demo-db/csv/user.csv" RETURN cast(column1, "int64"); +---- 4 +2 +3 +34 +4 -STATEMENT LOAD FROM "${KUZU_ROOT_DIRECTORY}/dataset/demo-db/csv/user.csv" CREATE (:User2{name: column0, age: to_int64(column1)}); ---- ok diff --git a/test/test_files/tinysnb/cast/cast_error.test b/test/test_files/tinysnb/cast/cast_error.test index 21b15136309..f1858328c0d 100644 --- a/test/test_files/tinysnb/cast/cast_error.test +++ b/test/test_files/tinysnb/cast/cast_error.test @@ -9,89 +9,140 @@ -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return to_int64(e.code); ---- error Overflow exception: Value 9223372036854775808 is not within INT64 range +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return cast(e.code, "int64"); +---- error +Overflow exception: Value 9223372036854775808 is not within INT64 range -LOG CastUint64ToInt32OutOfRange -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return to_int32(e.code); ---- error Overflow exception: Value 9223372036854775808 is not within INT32 range +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return cast(e.code, "int32"); +---- error +Overflow exception: Value 9223372036854775808 is not within INT32 range --LOG CastInt64ToInt32OutOfRange +-LOG CastStringToInt32OutOfRange -STATEMENT return to_int32("2147483648"); ---- error Conversion exception: Cast failed. 2147483648 is not in INT32 range. +-STATEMENT return cast("2147483648", "int32"); +---- error +Conversion exception: Cast failed. 2147483648 is not in INT32 range. -LOG CastUint32ToInt32OutOfRange -STATEMENT return to_int32(to_uint32(4294967295)); ---- error Overflow exception: Value 4294967295 is not within INT32 range +-STATEMENT return cast(cast(4294967295, "uint32"), "int32"); +---- error +Overflow exception: Value 4294967295 is not within INT32 range -LOG CastUint64ToInt16OutOfRange -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return to_int16(e.code); ---- error Overflow exception: Value 9223372036854775808 is not within INT16 range +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return cast(e.code, "int16"); +---- error +Overflow exception: Value 9223372036854775808 is not within INT16 range -LOG CastInt64ToInt16OutOfRange -STATEMENT RETURN to_int16("32768"); ---- error Conversion exception: Cast failed. 32768 is not in INT16 range. +-STATEMENT RETURN cast("32768", "int16"); +---- error +Conversion exception: Cast failed. 32768 is not in INT16 range. -LOG CastUint32ToInt16OutOfRange -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return to_int16(e.temprature); ---- error Overflow exception: Value 32800 is not within INT16 range +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return cast(e.temprature, "int16"); +---- error +Overflow exception: Value 32800 is not within INT16 range -LOG CastInt32ToInt16OutOfRange -STATEMENT RETURN to_int16(to_int32("-32770")); ---- error Overflow exception: Value -32770 is not within INT16 range +-STATEMENT RETURN cast(cast("-32770", "int32"), "int16"); +---- error +Overflow exception: Value -32770 is not within INT16 range -LOG CastUint16ToInt16OutOfRange -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return to_int16(e.ulength); ---- error Overflow exception: Value 33768 is not within INT16 range +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return cast(e.ulength, "int16"); +---- error +Overflow exception: Value 33768 is not within INT16 range -LOG CastUint64ToInt8OutOfRange -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return to_int8(e.code); ---- error Overflow exception: Value 9223372036854775808 is not within INT8 range +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return cast(e.code, "int8"); +---- error +Overflow exception: Value 9223372036854775808 is not within INT8 range -LOG CastInt64ToInt8OutOfRange -STATEMENT return to_int8(-1000); ---- error Overflow exception: Value -1000 is not within INT8 range +-STATEMENT return cast(-1000, "int8"); +---- error +Overflow exception: Value -1000 is not within INT8 range -LOG CastUint32ToInt8OutOfRange -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return to_int8(e.temprature); ---- error Overflow exception: Value 32800 is not within INT8 range +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return cast(e.temprature, "int8"); +---- error +Overflow exception: Value 32800 is not within INT8 range -LOG CastInt32ToInt8OutOfRange -STATEMENT return to_int8(to_int32(1000)); ---- error Overflow exception: Value 1000 is not within INT8 range +-STATEMENT return cast(cast(1000, "int32"), "int8"); +---- error +Overflow exception: Value 1000 is not within INT8 range -LOG CastUint16ToInt8OutOfRange -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return to_int8(e.ulength); ---- error Overflow exception: Value 33768 is not within INT8 range +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return cast(e.ulength, "int8"); +---- error +Overflow exception: Value 33768 is not within INT8 range -LOG CastInt16ToInt8OutOfRange -STATEMENT return to_int8(to_int16(520)); ---- error Overflow exception: Value 520 is not within INT8 range +-STATEMENT return cast(cast(520, "int16"), "int8"); +---- error +Overflow exception: Value 520 is not within INT8 range -LOG CastUint8ToInt8OutOfRange -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return to_int8(e.ulevel); ---- error Overflow exception: Value 250 is not within INT8 range +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) return cast(e.ulevel, "int8"); +---- error +Overflow exception: Value 250 is not within INT8 range -LOG CastInt64ToUint64OutOfRange -STATEMENT return to_uint64(-500); ---- error Overflow exception: Value -500 is not within UINT64 range +-STATEMENT return cast(-500, "uint64"); +---- error +Overflow exception: Value -500 is not within UINT64 range -LOG CastInt32ToUint64OutOfRange --STATEMENT return to_uint64(to_int32(-1024)); +-STATEMENT return cast(cast(-1024, "int32"), "uint64"); ---- error Overflow exception: Value -1024 is not within UINT64 range @@ -99,69 +150,99 @@ Overflow exception: Value -1024 is not within UINT64 range -STATEMENT return to_uint64(to_int16(-1)); ---- error Overflow exception: Value -1 is not within UINT64 range +-STATEMENT return cast(cast(-1, "int16"), "uint64"); +---- error +Overflow exception: Value -1 is not within UINT64 range -LOG CastInt8ToUint64OutOfRange -STATEMENT return to_uint64(to_int8(-2)); ---- error Overflow exception: Value -2 is not within UINT64 range +-STATEMENT return cast(cast(-2, "int8"), "uint64"); +---- error +Overflow exception: Value -2 is not within UINT64 range -LOG CastUint64ToUint32OutOfRange -STATEMENT return to_uint32(to_uint64(922337203685477580)); ---- error Overflow exception: Value 922337203685477580 is not within UINT32 range +-STATEMENT return cast(cast(922337203685477580, "uint64"), "uint32"); +---- error +Overflow exception: Value 922337203685477580 is not within UINT32 range -LOG CastInt64ToUint32OutOfRange -STATEMENT return to_uint32(9223372036854775807); ---- error Overflow exception: Value 9223372036854775807 is not within UINT32 range +-STATEMENT return cast(9223372036854775807, "uint32"); +---- error +Overflow exception: Value 9223372036854775807 is not within UINT32 range -LOG CastInt32ToUint32OutOfRange -STATEMENT return to_uint32(to_int32("-10244")); ---- error Overflow exception: Value -10244 is not within UINT32 range +-STATEMENT return cast(cast("-10244", "int32"), "uint32"); +---- error +Overflow exception: Value -10244 is not within UINT32 range -LOG CastInt16ToUint32OutOfRange -STATEMENT return to_uint32(to_int16(-100)); ---- error Overflow exception: Value -100 is not within UINT32 range +-STATEMENT return cast(cast(-100, "int16"), "uint32"); +---- error +Overflow exception: Value -100 is not within UINT32 range -LOG CastInt8ToUint32OutOfRange -STATEMENT return to_uint32(to_int8(-110)); ---- error Overflow exception: Value -110 is not within UINT32 range +-STATEMENT return cast(cast(-110, "int8"), "uint32"); +---- error +Overflow exception: Value -110 is not within UINT32 range -LOG CastUint64ToUint16OutOfRange -STATEMENT return to_uint16(to_uint64(922337203685477580)); ---- error Overflow exception: Value 922337203685477580 is not within UINT16 range +-STATEMENT return cast(cast(922337203685477580, "uint64"), "uint16"); +---- error +Overflow exception: Value 922337203685477580 is not within UINT16 range -LOG CastInt64ToUint16OutOfRange -STATEMENT return to_uint16("922337203685"); ---- error Conversion exception: Cast failed. 922337203685 is not in UINT16 range. +-STATEMENT return cast("922337203685", "uint16"); +---- error +Conversion exception: Cast failed. 922337203685 is not in UINT16 range. -LOG CastUint32ToUint16OutOfRange -STATEMENT return to_uint16(to_uint32(65536)); ---- error Overflow exception: Value 65536 is not within UINT16 range +-STATEMENT return cast(cast(65536, "uint32"), "uint16"); +---- error +Overflow exception: Value 65536 is not within UINT16 range -LOG CastInt32ToUint16OutOfRange --STATEMENT return to_uint16(to_int32("-10244")); +-STATEMENT return cast(cast("-10244", "int32"), "uint16"); ---- error Overflow exception: Value -10244 is not within UINT16 range -LOG CastInt16ToUint16OutOfRange --STATEMENT return to_uint16(to_int16(-100)); +-STATEMENT return cast(cast(-100, "int16"), "uint16"); ---- error Overflow exception: Value -100 is not within UINT16 range -LOG CastInt8ToUint16OutOfRange --STATEMENT return to_uint16(to_int8(-110)); +-STATEMENT return cast(cast(-110, "int8"), "uint16"); ---- error Overflow exception: Value -110 is not within UINT16 range -LOG CastUint64ToUint8OutOfRange --STATEMENT return to_uint8(to_uint64(922337203685477580)); +-STATEMENT return cast(cast(922337203685477580, "uint64"), "uint8"); ---- error Overflow exception: Value 922337203685477580 is not within UINT8 range @@ -169,41 +250,68 @@ Overflow exception: Value 922337203685477580 is not within UINT8 range -STATEMENT return to_uint8(257); ---- error Overflow exception: Value 257 is not within UINT8 range +-STATEMENT return cast(257, "uint8"); +---- error +Overflow exception: Value 257 is not within UINT8 range -LOG CastUint32ToUint8OutOfRange -STATEMENT return to_uint8(to_uint32(300)); ---- error Overflow exception: Value 300 is not within UINT8 range +-STATEMENT return cast(cast(300, "uint32"), "uint8"); +---- error +Overflow exception: Value 300 is not within UINT8 range -LOG CastInt32ToUint8OutOfRange -STATEMENT return to_uint8(to_int32("-10244")); ---- error Overflow exception: Value -10244 is not within UINT8 range +-STATEMENT return cast(cast("-10244", "int32"), "uint8"); +---- error +Overflow exception: Value -10244 is not within UINT8 range -LOG CastUint16ToUint8OutOfRange -STATEMENT return to_uint8(to_uint16(312)); ---- error Overflow exception: Value 312 is not within UINT8 range +-STATEMENT return cast(cast(312, "uint16"), "uint8"); +---- error +Overflow exception: Value 312 is not within UINT8 range + -LOG CastInt16ToUint8OutOfRange -STATEMENT return to_uint8(to_int16(-100)); ---- error Overflow exception: Value -100 is not within UINT8 range +-STATEMENT return cast(cast(-100, "int16"), "uint8"); +---- error +Overflow exception: Value -100 is not within UINT8 range -LOG CastInt8ToUint8OutOfRange -STATEMENT return to_uint8(to_int8(-3)); ---- error Overflow exception: Value -3 is not within UINT8 range +-STATEMENT return cast(cast(-3, "int8"), "uint8"); +---- error +Overflow exception: Value -3 is not within UINT8 range -LOG CastDoubleToInt64OutOfRange -STATEMENT return to_int64(9223372038854775807.452313); ---- error Overflow exception: Value 9223372038854774784.000000 is not within INT64 range +-STATEMENT return cast(9223372038854775807.452313, "int64"); +---- error +Overflow exception: Value 9223372038854774784.000000 is not within INT64 range -LOG CastFloatToUint8OutOfRange -STATEMENT return to_uint8(-728.923); ---- error Overflow exception: Value -728.923000 is not within UINT8 range +-STATEMENT return cast(-728.923, "uint8"); +---- error +Overflow exception: Value -728.923000 is not within UINT8 range + +-LOG CastStringOutoOfRange -STATEMENT RETURN TO_INT32("2147483648"); ---- error Conversion exception: Cast failed. 2147483648 is not in INT32 range. @@ -276,29 +384,116 @@ Conversion exception: Value fasdf is not a valid boolean -STATEMENT RETURN TO_BOOL("fal"); ---- error Conversion exception: Value fal is not a valid boolean +-STATEMENT RETURN cast("2147483648", "int32"); +---- error +Conversion exception: Cast failed. 2147483648 is not in INT32 range. +-STATEMENT RETURN cast("-2147483649", "int32"); +---- error +Conversion exception: Cast failed. -2147483649 is not in INT32 range. +-STATEMENT RETURN cast("32768", "int16"); +---- error +Conversion exception: Cast failed. 32768 is not in INT16 range. +-STATEMENT RETURN cast("-32769", "int16"); +---- error +Conversion exception: Cast failed. -32769 is not in INT16 range. +-STATEMENT RETURN cast("128", "int8"); +---- error +Conversion exception: Cast failed. 128 is not in INT8 range. +-STATEMENT RETURN cast("-129", "int8"); +---- error +Conversion exception: Cast failed. -129 is not in INT8 range. +-STATEMENT RETURN cast("-1", "uint64"); +---- error +Conversion exception: Cast failed. -1 is not in UINT64 range. +-STATEMENT RETURN cast("4294967296", "uint32"); +---- error +Conversion exception: Cast failed. 4294967296 is not in UINT32 range. +-STATEMENT RETURN cast("65536", "uint16"); +---- error +Conversion exception: Cast failed. 65536 is not in UINT16 range. +-STATEMENT RETURN cast("256", "uint8"); +---- error +Conversion exception: Cast failed. 256 is not in UINT8 range. +-STATEMENT RETURN cast("-1", "uint8"); +---- error +Conversion exception: Cast failed. -1 is not in UINT8 range. +-STATEMENT RETURN cast(170141183460469231731687303715884105728, "int128"); +---- error +Conversion exception: Cast failed. 170141183460469231731687303715884105728 is not within INT128 range. +-STATEMENT RETURN cast("-170141183460469231731687303715884105728", "int128"); +---- error +Conversion exception: Cast failed. -170141183460469231731687303715884105728 is not within INT128 range. +-STATEMENT RETURN TO_UINT8(cast(-1, "int128")); +---- error +Overflow exception: Cast failed. Cannot cast -1 to unsigned type. +-STATEMENT RETURN TO_UINT16(cast(-10, "int128")); +---- error +Overflow exception: Cast failed. Cannot cast -10 to unsigned type. +-STATEMENT RETURN TO_UINT64(cast(-15, "int128")); +---- error +Overflow exception: Cast failed. Cannot cast -15 to unsigned type. +-STATEMENT RETURN cast(170141183460469231731687303715884105727, "int128") + cast(10, "int128"); +---- error +Overflow exception: INT128 is out of range: cannot add. +-STATEMENT RETURN cast(-170141183460469231731687303715884105727, "int128") + cast(-10, "int128"); +---- error +Overflow exception: INT128 is out of range: cannot add. +-STATEMENT RETURN cast(170141183460469231731687303715884105727, "int128") - cast(-10, "int128"); +---- error +Overflow exception: INT128 is out of range: cannot subtract. +-STATEMENT RETURN cast(-170141183460469231731687303715884105727, "int128") - cast(10, "int128"); +---- error +Overflow exception: INT128 is out of range: cannot subtract. +-STATEMENT RETURN cast(-170141183460469231731687303715884105727, "int128") * cast(10, "int128"); +---- error +Overflow exception: INT128 is out of range: cannot multiply. +-STATEMENT RETURN cast(170141183460469231731687303715884105727, "int128") * cast(10, "int128"); +---- error +Overflow exception: INT128 is out of range: cannot multiply. +-STATEMENT RETURN cast("fasdf", "boolean"); +---- error +Conversion exception: Value fasdf is not a valid boolean +-STATEMENT RETURN cast("fal", "boolean"); +---- error +Conversion exception: Value fal is not a valid boolean -LOG SerialOutOfRange -STATEMENT RETURN TO_SERIAL(TO_UINT64(9223372036854775807) * TO_UINT64(2)); ---- error Overflow exception: Value 18446744073709551614 is not within INT64 range +-STATEMENT RETURN cast(cast(9223372036854775807, "uint64") * cast(2, "uint64"), "serial"); +---- error +Overflow exception: Value 18446744073709551614 is not within INT64 range -STATEMENT RETURN TO_SERIAL(TO_UINT64(18446744073709551615)); ---- error Overflow exception: Value 18446744073709551615 is not within INT64 range +-STATEMENT RETURN cast(cast(18446744073709551615, "uint64"), "serial"); +---- error +Overflow exception: Value 18446744073709551615 is not within INT64 range -LOG NonAsciiStringToBlob -STATEMENT RETURN BLOB('😀') ---- error Conversion exception: Invalid byte encountered in STRING -> BLOB conversion. All non-ascii characters must be escaped with hex codes (e.g. \xAA) +-STATEMENT RETURN cast('😀', "blob") +---- error +Conversion exception: Invalid byte encountered in STRING -> BLOB conversion. All non-ascii characters must be escaped with hex codes (e.g. \xAA) -LOG StringToBlobInvalidHexCode -STATEMENT RETURN BLOB('\\xgg') ---- error Conversion exception: Invalid hex escape code encountered in string -> blob conversion: \xgg +-STATEMENT RETURN cast('\\xgg', "blob") +---- error +Conversion exception: Invalid hex escape code encountered in string -> blob conversion: \xgg -LOG StringToBlobUnterminatedHexCode -STATEMENT RETURN BLOB('\\xa') ---- error Conversion exception: Invalid hex escape code encountered in string -> blob conversion: unterminated escape code at end of string +-STATEMENT RETURN cast('\\xa', "blob") +---- error +Conversion exception: Invalid hex escape code encountered in string -> blob conversion: unterminated escape code at end of string -LOG INT128CastToError -STATEMENT RETURN TO_SERIAL(TO_INT128(18446744073709551615)); @@ -328,3 +523,59 @@ Overflow exception: Value 18446744073709551615 is not within UINT16 range -STATEMENT RETURN TO_UINT8(TO_INT128(256)); ---- error Overflow exception: Value 256 is not within UINT8 range +-STATEMENT RETURN cast(cast(18446744073709551615, "int128"), "serial"); +---- error +Overflow exception: Value 18446744073709551615 is not within INT64 range +-STATEMENT RETURN cast(cast(18446744073709551615, "int128"), "int64"); +---- error +Overflow exception: Value 18446744073709551615 is not within INT64 range +-STATEMENT RETURN cast(cast(18446744073709551615, "int128"), "int32"); +---- error +Overflow exception: Value 18446744073709551615 is not within INT32 range +-STATEMENT RETURN cast(cast(18446744073709551615, "int128"), "int16"); +---- error +Overflow exception: Value 18446744073709551615 is not within INT16 range +-STATEMENT RETURN cast(cast(18446744073709551615, "int128"), "int8"); +---- error +Overflow exception: Value 18446744073709551615 is not within INT8 range +-STATEMENT RETURN cast(cast(184467440737095516151, "int128"), "uint64"); +---- error +Overflow exception: Value 184467440737095516151 is not within UINT64 range +-STATEMENT RETURN cast(cast(18446744073709551615, "int128"), "uint32"); +---- error +Overflow exception: Value 18446744073709551615 is not within UINT32 range +-STATEMENT RETURN cast(cast(18446744073709551615, "int128"), "uint16"); +---- error +Overflow exception: Value 18446744073709551615 is not within UINT16 range +-STATEMENT RETURN cast(cast(256, "int128"), "uint8"); +---- error +Overflow exception: Value 256 is not within UINT8 range + +-LOG CastFunctionError +-STATEMENT RETURN cast('T', "bool") +---- error +Cannot parse dataTypeID: BOOL +-STATEMENT RETURN cast("fd", "UNION(uint64, int128, BOOLEAN)"); +---- error +Conversion exception: Could not convert to union type UNION(uint64:UINT64, int128:INT128, BOOLEAN:BOOL): fd. +-STATEMENT RETURN cast("{a: 432412343242343241432432444444444444443244}", "STRUCT(a INT128)"); +---- error +Conversion exception: Cast failed. 432412343242343241432432444444444444443244 is not within INT128 range. +-STATEMENT RETURN cast("{a}", "STRUCT(a INT128)"); +---- error +Conversion exception: Cast failed. {a} is not in STRUCT(a:INT128) range. +-STATEMENT RETURN cast("{-12=34}", "MAP(UINT8, UINT16)"); +---- error +Conversion exception: Cast failed. -12 is not in UINT8 range. +-STATEMENT RETURN cast("{} fsdf", "MAP(UINT8, UINT16)"); +---- error +Conversion exception: Cast failed. {} fsdf is not in MAP(UINT8: UINT16) range. +-STATEMENT RETURN cast("[[432343243243254534554654654654234,2,3]]", "INT64[][]"); +---- error +Conversion exception: Cast failed. 432343243243254534554654654654234 is not in INT64 range. +-STATEMENT RETURN cast("[bool]", "INT64[][]"); +---- error +Conversion exception: Cast failed. bool is not in INT64[] range. +-STATEMENT RETURN cast("[[231|4324]", "INT64[][]"); +---- error +Conversion exception: Cast failed. [[231|4324] is not in INT64[][] range. diff --git a/test/test_files/tinysnb/cast/cast_string_to_nested_types.test b/test/test_files/tinysnb/cast/cast_string_to_nested_types.test index 141c30edf3e..19b905f44cd 100644 --- a/test/test_files/tinysnb/cast/cast_string_to_nested_types.test +++ b/test/test_files/tinysnb/cast/cast_string_to_nested_types.test @@ -168,3 +168,74 @@ Copy exception: Invalid UTF8-encoded string. -STATEMENT LOAD WITH HEADERS (a UNION(v1 INT64, v2 INT32)) FROM "${KUZU_ROOT_DIRECTORY}/dataset/load-from-test/union/union_error.csv" RETURN *; ---- error Conversion exception: Could not convert to union type UNION(v1:INT64, v2:INT32): fdsaf. + +-LOG CastToVarList +-STATEMENT RETURN cast(" [ [ 1 ,3, 423 , 124,43242 ] , [ 432 ]]", "INT64[][]"); +---- 1 +[[1,3,423,124,43242],[432]] +-STATEMENT RETURN cast("[[], [], [] ]", "INT64[][]"); +---- 1 +[[],[],[]] +-STATEMENT RETURN cast("[null, null, [1, 3] ]", "INT64[][]"); +---- 1 +[,,[1,3]] +-STATEMENT RETURN cast("[[null, null], [5, null], [1, 3] ]", "INT64[][]"); +---- 1 +[[,],[5,],[1,3]] +-STATEMENT RETURN cast("[[1,2,3]]", "INT64[][]"); +---- 1 +[[1,2,3]] +-STATEMENT RETURN cast("[this, is, a, list, of, string]", "STRING[]"); +---- 1 +[this, is, a, list, of, string] +-STATEMENT RETURN cast("[x\', ,\',6]", "STRING[]"); +---- 1 +[x', ,',6] +-STATEMENT RETURN cast("[1,2,3]", "UINT8[]"), cast("[1,2,3]", "UINT16[]"), cast("[1,2,3]", "UINT32[]"), cast("[1,2,3]", "UINT64[]"), cast("[1,2,3]", "INT8[]"), cast("[1,2,3]", "INT16[]"), cast("[1,2,3]", "INT32[]"), cast("[1,2,3]", "INT64[]"), cast("[1,2,3]", "INT128[]"), cast("[1,2,3]", "FLOAT[]"), cast("[1,2,3]", "DOUBLE[]"); +---- 1 +[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1,2,3]|[1.000000,2.000000,3.000000]|[1.000000,2.000000,3.000000] +# timestamp/date/... list + +-LOG CastToMap +-STATEMENT RETURN cast(" { c= {a = 3423 }, b = { g = 3421 } } ", "MAP(STRING, MAP(STRING, INT16))"), cast("{}", "MAP(STRING, MAP(STRING, INT16))"), cast("{d = {}}", "MAP(STRING, MAP(STRING, INT16))"); +---- 1 +{c={a=3423}, b={g=3421}}|{}|{d={}} +-STATEMENT LOAD WITH HEADERS (map STRING) FROM "${KUZU_ROOT_DIRECTORY}/dataset/load-from-test/map/nested_map2.csv" RETURN cast(map, "MAP(MAP(INT64, INT64), MAP(STRING, STRING))"); +---- 7 +{{18046=18046, 2=321, 3=423421}={dJ3cf6Y=dJ3cf6Y, dJ3cf6Y=dJ3cf6Y, dJ3cf6Y=dJ3cf6Y}} +{{30099=30099, 1=0}={3mTEf=3mTEf, 3mTEf=3mTEf, 3mTEf=3mTEf}} +{{31395=31395}={}} +{{}={YQcmYouhyFqD3y=YQcmYouhyFqD3y, YQcmYouhyFqD3y=YQcmYouhyFqD3y, YQcmYouhyFqD3y=YQcmYouhyFqD3y}} +{{}={}} +{=} +{} +-STATEMENT RETURN cast("{[432,24,12]={c: okay}, [90,11,43,54]={c: bad}, [0]={c: good}}", "MAP(INT32[], STRUCT(c STRING))"), cast("{[1000,143245,432,43241,-43214324,-432423,-4324324,-41412,-131242143,0,-4324324,0]={}}", "MAP(INT32[], STRUCT(c STRING))"); +---- 1 +{[432,24,12]={c: okay}, [90,11,43,54]={c: bad}, [0]={c: good}}|{[1000,143245,432,43241,-43214324,-432423,-4324324,-41412,-131242143,0,-4324324,0]={c: }} +-STATEMENT RETURN cast("{12=34}", "MAP(UINT8, UINT16)"), cast("{12=}", "MAP(UINT32, UINT64)"), cast("{=43}", "MAP(INT8, INT16)"), cast("{=}", "MAP(INT32, INT64)"), cast("{12=12}", "MAP(float, double)"); +---- 1 +{12=34}|{12=}|{=43}|{=}|{12.000000=12.000000} + +-LOG CastToStruct +-STATEMENT RETURN cast("{ a : { b : {c:[ 3432423 ,-43423 ,31231 ,NULL ]} } } ", "STRUCT(a STRUCT(b STRUCT(c INT64[])))"), cast("{ a : { b : {c:[]} } } ", "STRUCT(a STRUCT(b STRUCT(c INT64[])))"), cast("{ a : { b : {c:} } } ", "STRUCT(a STRUCT(b STRUCT(c INT64[])))"), cast("{ a : { b : {} } } ", "STRUCT(a STRUCT(b STRUCT(c INT64[])))"), cast("{ a : {} } ", "STRUCT(a STRUCT(b STRUCT(c INT64[])))"), cast("{} ", "STRUCT(a STRUCT(b STRUCT(c INT64[])))"); +---- 1 +{a: {b: {c: [3432423,-43423,31231,]}}}|{a: {b: {c: []}}}|{a: {b: {c: }}}|{a: {b: {c: []}}}|{a: {b: {c: []}}}|{a: {b: {c: []}}} +-STATEMENT RETURN cast("{a: 432412343242343241}", "STRUCT(a INT128)"), cast("{a: -44332412343242343241}", "STRUCT(a INT128)"); +---- 1 +{a: 432412343242343241}|{a: -44332412343242343241} + +-LOG CastToUnion +-STATEMENT RETURN cast("324324", "UNION(uint64, int128, BOOLEAN)"), cast("-324324", "UNION(uint64, int128, BOOLEAN)"), cast("false", "UNION(uint64, int128, BOOLEAN)"); +---- 1 +324324|-324324|False +-STATEMENT LOAD WITH HEADERS (u STRING, u2 STRING, u3 STRING, u4 STRING, u5 STRING) FROM "${KUZU_ROOT_DIRECTORY}/dataset/load-from-test/union/union_correct1.csv" RETURN cast(u, "UNION(v1 INT64, v2 BOOLEAN)"), cast(u2, "UNION(v1 UINT32, v2 INT32, v3 DATE)"), cast(u3, "UNION(v1 UINT8, v2 UINT16, v3 TIMESTAMP, v4 FLOAT)"), cast(u4, "UNION(v1 UINT64, v2 INT8, v3 INT16, v4 DOUBLE)"), cast(u5, "UNION(v1 STRING)"); +---- 9 +False|34|255|18446744073709551615|fsdfa +True|432|0|-1.432415|543fasf +34234|4294967295|65535|-128|432 +-42342345|-1|-1.000000|-129|fasf +True|2022-06-06|4324.123047|-32768|fds +True|2019-03-19|-12.343200|32768| +1|-2147483648|1970-01-01 10:00:00.004666|-32769.000000|fsdxcv +0|0|2014-05-12 12:11:59|4324254534123134124032.000000|fsaf +False|-4325|14|18446744073709551616.000000| dfsa diff --git a/test/test_files/tinysnb/function/cast.test b/test/test_files/tinysnb/function/cast.test index 2fe57a96eb4..3b2a3b1a65d 100644 --- a/test/test_files/tinysnb/function/cast.test +++ b/test/test_files/tinysnb/function/cast.test @@ -9,47 +9,74 @@ -STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=0 RETURN to_int16(e.level), to_int32(e.level), to_int64(e.level), to_int128(e.level), to_float(e.level), to_double(e.level) ---- 1 5|5|5|5|5.000000|5.000000 +-STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=0 RETURN CAST(e.level, "INT16"), CAST(e.level, "INT32"), CAST(e.level, "INT64"), CAST(e.level, "INT128"), CAST(e.level, "FLOAT"), CAST(e.level, "DOUBLE") +---- 1 +5|5|5|5|5.000000|5.000000 -LOG CastFromINT16 -STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=0 RETURN to_int8(e.length), to_int32(e.length), to_int64(e.length), to_int128(e.length), to_float(e.length), to_double(e.length) ---- 1 5|5|5|5|5.000000|5.000000 +-STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=0 RETURN cast(e.length, "int8"), cast(e.length, "int32"), cast(e.length, "int64"), cast(e.length, "int128"), cast(e.length, "float"), cast(e.length, "double") +---- 1 +5|5|5|5|5.000000|5.000000 -LOG CastFromINT32 -STATEMENT MATCH (m:movies) WHERE m.name='Roma' RETURN to_int16(m.length), to_int64(m.length), to_int128(m.length), to_float(m.length), to_double(m.length) ---- 1 298|298|298|298.000000|298.000000 +-STATEMENT MATCH (m:movies) WHERE m.name='Roma' RETURN cast(m.length, "int16"), cast(m.length, "int64"), cast(m.length, "int128"), cast(m.length, "float"), cast(m.length, "double") +---- 1 +298|298|298|298.000000|298.000000 -LOG CastFromINT64 -STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN to_int8(p.gender), to_int16(p.gender), to_int32(p.gender), to_int128(p.gender), to_float(p.gender), to_double(p.gender) ---- 1 1|1|1|1|1.000000|1.000000 +-STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN cast(p.gender, "int8"), cast(p.gender, "int16"), cast(p.gender, "int32"), cast(p.gender, "int64"), cast(p.gender, "int128"), cast(p.gender, "float"), cast(p.gender, "double") +---- 1 +1|1|1|1|1|1.000000|1.000000 -LOG CastFromUINT8 -STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=0 RETURN to_int16(e.ulevel), to_int32(e.ulevel), to_int64(e.ulevel), to_int128(e.ulevel), to_float(e.ulevel), to_double(e.ulevel) ---- 1 250|250|250|250|250.000000|250.000000 +-STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=0 RETURN cast(e.ulevel, "int16"), cast(e.ulevel, "int32"), cast(e.ulevel, "int64"), cast(e.ulevel, "int128"), cast(e.ulevel, "float"), cast(e.ulevel, "double") +---- 1 +250|250|250|250|250.000000|250.000000 -LOG CastFromUINT16 -STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=2 RETURN to_int8(e.ulength), to_int32(e.ulength), to_int64(e.ulength), to_int128(e.ulength), to_float(e.ulength), to_double(e.ulength) ---- 1 90|90|90|90|90.000000|90.000000 +-STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=2 RETURN cast(e.ulength, "uint8"), cast(e.ulength, "uint16"), cast(e.ulength, "uint32"), cast(e.ulength, "uint64"), cast(e.ulength, "float"), cast(e.ulength, "double") +---- 1 +90|90|90|90|90.000000|90.000000 -LOG CastFromUINT32 -STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=2 RETURN to_int8(e.temprature), to_int16(e.temprature), to_int32(e.temprature), to_int128(e.temprature), to_float(e.temprature), to_double(e.temprature) ---- 1 1|1|1|1|1.000000|1.000000 +-STATEMENT MATCH (p:person)-[e:studyAt]->(o:organisation) WHERE p.ID=2 RETURN cast(e.temprature, "int8"), cast(e.temprature, "int16"), cast(e.temprature, "int32"), cast(e.temprature, "int128"), cast(e.temprature, "float"), cast(e.temprature, "double") +---- 1 +1|1|1|1|1.000000|1.000000 -LOG CastFromFLOAT -STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN to_int16(p.height), to_int32(p.height), to_int64(p.height), to_int128(p.height), to_double(p.height) ---- 1 2|2|2|1|1.731000 +-STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN cast(p.height, "uint8"), cast(p.height, "int16"), cast(p.height, "int32"), cast(p.height, "int64"), cast(p.height, "int128"), cast(p.height, "double"), cast(p.height, "float") +---- 1 +2|2|2|2|1|1.731000|1.731000 -LOG CastFromDOUBLE -STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN to_int16(p.eyeSight), to_int32(p.eyeSight), to_int64(p.eyeSight), to_int128(p.eyeSight), to_float(p.eyeSight) ---- 1 5|5|5|5|5.000000 +-STATEMENT MATCH (p:person) WHERE p.ID=0 RETURN cast(p.eyeSight, "uint8"), cast(p.eyeSight, "uint16"), cast(p.eyeSight, "uint32"), cast(p.eyeSight, "int32"), cast(p.eyeSight, "int64"), cast(p.eyeSight, "int128"), cast(p.eyeSight, "float"), cast(p.eyeSight, "double") +---- 1 +5|5|5|5|5|5|5.000000|5.000000 -CASE CastDataTypeToString @@ -71,6 +98,16 @@ True False False +-STATEMENT MATCH (p:person) RETURN cast(p.isStudent, "string") +---- 9 +True +True +False +False +False +True +False +False -LOG CastInt64ToString -STATEMENT MATCH (p:person) RETURN string(p.age) @@ -84,6 +121,17 @@ False 40 83 +-STATEMENT MATCH (p:person) RETURN cast(p.age, "string") +---- 9 +35 +30 +45 +20 +20 +25 +40 +83 + -LOG CastInt32ToString -STATEMENT MATCH (m:movies) RETURN string(m.length) @@ -91,6 +139,11 @@ False 126 2544 298 +-STATEMENT MATCH (m:movies) RETURN CAST(m.length, "STRING") +---- 3 +126 +2544 +298 -LOG CastInt16ToString -STATEMENT MATCH (p:person)-[s:studyAt]->(o:organisation) RETURN string(s.length) @@ -98,6 +151,11 @@ False 5 55 22 +-STATEMENT MATCH (p:person)-[s:studyAt]->(o:organisation) RETURN CAST(s.length, "STRING") +---- 3 +5 +55 +22 -LOG CastInt8ToString -STATEMENT MATCH (p:person)-[s:studyAt]->(o:organisation) RETURN string(s.level) @@ -105,6 +163,11 @@ False 5 120 2 +-STATEMENT MATCH (p:person)-[s:studyAt]->(o:organisation) RETURN cast(s.level, "string") +---- 3 +5 +120 +2 -LOG CastDoubleToString -STATEMENT MATCH (p:person) RETURN string(p.eyeSight) @@ -118,6 +181,17 @@ False 4.900000 4.900000 +-STATEMENT MATCH (p:person) RETURN cast(p.eyeSight, "string") +---- 9 +5.000000 +5.100000 +5.000000 +4.800000 +4.700000 +4.500000 +4.900000 +4.900000 + -LOG CastFloatToString -STATEMENT MATCH (p:person) RETURN string(p.height) @@ -131,6 +205,17 @@ False 1.600000 1.323000 +-STATEMENT MATCH (p:person) RETURN cast(p.height, "string") +---- 9 +1.731000 +0.990000 +1.000000 +1.300000 +1.463000 +1.510000 +1.600000 +1.323000 + -LOG CastDateToString -STATEMENT MATCH (p:person) RETURN string(p.birthdate) @@ -144,6 +229,17 @@ False 1980-10-26 1990-11-27 +-STATEMENT MATCH (p:person) RETURN cast(p.birthdate, "string") +---- 9 +1900-01-01 +1900-01-01 +1940-06-22 +1950-07-23 +1980-10-26 +1980-10-26 +1980-10-26 +1990-11-27 + -LOG CastTimestampToString -STATEMENT MATCH (p:person) RETURN string(p.registerTime) @@ -157,6 +253,17 @@ False 1976-12-23 04:41:42 2023-02-21 13:25:30 +-STATEMENT MATCH (p:person) RETURN cast(p.registerTime, "string") +---- 9 +2011-08-20 11:25:30 +2008-11-03 15:25:30.000526 +1911-08-20 02:32:21 +2031-11-30 12:25:30 +1976-12-23 11:21:42 +1972-07-31 13:22:30.678559 +1976-12-23 04:41:42 +2023-02-21 13:25:30 + -LOG CastIntervalToString -STATEMENT MATCH (p:person) RETURN string(p.lastJobDuration) @@ -170,6 +277,17 @@ False 10 years 5 months 13:00:00.000024 3 years 2 days 13:02:00 +-STATEMENT MATCH (p:person) RETURN cast(p.lastJobDuration, "string") +---- 9 +3 years 2 days 13:02:00 +10 years 5 months 13:00:00.000024 +48:24:11 +10 years 5 months 13:00:00.000024 +48:24:11 +00:18:00.024 +10 years 5 months 13:00:00.000024 +3 years 2 days 13:02:00 + -LOG CastStringToString -STATEMENT MATCH (p:person) RETURN string(p.fName) @@ -183,6 +301,17 @@ Farooq Greg Hubert Blaine Wolfeschlegelsteinhausenbergerdorff +-STATEMENT MATCH (p:person) RETURN cast(p.fName, "string") +---- 9 +Alice +Bob +Carol +Dan +Elizabeth +Farooq +Greg +Hubert Blaine Wolfeschlegelsteinhausenbergerdorff + -LOG CastListOfIntsToString -STATEMENT MATCH (p:person) RETURN string(p.workedHours) @@ -196,6 +325,17 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff [1] [10,11,12,3,4,5,6,7] +-STATEMENT MATCH (p:person) RETURN cast(p.workedHours, "string") +---- 9 +[10,5] +[12,8] +[4,5] +[1,9] +[2] +[3,4,5,6,7] +[1] +[10,11,12,3,4,5,6,7] + -LOG CastListOfListOfIntsToString -STATEMENT MATCH (p:person) RETURN string(p.courseScoresPerTerm) @@ -209,6 +349,17 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff [[10]] [[7],[10],[6,7]] +-STATEMENT MATCH (p:person) RETURN cast(p.courseScoresPerTerm, "string") +---- 9 +[[10,8],[6,7,8]] +[[8,9],[9,10]] +[[8,10]] +[[7,4],[8,8],[9]] +[[6],[7],[8]] +[[8]] +[[10]] +[[7],[10],[6,7]] + -LOG CastFixedListToString -STATEMENT MATCH (p:person) where p.ID > 1 RETURN string(p.grades) @@ -221,12 +372,28 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff [43,83,67,43] [77,64,100,54] +-STATEMENT MATCH (p:person) where p.ID > 1 RETURN cast(p.grades, "string") +---- 8 +[98,42,93,88] +[91,75,21,95] +[76,88,99,89] +[96,59,65,88] +[80,78,34,83] +[43,83,67,43] +[77,64,100,54] + + -LOG CastMapToString -STATEMENT MATCH (m:movies) RETURN string(m.audience) ---- 3 {audience1=33} {audience1=52, audience53=42} {} +-STATEMENT MATCH (m:movies) RETURN cast(m.audience, "string") +---- 3 +{audience1=33} +{audience1=52, audience53=42} +{} -LOG CastInternalIDToString -STATEMENT MATCH (p:person) RETURN string(id(p)) @@ -240,6 +407,17 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff 0:6 0:7 0:8 +-STATEMENT MATCH (p:person) RETURN cast(id(p), "string") +---- 9 +0:0 +0:1 +0:2 +0:3 +0:4 +0:5 +0:6 +0:7 +0:8 -LOG CastStructToString -STATEMENT MATCH (o:organisation) RETURN string(o.state) @@ -248,6 +426,12 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff {revenue: 152, location: ["vanco,uver north area"], stock: {price: [15,78,671], volume: 432}} {revenue: 558, location: ['very long city name', 'new york'], stock: {price: [22], volume: 99}} +-STATEMENT MATCH (o:organisation) RETURN cast(o.state, "string") +---- 4 +{revenue: 138, location: ['toronto', 'montr,eal'], stock: {price: [96,56], volume: 1000}} +{revenue: 152, location: ["vanco,uver north area"], stock: {price: [15,78,671], volume: 432}} +{revenue: 558, location: ['very long city name', 'new york'], stock: {price: [22], volume: 99}} + -LOG CastUnionToString -STATEMENT MATCH (o:organisation) RETURN string(o.info) @@ -256,6 +440,12 @@ Hubert Blaine Wolfeschlegelsteinhausenbergerdorff abcd 2023-12-15 +-STATEMENT MATCH (o:organisation) RETURN cast(o.info, "string") +---- 4 +3.120000 +abcd +2023-12-15 + -CASE DataTypeCasting -LOG CastInt8ToDouble @@ -264,6 +454,11 @@ abcd 5.000000 120.000000 2.000000 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.level, "double") +---- 3 +5.000000 +120.000000 +2.000000 -LOG CastInt8ToFloat -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN to_float(e.level) @@ -271,6 +466,11 @@ abcd 5.000000 120.000000 2.000000 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.level, "float") +---- 3 +5.000000 +120.000000 +2.000000 -LOG CastInt8ToInt64 -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN to_int64(e.level) @@ -278,6 +478,11 @@ abcd 5 120 2 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.level, "int64") +---- 3 +5 +120 +2 -LOG CastInt8ToUInt8 -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN to_uint8(e.level) @@ -285,6 +490,11 @@ abcd 5 120 2 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.level, "uint8") +---- 3 +5 +120 +2 -LOG CastInt8ToUInt32 -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN to_uint32(e.level) @@ -292,6 +502,11 @@ abcd 5 120 2 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.level, "uint32") +---- 3 +5 +120 +2 -LOG CastInt16ToDouble -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN to_double(e.length) @@ -299,6 +514,11 @@ abcd 5.000000 55.000000 22.000000 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "double") +---- 3 +5.000000 +55.000000 +22.000000 -LOG CastInt16ToFloat -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN to_float(e.length) @@ -306,6 +526,11 @@ abcd 5.000000 55.000000 22.000000 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "float") +---- 3 +5.000000 +55.000000 +22.000000 -LOG CastInt16ToInt64 -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN to_int64(e.length) @@ -313,6 +538,11 @@ abcd 5 55 22 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "int64") +---- 3 +5 +55 +22 -LOG CastInt16ToInt32 -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN to_int32(e.length) @@ -320,6 +550,11 @@ abcd 5 55 22 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "int32") +---- 3 +5 +55 +22 -LOG CastInt16ToInt8 -STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN to_int8(e.length) @@ -327,6 +562,48 @@ abcd 5 55 22 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "int8") +---- 3 +5 +55 +22 + +-LOG CastInt16ToUINT64 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "uint64") +---- 3 +5 +55 +22 + +-LOG CastInt16ToUINT32 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "uint32") +---- 3 +5 +55 +22 + + +-LOG CastInt16ToUINT16 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "uint16") +---- 3 +5 +55 +22 + + +-LOG CastInt16ToUINT8 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "uint8") +---- 3 +5 +55 +22 + +-LOG CastInt16ToINT128 +-STATEMENT MATCH (:person)-[e:studyAt]->(:organisation) RETURN cast(e.length, "int128") +---- 3 +5 +55 +22 -LOG CastInt32ToDouble -STATEMENT MATCH (m:movies) RETURN to_double(m.length) @@ -334,6 +611,11 @@ abcd 126.000000 2544.000000 298.000000 +-STATEMENT MATCH (m:movies) RETURN cast(m.length, "double") +---- 3 +126.000000 +2544.000000 +298.000000 -LOG CastInt32ToFloat -STATEMENT MATCH (m:movies) RETURN to_float(m.length) @@ -341,6 +623,11 @@ abcd 126.000000 2544.000000 298.000000 +-STATEMENT MATCH (m:movies) RETURN cast(m.length, "float") +---- 3 +126.000000 +2544.000000 +298.000000 -LOG CastInt32ToInt64 -STATEMENT MATCH (m:movies) RETURN to_int64(m.length) @@ -348,17 +635,31 @@ abcd 126 2544 298 +-STATEMENT MATCH (m:movies) RETURN cast(m.length, "int64") +---- 3 +126 +2544 +298 -LOG CastUIntToInt -STATEMENT RETURN TO_INT64(TO_UINT64(9223372036854775807)); ---- 1 9223372036854775807 +-STATEMENT RETURN cast(cast(9223372036854775807, "uint64"), "int64"); +---- 1 +9223372036854775807 -STATEMENT RETURN TO_INT32(TO_UINT64("43242")); ---- 1 43242 +-STATEMENT RETURN cast(cast("43242", "uint64"), "int32"); +---- 1 +43242 -STATEMENT RETURN TO_INT32(TO_UINT32("43242")); ---- 1 43242 +-STATEMENT RETURN cast(cast("43242", "uint32"), "int32"); +---- 1 +43242 -LOG CastInt64ToInt8 -STATEMENT MATCH (p:person) RETURN to_int8(p.age) @@ -371,27 +672,49 @@ abcd 25 40 83 +-STATEMENT MATCH (p:person) RETURN cast(p.age, "int8") +---- 8 +35 +30 +45 +20 +20 +25 +40 +83 -LOG CastFloatDoubleToInt8 -STATEMENT Return to_int8(1.1), to_int8(to_float(3.2)) ---- 1 1|3 +-STATEMENT Return cast(1.1, "int8"), cast(cast(3.2, "float"), "int8") +---- 1 +1|3 -LOG CastNumericalToInt8 -STATEMENT Return to_int8(to_int64(1)), to_int8(to_int32(2)), to_int8(to_int16(3)), to_int8(to_int8(4)), to_int8(to_uint8(5)), to_int8(to_uint16(6)), to_int8(to_uint32(7)), to_int8(to_uint64(8)), to_int8(to_int128(9)) ---- 1 1|2|3|4|5|6|7|8|9 +-STATEMENT Return cast(cast(1, "int64"), "int8"), cast(cast(2, "int32"), "int8"), cast(cast(3, "int16"), "int8"), cast(cast(4, "int8"), "int8"), cast(cast(5, "uint8"), "int8"), cast(cast(6, "uint16"), "int8"), cast(cast(7, "uint32"), "int8"), cast(cast(8, "uint64"), "int8"), cast(cast(9, "int128"), "int8"), cast(cast(10, "float"), "int8"), cast(cast(11, "double"), "int8") +---- 1 +1|2|3|4|5|6|7|8|9|10|11 -LOG CastNumericalToUInt8 -STATEMENT Return to_uint8(to_int64(1)), to_uint8(to_int32(2)), to_uint8(to_int16(3)), to_uint8(to_int8(4)), to_uint8(to_uint16(6)), to_uint8(to_uint32(7)), to_uint8(to_uint64(8)), to_uint8(to_int128(9)) ---- 1 1|2|3|4|6|7|8|9 +-STATEMENT Return cast(cast(1, "int64"), "uint8"), cast(cast(2, "int32"), "uint8"), cast(cast(3, "int16"), "uint8"), cast(cast(4, "int8"), "uint8"), cast(cast(5, "uint8"), "uint8"), cast(cast(6, "uint16"), "uint8"), cast(cast(7, "uint32"), "uint8"), cast(cast(8, "uint64"), "uint8"), cast(cast(9, "int128"), "uint8"), cast(cast(10, "float"), "uint8"), cast(cast(11, "double"), "uint8") +---- 1 +1|2|3|4|5|6|7|8|9|10|11 -LOG CastNumericalToUInt16 -STATEMENT Return to_uint16(to_int64(1)), to_uint16(to_int32(2)), to_uint16(to_int16(3)), to_uint16(to_int8(4)), to_uint16(to_uint8(5)), to_uint16(to_uint32(7)), to_uint16(to_uint64(8)), to_uint16(to_int128(9)) ---- 1 1|2|3|4|5|7|8|9 +-STATEMENT Return cast(cast(1, "int64"), "uint16"), cast(cast(2, "int32"), "uint16"), cast(cast(3, "int16"), "uint16"), cast(cast(4, "int8"), "uint16"), cast(cast(5, "uint8"), "uint16"), cast(cast(6, "uint16"), "uint16"), cast(cast(7, "uint32"), "uint16"), cast(cast(8, "uint64"), "uint16"), cast(cast(9, "int128"), "uint16"), cast(cast(10, "float"), "uint16"), cast(cast(11, "double"), "uint16") +---- 1 +1|2|3|4|5|6|7|8|9|10|11 -LOG CastUnsignedIntToFLoatDouble -STATEMENT Return to_float(to_uint64(1)), to_double(to_uint64(2)), to_float(to_uint32(3)), to_double(to_uint32(4)), to_float(to_uint16(5)), to_double(to_uint16(6)), to_float(to_uint8(7)), to_double(to_uint8(8)) @@ -402,16 +725,25 @@ abcd -STATEMENT Return to_uint32(to_int64(1)), to_uint32(to_int32(2)), to_uint32(to_int16(3)), to_uint32(to_int8(4)), to_uint32(to_uint8(5)), to_uint32(to_uint16(6)), to_uint32(to_uint64(8)), to_uint32(to_int128(9)) ---- 1 1|2|3|4|5|6|8|9 +-STATEMENT Return cast(cast(1, "int64"), "uint32"), cast(cast(2, "int32"), "uint32"), cast(cast(3, "int16"), "uint32"), cast(cast(4, "int8"), "uint32"), cast(cast(5, "uint16"), "uint32"), cast(cast(6, "uint8"), "uint32"), cast(cast(7, "uint32"), "uint32"), cast(cast(8, "uint64"), "uint32"), cast(cast(9, "int128"), "uint32"), cast(cast(10, "float"), "uint32"), cast(cast(11, "double"), "uint32") +---- 1 +1|2|3|4|5|6|7|8|9|10|11 -LOG CastNumericalToUInt64 --STATEMENT Return to_uint64(to_int64(1)), to_uint64(to_int32(2)), to_uint64(to_int16(3)), to_uint64(to_int8(4)), to_uint64(to_uint8(5)), to_uint64(to_uint16(6)), to_uint64(to_uint32(7)), to_uint64(to_int128(9)) +-STATEMENT Return to_uint64(to_int64(1)), to_uint64(to_int32(2)), to_uint64(to_int16(3)), to_uint64(to_int8(4)), to_uint64(to_uint8(5)), to_uint64(to_uint32(6)), to_uint64(to_uint32(7)), to_uint64(to_int128(9)) ---- 1 1|2|3|4|5|6|7|9 +-STATEMENT Return cast(cast(1, "int64"), "uint64"), cast(cast(2, "int32"), "uint64"), cast(cast(3, "int64"), "uint64"), cast(cast(4, "int64"), "uint64"), cast(cast(5, "uint8"), "uint64"), cast(cast(6, "uint16"), "uint64"), cast(cast(7, "uint64"), "uint64"), cast(cast(8, "uint64"), "uint64"), cast(cast(9, "int128"), "uint64"), cast(cast(10, "float"), "uint64"), cast(cast(11, "double"), "uint64") +---- 1 +1|2|3|4|5|6|7|8|9|10|11 -LOG CastNumericalToInt128 -STATEMENT Return to_int128(to_int64(1)), to_int128(to_int32(2)), to_int128(to_int16(3)), to_int128(to_int8(4)), to_int128(to_uint8(5)), to_int128(to_uint16(6)), to_int128(to_uint32(7)), to_int128(to_uint64(8)) ---- 1 1|2|3|4|5|6|7|8 +-STATEMENT Return cast(cast(1, "int64"), "int128"), cast(cast(2, "int32"), "int128"), cast(cast(3, "int64"), "int128"), cast(cast(4, "int64"), "int128"), cast(cast(5, "uint8"), "int128"), cast(cast(6, "uint16"), "int128"), cast(cast(7, "uint64"), "int128"), cast(cast(8, "uint64"), "int128"), cast(cast(9, "int128"), "int128"), cast(cast(10, "float"), "int128"), cast(cast(11, "double"), "int128") +---- 1 +1|2|3|4|5|6|7|8|9|10|11 -LOG CastInt64ToDouble -STATEMENT MATCH (p:person) RETURN to_double(p.age) @@ -424,6 +756,16 @@ abcd 25.000000 40.000000 83.000000 +-STATEMENT MATCH (p:person) RETURN cast(p.age, "double") +---- 8 +35.000000 +30.000000 +45.000000 +20.000000 +20.000000 +25.000000 +40.000000 +83.000000 -LOG CastInt64ToFloat -STATEMENT MATCH (p:person) RETURN to_float(p.gender) @@ -436,16 +778,45 @@ abcd 2.000000 2.000000 2.000000 +-STATEMENT MATCH (p:person) RETURN cast(p.gender, "float") +---- 8 +1.000000 +2.000000 +1.000000 +2.000000 +1.000000 +2.000000 +2.000000 +2.000000 + +-LOG CastInt64ToSerial +-STATEMENT MATCH (p:person) RETURN cast(p.gender, "Serial") +---- 8 +1 +2 +1 +2 +1 +2 +2 +2 -LOG CastDoubleToUInt -STATEMENT RETURN to_uint8(to_double(1)), to_uint16(to_double(2)), to_uint32(to_double(3)), to_uint64(to_double(4)), to_int128(to_double(5)) ---- 1 1|2|3|4|5 +-STATEMENT RETURN cast(cast(1, "double"), "uint8"), cast(cast(2, "double"), "uint16"), cast(cast(3, "double"), "uint32"), cast(cast(4, "double"), "uint64"), cast(cast(5, "double"), "int128") +---- 1 +1|2|3|4|5 -LOG CastFloatToUInt -STATEMENT RETURN to_uint8(to_float(1)), to_uint16(to_float(2)), to_uint32(to_float(3)), to_uint64(to_float(4)), to_int128(to_float(5)) ---- 1 1|2|3|4|5 +-LOG CastFloatToUInt +-STATEMENT RETURN cast(cast(1, "float"), "uint8"), cast(cast(2, "float"), "uint16"), cast(cast(3, "float"), "uint32"), cast(cast(4, "float"), "uint64"), cast(cast(5, "float"), "int128") +---- 1 +1|2|3|4|5 -LOG CastFloatToDouble -STATEMENT MATCH (p:person) RETURN to_double(p.height) @@ -458,6 +829,16 @@ abcd 1.510000 1.600000 1.323000 +-STATEMENT MATCH (p:person) RETURN cast(p.height, "double") +---- 8 +1.731000 +0.990000 +1.000000 +1.300000 +1.463000 +1.510000 +1.600000 +1.323000 -LOG CastStringToNum -STATEMENT RETURN TO_INT64("12"); @@ -556,6 +937,99 @@ False -STATEMENT RETURN TO_DATE("2022-10-21"); ---- 1 2022-10-21 +-STATEMENT RETURN cast("12", "int64"); +---- 1 +12 +-STATEMENT RETURN cast("9223372036854775807", "int64"); +---- 1 +9223372036854775807 +-STATEMENT RETURN cast("9223372036854775807", "serial"); +---- 1 +9223372036854775807 +-STATEMENT RETURN cast("-9223372036854775808", "serial"); +---- 1 +-9223372036854775808 +-STATEMENT RETURN cast("2147483647", "int32"); +---- 1 +2147483647 +-STATEMENT RETURN cast("-2147483648", "int32"); +---- 1 +-2147483648 +-STATEMENT RETURN cast("32767", "int16"); +---- 1 +32767 +-STATEMENT RETURN cast("-32768", "int16"); +---- 1 +-32768 +-STATEMENT RETURN cast("0", "int8"); +---- 1 +0 +-STATEMENT RETURN cast("-128", "int8"); +---- 1 +-128 +-STATEMENT RETURN cast("0", "uint64"); +---- 1 +0 +-STATEMENT RETURN cast("4320492", "uint64"); +---- 1 +4320492 +-STATEMENT RETURN cast("4294967295", "uint32"); +---- 1 +4294967295 +-STATEMENT RETURN cast("65535", "uint16"); +---- 1 +65535 +-STATEMENT RETURN cast("255", "uint8"); +---- 1 +255 +-STATEMENT RETURN cast("1844674407370955161811111111", "int128"); +---- 1 +1844674407370955161811111111 +-STATEMENT RETURN cast(" 3.294", "float"); +---- 1 +3.294000 +-STATEMENT RETURN cast("3.294", "float"); +---- 1 +3.294000 +-STATEMENT RETURN cast("0.99", "float"); +---- 1 +0.990000 +-STATEMENT RETURN cast("42341.43299123", "float"); +---- 1 +42341.433594 +-STATEMENT RETURN cast("3.294 ", "double"); +---- 1 +3.294000 +-STATEMENT RETURN cast(" 3.294 ", "double"); +---- 1 +3.294000 +-STATEMENT RETURN cast("TRUE", "boolean"); +---- 1 +True +-STATEMENT RETURN cast("1", "boolean"); +---- 1 +True +-STATEMENT RETURN cast("tRUE", "boolean"); +---- 1 +True +-STATEMENT RETURN cast(" t ", "boolean"); +---- 1 +True +-STATEMENT RETURN cast("T ", "boolean"); +---- 1 +True +-STATEMENT RETURN cast("false", "boolean"); +---- 1 +False +-STATEMENT RETURN cast(" F", "boolean"); +---- 1 +False +-STATEMENT RETURN cast("0", "boolean"); +---- 1 +False +-STATEMENT RETURN cast("0", "boolean"); +---- 1 +False -LOG CastDoubleToFloat -STATEMENT MATCH (p:person) RETURN to_float(p.eyeSight) @@ -568,23 +1042,45 @@ False 4.500000 4.900000 4.900000 +-STATEMENT MATCH (p:person) RETURN cast(p.eyeSight, "float") +---- 8 +5.000000 +5.100000 +5.000000 +4.800000 +4.700000 +4.500000 +4.900000 +4.900000 -LOG CastNumericalToINT128Test -STATEMENT RETURN to_int8(to_int128(1)), to_int16(to_int128(2)), to_int32(to_int128(3)), to_int64(to_int128(4)), to_uint8(to_int128(5)), to_uint16(to_int128(6)), to_uint32(to_int128(7)), to_uint64(to_int128(8)) ---- 1 1|2|3|4|5|6|7|8 +-STATEMENT RETURN cast(cast(1, "int128"), "int8"), cast(cast(2, "int128"), "int16"), cast(cast(3, "int128"), "int32"), cast(cast(4, "int128"), "int64"), cast(cast(5, "int128"), "int128"), cast(cast(6, "int128"), "uint8"), cast(cast(7, "int128"), "uint16"), cast(cast(8, "int128"), "uint32"), cast(cast(9, "int128"), "uint64"), cast(cast(10, "int128"), "double"), cast(cast(11, "int128"), "float"), cast(cast(12, "int128"), "string") +---- 1 +1|2|3|4|5|6|7|8|9|10.000000|11.000000|12 -LOG CastInt128ToOtherTypes -STATEMENT RETURN TO_FLOAT(170141183460469231731687303715884105727), TO_INT64(-4324324); ---- 1 170141183460469231731687303715884105728.000000|-4324324 +-STATEMENT RETURN cast(170141183460469231731687303715884105727, "float"), cast(-4324324, "int64"); +---- 1 +170141183460469231731687303715884105728.000000|-4324324 -LOG CastNegInt128ToFloatDouble -STATEMENT Return to_float(to_int128(-15)), to_double(to_int128(-1)), to_float(to_int128(15)), to_double(to_int128(1)) ---- 1 -15.000000|-1.000000|15.000000|1.000000 +-STATEMENT Return cast(cast(-15, "int128"), "float"), cast(cast(-1, "int128"), "double"), cast(cast(15, "int128"), "float"), cast(cast(1, "int128"), "double") +---- 1 +-15.000000|-1.000000|15.000000|1.000000 -LOG CastFloatDoubleToInt128 -STATEMENT Return to_int128(to_float(-15)), to_int128(to_double(-1)), to_int128(to_float(15)), to_int128(to_double(1)) ---- 1 -15|-1|15|1 +-STATEMENT Return cast(cast(-15, "float"), "int128"), cast(cast(-1, "double"), "int128"), cast(cast(15, "float"), "int128"), cast(cast(1, "double"), "int128") +---- 1 +-15|-1|15|1 diff --git a/test/test_files/tinysnb/function/list.test b/test/test_files/tinysnb/function/list.test index 9ee81775223..ad92aa7b61f 100644 --- a/test/test_files/tinysnb/function/list.test +++ b/test/test_files/tinysnb/function/list.test @@ -428,11 +428,17 @@ ad -STATEMENT RETURN LIST_CONCAT([1,2,NULL], [to_int64(NULL)]) ---- 1 [1,2,,] +-STATEMENT RETURN LIST_CONCAT([1,2,NULL], [cast(NULL, "int64")]) +---- 1 +[1,2,,] -LOG ListConcatNullAndINT64 -STATEMENT RETURN LIST_CONCAT([to_int64(NULL)], [NULL, 1, 3]) ---- 1 [,,1,3] +-STATEMENT RETURN LIST_CONCAT([cast(NULL, "int64")], [NULL, 1, 3]) +---- 1 +[,,1,3] -LOG ListAppendListOfINT64 -STATEMENT MATCH (a:person) RETURN list_append(a.workedHours, a.age) @@ -1096,26 +1102,41 @@ True -STATEMENT Return list_sort([to_int8(2), to_int8(3), to_int8(1), NULL, NULL]); ---- 1 [,,1,2,3] +-STATEMENT Return list_sort([cast(2, "int8"), cast(3, "int8"), cast(1, "int8"), NULL, NULL]); +---- 1 +[,,1,2,3] -LOG ListSortInt8Desc -STATEMENT Return list_sort([to_int8(2), to_int8(3), to_int8(1), to_int8(5), NULL], 'DESC'); ---- 1 [,5,3,2,1] +-STATEMENT Return list_sort([cast(2, "int8"), cast(3, "int8"), cast(1, "int8"), cast(5, "int8"), NULL], 'DESC'); +---- 1 +[,5,3,2,1] -LOG ListSortInt16Asc -STATEMENT Return list_sort([to_int16(2), to_int16(3), to_int16(1), NULL, NULL]); ---- 1 [,,1,2,3] +-STATEMENT Return list_sort([cast(2, "int16"), cast(3, "int16"), cast(1, "int16"), NULL, NULL]); +---- 1 +[,,1,2,3] -LOG ListSortInt16Desc -STATEMENT Return list_sort([to_int16(2), to_int16(3), to_int16(1), to_int16(5), NULL], 'DESC'); ---- 1 [,5,3,2,1] +-STATEMENT Return list_sort([cast(2, "int16"), cast(3, "int16"), cast(1, "int16"), to_int16(5), NULL], 'DESC'); +---- 1 +[,5,3,2,1] -LOG ListSortInt16DescWithNullsLast -STATEMENT Return list_sort([to_int16(2), to_int16(3), to_int16(1), NULL], 'DESC', 'NULLS LAST'); ---- 1 [3,2,1,] +-STATEMENT Return list_sort([cast(2, "int16"), cast(3, "int16"), cast(1, "int16"), NULL], 'DESC', 'NULLS LAST'); +---- 1 +[3,2,1,] -LOG ListSortInt32Asc -STATEMENT Return list_sort([to_int32(2), to_int32(3), to_int32(1), NULL, NULL]);