C++言語シミュレート行関数
この例では、次のファイル名を使用する: 'time.cpp
コード
#include <nzaefactory.hpp>
using namespace nz::ae;
static int run(nz::ae::NzaeFunction *aeFunc);
int main(int argc, char * argv[])
{
NzaeApiGenerator helper;
// The following line is only needed if a launcher is not used
helper.setName("testcapi");
for (int i=0; i < 2; i++) {
nz::ae::NzaeApi &api = helper.getApi(nz::ae::NzaeApi::FUNCTION);
run(api.aeFunction);
if (!helper.isRemote())
break;
}
return 0;
}
class MyHandler : public NzaeFunctionMessageHandler
{
public:
MyHandler() {
m_Once = false;
}
void doOnce(NzaeFunction& api) {
const NzaeMetadata& meta = api.getMetadata();
if (meta.getOutputColumnCount() != 3 ||
meta.getOutputType(0) != NzaeDataTypes::NZUDSUDX_INT32 ||
meta.getOutputType(1) != NzaeDataTypes::NZUDSUDX_INT32 ||
meta.getOutputType(2) != NzaeDataTypes::NZUDSUDX_INT32
) {
throw NzaeException("expecting three output columns of type \
int32");
}
if (meta.getInputColumnCount() != 1) {
throw NzaeException("expecting one input column");
}
if (meta.getInputType(0) != NzaeDataTypes::NZUDSUDX_FIXED &&
meta.getInputType(0) != NzaeDataTypes::NZUDSUDX_VARIABLE) {
throw NzaeException("first input column expected to be a string \
type");
}
m_Once = true;
}
void evaluate(NzaeFunction& api, NzaeRecord &input, NzaeRecord &result) {
if (!m_Once)
doOnce(api);
int32_t hour, min, sec;
NzaeField &field = input.get(0);
if (field.isNull())
throw NzaeException("first input column may not be null");
NzaeStringField &sf = (NzaeStringField&) input.get(0);
std::string str = (std::string)sf;
int nfound = sscanf(str.c_str(), "%d:%d:%d", &hour, &min, &sec);
if (nfound != 3)
throw NzaeException(NzaeException::format("Badly formatted time \
%s. Expected h:m:s", str.c_str()));
NzaeInt32Field &f = (NzaeInt32Field&)result.get(0);
f = hour;
NzaeInt32Field &f2 = (NzaeInt32Field&)result.get(1);
f2 = min;
NzaeInt32Field &f3 = (NzaeInt32Field&)result.get(2);
f3 = sec;
}
bool m_Once;
};
static int run(NzaeFunction *aeFunc)
{
aeFunc->run(new MyHandler());
return 0;
}
コンパイル
コードをコンパイルする:
$NZ_EXPORT_DIR/ae/utilities/bin/compile_ae --language cpp --template compile \
--exe timeae --compargs "-g -Wall" --linkargs "-g" time.cpp --version 3登録
NetezzaSQLには行関数の概念が組み込まれていないため、この関数は入力行ごとに1行しか出力しないにもかかわらず、テーブル関数として登録されます:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --sig "timeae(VARCHAR(ANY))" \
--return "table(hour int4, minute int4, second int4)" --language cpp \
--template udtf --exe timeae --version 3実行中
nzsqlでクエリを実行する:
SELECT * FROM TABLE WITH FINAL(timeae('12:30:15'));
HOUR | MINUTE | SECOND
------+--------+--------
12 | 30 | 15
(1 row)
SELECT * FROM TABLE WITH FINAL(timeae('12:30'));
ERROR: Badly formatted time 12:30. Expected h:m:s