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;
}
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();
}
}
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;
}
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;
}
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;
}
Aggregations