diff --git a/src/common/type_utils.cpp b/src/common/type_utils.cpp index bbd1d255d1..c72a20b680 100644 --- a/src/common/type_utils.cpp +++ b/src/common/type_utils.cpp @@ -54,6 +54,37 @@ std::string TypeUtils::castValueToString( } } +template<> +std::string TypeUtils::toString(const bool& val, void* valueVector) { + return val ? "True" : "False"; +} + +template<> +std::string TypeUtils::toString(const internalID_t& val, void* valueVector) { + return std::to_string(val.tableID) + ":" + std::to_string(val.offset); +} + +template<> +std::string TypeUtils::toString(const date_t& val, void* valueVector) { + return Date::toString(val); +} + +template<> +std::string TypeUtils::toString(const timestamp_t& val, void* valueVector) { + return Timestamp::toString(val); +} + +template<> +std::string TypeUtils::toString(const interval_t& val, void* valueVector) { + return Interval::toString(val); +} + +template<> +std::string TypeUtils::toString(const ku_string_t& val, void* valueVector) { + return val.getAsString(); +} + +template<> std::string TypeUtils::toString(const list_entry_t& val, void* valueVector) { auto listVector = (ValueVector*)valueVector; if (val.size == 0) { @@ -77,6 +108,7 @@ std::string TypeUtils::toString(const list_entry_t& val, void* valueVector) { return result; } +template<> std::string TypeUtils::toString(const struct_entry_t& val, void* valVector) { auto structVector = (ValueVector*)valVector; auto fields = StructType::getFields(&structVector->dataType); diff --git a/src/include/common/type_utils.h b/src/include/common/type_utils.h index 55d679b1ea..bed4a66d26 100644 --- a/src/include/common/type_utils.h +++ b/src/include/common/type_utils.h @@ -16,28 +16,15 @@ class TypeUtils { public: static uint32_t convertToUint32(const char* data); - - static inline std::string toString(bool boolVal) { return boolVal ? "True" : "False"; } - static inline std::string toString(int64_t val) { return std::to_string(val); } - static inline std::string toString(int32_t val) { return std::to_string(val); } - static inline std::string toString(int16_t val) { return std::to_string(val); } - static inline std::string toString(int8_t val) { return std::to_string(val); } - static inline std::string toString(uint64_t val) { return std::to_string(val); } - static inline std::string toString(uint32_t val) { return std::to_string(val); } - static inline std::string toString(uint16_t val) { return std::to_string(val); } - static inline std::string toString(uint8_t val) { return std::to_string(val); } - static inline std::string toString(double_t val) { return std::to_string(val); } - static inline std::string toString(float_t val) { return std::to_string(val); } - static inline std::string toString(const internalID_t& val) { - return std::to_string(val.tableID) + ":" + std::to_string(val.offset); + template + static inline std::string toString(const T& val, void* valueVector = nullptr) { + static_assert(std::is_same::value || std::is_same::value || + std::is_same::value || std::is_same::value || + std::is_same::value || std::is_same::value || + std::is_same::value || std::is_same::value || + std::is_same::value || std::is_same::value); + return std::to_string(val); } - static inline std::string toString(const date_t& val) { return Date::toString(val); } - static inline std::string toString(const timestamp_t& val) { return Timestamp::toString(val); } - static inline std::string toString(const interval_t& val) { return Interval::toString(val); } - static inline std::string toString(const ku_string_t& val) { return val.getAsString(); } - static inline std::string toString(const std::string& val) { return val; } - static std::string toString(const list_entry_t& val, void* valueVector); - static std::string toString(const struct_entry_t& val, void* valueVector); static inline void encodeOverflowPtr( uint64_t& overflowPtr, page_idx_t pageIdx, uint16_t pageOffset) { @@ -57,5 +44,23 @@ class TypeUtils { static std::string castValueToString(const LogicalType& dataType, uint8_t* value, void* vector); }; +// Forward declaration of template specializations. +template<> +std::string TypeUtils::toString(const bool& val, void* valueVector); +template<> +std::string TypeUtils::toString(const internalID_t& val, void* valueVector); +template<> +std::string TypeUtils::toString(const date_t& val, void* valueVector); +template<> +std::string TypeUtils::toString(const timestamp_t& val, void* valueVector); +template<> +std::string TypeUtils::toString(const interval_t& val, void* valueVector); +template<> +std::string TypeUtils::toString(const ku_string_t& val, void* valueVector); +template<> +std::string TypeUtils::toString(const list_entry_t& val, void* valueVector); +template<> +std::string TypeUtils::toString(const struct_entry_t& val, void* valueVector); + } // namespace common } // namespace kuzu diff --git a/src/include/processor/operator/persistent/csv_file_writer.h b/src/include/processor/operator/persistent/csv_file_writer.h index 7213bad972..c20066ae19 100644 --- a/src/include/processor/operator/persistent/csv_file_writer.h +++ b/src/include/processor/operator/persistent/csv_file_writer.h @@ -23,9 +23,6 @@ class CSVFileWriter : public FileWriter { template void writeToBuffer(common::ValueVector* vector, bool escapeStringValue = false); - template - void writeListToBuffer(common::ValueVector* vector); - inline void writeToBuffer(const std::string& value) { buffer << value; } inline void writeToBuffer(const char value) { buffer << value; } diff --git a/src/processor/operator/persistent/csv_file_writer.cpp b/src/processor/operator/persistent/csv_file_writer.cpp index 6ad0d8dee1..0740507a97 100644 --- a/src/processor/operator/persistent/csv_file_writer.cpp +++ b/src/processor/operator/persistent/csv_file_writer.cpp @@ -1,7 +1,5 @@ #include "processor/operator/persistent/csv_file_writer.h" -#include - #include "common/constants.h" #include "common/string_utils.h" #include "common/type_utils.h" @@ -57,23 +55,15 @@ void CSVFileWriter::writeValues(std::vector& outputVectors template void CSVFileWriter::writeToBuffer(common::ValueVector* vector, bool escapeStringValue) { auto selPos = vector->state->selVector->selectedPositions[0]; - auto value = vector->isNull(selPos) ? "" : TypeUtils::toString(vector->getValue(selPos)); + auto value = vector->isNull(selPos) ? "" : + TypeUtils::toString(vector->getValue(selPos), + reinterpret_cast(vector)); if (escapeStringValue) { escapeString(value); } writeToBuffer(value); } -template -void CSVFileWriter::writeListToBuffer(common::ValueVector* vector) { - // vectors are always flat - auto selPos = vector->state->selVector->selectedPositions[0]; - auto value = - vector->isNull(selPos) ? "" : TypeUtils::toString(vector->getValue(selPos), vector); - escapeString(value); - writeToBuffer(value); -} - void CSVFileWriter::escapeString(std::string& value) { StringUtils::replaceAll(value, "\"", "\"\""); value = "\"" + value + "\""; @@ -95,20 +85,20 @@ void CSVFileWriter::writeValue(common::ValueVector* vector) { case LogicalTypeID::FLOAT: return writeToBuffer(vector); case LogicalTypeID::DATE: - return writeToBuffer(vector, true); + return writeToBuffer(vector, true /* escapeStringValue */); case LogicalTypeID::TIMESTAMP: - return writeToBuffer(vector, true); + return writeToBuffer(vector, true /* escapeStringValue */); case LogicalTypeID::INTERVAL: - return writeToBuffer(vector, true); + return writeToBuffer(vector, true /* escapeStringValue */); case LogicalTypeID::STRING: - return writeToBuffer(vector, true); + return writeToBuffer(vector, true /* escapeStringValue */); case LogicalTypeID::INTERNAL_ID: - return writeToBuffer(vector, true); + return writeToBuffer(vector, true /* escapeStringValue */); case LogicalTypeID::VAR_LIST: case LogicalTypeID::FIXED_LIST: - return writeListToBuffer(vector); + return writeToBuffer(vector, true /* escapeStringValue */); case LogicalTypeID::STRUCT: - return writeListToBuffer(vector); + return writeToBuffer(vector, true /* escapeStringValue */); default: { throw NotImplementedException("CSVFileWriter::writeValue"); }