C++ 언어 집계

이 예제에서는 ' max.cpp' 파일 이름을 사용합니다

코드

이 예제의 코드는 가능한 모든 데이터 유형을 처리하도록 설계되었기 때문에 C 언어 집계 예제보다 약간 더 깁니다.
#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() != 1 ||
meta.getOutputType(0) != NzaeDataTypes::NZUDSUDX_VARIABLE) {
throw NzaeException("expecting one output column of type string");
}
if (meta.getInputColumnCount() != 2) {
throw NzaeException("expecting at least two input columns");
}
if (meta.getInputType(0) != NzaeDataTypes::NZUDSUDX_FIXED &&
meta.getInputType(0) != NzaeDataTypes::NZUDSUDX_VARIABLE) {
throw NzaeException("first input column expected to be a string
type");
}
if (meta.getInputType(1) != NzaeDataTypes::NZUDSUDX_INT32) {
throw NzaeException("second input column expected to be int32 type");
}
m_Once = true;
}
void evaluate(NzaeFunction& api, NzaeRecord &input, NzaeRecord &result) {
if (!m_Once)
doOnce(api);
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 strop = (std::string)sf;
NzaeField &field2 = input.get(1);
if (field2.isNull())
throw NzaeException("secondinput column may not be null");
NzaeInt32Field &inf = (NzaeInt32Field&) input.get(1);
int type = (int32_t)inf;
NzaeStringField &of= (NzaeStringField&)result.get(0);
if (type == 0) {
// env
const NzaeEnvironment& env = api.getEnvironment();
const char* val = env.getValue(strop.c_str());
if (!val)
of.setNull(true);
else
of = std::string(val);
}
else {
const NzaeLibrary& libs = api.getLibrary();
const NzaeLibrary::NzaeLibraryInfo *info =
libs.getLibraryInfo(strop.c_str(), false ,
NzaeLibrary::NzaeLibrarySearchBoth);
if (!info)
of.setNull(true);
else
of = info->libraryFullPath;
}
}
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 envae --compargs "-g -Wall" --linkargs "-g" env.cpp --version 3

등록

예제를 등록하세요:
$NZ_EXPORT_DIR/ae/utilities/bin/register_ae --sig "envae(VARCHAR(ANY),int4)" \
--return "table(val varchar(1000))" --language cpp --template udtf \
--exe envae --version 3 --deps inza..LIBNZAEADAPTERS
이 예제에서는 LIBNZAEADAPTERS 공유 라이브러리를 추가했다고 가정합니다.

실행 중

이 예제의 출력은 환경에 따라 다르다는 점에 유의하세요. 따라서 실제 출력은 아래 텍스트와 비슷하지만 일치하지는 않습니다.
SELECT * FROM TABLE WITH FINAL(envae('inza..libnzaeadapters', 1));
VAL
-----------------------------------------------------------------------
/nz/data.1.0/base/1/library/237951/host/libnzaeadapters.so
(1 row)
SELECT * FROM TABLE WITH FINAL(envae('libnzaeadapters2', 1));
VAL
-----
(1 row)
SELECT * FROM TABLE WITH FINAL(envae('NZAE_DYNAMIC_ENVIRONMENT', 0));
VAL
-----
0
(1 row)
SELECT * FROM TABLE WITH FINAL(envae('NZAE_DYNAMIC_ENVIRONMENT2', 0));
VAL
-----
(1 row)