Search in sources :

Example 6 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 7 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)

Example 8 with IllegalDataException

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

the class SqlTypeNameFunction 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[] sqlTypeNameBytes = PDataType.fromTypeId(sqlType).getSqlTypeNameBytes();
        ptr.set(sqlTypeNameBytes);
    } 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 9 with IllegalDataException

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

the class ScanUtil method getTenantIdBytes.

public static byte[] getTenantIdBytes(RowKeySchema schema, boolean isSalted, PName tenantId, boolean isSharedIndex) throws SQLException {
    int pkPos = (isSalted ? 1 : 0) + (isSharedIndex ? 1 : 0);
    Field field = schema.getField(pkPos);
    PDataType dataType = field.getDataType();
    byte[] convertedValue;
    try {
        Object value = dataType.toObject(tenantId.getString());
        convertedValue = dataType.toBytes(value);
        ImmutableBytesWritable ptr = new ImmutableBytesWritable(convertedValue);
        dataType.pad(ptr, field.getMaxLength(), field.getSortOrder());
        convertedValue = ByteUtil.copyKeyBytesIfNecessary(ptr);
    } catch (IllegalDataException ex) {
        throw new SQLExceptionInfo.Builder(SQLExceptionCode.TENANTID_IS_OF_WRONG_TYPE).build().buildException();
    }
    return convertedValue;
}
Also used : Field(org.apache.phoenix.schema.ValueSchema.Field) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Example 10 with IllegalDataException

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

the class DecodeFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression expression = getExpression();
    if (!expression.evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        // expression was evaluated, but evaluated to null
        return true;
    }
    PDataType type = expression.getDataType();
    String stringToDecode = (String) type.toObject(ptr);
    Expression encodingExpression = getEncodingExpression();
    if (!encodingExpression.evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        throw new IllegalDataException(new SQLExceptionInfo.Builder(SQLExceptionCode.ILLEGAL_DATA).setMessage("Missing bytes encoding").build().buildException());
    }
    type = encodingExpression.getDataType();
    String encoding = ((String) type.toObject(ptr)).toUpperCase();
    byte[] out;
    EncodeFormat format = EncodeFormat.valueOf(encoding);
    switch(format) {
        case HEX:
            out = decodeHex(stringToDecode);
            break;
        default:
            throw new IllegalDataException("Unsupported encoding \"" + encoding + "\"");
    }
    ptr.set(out);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Aggregations

IllegalDataException (org.apache.phoenix.schema.IllegalDataException)12 Expression (org.apache.phoenix.expression.Expression)5 PDataType (org.apache.phoenix.schema.types.PDataType)4 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)3 StringReader (java.io.StringReader)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 Iterator (java.util.Iterator)1 List (java.util.List)1 CSVParser (org.apache.commons.csv.CSVParser)1 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)1 Mutation (org.apache.hadoop.hbase.client.Mutation)1 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)1 Pair (org.apache.hadoop.hbase.util.Pair)1 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)1 RowMutationState (org.apache.phoenix.execute.MutationState.RowMutationState)1 RowTimestampColInfo (org.apache.phoenix.execute.MutationState.RowTimestampColInfo)1 ProjectedValueTuple (org.apache.phoenix.execute.TupleProjector.ProjectedValueTuple)1 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)1 Hint (org.apache.phoenix.parse.HintNode.Hint)1