Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix IMPORT_DATABASE path #3063

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/binder/bind/bind_import_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

#include "binder/binder.h"
#include "binder/copy/bound_import_database.h"
#include "common/copier_config/csv_reader_config.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,7 @@ using namespace kuzu::parser;
namespace kuzu {
namespace binder {

std::string getFilePath(
static 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 +40,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) {
hououou marked this conversation as resolved.
Show resolved Hide resolved
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 = stringFormat("COPY {} FROM \"{}\" {};", copyFromStatement->getTableName(),
copyFilePath, csvConfig.option.toCypher());
finalQueryStatements += csvQuery;
hououou marked this conversation as resolved.
Show resolved Hide resolved
}
finalQueryStatements += getQueryFromFile(fs, boundFilePath, ImportDBConstants::MACRO_NAME);
return std::make_unique<BoundImportDatabase>(boundFilePath, finalQueryStatements);
}
} // namespace binder
Expand Down
9 changes: 2 additions & 7 deletions src/catalog/catalog_entry/node_table_catalog_entry.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "catalog/catalog_entry/node_table_catalog_entry.h"

#include <sstream>

namespace kuzu {
namespace catalog {

Expand Down Expand Up @@ -44,11 +42,8 @@ std::unique_ptr<CatalogEntry> NodeTableCatalogEntry::copy() const {
}

std::string NodeTableCatalogEntry::toCypher(main::ClientContext* /*clientContext*/) const {
std::stringstream ss;
ss << "CREATE NODE TABLE " << getName() << "(";
Property::toCypher(getPropertiesRef(), ss);
ss << " PRIMARY KEY(" << getPrimaryKey()->getName() << "));";
return ss.str();
return common::stringFormat("CREATE NODE TABLE {} ({} PRIMARY KEY({}));", getName(),
Property::toCypher(getPropertiesRef()), getPrimaryKey()->getName());
}

} // namespace catalog
Expand Down
12 changes: 6 additions & 6 deletions src/catalog/catalog_entry/rel_table_catalog_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ std::string RelTableCatalogEntry::toCypher(main::ClientContext* clientContext) c
auto catalog = clientContext->getCatalog();
auto srcTableName = catalog->getTableName(clientContext->getTx(), srcTableID);
auto dstTableName = catalog->getTableName(clientContext->getTx(), dstTableID);
ss << "CREATE REL TABLE " << getName() << "( FROM " << srcTableName << " TO " << dstTableName
<< ", ";
Property::toCypher(getPropertiesRef(), ss);
auto srcMultiStr = srcMultiplicity == RelMultiplicity::MANY ? "MANY" : "ONE";
auto dstMultiStr = dstMultiplicity == RelMultiplicity::MANY ? "MANY" : "ONE";
ss << srcMultiStr << "_" << dstMultiStr << ");";
auto srcMultiStr = srcMultiplicity == common::RelMultiplicity::MANY ? "MANY" : "ONE";
auto dstMultiStr = dstMultiplicity == common::RelMultiplicity::MANY ? "MANY" : "ONE";
std::string tableInfo =
stringFormat("CREATE REL TABLE {} (FROM {} TO {}, ", getName(), srcTableName, dstTableName);
ss << tableInfo << Property::toCypher(getPropertiesRef()) << srcMultiStr << "_" << dstMultiStr
<< ");";
return ss.str();
}

Expand Down
5 changes: 3 additions & 2 deletions src/catalog/property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Property Property::deserialize(Deserializer& deserializer) {
return Property(name, std::move(dataType), propertyID, tableID);
}

void Property::toCypher(
const std::vector<kuzu::catalog::Property>& properties, std::stringstream& ss) {
std::string Property::toCypher(const std::vector<kuzu::catalog::Property>& properties) {
std::stringstream ss;
for (auto& prop : properties) {
if (prop.getDataType()->getPhysicalType() == PhysicalTypeID::INTERNAL_ID) {
continue;
Expand All @@ -42,6 +42,7 @@ void Property::toCypher(
}
ss << prop.getName() << " " << propStr << ",";
}
return ss.str();
}

} // namespace catalog
Expand Down
9 changes: 3 additions & 6 deletions src/function/scalar_macro_function.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "function/scalar_macro_function.h"

#include <sstream>

#include "common/serializer/deserializer.h"
#include "common/serializer/serializer.h"
#include "common/string_format.h"
#include "common/string_utils.h"

using namespace kuzu::common;
Expand Down Expand Up @@ -59,17 +58,15 @@ std::unique_ptr<ScalarMacroFunction> ScalarMacroFunction::deserialize(Deserializ
}

std::string ScalarMacroFunction::toCypher(const std::string& name) const {
std::stringstream ss;
std::vector<std::string> paramStrings;
for (auto& param : positionalArgs) {
paramStrings.push_back(param);
}
for (auto& defaultParam : defaultArgs) {
paramStrings.push_back(defaultParam.first + ":=" + defaultParam.second->toString());
}
ss << "CREATE MACRO " << name << "(" << StringUtils::join(paramStrings, ",") << ") AS "
<< expression->toString();
return ss.str();
return stringFormat("CREATE MACRO {} ({}) AS {}", name, StringUtils::join(paramStrings, ","),
expression->toString());
}
} // namespace function
} // namespace kuzu
3 changes: 1 addition & 2 deletions src/include/catalog/property.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class Property {
void serialize(common::Serializer& serializer) const;
static Property deserialize(common::Deserializer& deserializer);

static void toCypher(
const std::vector<kuzu::catalog::Property>& properties, std::stringstream& ss);
static std::string toCypher(const std::vector<kuzu::catalog::Property>& properties);

private:
Property(const Property& other)
Expand Down
15 changes: 4 additions & 11 deletions src/include/common/copier_config/csv_reader_config.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <sstream>

#include "common/constants.h"
#include "common/copy_constructors.h"
#include "common/types/value/value.h"
Expand All @@ -23,16 +21,11 @@ struct CSVOption {
hasHeader{CopyConstants::DEFAULT_CSV_HAS_HEADER} {}
EXPLICIT_COPY_DEFAULT_MOVE(CSVOption);

// TODO: COPY FROM and COPY TO should support transform special options, like '\'.
std::string toCypher() const {
std::stringstream ss;
ss << " (escape = '\\" << escapeChar << "' , delim = '" << delimiter << "' , quote = '\\"
<< quoteChar << "', header=";
if (hasHeader) {
ss << "true);";
} else {
ss << "false);";
}
return ss.str();
std::string header = hasHeader ? "true" : "false";
return stringFormat("(escape ='\\{}', delim ='{}', quote='\\{}', header={})", escapeChar,
delimiter, quoteChar, header);
}

private:
Expand Down
6 changes: 2 additions & 4 deletions src/processor/operator/persistent/export_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ static void writeStringStreamToFile(

static void writeCopyStatement(
stringstream& ss, std::string tableName, ReaderConfig* boundFileInfo) {
ss << "COPY ";
ss << tableName << " FROM \"" << boundFileInfo->filePaths[0] << "/" << tableName;
auto fileTypeStr = FileTypeUtils::toString(boundFileInfo->fileType);
StringUtils::toLower(fileTypeStr);
ss << "." << fileTypeStr;
auto csvConfig = common::CSVReaderConfig::construct(boundFileInfo->options);
ss << "\"" << csvConfig.option.toCypher() << std::endl;
ss << stringFormat("COPY {} FROM \"{}.{}\" {};\n", tableName, tableName, fileTypeStr,
csvConfig.option.toCypher());
}

std::string getSchemaCypher(main::ClientContext* clientContext, transaction::Transaction* tx) {
Expand Down
Loading