Search in sources :

Example 6 with ByteString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString in project calcite-avatica by apache.

the class AvaticaUtilsTest method testByteString.

@Test
public void testByteString() {
    final byte[] bytes = { 3, 14, 15, 92, 0, 65, 35, 0 };
    final ByteString s = new ByteString(bytes);
    final ByteString s2 = new ByteString(bytes.clone());
    final ByteString s3 = new ByteString(new byte[0]);
    final ByteString s4 = new ByteString(new byte[] { 0 });
    final ByteString s5 = new ByteString(new byte[] { 15, 92 });
    // length
    assertThat(s.length(), is(8));
    assertThat(s3.length(), is(0));
    assertThat(s4.length(), is(1));
    // equals and hashCode
    assertThat(s.hashCode(), is(s2.hashCode()));
    assertThat(s.equals(s2), is(true));
    assertThat(s2.equals(s), is(true));
    assertThat(s.equals(s3), is(false));
    assertThat(s3.equals(s), is(false));
    // toString
    assertThat(s.toString(), is("030e0f5c00412300"));
    assertThat(s3.toString(), is(""));
    assertThat(s4.toString(), is("00"));
    // indexOf
    assertThat(s.indexOf(s3), is(0));
    assertThat(s.indexOf(s3, 5), is(5));
    assertThat(s.indexOf(s3, 15), is(-1));
    assertThat(s.indexOf(s4), is(4));
    assertThat(s.indexOf(s4, 4), is(4));
    assertThat(s.indexOf(s4, 5), is(7));
    assertThat(s.indexOf(s5), is(2));
    assertThat(s.indexOf(s5, 2), is(2));
    assertThat(s.indexOf(s5, 3), is(-1));
    assertThat(s.indexOf(s5, 7), is(-1));
    // substring
    assertThat(s.substring(8), is(s3));
    assertThat(s.substring(7), is(s4));
    assertThat(s.substring(0), is(s));
    // getBytes
    assertThat(s.getBytes().length, is(8));
    assertThat(Arrays.equals(s.getBytes(), bytes), is(true));
    assertThat(s.getBytes()[3], is((byte) 92));
    final byte[] copyBytes = s.getBytes();
    copyBytes[3] = 11;
    assertThat(s.getBytes()[3], is((byte) 92));
    assertThat(s, is(s2));
}
Also used : ByteString(org.apache.calcite.avatica.util.ByteString) Test(org.junit.Test)

Example 7 with ByteString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString in project calcite-avatica by apache.

the class TypedValue method jdbcToSerial.

/**
 * Converts a value from JDBC format to a type that can be serialized as
 * JSON.
 */
private static Object jdbcToSerial(ColumnMetaData.Rep rep, Object value, Calendar calendar, SqlType componentType) {
    if (null == value) {
        return null;
    }
    switch(rep) {
        case BYTE_STRING:
            return new ByteString((byte[]) value).toBase64String();
        case JAVA_UTIL_DATE:
        case JAVA_SQL_TIMESTAMP:
        case JAVA_SQL_DATE:
        case JAVA_SQL_TIME:
            long t = ((Date) value).getTime();
            if (calendar != null) {
                t += calendar.getTimeZone().getOffset(t);
            }
            switch(rep) {
                case JAVA_SQL_DATE:
                    return (int) DateTimeUtils.floorDiv(t, DateTimeUtils.MILLIS_PER_DAY);
                case JAVA_SQL_TIME:
                    return (int) DateTimeUtils.floorMod(t, DateTimeUtils.MILLIS_PER_DAY);
                default:
                    return t;
            }
        case ARRAY:
            Array array = (Array) value;
            Objects.requireNonNull(componentType, "Component Type must not be null for ARRAYs");
            try {
                switch(componentType) {
                    case BINARY:
                    case VARBINARY:
                    case LONGVARBINARY:
                        Object[] byteStrings = (Object[]) array.getArray();
                        List<String> convertedStrings = new ArrayList<>(byteStrings.length);
                        for (Object byteString : byteStrings) {
                            convertedStrings.add((String) jdbcToSerial(Rep.BYTE_STRING, byteString, calendar, null));
                        }
                        return convertedStrings;
                    case DATE:
                    case TIME:
                        Object[] dates = (Object[]) array.getArray();
                        List<Integer> serializedDates = new ArrayList<>(dates.length);
                        for (Object obj : dates) {
                            Date date = (Date) obj;
                            if (null == obj) {
                                serializedDates.add(null);
                            } else if (componentType == SqlType.DATE) {
                                serializedDates.add((int) jdbcToSerial(Rep.JAVA_SQL_DATE, date, calendar, null));
                            } else if (componentType == SqlType.TIME) {
                                serializedDates.add((int) jdbcToSerial(Rep.JAVA_SQL_TIME, date, calendar, null));
                            } else {
                                throw new RuntimeException("Unexpected type: " + componentType);
                            }
                        }
                        return serializedDates;
                    case TIMESTAMP:
                        Object[] timestamps = (Object[]) array.getArray();
                        List<Long> serializedTimestamps = new ArrayList<>(timestamps.length);
                        for (Object obj : timestamps) {
                            Timestamp timestamp = (Timestamp) obj;
                            if (null == obj) {
                                serializedTimestamps.add(null);
                            } else {
                                serializedTimestamps.add((long) jdbcToSerial(Rep.JAVA_SQL_TIMESTAMP, timestamp, calendar, null));
                            }
                        }
                        return serializedTimestamps;
                    default:
                        // Either a primitive array or Object[], converted into List<Object>
                        return AvaticaUtils.primitiveList(array.getArray());
                }
            } catch (SQLException e) {
                throw new RuntimeException("Could not obtain base array object", e);
            }
        default:
            return value;
    }
}
Also used : SQLException(java.sql.SQLException) ByteString(org.apache.calcite.avatica.util.ByteString) ArrayList(java.util.ArrayList) ByteString(org.apache.calcite.avatica.util.ByteString) Timestamp(java.sql.Timestamp) Date(java.util.Date) Array(java.sql.Array) BigInteger(java.math.BigInteger)

Example 8 with ByteString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString in project calcite-avatica by apache.

the class TypedValue method getSerialFromProto.

/**
 * Converts the serialized value into the appropriate primitive/object.
 *
 * @param protoValue The serialized TypedValue.
 * @return The appropriate concrete type for the parameter value (as an Object).
 */
public static Object getSerialFromProto(Common.TypedValue protoValue) {
    // Deserialize the value again
    switch(protoValue.getType()) {
        case BOOLEAN:
        case PRIMITIVE_BOOLEAN:
            return protoValue.getBoolValue();
        case BYTE_STRING:
            if (protoValue.hasField(STRING_DESCRIPTOR) && !protoValue.hasField(BYTES_DESCRIPTOR)) {
                // server to support older clients, so only parse the string value when it is alone.
                return protoValue.getStringValue();
            }
            // across the wire natively as bytes. Return it as b64.
            return (new ByteString(protoValue.getBytesValue().toByteArray())).toBase64String();
        case STRING:
            return protoValue.getStringValue();
        case PRIMITIVE_CHAR:
        case CHARACTER:
            return protoValue.getStringValue().charAt(0);
        case BYTE:
        case PRIMITIVE_BYTE:
            return Long.valueOf(protoValue.getNumberValue()).byteValue();
        case DOUBLE:
        case PRIMITIVE_DOUBLE:
            return protoValue.getDoubleValue();
        case FLOAT:
        case PRIMITIVE_FLOAT:
            return Float.intBitsToFloat((int) protoValue.getNumberValue());
        case INTEGER:
        case PRIMITIVE_INT:
            return Long.valueOf(protoValue.getNumberValue()).intValue();
        case PRIMITIVE_SHORT:
        case SHORT:
            return Long.valueOf(protoValue.getNumberValue()).shortValue();
        case LONG:
        case PRIMITIVE_LONG:
            return Long.valueOf(protoValue.getNumberValue());
        case JAVA_SQL_DATE:
        case JAVA_SQL_TIME:
            return Long.valueOf(protoValue.getNumberValue()).intValue();
        case JAVA_SQL_TIMESTAMP:
        case JAVA_UTIL_DATE:
            return protoValue.getNumberValue();
        case BIG_INTEGER:
            return new BigInteger(protoValue.getBytesValue().toByteArray());
        case BIG_DECIMAL:
            // CALCITE-1103 shifts BigDecimals to be serialized as strings.
            if (protoValue.hasField(NUMBER_DESCRIPTOR)) {
                // This is the old (broken) style.
                BigInteger bigInt = new BigInteger(protoValue.getBytesValue().toByteArray());
                return new BigDecimal(bigInt, (int) protoValue.getNumberValue());
            }
            return new BigDecimal(protoValue.getStringValueBytes().toStringUtf8());
        case NUMBER:
            return Long.valueOf(protoValue.getNumberValue());
        case NULL:
            return null;
        case ARRAY:
            final List<Common.TypedValue> protoList = protoValue.getArrayValueList();
            final List<Object> list = new ArrayList<>(protoList.size());
            for (Common.TypedValue protoElement : protoList) {
                // Deserialize the TypedValue protobuf into the JDBC type
                TypedValue listElement = TypedValue.fromProto(protoElement);
                // Must preserve the TypedValue so serial/jdbc/local conversion can work as expected
                list.add(listElement);
            }
            return list;
        case OBJECT:
            if (protoValue.getNull()) {
                return null;
            }
        // fall through
        case UNRECOGNIZED:
            // Fail?
            throw new RuntimeException("Unhandled type: " + protoValue.getType());
        default:
            // Fail?
            throw new RuntimeException("Unknown type: " + protoValue.getType());
    }
}
Also used : ByteString(org.apache.calcite.avatica.util.ByteString) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Common(org.apache.calcite.avatica.proto.Common)

Example 9 with ByteString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString in project samza by apache.

the class TestAvroRelConversion method validateAvroSerializedData.

private void validateAvroSerializedData(byte[] serializedData, Object unionValue) throws IOException {
    GenericRecord complexRecordValue = genericRecordFromBytes(serializedData, ComplexRecord.SCHEMA$);
    SamzaSqlRelMessage message = complexRecordAvroRelConverter.convertToRelMessage(new KV<>("key", complexRecordValue));
    Assert.assertEquals(message.getSamzaSqlRelRecord().getFieldNames().size(), ComplexRecord.SCHEMA$.getFields().size() + 1);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("id").get(), id);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("bool_value").get(), boolValue);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("double_value").get(), doubleValue);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("string_value").get(), new Utf8(testStrValue));
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("float_value0").get(), floatValue);
    Assert.assertEquals(message.getSamzaSqlRelRecord().getField("long_value").get(), longValue);
    if (unionValue instanceof String) {
        Assert.assertEquals(message.getSamzaSqlRelRecord().getField("union_value").get(), new Utf8((String) unionValue));
    } else {
        Assert.assertEquals(message.getSamzaSqlRelRecord().getField("union_value").get(), unionValue);
    }
    Assert.assertTrue(arrayValue.stream().map(Utf8::new).collect(Collectors.toList()).equals(message.getSamzaSqlRelRecord().getField("array_values").get()));
    Assert.assertTrue(mapValue.entrySet().stream().collect(Collectors.toMap(x -> new Utf8(x.getKey()), y -> new Utf8(y.getValue()))).equals(message.getSamzaSqlRelRecord().getField("map_values").get()));
    Assert.assertTrue(Arrays.equals(((ByteString) message.getSamzaSqlRelRecord().getField("bytes_value").get()).getBytes(), testBytes.array()));
    Assert.assertTrue(Arrays.equals(((ByteString) message.getSamzaSqlRelRecord().getField("fixed_value").get()).getBytes(), DEFAULT_TRACKING_ID_BYTES));
    LOG.info(message.toString());
    KV<Object, Object> samzaMessage = complexRecordAvroRelConverter.convertToSamzaMessage(message);
    GenericRecord record = (GenericRecord) samzaMessage.getValue();
    for (Schema.Field field : ComplexRecord.SCHEMA$.getFields()) {
        if (field.name().equals("array_values")) {
            Assert.assertTrue(record.get(field.name()).equals(complexRecordValue.get(field.name())));
        } else {
            Object expected = complexRecordValue.get(field.name());
            Assert.assertEquals(expected, record.get(field.name()));
        }
    }
}
Also used : MyFixed(org.apache.samza.sql.avro.schemas.MyFixed) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Encoder(org.apache.avro.io.Encoder) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) RelSchemaConverter(org.apache.samza.sql.planner.RelSchemaConverter) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) ComplexRecord(org.apache.samza.sql.avro.schemas.ComplexRecord) SystemStream(org.apache.samza.system.SystemStream) Map(java.util.Map) ComplexUnion(org.apache.samza.sql.avro.schemas.ComplexUnion) StreetNumRecord(org.apache.samza.sql.avro.schemas.StreetNumRecord) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage) MapConfig(org.apache.samza.config.MapConfig) KV(org.apache.samza.operators.KV) IndexedRecord(org.apache.avro.generic.IndexedRecord) EncoderFactory(org.apache.avro.io.EncoderFactory) Profile(org.apache.samza.sql.avro.schemas.Profile) Utf8(org.apache.avro.util.Utf8) RelDataType(org.apache.calcite.rel.type.RelDataType) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) GenericRecord(org.apache.avro.generic.GenericRecord) Schema(org.apache.avro.Schema) DatumWriter(org.apache.avro.io.DatumWriter) Logger(org.slf4j.Logger) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) ByteString(org.apache.calcite.avatica.util.ByteString) RelRecordType(org.apache.calcite.rel.type.RelRecordType) BinaryDecoder(org.apache.avro.io.BinaryDecoder) SqlSchema(org.apache.samza.sql.schema.SqlSchema) IOException(java.io.IOException) Kind(org.apache.samza.sql.avro.schemas.Kind) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SimpleRecord(org.apache.samza.sql.avro.schemas.SimpleRecord) AddressRecord(org.apache.samza.sql.avro.schemas.AddressRecord) PhoneNumber(org.apache.samza.sql.avro.schemas.PhoneNumber) BinaryEncoder(org.apache.avro.io.BinaryEncoder) List(java.util.List) Ignore(org.junit.Ignore) Assert(org.junit.Assert) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) DecoderFactory(org.apache.avro.io.DecoderFactory) ByteString(org.apache.calcite.avatica.util.ByteString) Schema(org.apache.avro.Schema) SqlSchema(org.apache.samza.sql.schema.SqlSchema) Utf8(org.apache.avro.util.Utf8) ByteString(org.apache.calcite.avatica.util.ByteString) GenericRecord(org.apache.avro.generic.GenericRecord) SamzaSqlRelMessage(org.apache.samza.sql.data.SamzaSqlRelMessage)

Example 10 with ByteString

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.avatica.util.ByteString in project calcite by apache.

the class RexBuilder method makeLiteral.

/**
 * Creates a literal of a given type. The value is assumed to be
 * compatible with the type.
 *
 * @param value     Value
 * @param type      Type
 * @param allowCast Whether to allow a cast. If false, value is always a
 *                  {@link RexLiteral} but may not be the exact type
 * @return Simple literal, or cast simple literal
 */
public RexNode makeLiteral(Object value, RelDataType type, boolean allowCast) {
    if (value == null) {
        return makeCast(type, constantNull);
    }
    if (type.isNullable()) {
        final RelDataType typeNotNull = typeFactory.createTypeWithNullability(type, false);
        RexNode literalNotNull = makeLiteral(value, typeNotNull, allowCast);
        return makeAbstractCast(type, literalNotNull);
    }
    value = clean(value, type);
    RexLiteral literal;
    final List<RexNode> operands;
    switch(type.getSqlTypeName()) {
        case CHAR:
            return makeCharLiteral(padRight((NlsString) value, type.getPrecision()));
        case VARCHAR:
            literal = makeCharLiteral((NlsString) value);
            if (allowCast) {
                return makeCast(type, literal);
            } else {
                return literal;
            }
        case BINARY:
            return makeBinaryLiteral(padRight((ByteString) value, type.getPrecision()));
        case VARBINARY:
            literal = makeBinaryLiteral((ByteString) value);
            if (allowCast) {
                return makeCast(type, literal);
            } else {
                return literal;
            }
        case TINYINT:
        case SMALLINT:
        case INTEGER:
        case BIGINT:
        case DECIMAL:
            return makeExactLiteral((BigDecimal) value, type);
        case FLOAT:
        case REAL:
        case DOUBLE:
            return makeApproxLiteral((BigDecimal) value, type);
        case BOOLEAN:
            return (Boolean) value ? booleanTrue : booleanFalse;
        case TIME:
            return makeTimeLiteral((TimeString) value, type.getPrecision());
        case TIME_WITH_LOCAL_TIME_ZONE:
            return makeTimeWithLocalTimeZoneLiteral((TimeString) value, type.getPrecision());
        case DATE:
            return makeDateLiteral((DateString) value);
        case TIMESTAMP:
            return makeTimestampLiteral((TimestampString) value, type.getPrecision());
        case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
            return makeTimestampWithLocalTimeZoneLiteral((TimestampString) value, type.getPrecision());
        case INTERVAL_YEAR:
        case INTERVAL_YEAR_MONTH:
        case INTERVAL_MONTH:
        case INTERVAL_DAY:
        case INTERVAL_DAY_HOUR:
        case INTERVAL_DAY_MINUTE:
        case INTERVAL_DAY_SECOND:
        case INTERVAL_HOUR:
        case INTERVAL_HOUR_MINUTE:
        case INTERVAL_HOUR_SECOND:
        case INTERVAL_MINUTE:
        case INTERVAL_MINUTE_SECOND:
        case INTERVAL_SECOND:
            return makeIntervalLiteral((BigDecimal) value, type.getIntervalQualifier());
        case MAP:
            final MapSqlType mapType = (MapSqlType) type;
            @SuppressWarnings("unchecked") final Map<Object, Object> map = (Map) value;
            operands = new ArrayList<>();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                operands.add(makeLiteral(entry.getKey(), mapType.getKeyType(), allowCast));
                operands.add(makeLiteral(entry.getValue(), mapType.getValueType(), allowCast));
            }
            return makeCall(SqlStdOperatorTable.MAP_VALUE_CONSTRUCTOR, operands);
        case ARRAY:
            final ArraySqlType arrayType = (ArraySqlType) type;
            @SuppressWarnings("unchecked") final List<Object> listValue = (List) value;
            operands = new ArrayList<>();
            for (Object entry : listValue) {
                operands.add(makeLiteral(entry, arrayType.getComponentType(), allowCast));
            }
            return makeCall(SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR, operands);
        case MULTISET:
            final MultisetSqlType multisetType = (MultisetSqlType) type;
            operands = new ArrayList<>();
            for (Object entry : (List) value) {
                final RexNode e = entry instanceof RexLiteral ? (RexNode) entry : makeLiteral(entry, multisetType.getComponentType(), allowCast);
                operands.add(e);
            }
            if (allowCast) {
                return makeCall(SqlStdOperatorTable.MULTISET_VALUE, operands);
            } else {
                return new RexLiteral((Comparable) FlatLists.of(operands), type, type.getSqlTypeName());
            }
        case ROW:
            operands = new ArrayList<>();
            // noinspection unchecked
            for (Pair<RelDataTypeField, Object> pair : Pair.zip(type.getFieldList(), (List<Object>) value)) {
                final RexNode e = pair.right instanceof RexLiteral ? (RexNode) pair.right : makeLiteral(pair.right, pair.left.getType(), allowCast);
                operands.add(e);
            }
            return new RexLiteral((Comparable) FlatLists.of(operands), type, type.getSqlTypeName());
        case ANY:
            return makeLiteral(value, guessType(value), allowCast);
        default:
            throw Util.unexpected(type.getSqlTypeName());
    }
}
Also used : ByteString(org.apache.calcite.avatica.util.ByteString) RelDataType(org.apache.calcite.rel.type.RelDataType) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) MapSqlType(org.apache.calcite.sql.type.MapSqlType) MultisetSqlType(org.apache.calcite.sql.type.MultisetSqlType) NlsString(org.apache.calcite.util.NlsString) ArraySqlType(org.apache.calcite.sql.type.ArraySqlType) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map)

Aggregations

ByteString (org.apache.calcite.avatica.util.ByteString)15 BigDecimal (java.math.BigDecimal)9 DateString (org.apache.calcite.util.DateString)7 NlsString (org.apache.calcite.util.NlsString)7 TimeString (org.apache.calcite.util.TimeString)7 RelDataType (org.apache.calcite.rel.type.RelDataType)6 TimestampString (org.apache.calcite.util.TimestampString)6 ArrayList (java.util.ArrayList)5 List (java.util.List)4 Test (org.junit.Test)4 ImmutableList (com.google.common.collect.ImmutableList)3 Charset (java.nio.charset.Charset)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Calendar (java.util.Calendar)3 Map (java.util.Map)3 TimeZone (java.util.TimeZone)3 DateTimeUtils (org.apache.calcite.avatica.util.DateTimeUtils)3 SqlCollation (org.apache.calcite.sql.SqlCollation)3 SqlTypeName (org.apache.calcite.sql.type.SqlTypeName)3 BigInteger (java.math.BigInteger)2