You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by "czzmmc (via GitHub)" <gi...@apache.org> on 2023/06/19 08:14:56 UTC

[GitHub] [doris] czzmmc commented on issue #20896: [Bug] Java UDAF will be blocked when querying empty table

czzmmc commented on issue #20896:
URL: https://github.com/apache/doris/issues/20896#issuecomment-1596723106

   @zhangstar333 here is my test.
   My table:
   ```
   CREATE TABLE `user` (
     `id` varchar(1024) NULL,
     `f1` TEXT REPLACE_IF_NOT_NULL NULL
   ) ENGINE = OLAP AGGREGATE KEY(`id`) COMMENT 'OLAP' DISTRIBUTED BY HASH(`id`) BUCKETS 3 PROPERTIES (
     "replication_allocation" = "tag.location.default: 1",
     "in_memory" = "false",
     "storage_format" = "V2",
     "light_schema_change" = "true",
     "disable_auto_compaction" = "false"
   );
   
   INSERT INTO user VALUES (1, "1"), (2, "1"), (3, "2"), (4, "2"), (5, "2");
   ``` 
   My Java UDAF:
   ```java
   package org.example;
   
   import org.apache.hadoop.hive.ql.exec.UDAF;
   
   import java.io.DataInputStream;
   import java.io.DataOutputStream;
   import java.io.IOException;
   import java.util.HashMap;
   import java.util.Map;
   
   public class Add extends UDAF {
   
       public static class State {
           /*some variables if you need */
           Map<String, Integer> map = new HashMap<>();
       }
   
       /*required*/
       public State create() {
           /* here could do some init work if needed */
           return new State();
       }
   
       /*required*/
       public void destroy(State state) {
           /* here could do some destroy work if needed */
       }
   
       /*required*/
       public void add(State state, String val) {
           /* here doing update work when input data*/
           if (val != null) {
               state.map.put(val, state.map.getOrDefault(val, 0) + 1);
           }
       }
   
       /*required*/
       public void serialize(State state, DataOutputStream out) {
           /* serialize some data into buffer */
           try {
               for (Map.Entry<String, Integer> entry : state.map.entrySet()) {
                   out.writeChars(entry.getKey());
                   out.writeChar('\t');
                   out.writeInt(entry.getValue());
               }
               out.writeChar('\n');
           } catch (IOException e) {
               throw new RuntimeException(e);
           }
       }
   
       /*required*/
       public void deserialize(State state, DataInputStream in) {
           /* deserialize get data from buffer before you put */
           char c;
           try {
               while (true) {
                   StringBuilder sb = new StringBuilder();
                   while ((c = in.readChar()) != '\t' && c != '\n') {
                       sb.append(c);
                   }
                   if (c == '\n') {
                       break;
                   } else {
                       String key = sb.toString();
                       int val = in.readInt();
                       state.map.put(key, val);
                   }
               }
           } catch (IOException e) {
               throw new RuntimeException(e);
           }
       }
   
       /*required*/
       public void merge(State state, State rhs) {
           /* merge data from state */
           for (Map.Entry<String, Integer> entry : rhs.map.entrySet()) {
               state.map.put(entry.getKey(), state.map.getOrDefault(entry.getKey(), 0) + entry.getValue());
           }
       }
   
       /*required*/
       // return Type you defined
       public String getValue(State state) {
           /* return finally result */
           return state.map.toString();
       }
   }
   ``` 
   Test code
   ```
   CREATE AGGREGATE FUNCTION myadd(TEXT) RETURNS TEXT PROPERTIES (
       "file"="http://10.216.200.24:9005/java-udaf-demo.jar",
       "symbol"="org.example.Add",
       "always_nullable"="true",
       "type"="JAVA_UDF"
   );
   
   select myadd(f1) from user;  (it's ok.)
   select myadd(f1) from user where id='6'; (Dead)
   ``` 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org