Java Code Examples for org.apache.calcite.avatica.util.ByteString#substring()

The following examples show how to use org.apache.calcite.avatica.util.ByteString#substring() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlFunctions.java    From Quicksql with MIT License 6 votes vote down vote up
/** SQL SUBSTRING(binary FROM ... FOR ...) function. */
public static ByteString substring(ByteString c, int s, int l) {
  int lc = c.length();
  if (s < 0) {
    s += lc + 1;
  }
  int e = s + l;
  if (e < s) {
    throw RESOURCE.illegalNegativeSubstringLength().ex();
  }
  if (s > lc || e < 1) {
    return ByteString.EMPTY;
  }
  int s1 = Math.max(s, 1);
  int e1 = Math.min(e, lc + 1);
  return c.substring(s1 - 1, e1 - 1);
}
 
Example 2
Source File: SqlFunctions.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** SQL SUBSTRING(binary FROM ... FOR ...) function. */
public static ByteString substring(ByteString c, int s, int l) {
  int lc = c.length();
  if (s < 0) {
    s += lc + 1;
  }
  int e = s + l;
  if (e < s) {
    throw RESOURCE.illegalNegativeSubstringLength().ex();
  }
  if (s > lc || e < 1) {
    return ByteString.EMPTY;
  }
  int s1 = Math.max(s, 1);
  int e1 = Math.min(e, lc + 1);
  return c.substring(s1 - 1, e1 - 1);
}
 
Example 3
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/** SQL LEFT(ByteString, integer) function. */
public static @Nonnull ByteString left(@Nonnull ByteString s, int n) {
  if (n <= 0) {
    return ByteString.EMPTY;
  }
  int len = s.length();
  if (n >= len) {
    return s;
  }
  return s.substring(0, n);
}
 
Example 4
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/** SQL RIGHT(ByteString, integer) function. */
public static @Nonnull ByteString right(@Nonnull ByteString s, int n) {
  if (n <= 0) {
    return ByteString.EMPTY;
  }
  final int len = s.length();
  if (n >= len) {
    return s;
  }
  return s.substring(len - n);
}
 
Example 5
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/** SQL {@code TRIM} function applied to binary string. */
private static ByteString trim_(ByteString s, boolean left, boolean right) {
  int j = s.length();
  if (right) {
    for (;;) {
      if (j == 0) {
        return ByteString.EMPTY;
      }
      if (s.byteAt(j - 1) != 0) {
        break;
      }
      --j;
    }
  }
  int i = 0;
  if (left) {
    for (;;) {
      if (i == j) {
        return ByteString.EMPTY;
      }
      if (s.byteAt(i) != 0) {
        break;
      }
      ++i;
    }
  }
  return s.substring(i, j);
}
 
Example 6
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/** Helper for CAST(... AS VARBINARY(maxLength)). */
public static ByteString truncate(ByteString s, int maxLength) {
  if (s == null) {
    return null;
  } else if (s.length() > maxLength) {
    return s.substring(0, maxLength);
  } else {
    return s;
  }
}
 
Example 7
Source File: SqlFunctions.java    From Quicksql with MIT License 5 votes vote down vote up
/** Helper for CAST(... AS BINARY(maxLength)). */
public static ByteString truncateOrPad(ByteString s, int maxLength) {
  if (s == null) {
    return null;
  } else {
    final int length = s.length();
    if (length > maxLength) {
      return s.substring(0, maxLength);
    } else if (length < maxLength) {
      return s.concat(new ByteString(new byte[maxLength - length]));
    } else {
      return s;
    }
  }
}
 
Example 8
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** SQL LEFT(ByteString, integer) function. */
public static @Nonnull ByteString left(@Nonnull ByteString s, int n) {
  if (n <= 0) {
    return ByteString.EMPTY;
  }
  int len = s.length();
  if (n >= len) {
    return s;
  }
  return s.substring(0, n);
}
 
Example 9
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** SQL RIGHT(ByteString, integer) function. */
public static @Nonnull ByteString right(@Nonnull ByteString s, int n) {
  if (n <= 0) {
    return ByteString.EMPTY;
  }
  final int len = s.length();
  if (n >= len) {
    return s;
  }
  return s.substring(len - n);
}
 
Example 10
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** SQL {@code TRIM} function applied to binary string. */
private static ByteString trim_(ByteString s, boolean left, boolean right) {
  int j = s.length();
  if (right) {
    for (;;) {
      if (j == 0) {
        return ByteString.EMPTY;
      }
      if (s.byteAt(j - 1) != 0) {
        break;
      }
      --j;
    }
  }
  int i = 0;
  if (left) {
    for (;;) {
      if (i == j) {
        return ByteString.EMPTY;
      }
      if (s.byteAt(i) != 0) {
        break;
      }
      ++i;
    }
  }
  return s.substring(i, j);
}
 
Example 11
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Helper for CAST(... AS VARBINARY(maxLength)). */
public static ByteString truncate(ByteString s, int maxLength) {
  if (s == null) {
    return null;
  } else if (s.length() > maxLength) {
    return s.substring(0, maxLength);
  } else {
    return s;
  }
}
 
Example 12
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Helper for CAST(... AS BINARY(maxLength)). */
public static ByteString truncateOrPad(ByteString s, int maxLength) {
  if (s == null) {
    return null;
  } else {
    final int length = s.length();
    if (length > maxLength) {
      return s.substring(0, maxLength);
    } else if (length < maxLength) {
      return s.concat(new ByteString(new byte[maxLength - length]));
    } else {
      return s;
    }
  }
}