org.apache.calcite.avatica.util.ByteString Java Examples

The following examples show how to use org.apache.calcite.avatica.util.ByteString. 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: TypedValueTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Test public void testBase64() {
  byte[] bytes = "qwertyasdf".getBytes(UTF_8);
  // Plain bytes get put into protobuf for simplicitly
  Common.TypedValue proto = Common.TypedValue.newBuilder().setBytesValue(
      com.google.protobuf.ByteString.copyFrom(bytes))
      .setType(Common.Rep.BYTE_STRING).build();

  // But we should get back a b64-string to make sure TypedValue doesn't get confused.
  Object deserializedObj = TypedValue.getSerialFromProto(proto);
  assertThat(deserializedObj, is(instanceOf(String.class)));
  assertEquals(new ByteString(bytes).toBase64String(), (String) deserializedObj);

  // But we should get a non-b64 byte array as the JDBC representation
  deserializedObj =
      TypedValue.protoToJdbc(proto, DateTimeUtils.calendar());
  assertThat(deserializedObj, is(instanceOf(byte[].class)));
  assertArrayEquals(bytes, (byte[]) deserializedObj);
}
 
Example #2
Source File: RexBuilder.java    From Quicksql with MIT License 6 votes vote down vote up
private RelDataType guessType(Object value) {
  if (value == null) {
    return typeFactory.createSqlType(SqlTypeName.NULL);
  }
  if (value instanceof Float || value instanceof Double) {
    return typeFactory.createSqlType(SqlTypeName.DOUBLE);
  }
  if (value instanceof Number) {
    return typeFactory.createSqlType(SqlTypeName.BIGINT);
  }
  if (value instanceof Boolean) {
    return typeFactory.createSqlType(SqlTypeName.BOOLEAN);
  }
  if (value instanceof String) {
    return typeFactory.createSqlType(SqlTypeName.CHAR,
        ((String) value).length());
  }
  if (value instanceof ByteString) {
    return typeFactory.createSqlType(SqlTypeName.BINARY,
        ((ByteString) value).length());
  }
  throw new AssertionError("unknown type " + value.getClass());
}
 
Example #3
Source File: ExplainVisitor.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void visit(Literal literal) {
    Object value = literal.getValue();
    String target;
    if (value instanceof String) {
        target = "'" + value + "'";
    } else if (value instanceof byte[]) {
        byte[] value1 = (byte[]) value;
        ByteString byteString = new ByteString(value1);
        target = "X'" + byteString.toString() + "'";
    } else if (value instanceof Number) {
        target = "" + value + "";
    } else if (value instanceof LocalDate) {
        target = "" + (value) + "";
    } else if (value instanceof LocalDateTime) {
        target = "" + (value) + "";
    } else if (value instanceof LocalTime) {
        target = "" + (value) + "";
    } else {
        target = "" + value + "";
    }
    append(target);
}
 
Example #4
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 #5
Source File: AvroRelConverter.java    From samza with Apache License 2.0 6 votes vote down vote up
private static boolean isSchemaCompatibleWithRelObj(Object relObj, Schema unionSchema) {
  Validate.notNull(unionSchema, "Schema cannot be null");
  if (unionSchema.getType() == Schema.Type.NULL) {
    return relObj == null;
  }

  switch (unionSchema.getType()) {
    case RECORD:
      return relObj instanceof SamzaSqlRelRecord;
    case ARRAY:
      return relObj instanceof List;
    case MAP:
      return relObj instanceof Map;
    case FIXED:
      return relObj instanceof ByteString;
    case BYTES:
      return relObj instanceof ByteString;
    default:
      return true;
  }
}
 
Example #6
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 #7
Source File: RexExecutorTest.java    From Quicksql with MIT License 6 votes vote down vote up
@Test public void testBinarySubstring() throws Exception {
  check((rexBuilder, executor) -> {
    final List<RexNode> reducedValues = new ArrayList<>();
    // hello world! -> 48656c6c6f20776f726c6421
    final RexLiteral binaryHello =
        rexBuilder.makeBinaryLiteral(
            new ByteString("Hello world!".getBytes(UTF_8)));
    final RexNode plus =
        rexBuilder.makeCall(SqlStdOperatorTable.PLUS,
            rexBuilder.makeExactLiteral(BigDecimal.ONE),
            rexBuilder.makeExactLiteral(BigDecimal.ONE));
    RexLiteral four = rexBuilder.makeExactLiteral(BigDecimal.valueOf(4));
    final RexNode substring =
        rexBuilder.makeCall(SqlStdOperatorTable.SUBSTRING,
            binaryHello, plus, four);
    executor.reduce(rexBuilder, ImmutableList.of(substring, plus),
        reducedValues);
    assertThat(reducedValues.size(), equalTo(2));
    assertThat(reducedValues.get(0), instanceOf(RexLiteral.class));
    assertThat(((RexLiteral) reducedValues.get(0)).getValue2().toString(),
        equalTo((Object) "656c6c6f")); // substring('Hello world!, 2, 4)
    assertThat(reducedValues.get(1), instanceOf(RexLiteral.class));
    assertThat(((RexLiteral) reducedValues.get(1)).getValue2(),
        equalTo((Object) 2L));
  });
}
 
Example #8
Source File: RexExecutorTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testBinarySubstring() throws Exception {
  check((rexBuilder, executor) -> {
    final List<RexNode> reducedValues = new ArrayList<>();
    // hello world! -> 48656c6c6f20776f726c6421
    final RexLiteral binaryHello =
        rexBuilder.makeBinaryLiteral(
            new ByteString("Hello world!".getBytes(UTF_8)));
    final RexNode plus =
        rexBuilder.makeCall(SqlStdOperatorTable.PLUS,
            rexBuilder.makeExactLiteral(BigDecimal.ONE),
            rexBuilder.makeExactLiteral(BigDecimal.ONE));
    RexLiteral four = rexBuilder.makeExactLiteral(BigDecimal.valueOf(4));
    final RexNode substring =
        rexBuilder.makeCall(SqlStdOperatorTable.SUBSTRING,
            binaryHello, plus, four);
    executor.reduce(rexBuilder, ImmutableList.of(substring, plus),
        reducedValues);
    assertThat(reducedValues.size(), equalTo(2));
    assertThat(reducedValues.get(0), instanceOf(RexLiteral.class));
    assertThat(((RexLiteral) reducedValues.get(0)).getValue2().toString(),
        equalTo((Object) "656c6c6f")); // substring('Hello world!, 2, 4)
    assertThat(reducedValues.get(1), instanceOf(RexLiteral.class));
    assertThat(((RexLiteral) reducedValues.get(1)).getValue2(),
        equalTo((Object) 2L));
  });
}
 
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
/** 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 #11
Source File: SqlUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Validate if value can be decoded by given charset.
 *
 * @param value nls string in byte array
 * @param charset charset
 * @throws RuntimeException If the given value cannot be represented in the
 *     given charset
 */
public static void validateCharset(ByteString value, Charset charset) {
  if (charset == StandardCharsets.UTF_8) {
    final byte[] bytes = value.getBytes();
    if (!Utf8.isWellFormed(bytes)) {
      //CHECKSTYLE: IGNORE 1
      final String string = new String(bytes, charset);
      throw RESOURCE.charsetEncoding(string, charset.name()).ex();
    }
  }
}
 
Example #12
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** SQL {@code OVERLAY} function applied to binary strings. */
public static ByteString overlay(ByteString s, ByteString r, int start,
    int length) {
  return s.substring(0, start - 1)
      .concat(r)
      .concat(s.substring(start - 1 + length));
}
 
Example #13
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 #14
Source File: SqlFunctionsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void thereAndBack(byte[] bytes) {
  final ByteString byteString = new ByteString(bytes);
  final byte[] bytes2 = byteString.getBytes();
  assertThat(bytes, equalTo(bytes2));

  final String base64String = byteString.toBase64String();
  final ByteString byteString1 = ByteString.ofBase64(base64String);
  assertThat(byteString, equalTo(byteString1));
}
 
Example #15
Source File: CassandraEnumerator.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Convert an object into the expected internal representation.
 *
 * @param obj Object to convert, if needed
 */
private Object convertToEnumeratorObject(Object obj) {
  if (obj instanceof ByteBuffer) {
    ByteBuffer buf = (ByteBuffer) obj;
    byte [] bytes = new byte[buf.remaining()];
    buf.get(bytes, 0, bytes.length);
    return new ByteString(bytes);
  } else if (obj instanceof LocalDate) {
    // converts dates to the expected numeric format
    return ((LocalDate) obj).getMillisSinceEpoch()
        / DateTimeUtils.MILLIS_PER_DAY;
  } else if (obj instanceof Date) {
    return ((Date) obj).toInstant().toEpochMilli();
  } else if (obj instanceof LinkedHashSet) {
    // MULTISET is handled as an array
    return ((LinkedHashSet) obj).toArray();
  } else if (obj instanceof TupleValue) {
    // STRUCT can be handled as an array
    final TupleValue tupleValue = (TupleValue) obj;
    int numComponents = tupleValue.getType().getComponentTypes().size();
    return IntStream.range(0, numComponents)
        .mapToObj(i ->
            tupleValue.get(i,
                CassandraSchema.CODEC_REGISTRY.codecFor(
                    tupleValue.getType().getComponentTypes().get(i)))
        ).map(this::convertToEnumeratorObject)
        .toArray();
  }

  return obj;
}
 
Example #16
Source File: RexBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a byte array literal.
 */
public RexLiteral makeBinaryLiteral(ByteString byteString) {
  return makeLiteral(
      byteString,
      typeFactory.createSqlType(SqlTypeName.BINARY, byteString.length()),
      SqlTypeName.BINARY);
}
 
Example #17
Source File: NlsString.java    From calcite with Apache License 2.0 5 votes vote down vote up
public String load(@Nonnull Pair<ByteString, Charset> key) {
  final Charset charset = key.right;
  final CharsetDecoder decoder = charset.newDecoder();
  final byte[] bytes = key.left.getBytes();
  final ByteBuffer buffer = ByteBuffer.wrap(bytes);
  try {
    return decoder.decode(buffer).toString();
  } catch (CharacterCodingException ex) {
    throw RESOURCE.charsetEncoding(
        //CHECKSTYLE: IGNORE 1
        new String(bytes, Charset.defaultCharset()),
        charset.name()).ex();
  }
}
 
Example #18
Source File: SqlFunctionsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testSha1() {
  assertThat("da39a3ee5e6b4b0d3255bfef95601890afd80709", is(sha1("")));
  assertThat("da39a3ee5e6b4b0d3255bfef95601890afd80709", is(sha1(ByteString.of("", 16))));
  assertThat("3c01bdbb26f358bab27f267924aa2c9a03fcfdb8", is(sha1("ABC")));
  assertThat("3c01bdbb26f358bab27f267924aa2c9a03fcfdb8",
      is(sha1(new ByteString("ABC".getBytes(UTF_8)))));
  try {
    String o = sha1((String) null);
    fail("Expected NPE, got " + o);
  } catch (NullPointerException e) {
    // ok
  }
}
 
Example #19
Source File: AvroRelConverter.java    From samza with Apache License 2.0 5 votes vote down vote up
public static Object convertToAvroObject(Object relObj, Schema schema) {
  if (relObj == null) {
    return null;
  }
  switch (schema.getType()) {
    case RECORD:
      return convertToGenericRecord((SamzaSqlRelRecord) relObj, getNonNullUnionSchema(schema));
    case ARRAY:
      List<Object> avroList = ((List<Object>) relObj).stream()
          .map(o -> convertToAvroObject(o, getNonNullUnionSchema(schema).getElementType()))
          .collect(Collectors.toList());
      return avroList;
    case MAP:
      return ((Map<String, ?>) relObj).entrySet()
          .stream()
          .collect(Collectors.toMap(Map.Entry::getKey,
            e -> convertToAvroObject(e.getValue(), getNonNullUnionSchema(schema).getValueType())));
    case UNION:
      for (Schema unionSchema : schema.getTypes()) {
        if (isSchemaCompatibleWithRelObj(relObj, unionSchema)) {
          return convertToAvroObject(relObj, unionSchema);
        }
      }
      return null;
    case ENUM:
      return new GenericData.EnumSymbol(schema, (String) relObj);
    case FIXED:
      return new GenericData.Fixed(schema, ((ByteString) relObj).getBytes());
    case BYTES:
      return ByteBuffer.wrap(((ByteString) relObj).getBytes());
    default:
      return relObj;
  }
}
 
Example #20
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 #21
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 #22
Source File: TypedValue.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
/** Converts a value from internal format to a type that can be serialized
 * as JSON. */
private static Object localToSerial(ColumnMetaData.Rep rep, Object value) {
  switch (rep) {
  case BYTE_STRING:
    return ((ByteString) value).toBase64String();
  default:
    return value;
  }
}
 
Example #23
Source File: SqlFunctionsTest.java    From Quicksql with MIT License 5 votes vote down vote up
@Test public void testSha1() {
  assertThat("da39a3ee5e6b4b0d3255bfef95601890afd80709", is(sha1("")));
  assertThat("da39a3ee5e6b4b0d3255bfef95601890afd80709", is(sha1(ByteString.of("", 16))));
  assertThat("3c01bdbb26f358bab27f267924aa2c9a03fcfdb8", is(sha1("ABC")));
  assertThat("3c01bdbb26f358bab27f267924aa2c9a03fcfdb8",
      is(sha1(new ByteString("ABC".getBytes(UTF_8)))));
  try {
    String o = sha1((String) null);
    fail("Expected NPE, got " + o);
  } catch (NullPointerException e) {
    // ok
  }
}
 
Example #24
Source File: SqlFunctionsTest.java    From Quicksql with MIT License 5 votes vote down vote up
@Test public void testMd5() {
  assertThat("d41d8cd98f00b204e9800998ecf8427e", is(md5("")));
  assertThat("d41d8cd98f00b204e9800998ecf8427e", is(md5(ByteString.of("", 16))));
  assertThat("902fbdd2b1df0c4f70b4a5d23525e932", is(md5("ABC")));
  assertThat("902fbdd2b1df0c4f70b4a5d23525e932",
      is(md5(new ByteString("ABC".getBytes(UTF_8)))));
  try {
    String o = md5((String) null);
    fail("Expected NPE, got " + o);
  } catch (NullPointerException e) {
    // ok
  }
}
 
Example #25
Source File: SqlFunctionsTest.java    From Quicksql with MIT License 5 votes vote down vote up
private void thereAndBack(byte[] bytes) {
  final ByteString byteString = new ByteString(bytes);
  final byte[] bytes2 = byteString.getBytes();
  assertThat(bytes, equalTo(bytes2));

  final String base64String = byteString.toBase64String();
  final ByteString byteString1 = ByteString.ofBase64(base64String);
  assertThat(byteString, equalTo(byteString1));
}
 
Example #26
Source File: SqlFunctionsTest.java    From Quicksql with MIT License 5 votes vote down vote up
@Test public void testFromBase64() {
  final List<String> expectedList =
      Arrays.asList("", "\0", "0", "a", " ", "\n", "\r\n", "\u03C0",
          "hello\tword");
  for (String expected : expectedList) {
    assertThat(fromBase64(toBase64(expected)),
        is(new ByteString(expected.getBytes(UTF_8))));
  }
  assertThat("546869732069732061207465737420537472696e672e",
      is(fromBase64("VGhpcyB  pcyBh\rIHRlc3Qg\tU3Ry\naW5nLg==").toString()));
  assertThat(fromBase64("-1"), nullValue());
}
 
Example #27
Source File: RexLiteral.java    From Quicksql with MIT License 5 votes vote down vote up
/** Returns whether a value is valid as a constant value, using the same
 * criteria as {@link #valueMatchesType}. */
public static boolean validConstant(Object o, Litmus litmus) {
  if (o == null
      || o instanceof BigDecimal
      || o instanceof NlsString
      || o instanceof ByteString) {
    return litmus.succeed();
  } else if (o instanceof List) {
    List list = (List) o;
    for (Object o1 : list) {
      if (!validConstant(o1, litmus)) {
        return litmus.fail("not a constant: {}", o1);
      }
    }
    return litmus.succeed();
  } else if (o instanceof Map) {
    @SuppressWarnings("unchecked") final Map<Object, Object> map = (Map) o;
    for (Map.Entry entry : map.entrySet()) {
      if (!validConstant(entry.getKey(), litmus)) {
        return litmus.fail("not a constant: {}", entry.getKey());
      }
      if (!validConstant(entry.getValue(), litmus)) {
        return litmus.fail("not a constant: {}", entry.getValue());
      }
    }
    return litmus.succeed();
  } else {
    return litmus.fail("not a constant: {}", o);
  }
}
 
Example #28
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));
}
 
Example #29
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 #30
Source File: NlsString.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Internal constructor; other constructors must call it. */
private NlsString(String stringValue, ByteString bytesValue,
    String charsetName, SqlCollation collation) {
  if (charsetName != null) {
    this.charsetName = charsetName.toUpperCase(Locale.ROOT);
    this.charset = SqlUtil.getCharset(charsetName);
  } else {
    this.charsetName = null;
    this.charset = null;
  }
  if ((stringValue != null) == (bytesValue != null)) {
    throw new IllegalArgumentException("Specify stringValue or bytesValue");
  }
  if (bytesValue != null) {
    if (charsetName == null) {
      throw new IllegalArgumentException("Bytes value requires charset");
    }
    SqlUtil.validateCharset(bytesValue, charset);
  } else {
    // Java string can be malformed if LATIN1 is required.
    if (this.charsetName != null
        && (this.charsetName.equals("LATIN1")
        || this.charsetName.equals("ISO-8859-1"))) {
      if (!charset.newEncoder().canEncode(stringValue)) {
        throw RESOURCE.charsetEncoding(stringValue, charset.name()).ex();
      }
    }
  }
  this.collation = collation;
  this.stringValue = stringValue;
  this.bytesValue = bytesValue;
}