Search in sources :

Example 16 with SortOrder

use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.

the class PDataTypeTest method testRealNumberComparison.

private static void testRealNumberComparison(PDataType type, Object[] a) {
    for (SortOrder sortOrder : SortOrder.values()) {
        int factor = (sortOrder == SortOrder.ASC ? 1 : -1);
        byte[] prev_b = null;
        Object prev_o = null;
        for (Object o : a) {
            byte[] b = type.toBytes(o, sortOrder);
            if (prev_b != null) {
                assertTrue("Compare of " + o + " with " + prev_o + " " + sortOrder + " failed.", ScanUtil.getComparator(type.isFixedWidth(), sortOrder).compare(prev_b, 0, prev_b.length, b, 0, b.length) * factor < 0);
            }
            prev_b = b;
            prev_o = o;
        }
    }
}
Also used : SortOrder(org.apache.phoenix.schema.SortOrder)

Example 17 with SortOrder

use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.

the class UnionCompiler method compareExperssions.

private static void compareExperssions(int i, Expression expression, List<TargetDataExpression> targetTypes) throws SQLException {
    PDataType type = expression.getDataType();
    if (type != null && type.isCoercibleTo(targetTypes.get(i).getType())) {
        ;
    } else if (targetTypes.get(i).getType() == null || targetTypes.get(i).getType().isCoercibleTo(type)) {
        targetTypes.get(i).setType(type);
    } else {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.SELECT_COLUMN_TYPE_IN_UNIONALL_DIFFS).setMessage("Column # " + i + " is " + targetTypes.get(i).getType().getSqlTypeName() + " in 1st query where as it is " + type.getSqlTypeName() + " in 2nd query").build().buildException();
    }
    Integer len = expression.getMaxLength();
    if (len != null && (targetTypes.get(i).getMaxLength() == null || len > targetTypes.get(i).getMaxLength())) {
        targetTypes.get(i).setMaxLength(len);
    }
    Integer scale = expression.getScale();
    if (scale != null && (targetTypes.get(i).getScale() == null || scale > targetTypes.get(i).getScale())) {
        targetTypes.get(i).setScale(scale);
    }
    SortOrder sortOrder = expression.getSortOrder();
    if (sortOrder != null && (!sortOrder.equals(targetTypes.get(i).getSortOrder())))
        targetTypes.get(i).setSortOrder(SortOrder.getDefault());
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder)

Example 18 with SortOrder

use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.

the class MetaDataEndpointImpl method addColumnToTable.

private void addColumnToTable(List<Cell> results, PName colName, PName famName, Cell[] colKeyValues, List<PColumn> columns, boolean isSalted) {
    int i = 0;
    int j = 0;
    while (i < results.size() && j < COLUMN_KV_COLUMNS.size()) {
        Cell kv = results.get(i);
        Cell searchKv = COLUMN_KV_COLUMNS.get(j);
        int cmp = Bytes.compareTo(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength(), searchKv.getQualifierArray(), searchKv.getQualifierOffset(), searchKv.getQualifierLength());
        if (cmp == 0) {
            colKeyValues[j++] = kv;
            i++;
        } else if (cmp > 0) {
            colKeyValues[j++] = null;
        } else {
            // shouldn't happen - means unexpected KV in system table column row
            i++;
        }
    }
    if (colKeyValues[DATA_TYPE_INDEX] == null || colKeyValues[NULLABLE_INDEX] == null || colKeyValues[ORDINAL_POSITION_INDEX] == null) {
        throw new IllegalStateException("Didn't find all required key values in '" + colName.getString() + "' column metadata row");
    }
    Cell columnSizeKv = colKeyValues[COLUMN_SIZE_INDEX];
    Integer maxLength = columnSizeKv == null ? null : PInteger.INSTANCE.getCodec().decodeInt(columnSizeKv.getValueArray(), columnSizeKv.getValueOffset(), SortOrder.getDefault());
    Cell decimalDigitKv = colKeyValues[DECIMAL_DIGITS_INDEX];
    Integer scale = decimalDigitKv == null ? null : PInteger.INSTANCE.getCodec().decodeInt(decimalDigitKv.getValueArray(), decimalDigitKv.getValueOffset(), SortOrder.getDefault());
    Cell ordinalPositionKv = colKeyValues[ORDINAL_POSITION_INDEX];
    int position = PInteger.INSTANCE.getCodec().decodeInt(ordinalPositionKv.getValueArray(), ordinalPositionKv.getValueOffset(), SortOrder.getDefault()) + (isSalted ? 1 : 0);
    Cell nullableKv = colKeyValues[NULLABLE_INDEX];
    boolean isNullable = PInteger.INSTANCE.getCodec().decodeInt(nullableKv.getValueArray(), nullableKv.getValueOffset(), SortOrder.getDefault()) != ResultSetMetaData.columnNoNulls;
    Cell dataTypeKv = colKeyValues[DATA_TYPE_INDEX];
    PDataType dataType = PDataType.fromTypeId(PInteger.INSTANCE.getCodec().decodeInt(dataTypeKv.getValueArray(), dataTypeKv.getValueOffset(), SortOrder.getDefault()));
    // For
    if (maxLength == null && dataType == PBinary.INSTANCE)
        dataType = PVarbinary.INSTANCE;
    // backward
    // compatibility.
    Cell sortOrderKv = colKeyValues[SORT_ORDER_INDEX];
    SortOrder sortOrder = sortOrderKv == null ? SortOrder.getDefault() : SortOrder.fromSystemValue(PInteger.INSTANCE.getCodec().decodeInt(sortOrderKv.getValueArray(), sortOrderKv.getValueOffset(), SortOrder.getDefault()));
    Cell arraySizeKv = colKeyValues[ARRAY_SIZE_INDEX];
    Integer arraySize = arraySizeKv == null ? null : PInteger.INSTANCE.getCodec().decodeInt(arraySizeKv.getValueArray(), arraySizeKv.getValueOffset(), SortOrder.getDefault());
    Cell viewConstantKv = colKeyValues[VIEW_CONSTANT_INDEX];
    byte[] viewConstant = viewConstantKv == null ? null : viewConstantKv.getValue();
    Cell isViewReferencedKv = colKeyValues[IS_VIEW_REFERENCED_INDEX];
    boolean isViewReferenced = isViewReferencedKv != null && Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(isViewReferencedKv.getValueArray(), isViewReferencedKv.getValueOffset(), isViewReferencedKv.getValueLength()));
    Cell columnDefKv = colKeyValues[COLUMN_DEF_INDEX];
    String expressionStr = columnDefKv == null ? null : (String) PVarchar.INSTANCE.toObject(columnDefKv.getValueArray(), columnDefKv.getValueOffset(), columnDefKv.getValueLength());
    Cell isRowTimestampKV = colKeyValues[IS_ROW_TIMESTAMP_INDEX];
    boolean isRowTimestamp = isRowTimestampKV == null ? false : Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(isRowTimestampKV.getValueArray(), isRowTimestampKV.getValueOffset(), isRowTimestampKV.getValueLength()));
    boolean isPkColumn = famName == null || famName.getString() == null;
    Cell columnQualifierKV = colKeyValues[COLUMN_QUALIFIER_INDEX];
    // Older tables won't have column qualifier metadata present. To make things simpler, just set the
    // column qualifier bytes by using the column name.
    byte[] columnQualifierBytes = columnQualifierKV != null ? Arrays.copyOfRange(columnQualifierKV.getValueArray(), columnQualifierKV.getValueOffset(), columnQualifierKV.getValueOffset() + columnQualifierKV.getValueLength()) : (isPkColumn ? null : colName.getBytes());
    PColumn column = new PColumnImpl(colName, famName, dataType, maxLength, scale, isNullable, position - 1, sortOrder, arraySize, viewConstant, isViewReferenced, expressionStr, isRowTimestamp, false, columnQualifierBytes);
    columns.add(column);
}
Also used : PInteger(org.apache.phoenix.schema.types.PInteger) PColumn(org.apache.phoenix.schema.PColumn) PColumnImpl(org.apache.phoenix.schema.PColumnImpl) PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) ByteString(com.google.protobuf.ByteString) Cell(org.apache.hadoop.hbase.Cell) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint)

Example 19 with SortOrder

use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.

the class SortOrderIT method testNonPKCompare.

@Test
public void testNonPKCompare() throws Exception {
    List<Integer> expectedResults = Lists.newArrayList(2, 3, 4);
    Integer[] saltBuckets = new Integer[] { null, 3 };
    PDataType[] dataTypes = new PDataType[] { PDecimal.INSTANCE, PDouble.INSTANCE, PFloat.INSTANCE };
    for (Integer saltBucket : saltBuckets) {
        for (PDataType dataType : dataTypes) {
            for (SortOrder sortOrder : SortOrder.values()) {
                testCompareCompositeKey(saltBucket, dataType, sortOrder, "", expectedResults, "");
            }
        }
    }
}
Also used : BigInteger(java.math.BigInteger) PDataType(org.apache.phoenix.schema.types.PDataType) SortOrder(org.apache.phoenix.schema.SortOrder) Test(org.junit.Test)

Example 20 with SortOrder

use of org.apache.phoenix.schema.SortOrder in project phoenix by apache.

the class TestUtil method getSingleSumAggregator.

public static ClientAggregators getSingleSumAggregator(String url, Properties props) throws SQLException {
    try (PhoenixConnection pconn = DriverManager.getConnection(url, props).unwrap(PhoenixConnection.class)) {
        PhoenixStatement statement = new PhoenixStatement(pconn);
        StatementContext context = new StatementContext(statement, null, new Scan(), new SequenceManager(statement));
        AggregationManager aggregationManager = context.getAggregationManager();
        SumAggregateFunction func = new SumAggregateFunction(Arrays.<Expression>asList(new KeyValueColumnExpression(new PLongColumn() {

            @Override
            public PName getName() {
                return SINGLE_COLUMN_NAME;
            }

            @Override
            public PName getFamilyName() {
                return SINGLE_COLUMN_FAMILY_NAME;
            }

            @Override
            public int getPosition() {
                return 0;
            }

            @Override
            public SortOrder getSortOrder() {
                return SortOrder.getDefault();
            }

            @Override
            public Integer getArraySize() {
                return 0;
            }

            @Override
            public byte[] getViewConstant() {
                return null;
            }

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

            @Override
            public String getExpressionStr() {
                return null;
            }

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

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

            @Override
            public byte[] getColumnQualifierBytes() {
                return SINGLE_COLUMN_NAME.getBytes();
            }
        })), null);
        aggregationManager.setAggregators(new ClientAggregators(Collections.<SingleAggregateFunction>singletonList(func), 1));
        ClientAggregators aggregators = aggregationManager.getAggregators();
        return aggregators;
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ClientAggregators(org.apache.phoenix.expression.aggregator.ClientAggregators) SortOrder(org.apache.phoenix.schema.SortOrder) PhoenixStatement(org.apache.phoenix.jdbc.PhoenixStatement) SequenceManager(org.apache.phoenix.compile.SequenceManager) StatementContext(org.apache.phoenix.compile.StatementContext) AggregationManager(org.apache.phoenix.compile.AggregationManager) PName(org.apache.phoenix.schema.PName) SumAggregateFunction(org.apache.phoenix.expression.function.SumAggregateFunction) Scan(org.apache.hadoop.hbase.client.Scan) PLongColumn(org.apache.phoenix.schema.PLongColumn) SingleAggregateFunction(org.apache.phoenix.expression.function.SingleAggregateFunction) KeyValueColumnExpression(org.apache.phoenix.expression.KeyValueColumnExpression)

Aggregations

SortOrder (org.apache.phoenix.schema.SortOrder)43 PDataType (org.apache.phoenix.schema.types.PDataType)29 BigDecimal (java.math.BigDecimal)8 Expression (org.apache.phoenix.expression.Expression)8 PDatum (org.apache.phoenix.schema.PDatum)7 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)6 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)5 Timestamp (java.sql.Timestamp)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 DataExceedsCapacityException (org.apache.phoenix.exception.DataExceedsCapacityException)4 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)4 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)4 Field (org.apache.phoenix.schema.ValueSchema.Field)4 PTimestamp (org.apache.phoenix.schema.types.PTimestamp)4 PUnsignedTimestamp (org.apache.phoenix.schema.types.PUnsignedTimestamp)4 IOException (java.io.IOException)3 SingleCellColumnExpression (org.apache.phoenix.expression.SingleCellColumnExpression)3 SingleCellConstructorExpression (org.apache.phoenix.expression.SingleCellConstructorExpression)3 RowKeySchemaBuilder (org.apache.phoenix.schema.RowKeySchema.RowKeySchemaBuilder)3