Example of the computation of the sum of squares, summing the squares of
input table columns and storing the result in an output table. This UDF
uses CUDA libraries and must be run on a CUDA build of Kinetica.
Sample Code Block
A simple distributed example of a UDF which copies data from one set of tables
to another would look like the following:
/* Sourced from: gpudb-udf-api-cpp/proc-example/proc-example.cpp */#include"kinetica/Proc.hpp"#include<iostream>intmain(intargc,char*argv[]){try{kinetica::ProcData*procData=kinetica::ProcData::get();constkinetica::ProcData::InputDataSet&inputData=procData->getInputData();kinetica::ProcData::OutputDataSet&outputData=procData->getOutputData();// Loop through input and output tables (assume the same number)
for(size_ti=0;i<inputData.getTableCount();++i){constkinetica::ProcData::InputTable&inputTable=inputData[i];kinetica::ProcData::OutputTable&outputTable=outputData[i];outputTable.setSize(inputTable.getSize());// Loop through columns in the input and output tables (assume the same number and types)
for(size_tj=0;j<inputTable.getColumnCount();++j){constkinetica::ProcData::InputColumn&inputColumn=inputTable[j];kinetica::ProcData::OutputColumn&outputColumn=outputTable[j];// For each record, copy the data from the input column to the output column
for(size_tk=0;k<inputTable.getSize();++k){if(inputColumn.isNull(k)){outputColumn.appendNull();continue;}switch(inputColumn.getType()){casekinetica::ProcData::Column::BYTES:outputColumn.appendVarBytes(inputColumn.getVarBytes(k));break;casekinetica::ProcData::Column::CHAR1:outputColumn.appendValue(inputColumn.getValue<kinetica::CharN<1>>(k));break;casekinetica::ProcData::Column::CHAR2:outputColumn.appendValue(inputColumn.getValue<kinetica::CharN<2>>(k));break;casekinetica::ProcData::Column::CHAR4:outputColumn.appendValue(inputColumn.getValue<kinetica::CharN<4>>(k));break;casekinetica::ProcData::Column::CHAR8:outputColumn.appendValue(inputColumn.getValue<kinetica::CharN<8>>(k));break;casekinetica::ProcData::Column::CHAR16:outputColumn.appendValue(inputColumn.getValue<kinetica::CharN<16>>(k));break;casekinetica::ProcData::Column::CHAR32:outputColumn.appendValue(inputColumn.getValue<kinetica::CharN<32>>(k));break;casekinetica::ProcData::Column::CHAR64:outputColumn.appendValue(inputColumn.getValue<kinetica::CharN<64>>(k));break;casekinetica::ProcData::Column::CHAR128:outputColumn.appendValue(inputColumn.getValue<kinetica::CharN<128>>(k));break;casekinetica::ProcData::Column::CHAR256:outputColumn.appendValue(inputColumn.getValue<kinetica::CharN<256>>(k));break;casekinetica::ProcData::Column::DATE:outputColumn.appendValue(inputColumn.getValue<kinetica::Date>(k));break;casekinetica::ProcData::Column::DATETIME:outputColumn.appendValue(inputColumn.getValue<kinetica::DateTime>(k));break;casekinetica::ProcData::Column::DECIMAL:outputColumn.appendValue(inputColumn.getValue<int64_t>(k));break;casekinetica::ProcData::Column::DOUBLE:outputColumn.appendValue(inputColumn.getValue<double>(k));break;casekinetica::ProcData::Column::FLOAT:outputColumn.appendValue(inputColumn.getValue<float>(k));break;casekinetica::ProcData::Column::INT:outputColumn.appendValue(inputColumn.getValue<int32_t>(k));break;casekinetica::ProcData::Column::INT8:outputColumn.appendValue(inputColumn.getValue<int8_t>(k));break;casekinetica::ProcData::Column::INT16:outputColumn.appendValue(inputColumn.getValue<int16_t>(k));break;casekinetica::ProcData::Column::IPV4:outputColumn.appendValue(inputColumn.getValue<uint32_t>(k));break;casekinetica::ProcData::Column::LONG:outputColumn.appendValue(inputColumn.getValue<int64_t>(k));break;casekinetica::ProcData::Column::STRING:outputColumn.appendVarString(inputColumn.getVarString(k));break;casekinetica::ProcData::Column::TIME:outputColumn.appendValue(inputColumn.getValue<kinetica::Time>(k));break;casekinetica::ProcData::Column::TIMESTAMP:outputColumn.appendValue(inputColumn.getValue<int64_t>(k));break;}}}}// Copy any parameters from the input parameter map into the output results map (not necessary for table copying, just for illustrative purposes)
std::map<std::string,std::string>params=procData->getParams();procData->getResults().insert(params.begin(),params.end());std::map<std::string,std::vector<uint8_t>>binParams=procData->getBinParams();procData->getBinResults().insert(binParams.begin(),binParams.end());// Inform Kinetica that the proc has finished successfully
procData->complete();}catch(conststd::exception&ex){std::cout<<"ERROR: "<<ex.what()<<std::endl;return1;}return0;}