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 c3decc2 commit 5bd546e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
28 changes: 24 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,8 @@
#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 +14,7 @@ using namespace kuzu::parser;
namespace kuzu {
namespace binder {

std::string getFilePath(
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 +39,27 @@ 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);
// replace the path in copy from statement with the bound path
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->getSource()->type == common::ScanSourceType::FILE);
auto filePaths = ku_dynamic_cast<parser::BaseScanSource*, parser::FileScanSource*>(
copyFromStatement->getSource())
->filePaths;
KU_ASSERT(filePaths.size() == 1);
auto copyFilePath = boundFilePath + "/" + filePaths[0];
auto csvConfig = CSVReaderConfig::construct(
bindParsingOptions(copyFromStatement->getParsingOptionsRef()));
auto csvQuery = "COPY " + copyFromStatement->getTableName() + " FROM '" + copyFilePath +
"' " + csvConfig.option.toCypher() + "\n";
finalQueryStatements += csvQuery;
}
finalQueryStatements += getQueryFromFile(fs, boundFilePath, ImportDBConstants::MACRO_NAME);
return std::make_unique<BoundImportDatabase>(boundFilePath, finalQueryStatements);
}
} // namespace binder
Expand Down
2 changes: 1 addition & 1 deletion src/processor/operator/persistent/export_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void writeStringStreamToFile(
static void writeCopyStatement(
stringstream& ss, std::string tableName, ReaderConfig* boundFileInfo) {
ss << "COPY ";
ss << tableName << " FROM \"" << boundFileInfo->filePaths[0] << "/" << tableName;
ss << tableName << " FROM \"" << tableName;
auto fileTypeStr = FileTypeUtils::toString(boundFileInfo->fileType);
StringUtils::toLower(fileTypeStr);
ss << "." << fileTypeStr;
Expand Down

0 comments on commit 5bd546e

Please sign in to comment.