0

I'm working on a Monetdb Database with embedded R. Now, I want to process the data from a column, which basically reduces the amount of values.

The question now is if it is possible to create a R function that takes an input column with n values and returns a columns with m (m <> n)columns.

I tried it but the output of the function is somehow irritating:

-- create data
CREATE FUNCTION generate(n INTEGER) RETURNS TABLE(data DOUBLE) LANGUAGE R{
 data.frame(rnorm(n))
};

-- reduce data 
CREATE FUNCTION reduce(data REAL) returns DOUBLE LANGUAGE R{
 data[1:(length(data)-1)]
};

-- actually reduce generated data
select reduce(data) from generate(3);

This outputs the following:

sql>select reduce(data) from generate(3);
+--------------------------+
| L4                       |
+==========================+
|      -0.6561001539230347 |
|       1.0922646522521973 |
+--------------------------+
2 tuples (47.601ms)

The interesting think happens, when I add another column to the output:

sql>select 1,reduce(data) from generate(3);
+------+--------------------------+
| L4   | L6                       |
+======+==========================+
|    1 |      -1.2222824096679688 |
|    1 |        2.696558952331543 |
+------+--------------------------+
3 tuples (5.455ms)

The number of tuples which monetdb thinks to deliver changes. Is anything wrong with the Code or is it simply wrong to reduce the length of the output column?

This is only a simplified example of the real problem. The actual problem occurs when I insert the result into another table.

CREATE TABLE test(i INTEGER, r real);
insert into test select 1,reduce(data) from generate(3);
2 affected rows (4.404ms)

gives

select * from test;
GDK reported error.
BATproject: does not match always

What is the reason for the problem? I assume that it is somehow related to the above described problem.

Frank
  • 98
  • 5
  • Note: You definitely should change reduce to use `1:(length(data)-1)`. Right now it's actually doing `1:length(data)` and then subtracting 1 from each value of the vector. Typically this causes the first value of 0 to get get dropped but it's not doing what I think you intend it to do. – Dason Apr 04 '17 at 16:03
  • @Dason yes of course, thanks. Sometimes one need a second pair of eyes – Frank Apr 04 '17 at 16:56

0 Answers0