Skip to content

Commit

Permalink
Add function: chdb() to return version (#25)
Browse files Browse the repository at this point in the history
* add function: chdb() to return version

This is an example of a C++ function that returns the current version of chDB as a string. The version number is defined using the CHDB_VERSION_STRING macro, which should be defined during the build process.

The FunctionChdbVersion class is a subclass of FunctionConstantBase, which is a base class for functions that return a constant value. The FunctionChdbVersion constructor takes the CHDB_VERSION_STRING macro as an argument, which is passed to the FunctionConstantBase constructor to set the constant value of the function.

The REGISTER_FUNCTION macro is used to register the FunctionChdbVersion function with the FunctionFactory. The REGISTER_FUNCTION macro should be conditionally compiled using an #if directive that checks whether the CHDB_VERSION_STRING macro is defined, to avoid registering the function if the version number is not available.

* Update chdbVersion.cpp

* Update chdbVersion.cpp

* Update CMakeLists.txt

* Update CMakeLists.txt
  • Loading branch information
lmangani authored Apr 28, 2023
1 parent 08eed1e commit d958feb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Functions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@ else()
add_library(clickhouse_functions SHARED ${OBJECT_LIBS})
target_link_libraries(clickhouse_functions PUBLIC ${PUBLIC_LIBS} PRIVATE ${PRIVATE_LIBS})
endif ()

if (CHDB_VERSION)
add_compile_definitions(CHDB_VERSION_STRING="${CHDB_VERSION}")
endif ()
34 changes: 34 additions & 0 deletions src/Functions/chdbVersion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if defined(CHDB_VERSION_STRING)

#include <DataTypes/DataTypeString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionConstantBase.h>

namespace DB
{

namespace
{
/// chdb() - returns the current chdb version as a string.
class FunctionChdbVersion : public FunctionConstantBase<FunctionChdbVersion, String, DataTypeString>
{
public:
static constexpr auto name = "chdb";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionChdbVersion>(context); }
explicit FunctionChdbVersion(ContextPtr context) : FunctionConstantBase(CHDB_VERSION_STRING, context->isDistributed()) {}
};
}

REGISTER_FUNCTION(ChdbVersion)
{
factory.registerFunction<FunctionChdbVersion>(
{
R"(
Returns the version of chDB. The result type is String.
)",
Documentation::Examples{{"chdb", "SELECT chdb()"}},
Documentation::Categories{"String"}
}, FunctionFactory::CaseInsensitive);
}
}
#endif

0 comments on commit d958feb

Please sign in to comment.