Skip to content

Commit

Permalink
Remove writeToCsv API (#2917)
Browse files Browse the repository at this point in the history
* Remove write_to_csv method from all API bindings

* Remove writeToCsv from C++ API

* Remove unnecessary include statement in query_result.cpp
  • Loading branch information
mewim committed Feb 20, 2024
1 parent 2ebf2cd commit 351ea0c
Show file tree
Hide file tree
Showing 19 changed files with 1 addition and 395 deletions.
6 changes: 0 additions & 6 deletions src/c_api/query_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ char* kuzu_query_result_to_string(kuzu_query_result* query_result) {
static_cast<QueryResult*>(query_result->_query_result)->toString());
}

void kuzu_query_result_write_to_csv(kuzu_query_result* query_result, const char* file_path,
char delimiter, char escape_char, char new_line) {
static_cast<QueryResult*>(query_result->_query_result)
->writeToCSV(file_path, delimiter, escape_char, new_line);
}

void kuzu_query_result_reset_iterator(kuzu_query_result* query_result) {
static_cast<QueryResult*>(query_result->_query_result)->resetIterator();
}
Expand Down
10 changes: 0 additions & 10 deletions src/include/c_api/kuzu.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,16 +630,6 @@ KUZU_C_API kuzu_flat_tuple* kuzu_query_result_get_next(kuzu_query_result* query_
* @param query_result The query result instance to return.
*/
KUZU_C_API char* kuzu_query_result_to_string(kuzu_query_result* query_result);
/**
* @brief Writes the query result to the given file path as CSV.
* @param query_result The query result instance to write.
* @param file_path The file path to write the query result.
* @param delimiter The delimiter character to use when writing csv file.
* @param escape_char The escape character to use when writing csv file.
* @param new_line The new line character to use when writing csv file.
*/
KUZU_C_API void kuzu_query_result_write_to_csv(kuzu_query_result* query_result,
const char* file_path, char delimiter, char escape_char, char new_line);
/**
* @brief Resets the iterator of the query result to the beginning of the query result.
* @param query_result The query result instance to reset iterator.
Expand Down
10 changes: 1 addition & 9 deletions src/include/main/query_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,7 @@ class QueryResult {
KUZU_API std::shared_ptr<processor::FlatTuple> getNext();

KUZU_API std::string toString();
/**
* @brief writes the query result to a csv file.
* @param fileName name of the csv file.
* @param delimiter delimiter of the csv file.
* @param escapeCharacter escape character of the csv file.
* @param newline newline character of the csv file.
*/
KUZU_API void writeToCSV(std::string fileName, char delimiter = ',', char escapeCharacter = '"',
char newline = '\n');

/**
* @brief Resets the result tuple iterator.
*/
Expand Down
61 changes: 0 additions & 61 deletions src/main/query_result.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "main/query_result.h"

#include <fstream>

#include "binder/expression/expression.h"
#include "common/arrow/arrow_converter.h"
#include "common/types/value/node.h"
Expand Down Expand Up @@ -185,65 +183,6 @@ std::string QueryResult::toString() {
return result;
}

void QueryResult::writeToCSV(
std::string fileName, char delimiter, char escapeCharacter, char newline) {
std::ofstream file;
file.open(fileName);
std::shared_ptr<FlatTuple> nextTuple;
KU_ASSERT(delimiter != '\0');
KU_ASSERT(newline != '\0');
while (hasNext()) {
nextTuple = getNext();
for (auto idx = 0ul; idx < nextTuple->len(); idx++) {
std::string resultVal = nextTuple->getValue(idx)->toString();
bool isStringList = false;
if (nextTuple->getValue(idx)->getDataType()->toString() == "STRING[]") {
isStringList = true;
}
bool surroundQuotes = false;
std::string csvStr;
for (long unsigned int j = 0; j < resultVal.length(); j++) {
if (!surroundQuotes) {
if (resultVal[j] == escapeCharacter || resultVal[j] == newline ||
resultVal[j] == delimiter) {
surroundQuotes = true;
}
}
if (resultVal[j] == escapeCharacter) {
csvStr += escapeCharacter;
csvStr += escapeCharacter;
} else if (resultVal[j] == ',' && isStringList) {
csvStr += escapeCharacter;
csvStr += escapeCharacter;
csvStr += ',';
csvStr += escapeCharacter;
csvStr += escapeCharacter;
} else if (resultVal[j] == '[' && isStringList) {
csvStr += "[";
csvStr += escapeCharacter;
csvStr += escapeCharacter;
} else if (resultVal[j] == ']' && isStringList) {
csvStr += escapeCharacter;
csvStr += escapeCharacter;
csvStr += "]";
} else {
csvStr += resultVal[j];
}
}
if (surroundQuotes) {
csvStr = escapeCharacter + std::move(csvStr) + escapeCharacter;
}
file << csvStr;
if (idx < nextTuple->len() - 1) {
file << delimiter;
} else {
file << newline;
}
}
}
file.close();
}

void QueryResult::validateQuerySucceed() const {
if (!success) {
throw Exception(errMsg);
Expand Down
21 changes: 0 additions & 21 deletions test/c_api/query_result_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,6 @@ TEST_F(CApiQueryResultTest, GetNext) {
kuzu_query_result_destroy(result);
}

TEST_F(CApiQueryResultTest, WriteToCSV) {
std::string newline = "\n";
std::string basicOutput =
R"(Carol,1,5.000000,1940-06-22,1911-08-20 02:32:21,CsWork)" + newline +
R"(Dan,2,4.800000,1950-07-23,2031-11-30 12:25:30,DEsWork)" + newline +
R"(Elizabeth,1,4.700000,1980-10-26,1976-12-23 11:21:42,DEsWork)" + newline;
auto query = "MATCH (a:person)-[:workAt]->(o:organisation) RETURN a.fName, a.gender,"
"a.eyeSight, a.birthdate, a.registerTime, o.name";
auto connection = getConnection();
auto result = kuzu_connection_query(connection, query);
ASSERT_TRUE(kuzu_query_result_is_success(result));
auto outputPath = databasePath + "/output_CSV_CAPI.csv";
kuzu_query_result_write_to_csv(result, outputPath.c_str(), ',', '"', '\n');
std::ifstream f(outputPath);
std::ostringstream ss;
ss << f.rdbuf();
std::string fileString = ss.str();
ASSERT_STREQ(fileString.c_str(), basicOutput.c_str());
kuzu_query_result_destroy(result);
}

TEST_F(CApiQueryResultTest, ResetIterator) {
auto connection = getConnection();
auto result = kuzu_connection_query(
Expand Down
2 changes: 0 additions & 2 deletions test/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ if(MSVC)
add_kuzu_api_test(main_test
arrow_test.cpp
connection_test.cpp
csv_output_test.cpp
prepare_test.cpp
result_value_test.cpp
storage_driver_test.cpp
Expand All @@ -12,7 +11,6 @@ else()
access_mode_test.cpp
arrow_test.cpp
connection_test.cpp
csv_output_test.cpp
db_locking_test.cpp
prepare_test.cpp
result_value_test.cpp
Expand Down
76 changes: 0 additions & 76 deletions test/main/csv_output_test.cpp

This file was deleted.

15 changes: 0 additions & 15 deletions tools/java_api/src/jni/kuzu_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,21 +467,6 @@ JNIEXPORT jstring JNICALL Java_com_kuzudb_KuzuNative_kuzu_1query_1result_1to_1st
return ret;
}

JNIEXPORT void JNICALL Java_com_kuzudb_KuzuNative_kuzu_1query_1result_1write_1to_1csv(JNIEnv* env,
jclass, jobject thisQR, jstring file_path, jchar delimiter, jchar escape_char, jchar new_line) {
QueryResult* qr = getQueryResult(env, thisQR);
const char* cpp_file_path = env->GetStringUTFChars(file_path, JNI_FALSE);

// TODO: confirm this convertion is ok to do.
// jchar is 16-bit unicode character so converting to char will lose the higher oreder-bits
char cpp_delimiter = static_cast<char>(delimiter);
char cpp_escape_char = static_cast<char>(escape_char);
char cpp_new_line = static_cast<char>(new_line);

qr->writeToCSV(cpp_file_path, cpp_delimiter, cpp_escape_char, cpp_new_line);
env->ReleaseStringUTFChars(file_path, cpp_file_path);
}

JNIEXPORT void JNICALL Java_com_kuzudb_KuzuNative_kuzu_1query_1result_1reset_1iterator(
JNIEnv* env, jclass, jobject thisQR) {
QueryResult* qr = getQueryResult(env, thisQR);
Expand Down
3 changes: 0 additions & 3 deletions tools/java_api/src/main/java/com/kuzudb/KuzuNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ protected static native KuzuDataType kuzu_query_result_get_column_data_type(

protected static native String kuzu_query_result_to_string(KuzuQueryResult query_result);

protected static native void kuzu_query_result_write_to_csv(KuzuQueryResult query_result,
String file_path, char delimiter, char escape_char, char new_line);

protected static native void kuzu_query_result_reset_iterator(KuzuQueryResult query_result);

// FlatTuple
Expand Down
14 changes: 0 additions & 14 deletions tools/java_api/src/main/java/com/kuzudb/KuzuQueryResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,6 @@ public String toString() {
return KuzuNative.kuzu_query_result_to_string(this);
}

/**
* Write the query result to CSV file.
* @param filePath: The path of the CSV file.
* @param delimiter: The delimiter of the CSV file.
* @param escapeChar: The escape character of the CSV file.
* @param newLine: The new line character of the CSV file.
* @throws KuzuObjectRefDestroyedException If the query result has been destroyed.
*
*/
public void writeToCsv(String filePath, char delimiter, char escapeChar, char newLine) throws KuzuObjectRefDestroyedException {
checkNotDestroyed();
KuzuNative.kuzu_query_result_write_to_csv(this, filePath, delimiter, escapeChar, newLine);
}

/**
* Reset the query result iterator.
* @throws KuzuObjectRefDestroyedException If the query result has been destroyed.
Expand Down
26 changes: 0 additions & 26 deletions tools/java_api/src/test/java/com/kuzudb/test/QueryResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,6 @@ void QueryResultGetNext() throws KuzuObjectRefDestroyedException {
result.destroy();
}

@Test
void QueryResultWriteToCSV() throws IOException, KuzuObjectRefDestroyedException {
String query = "MATCH (a:person)-[:workAt]->(o:organisation) RETURN a.fName, a.gender," +
"a.eyeSight, a.birthdate, a.registerTime, o.name";
KuzuQueryResult result = conn.query(query);
assertTrue(result.isSuccess());

final Path tempFile = Files.createFile(tempDir.resolve("test.csv"));
String outputPath = tempFile.toFile().getAbsolutePath();
result.writeToCsv(outputPath, ',', '"', '\n');

try {
File csv = new File(outputPath);
Scanner scanner = new Scanner(csv);
String line = scanner.nextLine();
assertEquals(line, "Carol,1,5.000000,1940-06-22,1911-08-20 02:32:21,CsWork");
line = scanner.nextLine();
assertEquals(line, "Dan,2,4.800000,1950-07-23,2031-11-30 12:25:30,DEsWork");
line = scanner.nextLine();
assertEquals(line, "Elizabeth,1,4.700000,1980-10-26,1976-12-23 11:21:42,DEsWork");
scanner.close();
} catch (FileNotFoundException e) {
fail("QueryResultWriteToCSV failed, csv file not found");
}
}

@Test
void QueryResultResetIterator() throws KuzuObjectRefDestroyedException {
KuzuQueryResult result = conn.query("MATCH (a:person) RETURN a.fName, a.age ORDER BY a.fName");
Expand Down
3 changes: 0 additions & 3 deletions tools/python_api/src_cpp/include/py_query_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ class PyQueryResult {

py::list getNext();

void writeToCSV(const py::str& filename, const py::str& delimiter,
const py::str& escapeCharacter, const py::str& newline);

void close();

static py::object convertValueToPyObject(const kuzu::common::Value& value);
Expand Down
15 changes: 0 additions & 15 deletions tools/python_api/src_cpp/py_query_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ void PyQueryResult::initialize(py::handle& m) {
py::class_<PyQueryResult>(m, "result")
.def("hasNext", &PyQueryResult::hasNext)
.def("getNext", &PyQueryResult::getNext)
.def("writeToCSV", &PyQueryResult::writeToCSV, py::arg("filename"),
py::arg("delimiter") = ",", py::arg("escapeCharacter") = "\"",
py::arg("newline") = "\n")
.def("close", &PyQueryResult::close)
.def("getAsDF", &PyQueryResult::getAsDF)
.def("getAsArrow", &PyQueryResult::getAsArrow)
Expand Down Expand Up @@ -54,18 +51,6 @@ py::list PyQueryResult::getNext() {
return result;
}

void PyQueryResult::writeToCSV(const py::str& filename, const py::str& delimiter,
const py::str& escapeCharacter, const py::str& newline) {
std::string delimiterStr = delimiter;
std::string escapeCharacterStr = escapeCharacter;
std::string newlineStr = newline;
KU_ASSERT(delimiterStr.size() == 1);
KU_ASSERT(escapeCharacterStr.size() == 1);
KU_ASSERT(newlineStr.size() == 1);
queryResult->writeToCSV(
std::string(filename), delimiterStr[0], escapeCharacterStr[0], newlineStr[0]);
}

void PyQueryResult::close() {
// Note: Python does not guarantee objects to be deleted in the reverse order. Therefore, we
// expose close() interface so that users can explicitly call close() and ensure that
Expand Down
23 changes: 0 additions & 23 deletions tools/python_api/src_py/query_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,6 @@ def get_next(self):
self.check_for_query_result_close()
return self._query_result.getNext()

def write_to_csv(self, filename, delimiter=',', escape_character='"', newline='\n'):
"""
Write the query result to a CSV file.
Parameters
----------
filename : str
Name of the CSV file to write to.
delimiter : str
Delimiter to use in the CSV file. Defaults to ','.
escape_character : str
Escape character to use in the CSV file. Defaults to '"'.
newline : str
Newline character to use in the CSV file. Defaults to '\\n'.
"""

self.check_for_query_result_close()
self._query_result.writeToCSV(
filename, delimiter, escape_character, newline)

def close(self):
"""
Close the query result.
Expand Down
Loading

0 comments on commit 351ea0c

Please sign in to comment.