ClickHouse/src/Functions/FunctionsTransactionCounters.cpp

72 lines
2.7 KiB
C++
Raw Normal View History

2021-12-30 13:15:28 +00:00
#include <Functions/FunctionConstantBase.h>
#include <DataTypes/DataTypeNothing.h>
#include <DataTypes/DataTypesNumber.h>
2021-03-31 17:55:04 +00:00
#include <Functions/FunctionFactory.h>
#include <Interpreters/Context.h>
#include <Interpreters/MergeTreeTransaction.h>
2021-12-30 13:15:28 +00:00
#include <Interpreters/TransactionLog.h>
2021-03-31 17:55:04 +00:00
namespace DB
{
namespace
{
2021-12-30 13:15:28 +00:00
class FunctionTransactionID : public FunctionConstantBase<FunctionTransactionID, Tuple, DataTypeNothing>
2021-03-31 17:55:04 +00:00
{
public:
static constexpr auto name = "transactionID";
2021-12-30 13:15:28 +00:00
static Tuple getValue(const MergeTreeTransactionPtr & txn)
2021-03-31 17:55:04 +00:00
{
Tuple res;
if (txn)
res = {txn->tid.start_csn, txn->tid.local_tid, txn->tid.host_id};
else
res = {static_cast<UInt64>(0), static_cast<UInt64>(0), UUIDHelpers::Nil};
2021-12-30 13:15:28 +00:00
return res;
2021-03-31 17:55:04 +00:00
}
2021-12-30 13:15:28 +00:00
DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override { return getTransactionIDDataType(); }
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionTransactionID>(context); }
explicit FunctionTransactionID(ContextPtr context) : FunctionConstantBase(getValue(context->getCurrentTransaction()), context->isDistributed()) {}
};
class FunctionTransactionLatestSnapshot : public FunctionConstantBase<FunctionTransactionLatestSnapshot, UInt64, DataTypeUInt64>
{
2022-02-03 21:40:17 +00:00
static UInt64 getLatestSnapshot(ContextPtr context)
{
context->checkTransactionsAreAllowed(/* explicit_tcl_query */ true);
return TransactionLog::instance().getLatestSnapshot();
}
2021-12-30 13:15:28 +00:00
public:
static constexpr auto name = "transactionLatestSnapshot";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionTransactionLatestSnapshot>(context); }
2022-02-03 21:40:17 +00:00
explicit FunctionTransactionLatestSnapshot(ContextPtr context) : FunctionConstantBase(getLatestSnapshot(context), context->isDistributed()) {}
2021-12-30 13:15:28 +00:00
};
class FunctionTransactionOldestSnapshot : public FunctionConstantBase<FunctionTransactionOldestSnapshot, UInt64, DataTypeUInt64>
{
2022-02-03 21:40:17 +00:00
static UInt64 getOldestSnapshot(ContextPtr context)
{
context->checkTransactionsAreAllowed(/* explicit_tcl_query */ true);
return TransactionLog::instance().getOldestSnapshot();
}
2021-12-30 13:15:28 +00:00
public:
static constexpr auto name = "transactionOldestSnapshot";
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionTransactionOldestSnapshot>(context); }
2022-02-03 21:40:17 +00:00
explicit FunctionTransactionOldestSnapshot(ContextPtr context) : FunctionConstantBase(getOldestSnapshot(context), context->isDistributed()) {}
2021-03-31 17:55:04 +00:00
};
}
void registerFunctionsTransactionCounters(FunctionFactory & factory)
{
factory.registerFunction<FunctionTransactionID>();
2021-12-30 13:15:28 +00:00
factory.registerFunction<FunctionTransactionLatestSnapshot>();
factory.registerFunction<FunctionTransactionOldestSnapshot>();
2021-03-31 17:55:04 +00:00
}
}