Skip to content

Commit

Permalink
Fix export database regression
Browse files Browse the repository at this point in the history
  • Loading branch information
andyfengHKU committed Mar 28, 2024
1 parent 2ec13b2 commit 17a19d5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 0 additions & 7 deletions src/binder/bind/bind_export_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "catalog/catalog_entry/node_table_catalog_entry.h"
#include "catalog/catalog_entry/rel_table_catalog_entry.h"
#include "common/exception/binder.h"
#include "common/file_system/virtual_file_system.h"
#include "common/string_utils.h"
#include "main/client_context.h"
#include "parser/parser.h"
Expand Down Expand Up @@ -93,12 +92,6 @@ std::unique_ptr<BoundStatement> Binder::bindExportDatabaseClause(const Statement
if (fileType != FileType::CSV && parsedOptions.size() != 0) {
throw BinderException{"Only export to csv can have options."};
}
auto fs = clientContext->getVFSUnsafe();
if (!fs->fileOrPathExists(boundFilePath)) {
fs->createDir(boundFilePath);
} else {
throw BinderException(stringFormat("Directory {} already exists.", boundFilePath));
}
return std::make_unique<BoundExportDatabase>(
boundFilePath, fileType, std::move(exportData), std::move(parsedOptions));
}
Expand Down
12 changes: 12 additions & 0 deletions src/processor/map/map_port_db.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common/file_system/virtual_file_system.h"
#include "planner/operator/persistent/logical_export_db.h"
#include "planner/operator/persistent/logical_import_db.h"
#include "processor/operator/persistent/export_db.h"
Expand All @@ -20,6 +21,17 @@ std::unique_ptr<PhysicalOperator> PlanMapper::mapExportDatabase(
auto childPhysicalOperator = mapOperator(childCopyTo.get());
children.push_back(std::move(childPhysicalOperator));
}
// Ideally we should create directory inside operator but ExportDatabase is executed before
// CopyTo which requires the directory to exist. So we create directory in mapper inside.
auto fs = clientContext->getVFSUnsafe();
auto boundFileInfo = exportDatabase->getBoundFileInfo();
KU_ASSERT(boundFileInfo->filePaths.size() == 1);
auto filePath = boundFileInfo->filePaths[0];
if (!fs->fileOrPathExists(filePath)) {
fs->createDir(filePath);
} else {
throw RuntimeException(stringFormat("Directory {} already exists.", filePath));
}
std::unique_ptr<ResultSetDescriptor> resultSetDescriptor;
return std::make_unique<ExportDB>(exportDatabase->getBoundFileInfo()->copy(), getOperatorID(),
exportDatabase->getExpressionsForPrinting(), std::move(children));
Expand Down
7 changes: 7 additions & 0 deletions test/main/prepare_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,10 @@ TEST_F(ApiTest, issueTest4) {
checkTuple(result->getNext().get(), "-123456789\n");
ASSERT_FALSE(result->hasNext());
}

TEST_F(ApiTest, PrepareExport) {
auto newDBPath = databasePath + "/newdb";
auto preparedStatement = conn->prepare("EXPORT DATABASE '" + newDBPath + '\'');
auto result = conn->execute(preparedStatement.get());
ASSERT_TRUE(result->isSuccess());
}
2 changes: 1 addition & 1 deletion test/test_files/copy/export_import_db.test
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
50
-STATEMENT Export Database "${KUZU_EXPORT_DB_DIRECTORY}_case2/demo-db2" (format='csv', header=true)
---- error
Binder exception: Directory ${KUZU_EXPORT_DB_DIRECTORY}_case2/demo-db2 already exists.
Runtime exception: Directory ${KUZU_EXPORT_DB_DIRECTORY}_case2/demo-db2 already exists.

-CASE ExportImportDatabaseWithPARQUET
-STATEMENT Export Database "${KUZU_EXPORT_DB_DIRECTORY}_case4/demo-db3" (format='parquet')
Expand Down

0 comments on commit 17a19d5

Please sign in to comment.