Java Code Examples for org.apache.calcite.avatica.util.ByteString#EMPTY

The following examples show how to use org.apache.calcite.avatica.util.ByteString#EMPTY . 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: RexBuilder.java    From Quicksql with MIT License 5 votes vote down vote up
private static Comparable zeroValue(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case CHAR:
    return new NlsString(Spaces.of(type.getPrecision()), null, null);
  case VARCHAR:
    return new NlsString("", null, null);
  case BINARY:
    return new ByteString(new byte[type.getPrecision()]);
  case VARBINARY:
    return ByteString.EMPTY;
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
  case DECIMAL:
  case FLOAT:
  case REAL:
  case DOUBLE:
    return BigDecimal.ZERO;
  case BOOLEAN:
    return false;
  case TIME:
  case DATE:
  case TIMESTAMP:
    return DateTimeUtils.ZERO_CALENDAR;
  case TIME_WITH_LOCAL_TIME_ZONE:
    return new TimeString(0, 0, 0);
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return new TimestampString(0, 0, 0, 0, 0, 0);
  default:
    throw Util.unexpected(type.getSqlTypeName());
  }
}
 
Example 7
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 8
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 9
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 10
Source File: RexBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static Comparable zeroValue(RelDataType type) {
  switch (type.getSqlTypeName()) {
  case CHAR:
    return new NlsString(Spaces.of(type.getPrecision()), null, null);
  case VARCHAR:
    return new NlsString("", null, null);
  case BINARY:
    return new ByteString(new byte[type.getPrecision()]);
  case VARBINARY:
    return ByteString.EMPTY;
  case TINYINT:
  case SMALLINT:
  case INTEGER:
  case BIGINT:
  case DECIMAL:
  case FLOAT:
  case REAL:
  case DOUBLE:
    return BigDecimal.ZERO;
  case BOOLEAN:
    return false;
  case TIME:
  case DATE:
  case TIMESTAMP:
    return DateTimeUtils.ZERO_CALENDAR;
  case TIME_WITH_LOCAL_TIME_ZONE:
    return new TimeString(0, 0, 0);
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    return new TimestampString(0, 0, 0, 0, 0, 0);
  default:
    throw Util.unexpected(type.getSqlTypeName());
  }
}