This is an example on how to create a simple user-defined type in plinyCompute. Here simple means a user-defined type that does not have pdb containers (such as pdb::Vector, pdb::Map, etc.) or nested types that derive from pdb :: Object. Let us imagine a class that contains information about a Supplier. For this type to properly work in PlinyCompute, the following conditions have to be satisfied:
- the class has to derive from
pdb::Object
(see line 22) - the
ENABLE_DEEP_COPY
macro has to be included in the public method/member section(see line 34)
This code has to be compiled and built as a shared library (see Step 3 here). Lastly, users have to register the shared library into PlinyCompute’s catalog in their client code (see Step 4 here).
Here are the listings for the Supplier.cc and Supplier.h files that define the Supplier data type.
To see more user-defined data types, see the following examples:
#ifndef Supplier_CC #define Supplier_CC #include "GetVTable.h" #include "Supplier.h" GET_V_TABLE(Supplier) #endif
#ifndef SUPPLIER_H #define SUPPLIER_H #include "Object.h" #include "PDBVector.h" #include "PDBString.h" #include "Handle.h" #include <vector> // This class represents a Supplier Object according to the TPC-H Database Benchmark // CREATE TABLE [dbo].[SUPPLIER]( // [S_SUPPKEY] [int] NOT NULL, // [S_NAME] [char](25) NOT NULL, // [S_ADDRESS] [varchar](40) NOT NULL, // [S_NATIONKEY] [int] NOT NULL, // [S_PHONE] [char](15) NOT NULL, // [S_ACCTBAL] [decimal](15, 2) NOT NULL, // [S_COMMENT] [varchar](101) NOT NULL // ); class Supplier : public pdb::Object { public: int supplierKey; pdb::String name; pdb::String address; int nationKey; pdb::String phone; double accbal; pdb::String comment; ENABLE_DEEP_COPY ~Supplier() {} Supplier() {} // Constructor with arguments: Supplier(int supplierKey, std::string name, std::string address, int nationKey, std::string phone, double accbal, std::string comment) { this->supplierKey = supplierKey; this->name = name; this->address = address; this->nationKey = nationKey; this->phone = phone; this->accbal = accbal; this->comment = comment; } double getAccbal() const { return accbal; } void setAccbal(double accbal) { this->accbal = accbal; } const pdb::String& getAddress() const { return address; } void setAddress(const pdb::String& address) { this->address = address; } const pdb::String& getComment() const { return comment; } void setComment(const pdb::String& comment) { this->comment = comment; } const pdb::String& getName() const { return name; } void setName(const pdb::String& name) { this->name = name; } int getNationKey() const { return nationKey; } void setNationKey(int nationKey) { this->nationKey = nationKey; } const pdb::String& getPhone() const { return phone; } void setPhone(const pdb::String& phone) { this->phone = phone; } int getSupplierKey() const { return supplierKey; } void setSupplierKey(int supplierKey) { this->supplierKey = supplierKey; } }; #endif
Give a name to this file (e.g. Supplier.h) and save it in the sharedLibraries/headers/ folder.
Step 2: Create the following file Supplier.cc, which has to include the following lines:
- #include “Supplier.h”, which contains the code from the file created in the previous step.
- #include “GetVTable.h”, which contains code for properly handling classes defined in shared libraries.
- The GET_V_TABLE macro.
#include "Supplier.h" #include "GetVTable.h" GET_V_TABLE(Supplier)
save the file in the sharedLibraries/source/ folder.
Step 3: Compile and build.
Run the following make command
$ make Supplier
This will create the shared library named libSupplier.so in the libraries/ folder.
At this point, a user-defined data type has been successfully created as a shared library. You can repeat the previous steps to create additional user-defined data types.
Step 4: Register the shared library in PlinyCompute’s catalog.
The final step to make this user-defined data type available to PlinyCompute, it has to be registered in an instance of PlinyCompute’s catalog. This can be done by including the following statement in your client code, which assumes that the shared library created in the previous step is located in the libraries folder.
pdbClient.registerType("libraries/libSupplier.so");