Skip to content

Commit

Permalink
fix path
Browse files Browse the repository at this point in the history
  • Loading branch information
hououou committed Mar 21, 2024
1 parent 4a7b109 commit 02f8798
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions src/binder/bind/bind_import_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "binder/copy/bound_import_database.h"
#include "common/exception/binder.h"
#include "common/file_system/virtual_file_system.h"
#include "common/string_utils.h"
#include "parser/copy.h"
#include "parser/parser.h"
#include "parser/port_db.h"

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

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

std::string convertSeparators(const std::string& path) {
auto separatorStr = getPathSeparator();
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, "/");
return returnPath;
}

std::string extractTableName(const std::string& path) {
if (path.empty()) {
return std::string();
}
auto normalizedPath = convertSeparators(path);
auto splits = StringUtils::split(normalizedPath, "/");
KU_ASSERT(!splits.empty());
return splits.back();
}

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 @@ -37,9 +71,29 @@ 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);
// replace the path in copy from statement with the bound path
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->getSource()->type == common::ScanSourceType::FILE);
auto filePaths = ku_dynamic_cast<parser::BaseScanSource*, parser::FileScanSource*>(
copyFromStatement->getSource())
->filePaths;
KU_ASSERT(filePaths.size() == 1);
auto csvFilePath = filePaths[0];
auto extractedFileName = extractTableName(csvFilePath);
csvFilePath = 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

0 comments on commit 02f8798

Please sign in to comment.