Search in sources :

Example 71 with PDataType

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

the class PhoenixRuntime method getSqlTypeName.

/**
     * 
     * @param pCol
     * @return sql type name that could be used in DDL statements, dynamic column types etc. 
     */
public static String getSqlTypeName(PColumn pCol) {
    PDataType dataType = pCol.getDataType();
    Integer maxLength = pCol.getMaxLength();
    Integer scale = pCol.getScale();
    return getSqlTypeName(dataType, maxLength, scale);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType)

Example 72 with PDataType

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

the class ScanRangesTest method foreach.

private static Collection<?> foreach(KeyRange[][] ranges, int[] widths, KeyRange keyRange, boolean expectedResult) {
    List<List<KeyRange>> slots = Lists.transform(Lists.newArrayList(ranges), ARRAY_TO_LIST);
    RowKeySchemaBuilder builder = new RowKeySchemaBuilder(10);
    for (final int width : widths) {
        if (width > 0) {
            builder.addField(new PDatum() {

                @Override
                public boolean isNullable() {
                    return false;
                }

                @Override
                public PDataType getDataType() {
                    return PChar.INSTANCE;
                }

                @Override
                public Integer getMaxLength() {
                    return width;
                }

                @Override
                public Integer getScale() {
                    return null;
                }

                @Override
                public SortOrder getSortOrder() {
                    return SortOrder.getDefault();
                }
            }, false, SortOrder.getDefault());
        } else {
            builder.addField(new PDatum() {

                @Override
                public boolean isNullable() {
                    return false;
                }

                @Override
                public PDataType getDataType() {
                    return PVarchar.INSTANCE;
                }

                @Override
                public Integer getMaxLength() {
                    return width;
                }

                @Override
                public Integer getScale() {
                    return null;
                }

                @Override
                public SortOrder getSortOrder() {
                    return SortOrder.getDefault();
                }
            }, false, SortOrder.getDefault());
        }
    }
    ScanRanges scanRanges = ScanRanges.createSingleSpan(builder.build(), slots);
    return foreach(scanRanges, widths, keyRange, expectedResult);
}
Also used : PDatum(org.apache.phoenix.schema.PDatum) PDataType(org.apache.phoenix.schema.types.PDataType) RowKeySchemaBuilder(org.apache.phoenix.schema.RowKeySchema.RowKeySchemaBuilder) SortOrder(org.apache.phoenix.schema.SortOrder) List(java.util.List)

Example 73 with PDataType

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

the class CoerceExpressionTest method testCoerceExpressionSupportsCoercingAllPDataTypesToVarBinary.

@Test
public void testCoerceExpressionSupportsCoercingAllPDataTypesToVarBinary() throws Exception {
    for (PDataType p : PDataType.values()) {
        if (!p.isArrayType()) {
            LiteralExpression v = LiteralExpression.newConstant(map.get(p.getJavaClass()), p);
            CoerceExpression e = new CoerceExpression(v, PVarbinary.INSTANCE);
            ImmutableBytesWritable ptr = new ImmutableBytesWritable();
            e.evaluate(null, ptr);
            Object obj = e.getDataType().toObject(ptr);
            assertTrue("Coercing to VARBINARY failed for PDataType " + p, obj instanceof byte[]);
        }
    }
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) Test(org.junit.Test)

Example 74 with PDataType

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

the class CoerceExpressionTest method testCoerceExpressionSupportsCoercingAllPDataTypesToBinary.

@Test
public void testCoerceExpressionSupportsCoercingAllPDataTypesToBinary() throws Exception {
    for (PDataType p : PDataType.values()) {
        if (!p.isArrayType()) {
            LiteralExpression v = LiteralExpression.newConstant(map.get(p.getJavaClass()), p);
            CoerceExpression e = new CoerceExpression(v, PBinary.INSTANCE);
            ImmutableBytesWritable ptr = new ImmutableBytesWritable();
            e.evaluate(null, ptr);
            Object obj = e.getDataType().toObject(ptr);
            assertTrue("Coercing to BINARY failed for PDataType " + p, obj instanceof byte[]);
        }
    }
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) Test(org.junit.Test)

Example 75 with PDataType

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

the class SortOrderExpressionTest method evaluateAndAssertResult.

private void evaluateAndAssertResult(Expression expression, Object expectedResult, String context) {
    context = context == null ? "" : context;
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    assertTrue(expression.evaluate(null, ptr));
    PDataType dataType = expression.getDataType();
    SortOrder sortOrder = expression.getSortOrder();
    Object result = dataType.toObject(ptr.get(), ptr.getOffset(), ptr.getLength(), dataType, sortOrder);
    assertEquals(context, expectedResult, result);
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder)

Aggregations

PDataType (org.apache.phoenix.schema.types.PDataType)152 Expression (org.apache.phoenix.expression.Expression)54 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)31 SortOrder (org.apache.phoenix.schema.SortOrder)29 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)21 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)18 Test (org.junit.Test)15 PDatum (org.apache.phoenix.schema.PDatum)12 BigDecimal (java.math.BigDecimal)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)11 List (java.util.List)10 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)10 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)10 SingleCellConstructorExpression (org.apache.phoenix.expression.SingleCellConstructorExpression)10 IOException (java.io.IOException)9 PreparedStatement (java.sql.PreparedStatement)7 Pair (org.apache.hadoop.hbase.util.Pair)7 Date (java.sql.Date)6 AndExpression (org.apache.phoenix.expression.AndExpression)6