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

The following examples show how to use org.apache.calcite.avatica.util.ByteString#length() . 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 Quicksql with MIT License 6 votes vote down vote up
/** SQL {@code POSITION(seek IN string FROM integer)} function for byte
 * strings. */
public static int position(ByteString seek, ByteString s, int from) {
  final int from0 = from - 1;
  if (from0 > s.length() || from0 < 0) {
    return 0;
  }

  // ByteString doesn't have indexOf(ByteString, int) until avatica-1.9
  // (see [CALCITE-1423]), so apply substring and find from there.
  Bug.upgrade("in avatica-1.9, use ByteString.substring(ByteString, int)");
  final int p = s.substring(from0).indexOf(seek);
  if (p < 0) {
    return 0;
  }
  return p + from;
}
 
Example 3
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 4
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 5
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 6
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 7
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 8
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 9
Source File: RexBuilder.java    From Quicksql with MIT License 5 votes vote down vote up
/** Returns a byte-string padded with zero bytes to make it at least a given
 * length, */
private static ByteString padRight(ByteString s, int length) {
  if (s.length() >= length) {
    return s;
  }
  return new ByteString(Arrays.copyOf(s.getBytes(), length));
}
 
Example 10
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 11
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 12
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 13
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 14
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;
    }
  }
}
 
Example 15
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** SQL {@code POSITION(seek IN string FROM integer)} function for byte
 * strings. */
public static int position(ByteString seek, ByteString s, int from) {
  final int from0 = from - 1;
  if (from0 > s.length() || from0 < 0) {
    return 0;
  }

  return s.indexOf(seek, from0) + 1;
}
 
Example 16
Source File: RexBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns a byte-string padded with zero bytes to make it at least a given
 * length, */
private static ByteString padRight(ByteString s, int length) {
  if (s.length() >= length) {
    return s;
  }
  return new ByteString(Arrays.copyOf(s.getBytes(), length));
}