Skip to content

Commit

Permalink
Remove legacy hacking in USE and SET clause
Browse files Browse the repository at this point in the history
  • Loading branch information
auxten committed Feb 12, 2025
1 parent 4f709cd commit 91b0aed
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 113 deletions.
65 changes: 0 additions & 65 deletions programs/local/LocalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,71 +833,6 @@ void LocalServer::processConfig()
global_context->getUserDefinedSQLObjectsStorage().loadObjects();

LOG_DEBUG(log, "Loaded metadata.");

/** Set default database if it is specified in default_database file.
* NOTE: We do it after loading metadata to let the '_local' and 'system' database initialization
* to be done correctly.
* I have tried to set default database during parsing '--database' option, but it leads to
* "Code: 82. DB::Exception: Database db_xxx already exists.: while loading database `db_xxx`
* from path .state_tmp_auxten_usedb_/metadata/db_xxx. (DATABASE_ALREADY_EXISTS)"
* This will also happen if we call:
* `clickhouse local --database=db_xxx --path=.state_tmp_auxten_usedb_ --query="select * FROM log_table_xxx"`
* with existing `db_xxx` database in the '.state_tmp_auxten_usedb_' directory.
*/
auto default_database_path = fs::path(path) / "default_database";
if (std::filesystem::exists(default_database_path))
{
std::ifstream ifs(default_database_path);
std::string user_default_database;
if (ifs.is_open())
{
ifs >> user_default_database;
// strip default_database
user_default_database.erase(
std::remove_if(
user_default_database.begin(), user_default_database.end(), [](unsigned char x) { return std::isspace(x); }),
user_default_database.end());
if (!user_default_database.empty())
{
global_context->setCurrentDatabase(user_default_database);
LOG_DEBUG(log, "Set default database to {} recorded in {}", user_default_database, default_database_path);
}
ifs.close();
}
else
{
LOG_ERROR(log, "Cannot read default database from {}", default_database_path);
}
}

/** Replay SET statements saved from last query() of chDB.
* See `InterpreterSetQuery::execute()` for details.
*/
auto set_path = fs::path(path) / "set_statements";
if (std::filesystem::exists(set_path))
{
std::ifstream ifs(set_path);
std::string set_statement;
while (std::getline(ifs, set_statement))
{
if (!set_statement.empty())
{
// We should not use processQueryText(set_statement) here to avoid the statement to be echoed.
// Just construct AST changes and apply them.
ParserSetQuery parser;
const char * start = set_statement.data();
ASTPtr ast = parseQuery(start, start + set_statement.size(), global_context->getSettingsRef(), false);
auto * set_query = typeid_cast<ASTSetQuery *>(ast.get());
if (set_query)
{
// Get the changes from the ASTSetQuery
const auto & changes = set_query->changes;
// Process the changes as needed
global_context->applySettingsChanges(changes);
}
}
}
}
}
else if (!getClientConfiguration().has("no-system-tables"))
{
Expand Down
26 changes: 0 additions & 26 deletions src/Interpreters/InterpreterSetQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@
#include <Parsers/ASTQueryWithOutput.h>
#include <Parsers/ASTSelectWithUnionQuery.h>

#include <filesystem>
#include <fstream>

namespace DB
{

namespace ErrorCodes
{
extern const int CANNOT_OPEN_FILE;
}

BlockIO InterpreterSetQuery::execute()
{
Expand All @@ -28,25 +21,6 @@ BlockIO InterpreterSetQuery::execute()
session_context->applySettingsChanges(ast.changes);
session_context->addQueryParameters(NameToNameMap{ast.query_parameters.begin(), ast.query_parameters.end()});
session_context->resetSettingsToDefaultValue(ast.default_settings);

// Define the path for the file where SET statements will be logged. Assuming `getContext()->getPath()`
// provides a base directory suitable for such logs.
auto set_statements_path = std::filesystem::path(getContext()->getPath()) / "set_statements";
// Open the log file in append mode. If the file doesn't exist, it will be created.
std::ofstream set_statements_fs(set_statements_path, std::ofstream::out | std::ofstream::app);
if (!set_statements_fs.is_open())
throw Exception(ErrorCodes::CANNOT_OPEN_FILE, "Cannot open file {} for appending", set_statements_path.string());

// Loop through each setting change requested in the SET query. This assumes the primary intent
// is to log the names and values of the settings being changed.
for (const auto & change : ast.changes)
{
// Write the SET command for each setting change to the log file, one per line.
set_statements_fs << "SET " << change.name << " = " << toString(change.value) << ";\n";
}

// Close the file stream after logging all SET commands.
set_statements_fs.close();
return {};
}

Expand Down
22 changes: 0 additions & 22 deletions src/Interpreters/InterpreterUseQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,16 @@
#include <Common/typeid_cast.h>

#include <filesystem>
#include <fstream>

namespace DB
{

namespace ErrorCodes
{
extern const int CANNOT_OPEN_FILE;
}

BlockIO InterpreterUseQuery::execute()
{
const String & new_database = query_ptr->as<ASTUseQuery &>().getDatabase();
getContext()->checkAccess(AccessType::SHOW_DATABASES, new_database);
getContext()->getSessionContext()->setCurrentDatabase(new_database);

// Save the current using database in default_database stored in getPath()
// for the case when the database is changed in chDB session.
// The default_database content is used in the LocalServer::processConfig() method.
auto default_database_path = std::filesystem::path(getContext()->getPath()) / "default_database";
std::ofstream tmp_path_fs(default_database_path, std::ofstream::out | std::ofstream::trunc);
if (tmp_path_fs.is_open())
{
tmp_path_fs << new_database;
tmp_path_fs.close();
}
//chdb todo: fix the following code on bgClickHouseLocal mode
// else
// {
// throw Exception(ErrorCodes::CANNOT_OPEN_FILE, "Cannot open file {} for writing", default_database_path.string());
// }

return {};
}

Expand Down

0 comments on commit 91b0aed

Please sign in to comment.