@@ -255,18 +255,30 @@ func leadLagImpl(name string, expr Expression, offsetAndDefault ...interface{})
255255
256256//------------ String functions ------------------//
257257
258+ // HEX function takes an input and returns its equivalent hexadecimal representation
259+ func HEX (expression Expression ) StringExpression {
260+ return StringExp (Func ("HEX" , expression ))
261+ }
262+
263+ // UNHEX for a string argument str, UNHEX(str) interprets each pair of characters in the argument
264+ // as a hexadecimal number and converts it to the byte represented by the number.
265+ // The return value is a binary string.
266+ func UNHEX (expression StringExpression ) BlobExpression {
267+ return BlobExp (Func ("UNHEX" , expression ))
268+ }
269+
258270// BIT_LENGTH returns number of bits in string expression
259- func BIT_LENGTH (stringExpression StringExpression ) IntegerExpression {
271+ func BIT_LENGTH (stringExpression StringOrBlobExpression ) IntegerExpression {
260272 return newIntegerFunc ("BIT_LENGTH" , stringExpression )
261273}
262274
263275// CHAR_LENGTH returns number of characters in string expression
264- func CHAR_LENGTH (stringExpression StringExpression ) IntegerExpression {
276+ func CHAR_LENGTH (stringExpression StringOrBlobExpression ) IntegerExpression {
265277 return newIntegerFunc ("CHAR_LENGTH" , stringExpression )
266278}
267279
268280// OCTET_LENGTH returns number of bytes in string expression
269- func OCTET_LENGTH (stringExpression StringExpression ) IntegerExpression {
281+ func OCTET_LENGTH (stringExpression StringOrBlobExpression ) IntegerExpression {
270282 return newIntegerFunc ("OCTET_LENGTH" , stringExpression )
271283}
272284
@@ -282,7 +294,7 @@ func UPPER(stringExpression StringExpression) StringExpression {
282294
283295// BTRIM removes the longest string consisting only of characters
284296// in characters (a space by default) from the start and end of string
285- func BTRIM (stringExpression StringExpression , trimChars ... StringExpression ) StringExpression {
297+ func BTRIM (stringExpression StringOrBlobExpression , trimChars ... StringOrBlobExpression ) StringExpression {
286298 if len (trimChars ) > 0 {
287299 return NewStringFunc ("BTRIM" , stringExpression , trimChars [0 ])
288300 }
@@ -291,7 +303,7 @@ func BTRIM(stringExpression StringExpression, trimChars ...StringExpression) Str
291303
292304// LTRIM removes the longest string containing only characters
293305// from characters (a space by default) from the start of string
294- func LTRIM (str StringExpression , trimChars ... StringExpression ) StringExpression {
306+ func LTRIM (str StringOrBlobExpression , trimChars ... StringOrBlobExpression ) StringExpression {
295307 if len (trimChars ) > 0 {
296308 return NewStringFunc ("LTRIM" , str , trimChars [0 ])
297309 }
@@ -300,7 +312,7 @@ func LTRIM(str StringExpression, trimChars ...StringExpression) StringExpression
300312
301313// RTRIM removes the longest string containing only characters
302314// from characters (a space by default) from the end of string
303- func RTRIM (str StringExpression , trimChars ... StringExpression ) StringExpression {
315+ func RTRIM (str StringOrBlobExpression , trimChars ... StringOrBlobExpression ) StringExpression {
304316 if len (trimChars ) > 0 {
305317 return NewStringFunc ("RTRIM" , str , trimChars [0 ])
306318 }
@@ -324,32 +336,32 @@ func CONCAT_WS(separator Expression, expressions ...Expression) StringExpression
324336
325337// CONVERT converts string to dest_encoding. The original encoding is
326338// specified by src_encoding. The string must be valid in this encoding.
327- func CONVERT (str StringExpression , srcEncoding StringExpression , destEncoding StringExpression ) StringExpression {
328- return NewStringFunc ( "CONVERT" , str , srcEncoding , destEncoding )
339+ func CONVERT (str BlobExpression , srcEncoding StringExpression , destEncoding StringExpression ) BlobExpression {
340+ return BlobExp ( Func ( "CONVERT" , str , srcEncoding , destEncoding ) )
329341}
330342
331343// CONVERT_FROM converts string to the database encoding. The original
332344// encoding is specified by src_encoding. The string must be valid in this encoding.
333- func CONVERT_FROM (str StringExpression , srcEncoding StringExpression ) StringExpression {
345+ func CONVERT_FROM (str BlobExpression , srcEncoding StringExpression ) StringExpression {
334346 return NewStringFunc ("CONVERT_FROM" , str , srcEncoding )
335347}
336348
337349// CONVERT_TO converts string to dest_encoding.
338- func CONVERT_TO (str StringExpression , toEncoding StringExpression ) StringExpression {
339- return NewStringFunc ( "CONVERT_TO" , str , toEncoding )
350+ func CONVERT_TO (str StringExpression , toEncoding StringExpression ) BlobExpression {
351+ return BlobExp ( Func ( "CONVERT_TO" , str , toEncoding ) )
340352}
341353
342354// ENCODE encodes binary data into a textual representation.
343355// Supported formats are: base64, hex, escape. escape converts zero bytes and
344356// high-bit-set bytes to octal sequences (\nnn) and doubles backslashes.
345- func ENCODE (data StringExpression , format StringExpression ) StringExpression {
346- return NewStringFunc ( "ENCODE" , data , format )
357+ func ENCODE (data BlobExpression , format StringExpression ) StringExpression {
358+ return StringExp ( Func ( "ENCODE" , data , format ) )
347359}
348360
349361// DECODE decodes binary data from textual representation in string.
350362// Options for format are same as in encode.
351- func DECODE (data StringExpression , format StringExpression ) StringExpression {
352- return NewStringFunc ( "DECODE" , data , format )
363+ func DECODE (data StringExpression , format StringExpression ) BlobExpression {
364+ return BlobExp ( Func ( "DECODE" , data , format ) )
353365}
354366
355367// FORMAT formats a number to a format like "#,###,###.##", rounded to a specified number of decimal places, then it returns the result as a string.
@@ -379,11 +391,11 @@ func RIGHT(str StringExpression, n IntegerExpression) StringExpression {
379391}
380392
381393// LENGTH returns number of characters in string with a given encoding
382- func LENGTH (str StringExpression , encoding ... StringExpression ) StringExpression {
394+ func LENGTH (str StringOrBlobExpression , encoding ... StringExpression ) IntegerExpression {
383395 if len (encoding ) > 0 {
384- return NewStringFunc ( "LENGTH" , str , encoding [0 ])
396+ return IntExp ( Func ( "LENGTH" , str , encoding [0 ]) )
385397 }
386- return NewStringFunc ( "LENGTH" , str )
398+ return IntExp ( Func ( "LENGTH" , str ) )
387399}
388400
389401// LPAD fills up the string to length length by prepending the characters
@@ -407,8 +419,13 @@ func RPAD(str StringExpression, length IntegerExpression, text ...StringExpressi
407419 return NewStringFunc ("RPAD" , str , length )
408420}
409421
422+ // BIT_COUNT returns the number of bits set in the binary string (also known as “popcount”).
423+ func BIT_COUNT (bytes BlobExpression ) IntegerExpression {
424+ return IntExp (Func ("BIT_COUNT" , bytes ))
425+ }
426+
410427// MD5 calculates the MD5 hash of string, returning the result in hexadecimal
411- func MD5 (stringExpression StringExpression ) StringExpression {
428+ func MD5 (stringExpression StringOrBlobExpression ) StringExpression {
412429 return NewStringFunc ("MD5" , stringExpression )
413430}
414431
@@ -434,7 +451,7 @@ func STRPOS(str, substring StringExpression) IntegerExpression {
434451}
435452
436453// SUBSTR extracts substring
437- func SUBSTR (str StringExpression , from IntegerExpression , count ... IntegerExpression ) StringExpression {
454+ func SUBSTR (str StringOrBlobExpression , from IntegerExpression , count ... IntegerExpression ) StringExpression {
438455 if len (count ) > 0 {
439456 return NewStringFunc ("SUBSTR" , str , from , count [0 ])
440457 }
0 commit comments