Vertica and R quickie – group_concat transform function

June 23, 2014
By

(This article was first published on My contRibution » R, and kindly contributed to R-bloggers)

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;

 

 


To leave a comment for the author, please follow the link and comment on his blog: My contRibution » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.