Java Code Examples for org.apache.calcite.avatica.util.ByteString
The following examples show how to use
org.apache.calcite.avatica.util.ByteString. These examples are extracted from open source projects.
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 Project: Quicksql Source File: SqlFunctions.java License: MIT License | 6 votes |
/** 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 2
Source Project: Quicksql Source File: RexBuilder.java License: MIT License | 6 votes |
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 Project: Quicksql Source File: RexExecutorTest.java License: MIT License | 6 votes |
@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 4
Source Project: Mycat2 Source File: ExplainVisitor.java License: GNU General Public License v3.0 | 6 votes |
@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 5
Source Project: calcite-avatica Source File: TypedValueTest.java License: Apache License 2.0 | 6 votes |
@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 6
Source Project: samza Source File: AvroRelConverter.java License: Apache License 2.0 | 6 votes |
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 7
Source Project: calcite Source File: RexExecutorTest.java License: Apache License 2.0 | 6 votes |
@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 8
Source Project: calcite Source File: SqlFunctions.java License: Apache License 2.0 | 6 votes |
/** 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 9
Source Project: kareldb Source File: KarelDbCellId.java License: Apache License 2.0 | 5 votes |
@Override public void funnel(Comparable[] array, PrimitiveSink into) { for (Comparable c : array) { if (c instanceof Boolean) { into.putBoolean((Boolean) c); } else if (c instanceof Integer) { into.putInt((Integer) c); } else if (c instanceof Long) { into.putLong((Long) c); } else if (c instanceof Float) { into.putFloat((Float) c); } else if (c instanceof Double) { into.putDouble((Double) c); } else if (c instanceof ByteString) { into.putBytes(((ByteString) c).getBytes()); } else if (c instanceof String) { into.putString((String) c, UTF_8); } else if (c instanceof BigDecimal) { into.putDouble(((BigDecimal) c).doubleValue()); } else if (c instanceof Date) { into.putLong(((Date) c).getTime()); } else if (c instanceof Time) { into.putLong(((Time) c).getTime()); } else if (c instanceof Timestamp) { into.putLong(((Timestamp) c).getTime()); } else { throw new IllegalArgumentException("Unsupported object of type " + c.getClass().getName()); } } }
Example 10
Source Project: calcite Source File: RexBuilder.java License: Apache License 2.0 | 5 votes |
/** * Creates a byte array literal. */ public RexLiteral makeBinaryLiteral(ByteString byteString) { return makeLiteral( byteString, typeFactory.createSqlType(SqlTypeName.BINARY, byteString.length()), SqlTypeName.BINARY); }
Example 11
Source Project: Quicksql Source File: SqlFunctions.java License: MIT License | 5 votes |
/** SQL FROM_BASE64(string) function. */ public static ByteString fromBase64(String base64) { try { base64 = FROM_BASE64_REGEXP.matcher(base64).replaceAll(""); return new ByteString(Base64.getDecoder().decode(base64)); } catch (IllegalArgumentException e) { return null; } }
Example 12
Source Project: calcite Source File: RexLiteral.java License: Apache License 2.0 | 5 votes |
/** 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 || o instanceof Boolean) { 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 13
Source Project: Quicksql Source File: SqlFunctions.java License: MIT License | 5 votes |
/** 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 14
Source Project: Quicksql Source File: SqlFunctions.java License: MIT License | 5 votes |
/** 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 15
Source Project: Quicksql Source File: SqlFunctions.java License: MIT License | 5 votes |
/** 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 16
Source Project: calcite Source File: SqlFunctionsTest.java License: Apache License 2.0 | 5 votes |
@Test 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 17
Source Project: Quicksql Source File: SqlFunctions.java License: MIT License | 5 votes |
/** 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 18
Source Project: Quicksql Source File: SqlUtil.java License: MIT License | 5 votes |
/** * 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 19
Source Project: Quicksql Source File: NlsString.java License: MIT License | 5 votes |
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 20
Source Project: Quicksql Source File: NlsString.java License: MIT License | 5 votes |
/** 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; }
Example 21
Source Project: Quicksql Source File: RexBuilder.java License: MIT License | 5 votes |
/** * Creates a byte array literal. */ public RexLiteral makeBinaryLiteral(ByteString byteString) { return makeLiteral( byteString, typeFactory.createSqlType(SqlTypeName.BINARY, byteString.length()), SqlTypeName.BINARY); }
Example 22
Source Project: calcite Source File: NlsString.java License: Apache License 2.0 | 5 votes |
/** 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; }
Example 23
Source Project: Quicksql Source File: RexBuilder.java License: MIT License | 5 votes |
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 24
Source Project: calcite Source File: RexBuilder.java License: Apache License 2.0 | 5 votes |
/** 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 25
Source Project: Quicksql Source File: RexLiteral.java License: MIT License | 5 votes |
/** 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 26
Source Project: Quicksql Source File: SqlFunctionsTest.java License: MIT License | 5 votes |
@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 Project: Quicksql Source File: SqlFunctionsTest.java License: MIT License | 5 votes |
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 28
Source Project: Quicksql Source File: SqlFunctionsTest.java License: MIT License | 5 votes |
@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 29
Source Project: Quicksql Source File: SqlFunctionsTest.java License: MIT License | 5 votes |
@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 30
Source Project: calcite-avatica Source File: TypedValue.java License: Apache License 2.0 | 5 votes |
/** 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; } }