[ 
https://issues.apache.org/jira/browse/CASSANDRA-10783?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15072238#comment-15072238
 ] 

Robert Stupp commented on CASSANDRA-10783:
------------------------------------------

I'm not yet fully convinced with [my 
patch|https://github.com/snazy/cassandra/tree/10783-udf-literal-trunk], but the 
CQL changes look fine, which allows to pass both literals and bound variables 
as function arguments. As a side-effect it also allows "selection" of literals 
and bound variables. Examples:
{code}
-- "weak" typing, works as long as maxFunc has no ambiguous overload
SELECT pk, maxFunc(valInt, 100) FROM foo;

-- explicit type
SELECT pk, maxFunc(valInt, (int)100) FROM foo;
SELECT pk, calc_something(valInt, (map<int,text>){1: 'foo', 2: 'bar', 3: 
'baz'}) FROM foo;

-- literal "selection"
cqlsh> select 'abc', 42, (map<int, text>){1: 'foo', 2: 'bar', 3: 'baz'}, 
(tuple<text,int>)('foo', 88), keyspace_name from system_schema.keyspaces ;

 'abc' | 42 | (map<int, text>){1: 'foo', 2: 'bar', 3: 'baz'} | (tuple<text, 
int>)('foo', 88) | keyspace_name
-------+----+------------------------------------------------+-------------------------------+--------------------
   abc | 42 |                 {1: 'foo', 2: 'bar', 3: 'baz'} |                  
 ('foo', 88) |        system_auth
   abc | 42 |                 {1: 'foo', 2: 'bar', 3: 'baz'} |                  
 ('foo', 88) |      system_schema
   abc | 42 |                 {1: 'foo', 2: 'bar', 3: 'baz'} |                  
 ('foo', 88) | system_distributed
   abc | 42 |                 {1: 'foo', 2: 'bar', 3: 'baz'} |                  
 ('foo', 88) |             system
   abc | 42 |                 {1: 'foo', 2: 'bar', 3: 'baz'} |                  
 ('foo', 88) |      system_traces

(5 rows)

-- bound variable "selection"
SELECT (int)?, (decimal):adecimal, (text)?, (tuple<int,text>):tuple FROM foo;

-- bound variable arguments (both named and unnamed)
SELECT maxFunc(someColumn, (int):theMax), (int):theMax FROM foo;
SELECT anotherFunc(someColumn, (some_user_type_name)?) FROM foo;
SELECT anotherFunc(someColumn, (tuple<text, int, bigint, map<int, text>>)?) 
FROM foo;
{code}

The following sequence would generate an error after the overload has been 
declared:
{code}
CREATE FUNCTION someFunc (val1 int, val2 bigint) ...;
-- the SELECT works fine
SELECT someFunc(someCol, 100) FROM foo;
CREATE FUNCTION someFunc (val1 int, val2 int) ...;
-- this one raises an exception: 'Ambiguous call to function...'
SELECT someFunc(someCol, 100) FROM foo;
{code}

Comments welcome :)

> Allow literal value as parameter of UDF & UDA
> ---------------------------------------------
>
>                 Key: CASSANDRA-10783
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-10783
>             Project: Cassandra
>          Issue Type: Improvement
>          Components: CQL
>            Reporter: DOAN DuyHai
>            Assignee: Robert Stupp
>            Priority: Minor
>             Fix For: 3.x
>
>
> I have defined the following UDF
> {code:sql}
> CREATE OR REPLACE FUNCTION  maxOf(current int, testValue int) RETURNS NULL ON 
> NULL INPUT 
> RETURNS int 
> LANGUAGE java 
> AS  'return Math.max(current,testValue);'
> CREATE TABLE maxValue(id int primary key, val int);
> INSERT INTO maxValue(id, val) VALUES(1, 100);
> SELECT maxOf(val, 101) FROM maxValue WHERE id=1;
> {code}
> I got the following error message:
> {code}
> SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query] 
> message="line 1:19 no viable alternative at input '101' (SELECT maxOf(val1, 
> [101]...)">
> {code}
>  It would be nice to allow literal value as parameter of UDF and UDA too.
>  I was thinking about an use-case for an UDA groupBy() function where the end 
> user can *inject* at runtime a literal value to select which aggregation he 
> want to display, something similar to GROUP BY ... HAVING <filter clause>



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to