Class SqlFunctionUtils


  • public class SqlFunctionUtils
    extends Object
    Built-in scalar runtime functions.

    NOTE: Before you add functions here, check if Calcite provides it in org.apache.calcite.runtime.SqlFunctions. Furthermore, make sure to implement the function efficiently. Sometimes it makes sense to create a org.apache.flink.table.codegen.calls.CallGenerator instead to avoid massive object creation and reuse instances.

    • Constructor Detail

      • SqlFunctionUtils

        public SqlFunctionUtils()
    • Method Detail

      • power

        public static double power​(double base,
                                   DecimalData exponent)
      • power

        public static double power​(DecimalData base,
                                   double exponent)
      • tanh

        public static double tanh​(DecimalData a)
        Calculates the hyperbolic tangent of a big decimal number.
      • cot

        public static double cot​(double b0)
        SQL COT operator applied to double values.
      • degrees

        public static double degrees​(DecimalData angrad)
      • radians

        public static double radians​(DecimalData angdeg)
      • abs

        public static byte abs​(byte b0)
        SQL ABS operator applied to byte values.
      • abs

        public static short abs​(short b0)
        SQL ABS operator applied to short values.
      • abs

        public static int abs​(int b0)
        SQL ABS operator applied to int values.
      • abs

        public static long abs​(long b0)
        SQL ABS operator applied to long values.
      • abs

        public static float abs​(float b0)
        SQL ABS operator applied to float values.
      • abs

        public static double abs​(double b0)
        SQL ABS operator applied to double values.
      • floor

        public static double floor​(double b0)
      • floor

        public static float floor​(float b0)
      • floor

        public static int floor​(int b0,
                                int b1)
        SQL FLOOR operator applied to int values.
      • floor

        public static long floor​(long b0,
                                 long b1)
        SQL FLOOR operator applied to long values.
      • ceil

        public static double ceil​(double b0)
      • ceil

        public static float ceil​(float b0)
      • ceil

        public static int ceil​(int b0,
                               int b1)
        SQL CEIL operator applied to int values.
      • ceil

        public static long ceil​(long b0,
                                long b1)
        SQL CEIL operator applied to long values.
      • log

        public static double log​(double x)
        Returns the natural logarithm of "x".
      • log

        public static double log​(double base,
                                 double x)
        Returns the logarithm of "x" with base "base".
      • log

        public static double log​(double base,
                                 DecimalData x)
      • log

        public static double log​(DecimalData base,
                                 double x)
      • log2

        public static double log2​(double x)
        Returns the logarithm of "a" with base 2.
      • log10

        public static double log10​(double x)
      • log10

        public static double log10​(DecimalData x)
      • lpad

        public static String lpad​(String base,
                                  int len,
                                  String pad)
        Returns the string str left-padded with the string pad to a length of len characters. If str is longer than len, the return value is shortened to len characters.
      • rpad

        public static String rpad​(String base,
                                  int len,
                                  String pad)
        Returns the string str right-padded with the string pad to a length of len characters. If str is longer than len, the return value is shortened to len characters.
      • repeat

        public static String repeat​(String str,
                                    int repeat)
        Returns a string that repeats the base string n times.
      • replace

        public static String replace​(String str,
                                     String oldStr,
                                     String replacement)
        Replaces all the old strings with the replacement string.
      • splitIndex

        public static String splitIndex​(String str,
                                        String separator,
                                        int index)
        Split target string with custom separator and pick the index-th(start with 0) result.
        Parameters:
        str - target string.
        separator - custom separator.
        index - index of the result which you want.
        Returns:
        the string at the index of split results.
      • splitIndex

        public static String splitIndex​(String str,
                                        int character,
                                        int index)
        Split target string with custom separator and pick the index-th(start with 0) result.
        Parameters:
        str - target string.
        character - int value of the separator character
        index - index of the result which you want.
        Returns:
        the string at the index of split results.
      • regexpReplace

        public static String regexpReplace​(String str,
                                           String regex,
                                           String replacement)
        Returns a string resulting from replacing all substrings that match the regular expression with replacement.
      • regexpExtract

        public static String regexpExtract​(String str,
                                           String regex,
                                           int extractIndex)
        Returns a string extracted with a specified regular expression and a regex match group index.
      • regexpExtract

        public static String regexpExtract​(String str,
                                           String regex,
                                           long extractIndex)
      • regexpExtract

        public static String regexpExtract​(String str,
                                           String regex)
        Returns the first string extracted with a specified regular expression.
      • getRegexpMatcher

        public static Matcher getRegexpMatcher​(@Nullable
                                               StringData str,
                                               @Nullable
                                               StringData regex)
        Returns a Matcher object that represents the result of matching given StringData against a specified regular expression pattern.
      • keyValue

        public static BinaryStringData keyValue​(BinaryStringData str,
                                                BinaryStringData pairSeparator,
                                                BinaryStringData kvSeparator,
                                                BinaryStringData keyName)
        Parse string as key-value string and return the value matches key name. example: keyvalue('k1=v1;k2=v2', ';', '=', 'k2') = 'v2' keyvalue('k1:v1,k2:v2', ',', ':', 'k3') = NULL
        Parameters:
        str - target string.
        pairSeparator - separator between key-value tuple.
        kvSeparator - separator between key and value.
        keyName - name of the key whose value you want return.
        Returns:
        target value.
      • hash

        public static String hash​(String algorithm,
                                  String str)
        Calculate the hash value of a given string.
        Parameters:
        algorithm - message digest algorithm.
        str - string to hash.
        Returns:
        hash value of string.
      • hash

        public static String hash​(String algorithm,
                                  String str,
                                  String charsetName)
        Calculate the hash value of a given string.
        Parameters:
        algorithm - message digest algorithm.
        str - string to hash.
        charsetName - charset of string.
        Returns:
        hash value of string.
      • parseUrl

        public static String parseUrl​(String urlStr,
                                      String partToExtract)
        Parse url and return various components of the URL. If accept any null arguments, return null.
        Parameters:
        urlStr - URL string.
        partToExtract - determines which components would return. accept values: HOST,PATH,QUERY,REF, PROTOCOL,FILE,AUTHORITY,USERINFO
        Returns:
        target value.
      • parseUrl

        public static String parseUrl​(String urlStr,
                                      String partToExtract,
                                      String key)
        Parse url and return various parameter of the URL. If accept any null arguments, return null.
        Parameters:
        urlStr - URL string.
        partToExtract - must be QUERY, or return null.
        key - parameter name.
        Returns:
        target value.
      • divideInt

        public static int divideInt​(int a,
                                    int b)
      • subString

        public static String subString​(String str,
                                       long start,
                                       long len)
      • subString

        public static String subString​(String str,
                                       long start)
      • chr

        public static String chr​(long chr)
      • overlay

        public static String overlay​(String s,
                                     String r,
                                     long start,
                                     long length)
      • hex

        public static String hex​(long x)
        Returns the hex string of a long argument.
      • hex

        public static String hex​(String x)
        Returns the hex string of a string argument.
      • strToMap

        public static Map<String,​String> strToMap​(String text)
        Creates a map by parsing text. Split text into key-value pairs using two delimiters. The first delimiter separates pairs, and the second delimiter separates key and value. If only one parameter is given, default delimiters are used: ',' as delimiter1 and '=' as delimiter2. Both delimiters are treated as regular expressions.
        Parameters:
        text - the input text
        Returns:
        the map
      • strToMap

        public static Map<String,​String> strToMap​(String text,
                                                        String listDelimiter,
                                                        String keyValueDelimiter)
        Creates a map by parsing text. Split text into key-value pairs using two delimiters. The first delimiter separates pairs, and the second delimiter separates key and value. Both listDelimiter and keyValueDelimiter are treated as regular expressions.
        Parameters:
        text - the input text
        listDelimiter - the delimiter to separates pairs
        keyValueDelimiter - the delimiter to separates key and value
        Returns:
        the map
      • sround

        public static byte sround​(byte b0)
        SQL ROUND operator applied to byte values.
      • sround

        public static byte sround​(byte b0,
                                  int b1)
        SQL ROUND operator applied to byte values.
      • sround

        public static short sround​(short b0)
        SQL ROUND operator applied to short values.
      • sround

        public static short sround​(short b0,
                                   int b1)
        SQL ROUND operator applied to short values.
      • sround

        public static int sround​(int b0)
        SQL ROUND operator applied to int values.
      • sround

        public static int sround​(int b0,
                                 int b1)
        SQL ROUND operator applied to int values.
      • sround

        public static long sround​(long b0)
        SQL ROUND operator applied to long values.
      • sround

        public static long sround​(long b0,
                                  int b1)
        SQL ROUND operator applied to long values.
      • sround

        public static BigDecimal sround​(BigDecimal b0)
        SQL ROUND operator applied to BigDecimal values.
      • sround

        public static BigDecimal sround​(BigDecimal b0,
                                        int b1)
        SQL ROUND operator applied to BigDecimal values.
      • sround

        public static float sround​(float b0)
        SQL ROUND operator applied to float values.
      • sround

        public static float sround​(float b0,
                                   int b1)
        SQL ROUND operator applied to float values.
      • sround

        public static double sround​(double b0)
        SQL ROUND operator applied to double values.
      • sround

        public static double sround​(double b0,
                                    int b1)
        SQL ROUND operator applied to double values.
      • sround

        public static DecimalData sround​(DecimalData b0)
        SQL ROUND operator applied to DecimalData values.
      • sround

        public static DecimalData sround​(DecimalData b0,
                                         int b1)
        SQL ROUND operator applied to DecimalData values.
      • isDecimal

        public static boolean isDecimal​(Object obj)
      • isDigit

        public static boolean isDigit​(Object obj)
      • isAlpha

        public static boolean isAlpha​(Object obj)
      • bitNot

        public static Byte bitNot​(Byte a)
      • bitNot

        public static Short bitNot​(Short a)
      • bitNot

        public static Long bitNot​(Long a)
      • toBase64

        public static String toBase64​(byte[] bytes)
      • uuid

        public static String uuid()
      • uuid

        public static String uuid​(byte[] b)
      • struncate

        public static int struncate​(int b0)
        SQL TRUNCATE operator applied to int values.
      • struncate

        public static int struncate​(int b0,
                                    int b1)
      • struncate

        public static long struncate​(long b0)
        SQL TRUNCATE operator applied to long values.
      • struncate

        public static long struncate​(long b0,
                                     int b1)
      • struncate

        public static DecimalData struncate​(DecimalData b0)
        SQL TRUNCATE operator applied to BigDecimal values.
      • struncate

        public static float struncate​(float b0)
        SQL TRUNCATE operator applied to double values.
      • struncate

        public static float struncate​(float b0,
                                      int b1)
      • struncate

        public static double struncate​(double b0)
        SQL TRUNCATE operator applied to double values.
      • struncate

        public static double struncate​(double b0,
                                       int b1)
      • byteArrayCompare

        public static int byteArrayCompare​(byte[] array1,
                                           byte[] array2)
        Compares two byte arrays in lexicographical order.

        The result is positive if array1 is great than array2, negative if array1 is less than array2 and 0 if array1 is equal to array2.

        Note: Currently, this is used in ScalarOperatorGens for comparing two fields of binary or varbinary type.

        Parameters:
        array1 - byte array to compare.
        array2 - byte array to compare.
        Returns:
        an Integer indicating which one is bigger
      • initcap

        public static String initcap​(String s)
        SQL INITCAP(string) function.