Vertica and R quickie – group_concat transform function

I’m guessing you can do this in C++, but hey… it works for me.

You’ll notice 3 functions that need to be created, the main function which will be called from Vertica, the params functions which is optional and holds the config of parameters optionally pased to the function. And finally, and mandatory, is the factory function which tells Vertica what to expect when running ‘group_concat’.

 

 

group_concat <- function(x,y) {

 options(useFancyQuotes=FALSE)

 # initialize parameters
 sep <- ifelse(is.null(y[['delimiter']]),',',as.character(y[['delimiter']]))
 if(is.null(y[['quote']])) y[['quote']] <- 0
 doQuote <- ifelse(y[['quote']]==1 ,TRUE,FALSE) 

 if(doQuote) ret <- paste0(sQuote(x[,1]),collapse=sep)
 else ret <- paste0(x[,1],collapse=sep)

 ret

}

group_concat_params <- function() {
 params <- data.frame(datatype=c("varchar", "int"), length=c(100,NA), scale=rep(NA,2),name=c("delimiter","quote"))
 params
}

group_concat_factory <- function() {
 list(
 name=group_concat
 ,udxtype=c("transform")
 ,intype=c("any")
 ,outtype=c("varchar(65000)")
 ,parametertypecallback=group_concat_params
 # ,volatility=c("stable")
 # ,strict=c("called_on_null_input")
 )
}

After distributiong the file into the same directory on all nodes.

In Vertica run :

 

create or replace library r_func as '.../group_concat.r' language 'R';
create or replace transform function group_concat as name 'group_concat_factory' library r_func;

select group_concat(id using parameters delimiter='|', quote=1) over() from sometable;

 

 

Advertisements

3 thoughts on “Vertica and R quickie – group_concat transform function

  1. Why stopped publishing your UDX in Vertica … to me have been really useful your examples
    Massimo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s