Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- * author: Daniel Leybovich
- *
- * COMPILE:
- *
- * $ g++ -D HAVE_LONG_INT_64 -I /opt/vertica/sdk/include -Wall \
- * > -shared -Wno-unused-value -fPIC -o Cast.so Cast.cpp /opt/vertica/sdk/include/Vertica.cpp
- *
- ****************************************************************************/
- /****************************************************************************
- *
- * daniel=> drop librARY castlib cascade;
- * ROLLBACK 3855: Library "castlib" does not exist
- *
- * daniel=> CREATE LIBRARY CastLib AS '/tmp/Cast.so';
- * CREATE LIBRARY
- *
- * daniel=> CREATE FUNCTION int2str AS LANGUAGE 'C++' NAME 'CastFactory' LIBRARY CastLib;
- * CREATE FUNCTION
- *
- * daniel=> select int2str(2345);
- * int2str
- * ---------------------
- * 2345 -*- append -*-
- * (1 row)
- *
- ****************************************************************************/
- #include <string>
- #include "Vertica.h"
- using namespace Vertica;
- class Cast : public ScalarFunction
- {
- public:
- virtual void processBlock(ServerInterface &srvInterface, BlockReader &argReader, BlockWriter &resWriter) {
- try {
- if (argReader.getNumCols() != 1) {
- vt_report_error(0, "Function only accept 1 arguments, but %zu provided", argReader.getNumCols());
- }
- // data type containers
- std::stringstream stream;
- std::string aString;
- do {
- // get integer
- stream << argReader.getIntRef(0);
- // convert to string
- stream >> aString;
- // append to a string
- aString.append(" -*- append -*-");
- // write a result as VARCHAR
- resWriter.getStringRef().copy(aString);
- // process next
- resWriter.next();
- } while (argReader.next());
- } catch(std::exception& e) {
- vt_report_error(0, "Exception while processing block: [%s]", e.what());
- }
- }
- };
- class CastFactory : public ScalarFunctionFactory {
- virtual ScalarFunction *createScalarFunction(ServerInterface &interface) {
- return vt_createFuncObject<Cast>(interface.allocator);
- }
- virtual void getPrototype(ServerInterface &interface, ColumnTypes &argTypes, ColumnTypes &returnType) {
- argTypes.addInt();
- returnType.addVarchar();
- }
- virtual void getReturnType(ServerInterface &srvInterface, const SizedColumnTypes &argTypes, SizedColumnTypes &returnType) {
- returnType.addVarchar(80);
- }
- };
- RegisterFactory(CastFactory);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement