Skip to content

Commit

Permalink
Merge pull request #2141 from kuzudb/copy-to-plan
Browse files Browse the repository at this point in the history
Refactor toString()
  • Loading branch information
acquamarin committed Oct 4, 2023
2 parents 37800fe + 988c398 commit af6bf97
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 44 deletions.
32 changes: 32 additions & 0 deletions src/common/type_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
47 changes: 26 additions & 21 deletions src/include/common/type_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
static inline std::string toString(const T& val, void* valueVector = nullptr) {
static_assert(std::is_same<T, int64_t>::value || std::is_same<T, int32_t>::value ||
std::is_same<T, int16_t>::value || std::is_same<T, int8_t>::value ||
std::is_same<T, uint64_t>::value || std::is_same<T, uint32_t>::value ||
std::is_same<T, uint16_t>::value || std::is_same<T, uint8_t>::value ||
std::is_same<T, double_t>::value || std::is_same<T, float_t>::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) {
Expand All @@ -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
3 changes: 0 additions & 3 deletions src/include/processor/operator/persistent/csv_file_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ class CSVFileWriter : public FileWriter {
template<typename T>
void writeToBuffer(common::ValueVector* vector, bool escapeStringValue = false);

template<typename T>
void writeListToBuffer(common::ValueVector* vector);

inline void writeToBuffer(const std::string& value) { buffer << value; }
inline void writeToBuffer(const char value) { buffer << value; }

Expand Down
30 changes: 10 additions & 20 deletions src/processor/operator/persistent/csv_file_writer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "processor/operator/persistent/csv_file_writer.h"

#include <fstream>

#include "common/constants.h"
#include "common/string_utils.h"
#include "common/type_utils.h"
Expand Down Expand Up @@ -57,23 +55,15 @@ void CSVFileWriter::writeValues(std::vector<common::ValueVector*>& outputVectors
template<typename T>
void CSVFileWriter::writeToBuffer(common::ValueVector* vector, bool escapeStringValue) {
auto selPos = vector->state->selVector->selectedPositions[0];
auto value = vector->isNull(selPos) ? "" : TypeUtils::toString(vector->getValue<T>(selPos));
auto value = vector->isNull(selPos) ? "" :
TypeUtils::toString(vector->getValue<T>(selPos),
reinterpret_cast<void*>(vector));
if (escapeStringValue) {
escapeString(value);
}
writeToBuffer(value);
}

template<typename T>
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<T>(selPos), vector);
escapeString(value);
writeToBuffer(value);
}

void CSVFileWriter::escapeString(std::string& value) {
StringUtils::replaceAll(value, "\"", "\"\"");
value = "\"" + value + "\"";
Expand All @@ -95,20 +85,20 @@ void CSVFileWriter::writeValue(common::ValueVector* vector) {
case LogicalTypeID::FLOAT:
return writeToBuffer<float>(vector);
case LogicalTypeID::DATE:
return writeToBuffer<date_t>(vector, true);
return writeToBuffer<date_t>(vector, true /* escapeStringValue */);
case LogicalTypeID::TIMESTAMP:
return writeToBuffer<timestamp_t>(vector, true);
return writeToBuffer<timestamp_t>(vector, true /* escapeStringValue */);
case LogicalTypeID::INTERVAL:
return writeToBuffer<interval_t>(vector, true);
return writeToBuffer<interval_t>(vector, true /* escapeStringValue */);
case LogicalTypeID::STRING:
return writeToBuffer<ku_string_t>(vector, true);
return writeToBuffer<ku_string_t>(vector, true /* escapeStringValue */);
case LogicalTypeID::INTERNAL_ID:
return writeToBuffer<internalID_t>(vector, true);
return writeToBuffer<internalID_t>(vector, true /* escapeStringValue */);
case LogicalTypeID::VAR_LIST:
case LogicalTypeID::FIXED_LIST:
return writeListToBuffer<list_entry_t>(vector);
return writeToBuffer<list_entry_t>(vector, true /* escapeStringValue */);
case LogicalTypeID::STRUCT:
return writeListToBuffer<struct_entry_t>(vector);
return writeToBuffer<struct_entry_t>(vector, true /* escapeStringValue */);
default: {
throw NotImplementedException("CSVFileWriter::writeValue");
}
Expand Down

0 comments on commit af6bf97

Please sign in to comment.