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

cherish peng commented on PHOENIX-4866:
---------------------------------------

In my scenes, I have two ways to solve this problem.

The preferred way:

I copied the org.apache.phoenix.compile.FromCompiler to my project lib, then 
did some changes:
{code:java}
+protected boolean isAlwaysEmptyFunctionMap = false;
private BaseColumnResolver(PhoenixConnection connection, int tsAddition, 
TableName mutatingTableName) {
  this.connection = connection;
  this.client = connection == null ? null : new MetaDataClient(connection);
  this.tsAddition = tsAddition;
  functionMap = new HashMap<String, PFunction>(1);

 +isAlwaysEmptyFunctionMap = true;

  this.functions = Collections.<PFunction>emptyList();
  this.mutatingTableName = mutatingTableName;
}

@Override
public PFunction resolveFunction(String functionName) throws SQLException {
  +if (functionMap.isEmpty() && isAlwaysEmptyFunctionMap) {
  +    functions = createFunctionRef(Arrays.asList(functionName), true);
  +    for (PFunction function : functions) {
  +      functionMap.put(function.getFunctionName(), function);
  +    }
  +}
  PFunction function = functionMap.get(functionName);
  if(function == null) {
    throw new FunctionNotFoundException(functionName);
  }
  return function;
}
{code}
then it works! I am newbie, so this way may be make more noise i am afraid...

Alternative way:

I have to split the whole SQL to two parts;

The first part CONTAINS the FIRST_VALUES function, then i have to insert it to 
a temporary table which i should build before this query(because the VIEW's 
dosen't support complex SQL)

The secondary part I will read from the temporary table, then use my custom 
UDFs(ARRAY_DISTINCT AND OTHERS), but my data size is so large, so this way 
drove me crazy!

 

> UDFs get error: org.apache.phoenix.schema.FunctionNotFoundException: ERROR 
> 6001 (42F01): Function undefined
> -----------------------------------------------------------------------------------------------------------
>
>                 Key: PHOENIX-4866
>                 URL: https://issues.apache.org/jira/browse/PHOENIX-4866
>             Project: Phoenix
>          Issue Type: Bug
>    Affects Versions: 5.0.0
>         Environment: [^ArrayDistinctFunction.java]
>            Reporter: cherish peng
>            Priority: Blocker
>              Labels: User-Defined-Function, functions, udfs
>         Attachments: ArrayDistinctFunction.java
>
>   Original Estimate: 72h
>  Remaining Estimate: 72h
>
>  I built an udf `ARRAY_DISTINCT`, When i exec:
> {color:#d04437}①{color} SELECT ARRAY_DISTINCT(ARRAY['A', 'B', 'A', 'C'], 
> TRUE);
> throw error:
>  
> {code:java}
> org.apache.phoenix.schema.FunctionNotFoundException: ERROR 6001 (42F01): 
> Function undefined. functionName=ARRAY_DISTINCT at 
> org.apache.phoenix.compile.FromCompiler$1.resolveFunction(FromCompiler.java:134)
>  at 
> org.apache.phoenix.compile.ExpressionCompiler.visitLeave(ExpressionCompiler.java:327)
> {code}
>  
> However when i exec:
> {color:#d04437}②{color} SELECT ARRAY_DISTINCT(ARRAY['A', 'B', 'A', 'C'], 
> TRUE) {color:#f79232}FROM TABLE_NAME;{color}
> output:
> *+---------------------+*
> *|* *ARRAY['A','B','C']* *|*
> *+---------------------+*
> *+---------------------+*
>  
> {color:#d04437}③{color} SELECT 
> ARRAY_DISTINCT({color:#f79232}array_elements{color}, TRUE) as arr 
> {color:#f79232}FROM TABLE_NAME;{color}**
> {color:#f79232}output:{color}
> *+---------------------+*
> *| ARR*  *  **  |*
> *+---------------------+*
> *| ARRAY['A','B','C'] |***
> *+---------------------+*
>  
> The DDL: 
> CREATE FUNCTION ARRAY_DISTINCT(VARCHAR[], BOOLEAN) RETURNS VARCHAR AS 
> 'com.godalgo.udf.ArrayDistinctFunction';
> My Environment:  apache-phoenix-5.0.0-HBase-2.0-bin  hbase-2.0.0 hadoop-3.1.0
>  
> I tried the built-in functions which provided by Phoenix, ALL OF THEM ARE OK .
> *I had putted ARRAY_DISTINCT's SOURCE TO attachment.*



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to