Snowpark Snowflake UDF input and return type error - snowflake-cloud-data-platform

I am new to snowpark, I am trying to write a custom UDF but facing errors while passing input and return types.
Code
#udf
def new(df: PandasDataFrame[int, float, float]) -> PandasDataFrame[int, str]):
// my code
return df
Error
Invalid ReturnType or input types for udf : return type PandasDataFrameType(), input types PandasDataFrametype()]

Snowpark Python UDFs are scalar functions; for each row passed to the UDF, the UDF returns a value, as documented here.
If you use Python UDF Batch API you can return batches of results as Pandas arrays or Series, but that is the closest you can get as of now.

Related

T-SQL UDF aggregate function

Any guidance an producing a UDF aggregate function so that when used in a grouping query, it will return results based on how many values in the group. I know the aggregate function needs to return an int but not sure how to pass the values (which vary) into it... Not a scalar type and not sure if and how it would constitute a tabular type.
E.g how would I build a udf function for average...
You can create UDF aggregates in c#.
A good start for reading is here:
https://learn.microsoft.com/en-us/sql/relational-databases/clr-integration-database-objects-user-defined-functions/clr-user-defined-aggregates?view=sql-server-2017

Groovy-Passing Integer array to PLSQL

I am calling a Stored Procedure that takes the input as array of numbers.
From the the Groovy side i am passing the int[] to that stored procedure.
But i am getting "Invalid Column Type" as the result from Stored Procedure.
Could somebody suggest me that how to pass a array of numbers from Groovy side?
The TYPE that i have created in Stored Procedure side is;
create or replace TYPE NUMBER_LIST IS TABLE OF NUMBER;
The int[] in groovy is;
int[] boxIntArr = new int[5];
Can we pass the boxIntArr in place of NUMBER_LIST?
You should be passing in an instance of java.sql.Array rather than a raw int[]. You can convert the Java array to a SQL array with java.sql.Connection.createArrayOf().
EDIT:
Oracle doesn't support the standard Connection.createArrayOf() method. Use the Oracle specific OracleConnection.createARRAY() method instead.

Aggregate function in PostgreSQL to pass array to C function

I have a C function extension libray for PostgreSQL (v8.4). The library contains a function foo() with the following signature:
double foo(const double* values, const size_t len);
I want to pass columnar data for a specific table in a postgresql db (v 8.4) to the C function, by using an "aggregate" function agg().
Assuming I am using the employees table in my db, with the following columns in the employees table: id, name, salary.
I want to pass an array of all of the salaries as a float8[] to my C function - i.e. the data I want to pass to the C function is that which is printed to the console when I type
select salary from employees
I discovered the array_agg() function and thought that I could use it to return a float8[] to my C function (via an adapter), so that I could execute a statement like:
select foo(array_agg(salary)) from employees;
however when I tried this I realised I was getting incorrect results.
I then run SELECT array_agg(wages) from employees and was suprised to find that instead of a simple array of numbers, a lot of blank lines were printed to the console - along with what a whole bunch of numbers "squeezed" into one array toward the end of the printout. Needless to say, the data in the table (salary column) does not accept NULL values, so I don't understand what array_agg() is returning to the screen.
What I need to find, is a way to create a function (aggregate or otherwise) that returns
data equivalent to "select [columnname] from tablename" as an array of floats.
I have spent most of the day searching online and the postgres documentation - and I have not as yet found a useful example of creating user defined aggregate functions or anything remotely useful for helping solve this problem.
I'd be grateful for any help in writing a function that returns the values in a column (of a table or query), so that I pass those values to a C function.
Firstly I'm not sure this needs C UDF aggregate function or that you need to select into an array? Why doesn't a select sum(salary), ... work?
Remember each time you include a select function into an array PostgreSQL does sequential table scan for each array.

Have table-valued function in T-SQL return table with variable number of columns

Is it possible to have a table-valued function in T-SQL return a table with a variable number of columns?
The column names may simply be 1, 2, …, n.
Right now I have a "string split" function that returns a single-columned 1 x n table, and I pivot the table afterwards to an n x 1 table, but I'd rather streamline the process by returning the correct table format to begin with.
I intend to use a CLR procedure in C# for this function, I just don't know how to set up the user-defined function to return my data in the format I want: with a variable number of columns, dependent on the input string.
It is not possible to return a non-static Result Set from a Table-Valued Function (TVF), whether it be written in T-SQL or .NET / SQLCLR. Only Stored Procedures can dynamically create a Result Set.
Basically, any function needs to return a consistent result type, whether it is a scalar value or a collection (i.e. Result Set).
However, in a SQLCLR stored procedure, you can create a dynamic Result Set via SqlMetaData. As long as you don't have an explicit need to SELECT ... FROM it, then maybe a stored procedure would work.
Of course, you might also be able to get away with doing this in T-SQL, using dynamic SQL to construct a SELECT statement based on the output of your split function.
A lot of this comes down to the exact context in which this functionality needs to be used.

sql server udf, return the same type as the input expression

is it possible for a udf to return the same data type as one of its parameters?
i would like my udf to accept a decimal of any precision and scale
and return the same type.
You can make the function accept and return a sql_variant.

Resources