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

Patchset post v3.0.0 #299

Merged
merged 6 commits into from
Feb 19, 2025
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
3 changes: 2 additions & 1 deletion .github/workflows/build_linux_arm64_wheels-gh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@ jobs:
gh release upload ${{ github.ref_name }} linux-aarch64-libchdb.tar.gz --clobber
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
path: |
./dist/*.whl
./linux-aarch64-libchdb.tar.gz
overwrite: true
- name: Upload pypi
if: startsWith(github.ref, 'refs/tags/v')
run: |
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/build_linux_x86_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ jobs:
gh release upload ${{ github.ref_name }} linux-x86_64-libchdb.tar.gz --clobber
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
path: |
./dist/*.whl
./linux-x86_64-libchdb.tar.gz
overwrite: true
- name: Upload pypi
if: startsWith(github.ref, 'refs/tags/v')
run: |
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/build_macos_x86_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,12 @@ jobs:
gh release upload ${{ github.ref_name }} macos-x86_64-libchdb.tar.gz --clobber
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
path: |
./dist/*.whl
./macos-x86_64-libchdb.tar.gz
overwrite: true
- name: Upload pypi
if: startsWith(github.ref, 'refs/tags/v')
run: |
Expand Down
2 changes: 1 addition & 1 deletion chdb/state/sqlitelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def cursor(self) -> "Cursor":
self._cursor = Cursor(self._conn)
return self._cursor

def query(self, query: str, format: str = "ArrowStream") -> Any:
def query(self, query: str, format: str = "CSV") -> Any:
return self._conn.query(query, format)

def close(self) -> None:
Expand Down
35 changes: 1 addition & 34 deletions programs/local/LocalChdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,38 +260,6 @@ connection_wrapper::build_clickhouse_args(const std::string & path, const std::m
return argv;
}

void connection_wrapper::initialize_database()
{
if (is_readonly)
{
return;
}
if (is_memory_db)
{
// Setup memory engine
query_result * ret = query("CREATE DATABASE IF NOT EXISTS default ENGINE = Memory; USE default");
if (ret->has_error())
{
auto err_msg = fmt::format("Failed to create memory database: {}", std::string(ret->error_message()));
delete ret;
throw std::runtime_error(err_msg);
}
}
else
{
// Create directory if it doesn't exist
std::filesystem::create_directories(db_path);
// Setup Atomic database
query_result * ret = query("CREATE DATABASE IF NOT EXISTS default ENGINE = Atomic; USE default");
if (ret->has_error())
{
auto err_msg = fmt::format("Failed to create database: {}", std::string(ret->error_message()));
delete ret;
throw std::runtime_error(err_msg);
}
}
}

connection_wrapper::connection_wrapper(const std::string & conn_str)
{
auto [path, params] = parse_connection_string(conn_str);
Expand All @@ -307,7 +275,6 @@ connection_wrapper::connection_wrapper(const std::string & conn_str)
conn = connect_chdb(argv_char.size(), argv_char.data());
db_path = path;
is_memory_db = (path == ":memory:");
initialize_database();
}

connection_wrapper::~connection_wrapper()
Expand Down Expand Up @@ -346,7 +313,7 @@ query_result * connection_wrapper::query(const std::string & query_str, const st
{
throw std::runtime_error(result->error_message);
}
return new query_result(result, true);
return new query_result(result, false);
}

void cursor_wrapper::execute(const std::string & query_str)
Expand Down
1 change: 0 additions & 1 deletion programs/local/LocalChdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class connection_wrapper
// Move the private methods declarations here
std::pair<std::string, std::map<std::string, std::string>> parse_connection_string(const std::string & conn_str);
std::vector<std::string> build_clickhouse_args(const std::string & path, const std::map<std::string, std::string> & params);
void initialize_database();
};

class local_result_wrapper
Expand Down
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
Loading