Skip to content

Commit

Permalink
add hll_create2 scalar function
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Tuval committed Jun 30, 2015
1 parent f06dffe commit f2dba53
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
19 changes: 18 additions & 1 deletion AggregateFunctions/Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,25 @@ void* HllAggregateFunctionBase::getAggregateState(void* state, char* aggPtr) {
return (void*)hll;
}

void* createHll() {
return new SerializedHyperLogLog(10, false);
}

void deleteHll(void* hll) {
delete (SerializedHyperLogLog*)hll;
}

void addItemToHll(void* hll, const VString& item) {
SerializedHyperLogLog* phll = (SerializedHyperLogLog*)hll;
phll->add(item.data(), item.length());
}

void getHllString(void* hll, VString& result) {
updateStringFromHll(result, (SerializedHyperLogLog*)hll);
}

void* SimpleHllAggregateFunctionBase::createHll() {
return new SerializedHyperLogLog(10, false);
return ::createHll();
}

void* SimpleHllAggregateFunctionBase::createLegacyHll() {
Expand Down
98 changes: 98 additions & 0 deletions ScalarFunctions/HllCreate2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Copyright (c) 2005 - 2012 Vertica, an HP company -*- C++ -*- */
/*
*
* Description: Example User Defined Scalar Function: Add 2 ints
*
* Create Date: Apr 29, 2011
*/
#include "Vertica.h"

using namespace Vertica;

int resultLength();
void* createHll();
void deleteHll(void* hll);
void addItemToHll(void* hll, const VString& item);
void getHllString(void* hll, VString& result);

class exception;

/*
* This is a simple function that adds two integers and returns the result
*/
class HllCreate2 : public ScalarFunction
{
public:

/*
* This method processes a block of rows in a single invocation.
*
* The inputs are retrieved via argReader
* The outputs are returned via resWriter
*/
virtual void processBlock(ServerInterface &srvInterface,
BlockReader &argReader,
BlockWriter &resWriter)
{
try {
// Basic error checking
const SizedColumnTypes& typeMetadata = argReader.getTypeMetaData();
for(unsigned int i = 0; i < typeMetadata.getColumnCount(); i++) {
if (!typeMetadata.getColumnType(i).isVarchar()){
vt_report_error(0, "Function only accept arguments of type varchar");
}
}

// While we have inputs to process
do {
void* hll = createHll();

for(unsigned int i = 0; i < argReader.getNumCols(); i++) {
const VString& item = argReader.getStringRef(i);

addItemToHll(hll, item);
}

getHllString(hll, resWriter.getStringRef());
deleteHll(hll);

resWriter.next();
} while (argReader.next());
} catch(std::exception& e) {
// Standard exception. Quit.
vt_report_error(0, "Exception while processing block: [%s]", e.what());
}
}
};

class HllCreate2Factory : public ScalarFunctionFactory
{
protected:
int mMaxResultLength;

public:

HllCreate2Factory() {
mMaxResultLength = resultLength();
}

// return an instance of Add2Ints to perform the actual addition.
virtual ScalarFunction *createScalarFunction(ServerInterface &interface)
{ return vt_createFuncObject<HllCreate2>(interface.allocator); }

virtual void getPrototype(ServerInterface &interface,
ColumnTypes &argTypes,
ColumnTypes &returnType)
{
argTypes.addAny();
returnType.addVarchar();
}

virtual void getReturnType(ServerInterface &srvfloaterface,
const SizedColumnTypes &inputTypes,
SizedColumnTypes &outputTypes) {
outputTypes.addVarchar(mMaxResultLength);
}
};

RegisterFactory(HllCreate2Factory);
3 changes: 2 additions & 1 deletion sql/udf.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Create or replace aggregate function HLL_CREATE_LEGACY as language 'C++' NAME 'H
Create or replace aggregate function HLL_MERGE as language 'C++' NAME 'HllMergeFactory' Library libverticahll;
Create or replace aggregate function HLL_MERGE_COMPUTE as language 'C++' NAME 'HllMergeComputeFactory' Library libverticahll;
Create or replace function HLL_MERGE2 as language 'C++' NAME 'HllMerge2Factory' Library libverticahll;
Create or replace function HLL_CONVERT_TO_LATEST as language 'C++' NAME 'HllConvertToLatestFactory' Library libverticahll NOT FENCED;
Create or replace function HLL_CONVERT_TO_LATEST as language 'C++' NAME 'HllConvertToLatestFactory' Library libverticahll NOT FENCED;
Create or replace function HLL_CREATE2 as language 'C++' NAME 'HllCreate2Factory' Library libverticahll NOT FENCED;

0 comments on commit f2dba53

Please sign in to comment.