Advertisement
Guest User

sKwa

a guest
Jan 28th, 2015
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. /****************************************************************************
  2.  * author: Daniel Leybovich
  3.  *
  4.  * COMPILE:
  5.  *
  6.  *  $ g++ -D HAVE_LONG_INT_64 -I /opt/vertica/sdk/include -Wall \
  7.  *  > -shared -Wno-unused-value -fPIC -o Cast.so Cast.cpp /opt/vertica/sdk/include/Vertica.cpp
  8.  *
  9.  ****************************************************************************/
  10.  
  11. /****************************************************************************
  12.  *
  13.  * daniel=> drop librARY castlib cascade;
  14.  * ROLLBACK 3855:  Library "castlib" does not exist
  15.  *
  16.  * daniel=> CREATE LIBRARY CastLib AS '/tmp/Cast.so';
  17.  * CREATE LIBRARY
  18.  *
  19.  * daniel=> CREATE FUNCTION int2str AS LANGUAGE 'C++' NAME 'CastFactory' LIBRARY CastLib;
  20.  * CREATE FUNCTION
  21.  *
  22.  * daniel=> select int2str(2345);
  23.  *        int2str      
  24.  * ---------------------
  25.  *  2345 -*- append -*-
  26.  * (1 row)
  27.  *
  28.  ****************************************************************************/
  29. #include <string>
  30. #include "Vertica.h"
  31.  
  32. using namespace Vertica;
  33.  
  34. class Cast : public ScalarFunction
  35. {
  36.     public:
  37.  
  38.         virtual void processBlock(ServerInterface &srvInterface, BlockReader &argReader, BlockWriter &resWriter) {
  39.             try {
  40.                 if (argReader.getNumCols() != 1) {
  41.                     vt_report_error(0, "Function only accept 1 arguments, but %zu provided", argReader.getNumCols());
  42.                 }
  43.  
  44.                 // data type containers
  45.                 std::stringstream stream;
  46.                 std::string aString;
  47.  
  48.                 do {
  49.  
  50.                     // get integer
  51.                     stream << argReader.getIntRef(0);
  52.  
  53.                     // convert to string
  54.                     stream >> aString;
  55.  
  56.                     // append to a string
  57.                     aString.append(" -*- append -*-");
  58.  
  59.                     // write a result as VARCHAR
  60.                     resWriter.getStringRef().copy(aString);
  61.  
  62.                     // process next
  63.                     resWriter.next();
  64.  
  65.                 } while (argReader.next());
  66.  
  67.             } catch(std::exception& e) {
  68.                 vt_report_error(0, "Exception while processing block: [%s]", e.what());
  69.             }
  70.         }
  71. };
  72.  
  73. class CastFactory : public ScalarFunctionFactory {
  74.  
  75.     virtual ScalarFunction *createScalarFunction(ServerInterface &interface) {
  76.         return vt_createFuncObject<Cast>(interface.allocator);
  77.     }
  78.  
  79.     virtual void getPrototype(ServerInterface &interface, ColumnTypes &argTypes, ColumnTypes &returnType) {
  80.         argTypes.addInt();
  81.         returnType.addVarchar();
  82.     }
  83.  
  84.     virtual void getReturnType(ServerInterface &srvInterface, const SizedColumnTypes &argTypes, SizedColumnTypes &returnType) {
  85.         returnType.addVarchar(80);
  86.     }
  87.  
  88. };
  89.  
  90. RegisterFactory(CastFactory);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement