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

ASF GitHub Bot commented on FLINK-9970:
---------------------------------------

yanghua commented on a change in pull request #6432: [FLINK-9970] Add ASCII/CHR 
function for table/sql API
URL: https://github.com/apache/flink/pull/6432#discussion_r205942899
 
 

 ##########
 File path: 
flink-libraries/flink-table/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala
 ##########
 @@ -196,9 +196,35 @@ object ScalarFunctions {
     new String(data)
   }
 
+  /**
+    * Returns the numeric value of the leftmost character of the string str.
+    * Returns 0 if str is the empty string. Returns NULL if str is NULL.
+    */
+  def ascii(str: String): String = {
+    if (str == null) {
+      null
+    } else if (str.equals("")) {
+      ""
+    } else {
+      str.charAt(0).toByte.toString
+    }
+  }
+
   /**
     * Returns the base string decoded with base64.
     */
   def fromBase64(str: String): String = new String(Base64.decodeBase64(str))
 
+  /**
+    * Returns string contains a character which converts from a ASCII integer.
+    * If the ASCII less then 0 or greater than 255, return null.
+    */
+  def chr(ascii: Integer): String = {
 
 Review comment:
   @hequn8128  maybe we could not add more functions to support 
`byte/short/int/long` type, I just tried change all the specific `Integer` to 
`Long` about this function, then tested : 
   
   ```
   testAllApis(
         'f36.chr(),
         "f36.chr()",
         "CHR(f36)",
         "A")
   
       testAllApis(
         'f36.chr(),
         "f36.chr()",
         "CHR(CAST(f36 AS SMALLINT))",
         "A")
   
       testAllApis(
         'f36.chr(),
         "f36.chr()",
         "CHR(CAST(f36 AS TINYINT))",
         "A")
   
       testAllApis(
         'f36.chr(),
         "f36.chr()",
         "CHR(CAST(f36 AS BIGINT))",
         "A")
   ```
   
   all test passed

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Add ASCII/CHR function for table/sql API
> ----------------------------------------
>
>                 Key: FLINK-9970
>                 URL: https://issues.apache.org/jira/browse/FLINK-9970
>             Project: Flink
>          Issue Type: Sub-task
>          Components: Table API & SQL
>            Reporter: vinoyang
>            Assignee: vinoyang
>            Priority: Minor
>              Labels: pull-request-available
>
> for ASCII function : 
> refer to : 
> [https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_ascii]
> for CHR function : 
> This function convert ASCII code to a character,
> refer to : [https://doc.ispirer.com/sqlways/Output/SQLWays-1-071.html]
> Considering "CHAR" always is a keyword in many database, so we use "CHR" 
> keyword.



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

Reply via email to