Search in sources :

Example 71 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class PDataTypeForArraysTest method testForVarCharArrayOneElement.

@Test
public void testForVarCharArrayOneElement() {
    String[] strArr = new String[1];
    strArr[0] = "ereref";
    PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(PVarchar.INSTANCE, strArr);
    byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr);
    PhoenixArray resultArr = (PhoenixArray) PVarcharArray.INSTANCE.toObject(bytes, 0, bytes.length);
    assertEquals(arr, resultArr);
}
Also used : PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) Test(org.junit.Test)

Example 72 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class PDataTypeForArraysTest method testForVarCharArrayForOddNumberWithIndex5.

@Test
public void testForVarCharArrayForOddNumberWithIndex5() {
    String[] strArr = new String[5];
    strArr[0] = "abx";
    strArr[1] = "ereref";
    strArr[2] = "random";
    strArr[3] = null;
    strArr[4] = "random12";
    PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(PVarchar.INSTANCE, strArr);
    byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr);
    ImmutableBytesWritable ptr = new ImmutableBytesWritable(bytes);
    PArrayDataTypeDecoder.positionAtArrayElement(ptr, 4, PVarchar.INSTANCE, PVarchar.INSTANCE.getByteSize());
    int offset = ptr.getOffset();
    int length = ptr.getLength();
    byte[] bs = ptr.get();
    byte[] res = new byte[length];
    System.arraycopy(bs, offset, res, 0, length);
    assertEquals("random12", Bytes.toString(res));
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) PUnsignedSmallint(org.apache.phoenix.schema.types.PUnsignedSmallint) PUnsignedTinyint(org.apache.phoenix.schema.types.PUnsignedTinyint) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint) Test(org.junit.Test)

Example 73 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class PDataTypeForArraysTest method testVarCharArrayWithGreatherThan255NullsInMiddle.

@Test
public void testVarCharArrayWithGreatherThan255NullsInMiddle() {
    String[] strArr = new String[300];
    strArr[0] = "abc";
    strArr[1] = "bcd";
    strArr[2] = null;
    strArr[3] = null;
    strArr[4] = "bcd";
    for (int i = 5; i < strArr.length - 2; i++) {
        strArr[i] = null;
    }
    strArr[strArr.length - 1] = "abc";
    PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(PVarchar.INSTANCE, strArr);
    byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr);
    PhoenixArray resultArr = (PhoenixArray) PVarcharArray.INSTANCE.toObject(bytes, 0, bytes.length);
    assertEquals(arr, resultArr);
}
Also used : PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) PUnsignedSmallint(org.apache.phoenix.schema.types.PUnsignedSmallint) PUnsignedTinyint(org.apache.phoenix.schema.types.PUnsignedTinyint) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint) Test(org.junit.Test)

Example 74 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class PatternPerformanceTest method testSplit.

private void testSplit(AbstractBaseSplitter pattern, String name) throws SQLException {
    timer.reset();
    for (int i = 0; i < maxTimes; ++i) {
        ImmutableBytesWritable ptr = dataPtr[i % 3];
        resultPtr.set(ptr.get(), ptr.getOffset(), ptr.getLength());
        boolean ret = pattern.split(resultPtr);
        if (ENABLE_ASSERT) {
            PhoenixArray array = (PhoenixArray) PVarcharArray.INSTANCE.toObject(resultPtr);
            assertTrue(ret && (i % 3 != 1 || ((String[]) array.getArray()).length == 2));
        }
    }
    timer.printTime(name);
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray)

Example 75 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class LiteralExpression method newConstant.

// TODO: cache?
public static LiteralExpression newConstant(Object value, PDataType type, Integer maxLength, Integer scale, SortOrder sortOrder, Determinism determinism, boolean rowKeyOrderOptimizable) throws SQLException {
    if (value == null) {
        return (type == null) ? getNullLiteralExpression(determinism) : getTypedNullLiteralExpression(type, determinism);
    } else if (value instanceof Boolean) {
        return getBooleanLiteralExpression((Boolean) value, determinism);
    }
    PDataType actualType = PDataType.fromLiteral(value);
    try {
        value = type.toObject(value, actualType);
    } catch (IllegalDataException e) {
        throw TypeMismatchException.newException(type, actualType, value.toString());
    }
    byte[] b = type.isArrayType() ? ((PArrayDataType) type).toBytes(value, PArrayDataType.arrayBaseType(type), sortOrder, rowKeyOrderOptimizable) : type.toBytes(value, sortOrder);
    if (type == PVarchar.INSTANCE || type == PChar.INSTANCE) {
        if (type == PChar.INSTANCE && maxLength != null && b.length < maxLength) {
            if (rowKeyOrderOptimizable) {
                b = type.pad(b, maxLength, sortOrder);
            } else {
                b = StringUtil.padChar(b, maxLength);
            }
        } else if (value != null) {
            maxLength = ((String) value).length();
        }
    } else if (type.isArrayType()) {
        maxLength = ((PhoenixArray) value).getMaxLength();
    }
    if (b.length == 0) {
        return getTypedNullLiteralExpression(type, determinism);
    }
    if (maxLength == null) {
        maxLength = type == null || !type.isFixedWidth() ? null : type.getMaxLength(value);
    }
    return new LiteralExpression(value, type, b, maxLength, scale, sortOrder, determinism);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) PBoolean(org.apache.phoenix.schema.types.PBoolean) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Aggregations

PhoenixArray (org.apache.phoenix.schema.types.PhoenixArray)107 Test (org.junit.Test)97 Connection (java.sql.Connection)25 ResultSet (java.sql.ResultSet)25 PSmallint (org.apache.phoenix.schema.types.PSmallint)25 PTinyint (org.apache.phoenix.schema.types.PTinyint)25 PUnsignedSmallint (org.apache.phoenix.schema.types.PUnsignedSmallint)25 PUnsignedTinyint (org.apache.phoenix.schema.types.PUnsignedTinyint)25 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)23 PreparedStatement (java.sql.PreparedStatement)7 Properties (java.util.Properties)7 BaseTest (org.apache.phoenix.query.BaseTest)7 Array (java.sql.Array)6 PDouble (org.apache.phoenix.schema.types.PDouble)5 PLong (org.apache.phoenix.schema.types.PLong)4 PUnsignedLong (org.apache.phoenix.schema.types.PUnsignedLong)4 Timestamp (java.sql.Timestamp)3 PTimestamp (org.apache.phoenix.schema.types.PTimestamp)3 PUnsignedDouble (org.apache.phoenix.schema.types.PUnsignedDouble)3 PUnsignedTimestamp (org.apache.phoenix.schema.types.PUnsignedTimestamp)3