Search in sources :

Example 1 with IllegalDataException

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

the class MutationState method addRowMutations.

private Iterator<Pair<PName, List<Mutation>>> addRowMutations(final TableRef tableRef, final Map<ImmutableBytesPtr, RowMutationState> values, final long timestamp, boolean includeAllIndexes, final boolean sendAll) {
    final PTable table = tableRef.getTable();
    final // Only maintain tables with immutable rows through this client-side mechanism
    Iterator<PTable> indexes = // TODO: remove check for isWALDisabled once PHOENIX-3137 is fixed.
    includeAllIndexes || table.isWALDisabled() ? IndexMaintainer.nonDisabledIndexIterator(table.getIndexes().iterator()) : table.isImmutableRows() ? IndexMaintainer.enabledGlobalIndexIterator(table.getIndexes().iterator()) : Iterators.<PTable>emptyIterator();
    final List<Mutation> mutationList = Lists.newArrayListWithExpectedSize(values.size());
    final List<Mutation> mutationsPertainingToIndex = indexes.hasNext() ? Lists.<Mutation>newArrayListWithExpectedSize(values.size()) : null;
    generateMutations(tableRef, timestamp, values, mutationList, mutationsPertainingToIndex);
    return new Iterator<Pair<PName, List<Mutation>>>() {

        boolean isFirst = true;

        @Override
        public boolean hasNext() {
            return isFirst || indexes.hasNext();
        }

        @Override
        public Pair<PName, List<Mutation>> next() {
            if (isFirst) {
                isFirst = false;
                return new Pair<PName, List<Mutation>>(table.getPhysicalName(), mutationList);
            }
            PTable index = indexes.next();
            List<Mutation> indexMutations;
            try {
                indexMutations = IndexUtil.generateIndexData(table, index, values, mutationsPertainingToIndex, connection.getKeyValueBuilder(), connection);
                // we may also have to include delete mutations for immutable tables if we are not processing all the tables in the mutations map
                if (!sendAll) {
                    TableRef key = new TableRef(index);
                    Map<ImmutableBytesPtr, RowMutationState> rowToColumnMap = mutations.remove(key);
                    if (rowToColumnMap != null) {
                        final List<Mutation> deleteMutations = Lists.newArrayList();
                        generateMutations(tableRef, timestamp, rowToColumnMap, deleteMutations, null);
                        indexMutations.addAll(deleteMutations);
                    }
                }
            } catch (SQLException e) {
                throw new IllegalDataException(e);
            }
            return new Pair<PName, List<Mutation>>(index.getPhysicalName(), indexMutations);
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : SQLException(java.sql.SQLException) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) PTable(org.apache.phoenix.schema.PTable) PName(org.apache.phoenix.schema.PName) Iterator(java.util.Iterator) List(java.util.List) Mutation(org.apache.hadoop.hbase.client.Mutation) PTableRef(org.apache.phoenix.schema.PTableRef) TableRef(org.apache.phoenix.schema.TableRef) Pair(org.apache.hadoop.hbase.util.Pair) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Example 2 with IllegalDataException

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

the class ExternalSqlTypeIdFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression child = children.get(0);
    if (!child.evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        return true;
    }
    int sqlType = child.getDataType().getCodec().decodeInt(ptr, child.getSortOrder());
    try {
        byte[] externalIdTypeBytes = PInteger.INSTANCE.toBytes(PDataType.fromTypeId(sqlType).getResultSetSqlType());
        ptr.set(externalIdTypeBytes);
    } catch (IllegalDataException e) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
    }
    return true;
}
Also used : Expression(org.apache.phoenix.expression.Expression) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Example 3 with IllegalDataException

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

the class UpsertCompiler method setValues.

private static void setValues(byte[][] values, int[] pkSlotIndex, int[] columnIndexes, PTable table, Map<ImmutableBytesPtr, RowMutationState> mutation, PhoenixStatement statement, boolean useServerTimestamp, IndexMaintainer maintainer, byte[][] viewConstants, byte[] onDupKeyBytes, int numSplColumns) throws SQLException {
    Map<PColumn, byte[]> columnValues = Maps.newHashMapWithExpectedSize(columnIndexes.length);
    byte[][] pkValues = new byte[table.getPKColumns().size()][];
    // here and we will fill in the byte later in PRowImpl.
    if (table.getBucketNum() != null) {
        pkValues[0] = new byte[] { 0 };
    }
    for (int i = 0; i < numSplColumns; i++) {
        pkValues[i + (table.getBucketNum() != null ? 1 : 0)] = values[i];
    }
    // case when the table doesn't have a row timestamp column
    Long rowTimestamp = null;
    RowTimestampColInfo rowTsColInfo = new RowTimestampColInfo(useServerTimestamp, rowTimestamp);
    for (int i = 0, j = numSplColumns; j < values.length; j++, i++) {
        byte[] value = values[j];
        PColumn column = table.getColumns().get(columnIndexes[i]);
        if (SchemaUtil.isPKColumn(column)) {
            pkValues[pkSlotIndex[i]] = value;
            if (SchemaUtil.getPKPosition(table, column) == table.getRowTimestampColPos()) {
                if (!useServerTimestamp) {
                    PColumn rowTimestampCol = table.getPKColumns().get(table.getRowTimestampColPos());
                    rowTimestamp = PLong.INSTANCE.getCodec().decodeLong(value, 0, rowTimestampCol.getSortOrder());
                    if (rowTimestamp < 0) {
                        throw new IllegalDataException("Value of a column designated as ROW_TIMESTAMP cannot be less than zero");
                    }
                    rowTsColInfo = new RowTimestampColInfo(useServerTimestamp, rowTimestamp);
                }
            }
        } else {
            columnValues.put(column, value);
        }
    }
    ImmutableBytesPtr ptr = new ImmutableBytesPtr();
    table.newKey(ptr, pkValues);
    if (table.getIndexType() == IndexType.LOCAL && maintainer != null) {
        byte[] rowKey = maintainer.buildDataRowKey(ptr, viewConstants);
        HRegionLocation region = statement.getConnection().getQueryServices().getTableRegionLocation(table.getParentName().getBytes(), rowKey);
        byte[] regionPrefix = region.getRegionInfo().getStartKey().length == 0 ? new byte[region.getRegionInfo().getEndKey().length] : region.getRegionInfo().getStartKey();
        if (regionPrefix.length != 0) {
            ptr.set(ScanRanges.prefixKey(ptr.get(), 0, regionPrefix, regionPrefix.length));
        }
    }
    mutation.put(ptr, new RowMutationState(columnValues, statement.getConnection().getStatementExecutionCounter(), rowTsColInfo, onDupKeyBytes));
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) PLong(org.apache.phoenix.schema.types.PLong) PUnsignedLong(org.apache.phoenix.schema.types.PUnsignedLong) RowTimestampColInfo(org.apache.phoenix.execute.MutationState.RowTimestampColInfo) Hint(org.apache.phoenix.parse.HintNode.Hint) PSmallint(org.apache.phoenix.schema.types.PSmallint) IllegalDataException(org.apache.phoenix.schema.IllegalDataException) RowMutationState(org.apache.phoenix.execute.MutationState.RowMutationState)

Example 4 with IllegalDataException

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

the class DateUtil method parseTimestamp.

public static Timestamp parseTimestamp(String timestampValue) {
    Timestamp timestamp = new Timestamp(parseDateTime(timestampValue));
    int period = timestampValue.indexOf('.');
    if (period > 0) {
        String nanosStr = timestampValue.substring(period + 1);
        if (nanosStr.length() > 9)
            throw new IllegalDataException("nanos > 999999999 or < 0");
        if (nanosStr.length() > 3) {
            int nanos = Integer.parseInt(nanosStr);
            for (int i = 0; i < 9 - nanosStr.length(); i++) {
                nanos *= 10;
            }
            timestamp.setNanos(nanos);
        }
    }
    return timestamp;
}
Also used : PUnsignedTimestamp(org.apache.phoenix.schema.types.PUnsignedTimestamp) Timestamp(java.sql.Timestamp) PTimestamp(org.apache.phoenix.schema.types.PTimestamp) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Example 5 with IllegalDataException

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

the class CSVCommonsLoaderIT method testCSVUpsertWithInvalidNumericalData_StrictMode.

// Ensure that strict mode also causes the import to stop if a data type on a single
// row is not correct
@Test
public void testCSVUpsertWithInvalidNumericalData_StrictMode() throws Exception {
    CSVParser parser = null;
    PhoenixConnection conn = null;
    try {
        String stockTableName = generateUniqueName();
        // Create table
        String statements = "CREATE TABLE IF NOT EXISTS " + stockTableName + "(SYMBOL VARCHAR NOT NULL PRIMARY KEY, COMPANY_ID BIGINT);";
        conn = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class);
        PhoenixRuntime.executeStatements(conn, new StringReader(statements), null);
        // Upsert CSV file in strict mode
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, stockTableName, Arrays.asList("SYMBOL", "COMPANY_ID"), true);
        try {
            csvUtil.upsert(new StringReader(STOCK_CSV_VALUES));
            fail("Running an upsert with data that can't be upserted in strict mode " + "should throw an exception");
        } catch (IllegalDataException e) {
        // Expected
        }
    } finally {
        if (parser != null)
            parser.close();
        if (conn != null)
            conn.close();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) CSVParser(org.apache.commons.csv.CSVParser) StringReader(java.io.StringReader) CSVCommonsLoader(org.apache.phoenix.util.CSVCommonsLoader) IllegalDataException(org.apache.phoenix.schema.IllegalDataException) Test(org.junit.Test)

Aggregations

IllegalDataException (org.apache.phoenix.schema.IllegalDataException)14 Expression (org.apache.phoenix.expression.Expression)5 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)4 PDataType (org.apache.phoenix.schema.types.PDataType)4 SQLException (java.sql.SQLException)2 Iterator (java.util.Iterator)2 List (java.util.List)2 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)2 Mutation (org.apache.hadoop.hbase.client.Mutation)2 Pair (org.apache.hadoop.hbase.util.Pair)2 RowMutationState (org.apache.phoenix.execute.MutationState.RowMutationState)2 RowTimestampColInfo (org.apache.phoenix.execute.MutationState.RowTimestampColInfo)2 Hint (org.apache.phoenix.parse.HintNode.Hint)2 PColumn (org.apache.phoenix.schema.PColumn)2 PName (org.apache.phoenix.schema.PName)2 PTable (org.apache.phoenix.schema.PTable)2 PTableRef (org.apache.phoenix.schema.PTableRef)2 TableRef (org.apache.phoenix.schema.TableRef)2 PLong (org.apache.phoenix.schema.types.PLong)2 PSmallint (org.apache.phoenix.schema.types.PSmallint)2