Skip to content

Commit

Permalink
fix path
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuzu CI committed Mar 15, 2024
1 parent 4a7b109 commit 9951547
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/binder/bind/bind_import_database.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include <fcntl.h>

#include "binder/binder.h"
#include "binder/copy/bound_file_scan_info.h"
#include "binder/copy/bound_import_database.h"
#include "common/exception/binder.h"
#include "common/file_system/virtual_file_system.h"
#include "parser/copy.h"
#include "parser/parser.h"
#include "parser/port_db.h"

using namespace kuzu::common;
Expand All @@ -12,7 +15,26 @@ using namespace kuzu::parser;
namespace kuzu {
namespace binder {

std::string getFilePath(
// std::string replaceCopyFromPath(common::VirtualFileSystem* vfs, const std::string boundFilePath,
// std::string query){
// auto parsedStatements = Parser::parseQuery(query);
// auto result = std::string();
// for(auto &parsedStatement : parsedStatements){
// KU_ASSERT(parsedStatement->getStatementType()==StatementType::COPY_FROM);
// auto copyFromStatement = ku_dynamic_cast<const Statement*, const
// CopyFrom*>(parsedStatement.get()); KU_ASSERT(copyFromStatement->getFilePath().size()==1);
// auto csvFilePath = copyFromStatement->getFilePath()[0];
// auto extractedFileName = vfs->extractName(csvFilePath);
// csvFilePath=vfs->joinPath(boundFilePath, extractedFileName);
// auto csvConfig =
// CSVReaderConfig::construct(bindParsingOptions(copyFromStatement->getParsingOptionsRef()));
// result+="COPY "+ copyFromStatement->getTableName()+ " FROM '"+csvFilePath+"
// "+csvConfig.option.toCypher()+"';";
// }
// return result;
//}

std::string getQueryFromFile(
common::VirtualFileSystem* vfs, const std::string boundFilePath, const std::string fileName) {
auto filePath = vfs->joinPath(boundFilePath, fileName);
if (!vfs->fileOrPathExists(filePath)) {
Expand All @@ -26,7 +48,10 @@ std::string getFilePath(
auto fsize = fileInfo->getFileSize();
auto buffer = std::make_unique<char[]>(fsize);
fileInfo->readFile(buffer.get(), fsize);
return std::string(buffer.get(), fsize);
auto query = std::string(buffer.get(), fsize);
return query;
// fileName==ImportDBConstants::COPY_NAME?
// replaceCopyFromPath(vfs,boundFilePath,query):std::string(buffer.get(), fsize);
}

std::unique_ptr<BoundStatement> Binder::bindImportDatabaseClause(const Statement& statement) {
Expand All @@ -37,9 +62,24 @@ std::unique_ptr<BoundStatement> Binder::bindImportDatabaseClause(const Statement
throw BinderException(stringFormat("Directory {} does not exist.", boundFilePath));
}
std::string finalQueryStatements;
finalQueryStatements += getFilePath(fs, boundFilePath, ImportDBConstants::SCHEMA_NAME);
finalQueryStatements += getFilePath(fs, boundFilePath, ImportDBConstants::COPY_NAME);
finalQueryStatements += getFilePath(fs, boundFilePath, ImportDBConstants::MACRO_NAME);
finalQueryStatements += getQueryFromFile(fs, boundFilePath, ImportDBConstants::SCHEMA_NAME);
auto copyQuery = getQueryFromFile(fs, boundFilePath, ImportDBConstants::COPY_NAME);
auto parsedStatements = Parser::parseQuery(copyQuery);
for (auto& parsedStatement : parsedStatements) {
KU_ASSERT(parsedStatement->getStatementType() == StatementType::COPY_FROM);
auto copyFromStatement =
ku_dynamic_cast<const Statement*, const CopyFrom*>(parsedStatement.get());
KU_ASSERT(copyFromStatement->getFilePath().size() == 1);
auto csvFilePath = copyFromStatement->getFilePath()[0];
auto extractedFileName = fs->extractName(csvFilePath);
csvFilePath = fs->joinPath(boundFilePath, extractedFileName);
auto csvConfig = CSVReaderConfig::construct(
bindParsingOptions(copyFromStatement->getParsingOptionsRef()));
auto csvQuery = "COPY " + copyFromStatement->getTableName() + " FROM '" + csvFilePath +
"' " + csvConfig.option.toCypher() + "\n";
finalQueryStatements += csvQuery;
}
finalQueryStatements += getQueryFromFile(fs, boundFilePath, ImportDBConstants::MACRO_NAME);
return std::make_unique<BoundImportDatabase>(boundFilePath, finalQueryStatements);
}
} // namespace binder
Expand Down
34 changes: 34 additions & 0 deletions src/common/file_system/file_system.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "common/file_system/file_system.h"

#include "common/string_utils.h"

namespace kuzu {
namespace common {

Expand Down Expand Up @@ -40,5 +42,37 @@ void FileSystem::truncate(FileInfo* /*fileInfo*/, uint64_t /*size*/) const {
KU_UNREACHABLE;
}

std::string FileSystem::pathSeparator() {
auto separatorStr = "/";
#ifdef _WIN32
separatorStr = "\\";
#endif
return separatorStr;
}

std::string FileSystem::convertSeparators(const std::string& path) {
auto separatorStr = pathSeparator();
char separator = separatorStr[0];
if (separator == '/') {
// on unix-based systems we only accept / as a separator
return path;
}
// on windows-based systems we accept both
auto returnPath = path;
StringUtils::replaceAll(returnPath, "/", separatorStr);

Check warning on line 62 in src/common/file_system/file_system.cpp

View check run for this annotation

Codecov / codecov/patch

src/common/file_system/file_system.cpp#L62

Added line #L62 was not covered by tests
return returnPath;
}

std::string FileSystem::extractName(const std::string& path) {
if (path.empty()) {
return std::string();
}
auto normalized_path = convertSeparators(path);
auto sep = pathSeparator();
auto splits = StringUtils::split(normalized_path, sep);
KU_ASSERT(!splits.empty());
return splits.back();
}

} // namespace common
} // namespace kuzu
6 changes: 6 additions & 0 deletions src/include/common/file_system/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class KUZU_API FileSystem {

virtual bool canHandleFile(const std::string& /*path*/) const { KU_UNREACHABLE; }

std::string pathSeparator();

std::string extractName(const std::string& path);

std::string convertSeparators(const std::string& path);

protected:
virtual void readFromFile(
FileInfo* fileInfo, void* buffer, uint64_t numBytes, uint64_t position) const = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/include/parser/copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class CopyFrom : public Copy {
inline bool byColumn() const { return byColumn_; }

inline BaseScanSource* getSource() const { return source.get(); }
inline std::vector<std::string> getFilePath() const {
KU_ASSERT(source->type == common::ScanSourceType::FILE);
auto fileSource = dynamic_cast<FileScanSource*>(source.get());
return fileSource->filePaths;
}

inline std::string getTableName() const { return tableName; }

Expand Down

0 comments on commit 9951547

Please sign in to comment.