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

Current setting/version function #1760

Merged
merged 1 commit into from
Jul 5, 2023
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
2 changes: 1 addition & 1 deletion src/binder/bind/bind_reading_clause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ std::unique_ptr<BoundReadingClause> Binder::bindInQueryCall(const ReadingClause&
catalog.getBuiltInTableOperation()->mathTableOperation(callStatement.getFuncName());
auto boundExpr = expressionBinder.bindLiteralExpression(*callStatement.getParameter());
auto inputValue = reinterpret_cast<LiteralExpression*>(boundExpr.get())->getValue();
auto bindData = tableFunctionDefinition->bindFunc(
auto bindData = tableFunctionDefinition->bindFunc(clientContext,
function::TableFuncBindInput{std::vector<common::Value>{*inputValue}},
catalog.getReadOnlyVersion());
expression_vector outputExpressions;
Expand Down
3 changes: 3 additions & 0 deletions src/function/built_in_table_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace function {

void BuiltInTableOperations::registerTableOperations() {
tableOperations.insert({common::TABLE_INFO_FUNC_NAME, TableInfoOperation::getDefinitions()});
tableOperations.insert({common::DB_VERSION_FUNC_NAME, DBVersionOperation::getDefinitions()});
tableOperations.insert(
{common::CURRENT_SETTING_FUNC_NAME, CurrentSettingOperation::getDefinitions()});
}

TableOperationDefinition* BuiltInTableOperations::mathTableOperation(const std::string& name) {
Expand Down
82 changes: 61 additions & 21 deletions src/function/table_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,6 @@
namespace kuzu {
namespace function {

std::unique_ptr<TableInfoBindData> TableInfoOperation::bindFunc(
kuzu::function::TableFuncBindInput input, catalog::CatalogContent* catalog) {
std::vector<std::string> returnColumnNames;
std::vector<common::LogicalType> returnTypes;
auto tableName = input.inputs[0].getValue<std::string>();
auto tableID = catalog->getTableID(tableName);
auto schema = catalog->getTableSchema(tableID);
returnColumnNames.emplace_back("property id");
returnTypes.emplace_back(common::LogicalTypeID::INT64);
returnColumnNames.emplace_back("name");
returnTypes.emplace_back(common::LogicalTypeID::STRING);
returnColumnNames.emplace_back("type");
returnTypes.emplace_back(common::LogicalTypeID::STRING);
if (schema->isNodeTable) {
returnColumnNames.emplace_back("primary key");
returnTypes.emplace_back(common::LogicalTypeID::BOOL);
}
return std::make_unique<TableInfoBindData>(
schema, std::move(returnTypes), std::move(returnColumnNames), schema->getNumProperties());
}

void TableInfoOperation::tableFunc(std::pair<common::offset_t, common::offset_t> morsel,
function::TableFuncBindData* bindData, std::vector<common::ValueVector*> outputVectors) {
auto tableSchema = reinterpret_cast<function::TableInfoBindData*>(bindData)->tableSchema;
Expand All @@ -52,5 +31,66 @@ void TableInfoOperation::tableFunc(std::pair<common::offset_t, common::offset_t>
}
}

std::unique_ptr<TableInfoBindData> TableInfoOperation::bindFunc(main::ClientContext* context,
kuzu::function::TableFuncBindInput input, catalog::CatalogContent* catalog) {
std::vector<std::string> returnColumnNames;
std::vector<common::LogicalType> returnTypes;
auto tableName = input.inputs[0].getValue<std::string>();
auto tableID = catalog->getTableID(tableName);
auto schema = catalog->getTableSchema(tableID);
returnColumnNames.emplace_back("property id");
returnTypes.emplace_back(common::LogicalTypeID::INT64);
returnColumnNames.emplace_back("name");
returnTypes.emplace_back(common::LogicalTypeID::STRING);
returnColumnNames.emplace_back("type");
returnTypes.emplace_back(common::LogicalTypeID::STRING);
if (schema->isNodeTable) {
returnColumnNames.emplace_back("primary key");
returnTypes.emplace_back(common::LogicalTypeID::BOOL);
}
return std::make_unique<TableInfoBindData>(
schema, std::move(returnTypes), std::move(returnColumnNames), schema->getNumProperties());
}

void DBVersionOperation::tableFunc(std::pair<common::offset_t, common::offset_t> morsel,
function::TableFuncBindData* bindData, std::vector<common::ValueVector*> outputVectors) {
auto outputVector = outputVectors[0];
auto pos = outputVector->state->selVector->selectedPositions[0];
outputVectors[0]->setValue(pos, std::string(common::KUZU_VERSION));
outputVectors[0]->setNull(pos, false);
outputVector->state->selVector->selectedSize = 1;
}

std::unique_ptr<TableFuncBindData> DBVersionOperation::bindFunc(main::ClientContext* context,
kuzu::function::TableFuncBindInput input, catalog::CatalogContent* catalog) {
std::vector<std::string> returnColumnNames;
std::vector<common::LogicalType> returnTypes;
returnColumnNames.emplace_back("KUZU_Version");
returnTypes.emplace_back(common::LogicalTypeID::STRING);
return std::make_unique<TableFuncBindData>(
std::move(returnTypes), std::move(returnColumnNames), 1 /* one row result */);
}

void CurrentSettingOperation::tableFunc(std::pair<common::offset_t, common::offset_t> morsel,
function::TableFuncBindData* bindData, std::vector<common::ValueVector*> outputVectors) {
auto currentSettingBindData = reinterpret_cast<CurrentSettingBindData*>(bindData);
auto outputVector = outputVectors[0];
auto pos = outputVector->state->selVector->selectedPositions[0];
outputVectors[0]->setValue(pos, currentSettingBindData->result);
outputVectors[0]->setNull(pos, false);
outputVector->state->selVector->selectedSize = 1;
}

std::unique_ptr<TableFuncBindData> CurrentSettingOperation::bindFunc(main::ClientContext* context,
kuzu::function::TableFuncBindInput input, catalog::CatalogContent* catalog) {
auto optionName = input.inputs[0].getValue<std::string>();
std::vector<std::string> returnColumnNames;
std::vector<common::LogicalType> returnTypes;
returnColumnNames.emplace_back(optionName);
returnTypes.emplace_back(common::LogicalTypeID::STRING);
return std::make_unique<CurrentSettingBindData>(context->getCurrentSetting(optionName),
std::move(returnTypes), std::move(returnColumnNames), 1 /* one row result */);
}

} // namespace function
} // namespace kuzu
9 changes: 7 additions & 2 deletions src/include/binder/binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include "query_normalizer.h"

namespace kuzu {
namespace main {
class ClientContext;
}

namespace binder {

class BoundCreateNode;
Expand Down Expand Up @@ -53,9 +57,9 @@ class Binder {
friend class ExpressionBinder;

public:
explicit Binder(const catalog::Catalog& catalog)
explicit Binder(const catalog::Catalog& catalog, main::ClientContext* clientContext)
: catalog{catalog}, lastExpressionId{0}, variableScope{std::make_unique<VariableScope>()},
expressionBinder{this} {}
expressionBinder{this}, clientContext{clientContext} {}

std::unique_ptr<BoundStatement> bind(const parser::Statement& statement);

Expand Down Expand Up @@ -250,6 +254,7 @@ class Binder {
uint32_t lastExpressionId;
std::unique_ptr<VariableScope> variableScope;
ExpressionBinder expressionBinder;
main::ClientContext* clientContext;
};

} // namespace binder
Expand Down
2 changes: 2 additions & 0 deletions src/include/common/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace kuzu {
namespace common {

constexpr char KUZU_VERSION[] = "v0.4.0";

constexpr uint64_t DEFAULT_VECTOR_CAPACITY_LOG_2 = 11;
constexpr uint64_t DEFAULT_VECTOR_CAPACITY = (uint64_t)1 << DEFAULT_VECTOR_CAPACITY_LOG_2;

Expand Down
2 changes: 2 additions & 0 deletions src/include/common/expression_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ const std::string DECODE_FUNC_NAME = "DECODE";

// TABLE functions
const std::string TABLE_INFO_FUNC_NAME = "TABLE_INFO";
const std::string DB_VERSION_FUNC_NAME = "DB_VERSION";
const std::string CURRENT_SETTING_FUNC_NAME = "CURRENT_SETTING";

enum ExpressionType : uint8_t {

Expand Down
47 changes: 44 additions & 3 deletions src/include/function/table_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "common/vector/value_vector.h"
#include "function/function_definition.h"
#include "main/client_context.h"

namespace kuzu {

Expand All @@ -17,7 +18,7 @@ struct TableFuncBindInput;
using table_func_t = std::function<void(std::pair<common::offset_t, common::offset_t>,
function::TableFuncBindData*, std::vector<common::ValueVector*>)>;
using table_bind_func_t = std::function<std::unique_ptr<TableFuncBindData>(
TableFuncBindInput input, catalog::CatalogContent* catalog)>;
main::ClientContext* context, TableFuncBindInput input, catalog::CatalogContent* catalog)>;

struct TableFuncBindData {
std::vector<common::LogicalType> returnTypes;
Expand All @@ -31,7 +32,9 @@ struct TableFuncBindData {

virtual ~TableFuncBindData() = default;

virtual std::unique_ptr<TableFuncBindData> copy() = 0;
virtual std::unique_ptr<TableFuncBindData> copy() {
return std::make_unique<TableFuncBindData>(returnTypes, returnColumnNames, maxOffset);
}
};

struct TableFuncBindInput {
Expand Down Expand Up @@ -72,7 +75,45 @@ struct TableInfoOperation {
function::TableFuncBindData* bindData, std::vector<common::ValueVector*> outputVectors);

static std::unique_ptr<TableInfoBindData> bindFunc(
TableFuncBindInput input, catalog::CatalogContent* catalog);
main::ClientContext* context, TableFuncBindInput input, catalog::CatalogContent* catalog);
};

struct DBVersionOperation {
inline static std::unique_ptr<TableOperationDefinition> getDefinitions() {
return std::make_unique<TableOperationDefinition>("db_version", tableFunc, bindFunc);
}

static void tableFunc(std::pair<common::offset_t, common::offset_t> morsel,
function::TableFuncBindData* bindData, std::vector<common::ValueVector*> outputVectors);

static std::unique_ptr<TableFuncBindData> bindFunc(
main::ClientContext* context, TableFuncBindInput input, catalog::CatalogContent* catalog);
};

struct CurrentSettingBindData : public TableFuncBindData {
std::string result;

CurrentSettingBindData(std::string result, std::vector<common::LogicalType> returnTypes,
std::vector<std::string> returnColumnNames, common::offset_t maxOffset)
: result{std::move(result)}, TableFuncBindData{std::move(returnTypes),
std::move(returnColumnNames), maxOffset} {}

std::unique_ptr<TableFuncBindData> copy() override {
return std::make_unique<CurrentSettingBindData>(
result, returnTypes, returnColumnNames, maxOffset);
}
};

struct CurrentSettingOperation {
inline static std::unique_ptr<TableOperationDefinition> getDefinitions() {
return std::make_unique<TableOperationDefinition>("current_setting", tableFunc, bindFunc);
}

static void tableFunc(std::pair<common::offset_t, common::offset_t> morsel,
function::TableFuncBindData* bindData, std::vector<common::ValueVector*> outputVectors);

static std::unique_ptr<TableFuncBindData> bindFunc(
main::ClientContext* context, TableFuncBindInput input, catalog::CatalogContent* catalog);
};

} // namespace function
Expand Down
2 changes: 2 additions & 0 deletions src/include/main/client_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class ClientContext {

void startTimingIfEnabled();

std::string getCurrentSetting(std::string optionName);

private:
uint64_t numThreadsForExecution;
std::unique_ptr<ActiveQuery> activeQuery;
Expand Down
2 changes: 2 additions & 0 deletions src/include/main/db_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ namespace kuzu {
namespace main {

typedef void (*set_context)(ClientContext* context, const common::Value& parameter);
typedef std::string (*get_setting)(ClientContext* context);

struct ConfigurationOption {
const char* name;
common::LogicalTypeID parameterType;
set_context setContext;
get_setting getSetting;
};

class DBConfig {
Expand Down
6 changes: 6 additions & 0 deletions src/include/main/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ struct ThreadsSetting {
assert(parameter.getDataType().getLogicalTypeID() == common::LogicalTypeID::INT64);
context->numThreadsForExecution = parameter.getValue<int64_t>();
}
static std::string getSetting(ClientContext* context) {
return std::to_string(context->numThreadsForExecution);
}
};

struct TimeoutSetting {
Expand All @@ -23,6 +26,9 @@ struct TimeoutSetting {
context->timeoutInMS = parameter.getValue<int64_t>();
context->startTimingIfEnabled();
}
static std::string getSetting(ClientContext* context) {
return std::to_string(context->timeoutInMS);
}
};

} // namespace main
Expand Down
9 changes: 9 additions & 0 deletions src/main/client_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <thread>

#include "common/constants.h"
#include "main/db_config.h"

namespace kuzu {
namespace main {
Expand All @@ -19,5 +20,13 @@
}
}

std::string ClientContext::getCurrentSetting(std::string optionName) {
auto option = main::DBConfig::getOptionByName(optionName);
if (option == nullptr) {
throw common::RuntimeException{"Invalid option name: " + optionName + "."};

Check warning on line 26 in src/main/client_context.cpp

View check run for this annotation

Codecov / codecov/patch

src/main/client_context.cpp#L26

Added line #L26 was not covered by tests
}
return option->getSetting(this);
}

} // namespace main
} // namespace kuzu
2 changes: 1 addition & 1 deletion src/main/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ std::unique_ptr<PreparedStatement> Connection::prepareNoLock(
// parsing
auto statement = Parser::parseQuery(query);
// binding
auto binder = Binder(*database->catalog);
auto binder = Binder(*database->catalog, clientContext.get());
auto boundStatement = binder.bind(*statement);
preparedStatement->preparedSummary.statementType = boundStatement->getStatementType();
preparedStatement->readOnly =
Expand Down
2 changes: 1 addition & 1 deletion src/main/db_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace kuzu {
namespace main {

#define GET_CONFIGURATION(_PARAM) \
{ _PARAM::name, _PARAM::inputType, _PARAM::setContext, }
{ _PARAM::name, _PARAM::inputType, _PARAM::setContext, _PARAM::getSetting }

static ConfigurationOption options[] = {
GET_CONFIGURATION(ThreadsSetting), GET_CONFIGURATION(TimeoutSetting)};
Expand Down
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ enable_testing()
add_subdirectory(test_helper)
add_subdirectory(test_runner)
add_subdirectory(graph_test)
add_subdirectory(binder)
add_subdirectory(c_api)
add_subdirectory(common)
add_subdirectory(copy)
Expand Down
1 change: 0 additions & 1 deletion test/binder/CMakeLists.txt

This file was deleted.

33 changes: 0 additions & 33 deletions test/binder/binder_test.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion test/graph_test/graph_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void BaseGraphTest::validateQueryBestPlanJoinOrder(
auto catalog = getCatalog(*database);
auto statement = parser::Parser::parseQuery(query);
auto parsedQuery = (parser::RegularQuery*)statement.get();
auto boundQuery = Binder(*catalog).bind(*parsedQuery);
auto boundQuery = Binder(*catalog, conn->clientContext.get()).bind(*parsedQuery);
auto plan = Planner::getBestPlan(*catalog,
getStorageManager(*database)->getNodesStore().getNodesStatisticsAndDeletedIDs(),
getStorageManager(*database)->getRelsStore().getRelsStatistics(), *boundQuery);
Expand Down
3 changes: 1 addition & 2 deletions test/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ add_kuzu_test(main_test
csv_output_test.cpp
prepare_test.cpp
result_value_test.cpp
storage_driver_test.cpp
call_test.cpp)
storage_driver_test.cpp)
Loading
Loading