use of org.voltdb.types.GeographyPointValue in project voltdb by VoltDB.
the class TestGeographyPointValue method testInsertDefaultNull.
public void testInsertDefaultNull() throws IOException, ProcCallException {
Client client = getClient();
validateTableOfScalarLongs(client, "insert into t (pk) values (1);", new long[] { 1 });
VoltTable vt = client.callProcedure("@AdHoc", "select pk, pt from t;").getResults()[0];
String actual = vt.toString();
String expectedPart = "NULL";
assertTrue(actual + " does not contain " + expectedPart, actual.contains(expectedPart));
assertTrue(vt.advanceRow());
long id = vt.getLong(0);
assertEquals(1, id);
GeographyPointValue ptByIndex = vt.getGeographyPointValue(1);
assertTrue(vt.wasNull());
assertNull(ptByIndex);
GeographyPointValue ptByColumnName = vt.getGeographyPointValue("pt");
assertTrue(vt.wasNull());
assertNull(ptByColumnName);
assertFalse(vt.advanceRow());
vt = client.callProcedure("@AdHoc", "select pt from t where pt is null;").getResults()[0];
assertTrue(vt.advanceRow());
ptByIndex = vt.getGeographyPointValue(0);
assert (vt.wasNull());
}
use of org.voltdb.types.GeographyPointValue in project voltdb by VoltDB.
the class TestGeographyPointValue method testPointUpdate.
public void testPointUpdate() throws Exception {
Client client = getClient();
fillTable(client, 0);
final GeographyPointValue CAMBRIDGE_PT = new GeographyPointValue(-71.1106, 42.3736);
final GeographyPointValue SAN_JOSE_PT = new GeographyPointValue(-121.8906, 37.3362);
validateTableOfScalarLongs(client, "update t set " + "name = 'Cambridge', " + "pt = pointfromtext('" + CAMBRIDGE_PT + "') " + "where pk = 0", new long[] { 1 });
validateTableOfScalarLongs(client, "update t set " + "name = 'San Jose', " + "pt = pointfromtext('" + SAN_JOSE_PT + "') " + "where pk = 1", new long[] { 1 });
VoltTable vt = client.callProcedure("@AdHoc", "select pk, name, pt from t order by pk").getResults()[0];
assertApproximateContentOfTable(new Object[][] { { 0, "Cambridge", CAMBRIDGE_PT }, { 1, "San Jose", SAN_JOSE_PT }, { 2, "Atlantis", null } }, vt, EPSILON);
}
use of org.voltdb.types.GeographyPointValue in project voltdb by VoltDB.
the class AdBrokerBenchmark method requestAd.
/**
* Invoke the stored procedure GetHighestBidForLocation, which, given a random
* point, returns the id of the bid that has the highest dollar amount.
*/
private void requestAd() {
long deviceId = Math.abs(m_rand.nextLong()) % AdBrokerBenchmark.NUM_DEVICES;
GeographyPointValue point = getRandomPoint();
try {
m_client.callProcedure(new NullCallback(), "GetHighestBidForLocation", deviceId, point);
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.voltdb.types.GeographyPointValue in project voltdb by VoltDB.
the class ParameterConverter method tryToMakeCompatible.
/**
* Convert the given value to the type given, if possible.
*
* This function is in the performance path, so some effort has been made to order
* the giant string of branches such that most likely things are first, and that
* if the type is already correct, it should move very quickly through the logic.
* Some clarity has been sacrificed for performance, but perfect clarity is pretty
* elusive with complicated logic like this anyway.
*
* @throws Exception with a message describing why the types are incompatible.
*/
public static Object tryToMakeCompatible(final Class<?> expectedClz, final Object param) throws VoltTypeException {
// after the basics to check for those.
if (param == null) {
return nullValueForType(expectedClz);
}
Class<?> inputClz = param.getClass();
// If we make it through this first block, memoize a number value for some range checks later
Number numberParam = null;
if (inputClz == Long.class) {
if (expectedClz == long.class)
return param;
if ((Long) param == VoltType.NULL_BIGINT)
return nullValueForType(expectedClz);
numberParam = (Number) param;
} else if (inputClz == Integer.class) {
if (expectedClz == int.class)
return param;
if ((Integer) param == VoltType.NULL_INTEGER)
return nullValueForType(expectedClz);
if (expectedClz == long.class)
return ((Integer) param).longValue();
numberParam = (Number) param;
} else if (inputClz == Short.class) {
if (expectedClz == short.class)
return param;
if ((Short) param == VoltType.NULL_SMALLINT)
return nullValueForType(expectedClz);
if (expectedClz == long.class)
return ((Short) param).longValue();
if (expectedClz == int.class)
return ((Short) param).intValue();
numberParam = (Number) param;
} else if (inputClz == Byte.class) {
if (expectedClz == byte.class)
return param;
if ((Byte) param == VoltType.NULL_TINYINT)
return nullValueForType(expectedClz);
if (expectedClz == long.class)
return ((Byte) param).longValue();
if (expectedClz == int.class)
return ((Byte) param).intValue();
if (expectedClz == short.class)
return ((Byte) param).shortValue();
numberParam = (Number) param;
} else if (inputClz == Double.class) {
if (expectedClz == double.class)
return param;
if ((Double) param == VoltType.NULL_FLOAT)
return nullValueForType(expectedClz);
} else if (inputClz == String.class) {
String stringParam = (String) param;
if (stringParam.equals(Constants.CSV_NULL))
return nullValueForType(expectedClz);
else if (expectedClz == String.class)
return param;
else // Hack allows hex-encoded strings to be passed into byte[] params
if (expectedClz == byte[].class) {
// unless the param really looks like an x-quoted literal
if (stringParam.startsWith("X") || stringParam.startsWith("x")) {
String hexDigits = SQLParser.getDigitsFromHexLiteral(stringParam);
if (hexDigits != null) {
stringParam = hexDigits;
}
}
return Encoder.hexDecode(stringParam);
}
// This code handles primitive types. Complex types come later.
if (expectedClz.isPrimitive()) {
return convertStringToPrimitive(stringParam, expectedClz);
}
} else if (inputClz == byte[].class) {
if (expectedClz == byte[].class)
return param;
else // allow byte arrays to be passed into string parameters
if (expectedClz == String.class) {
String value = new String((byte[]) param, Constants.UTF8ENCODING);
if (value.equals(Constants.CSV_NULL))
return nullValueForType(expectedClz);
else
return value;
}
} else // why do we have three sigils for three types??)
if (param == VoltType.NULL_TIMESTAMP || param == VoltType.NULL_STRING_OR_VARBINARY || param == VoltType.NULL_GEOGRAPHY || param == VoltType.NULL_POINT || param == VoltType.NULL_DECIMAL) {
return nullValueForType(expectedClz);
} else // If it cannot be converted (say out of range), just display the error message.
if (inputClz == BigDecimal.class) {
// Only conversion to primitive numeric types are considered
BigDecimal pBigDecimal = (BigDecimal) param;
if (expectedClz == long.class) {
try {
long result = pBigDecimal.longValueExact();
return result;
}// The error will be re-thrown below
catch (ArithmeticException e) {
}
} else if (expectedClz == double.class) {
// This conversion could potentially lose information, should a warning be
// given at a higher level ?
double result = pBigDecimal.doubleValue();
// The converted double could be infinity if out of range
if (result != Double.POSITIVE_INFINITY && result != Double.NEGATIVE_INFINITY) {
return result;
}
} else if (expectedClz == int.class) {
try {
int result = pBigDecimal.intValueExact();
return result;
}// The error will be re-thrown below
catch (ArithmeticException e) {
}
} else if (expectedClz == short.class) {
try {
short result = pBigDecimal.shortValueExact();
return result;
}// The error will be re-thrown below
catch (ArithmeticException e) {
}
} else if (expectedClz == byte.class) {
try {
byte result = pBigDecimal.byteValueExact();
return result;
} catch (ArithmeticException e) {
}
}
throw new VoltTypeException("tryToMakeCompatible: The provided value: (" + param.toString() + ") of type: " + inputClz.getName() + " is out of range for the target parameter type: " + expectedClz.getName());
}
// make sure we get the array/scalar match
if (expectedClz.isArray() != inputClz.isArray()) {
throw new VoltTypeException(String.format("Array / Scalar parameter mismatch (%s to %s)", inputClz.getName(), expectedClz.getName()));
}
// handle arrays in a factored-out method
if (expectedClz.isArray()) {
return tryToMakeCompatibleArray(expectedClz.getComponentType(), inputClz.getComponentType(), param);
}
if ((expectedClz == int.class) && (numberParam != null)) {
long val = numberParam.longValue();
if (val == VoltType.NULL_INTEGER) {
throw new VoltTypeException("tryToMakeCompatible: The provided long value: (" + param.toString() + ") might be interpreted as integer null. " + "Try explicitly using a int parameter.");
}
// if it's in the right range, crop the value and return
if ((val <= Integer.MAX_VALUE) && (val >= Integer.MIN_VALUE))
return numberParam.intValue();
} else if ((expectedClz == short.class) && (numberParam != null)) {
if ((inputClz == Long.class) || (inputClz == Integer.class)) {
long val = numberParam.longValue();
if (val == VoltType.NULL_SMALLINT) {
throw new VoltTypeException("tryToMakeCompatible: The provided int or long value: (" + param.toString() + ") might be interpreted as smallint null. " + "Try explicitly using a short parameter.");
}
// if it's in the right range, crop the value and return
if ((val <= Short.MAX_VALUE) && (val >= Short.MIN_VALUE))
return numberParam.shortValue();
}
} else if ((expectedClz == byte.class) && (numberParam != null)) {
if ((inputClz == Long.class) || (inputClz == Integer.class) || (inputClz == Short.class)) {
long val = numberParam.longValue();
if (val == VoltType.NULL_TINYINT) {
throw new VoltTypeException("tryToMakeCompatible: The provided short, int or long value: (" + param.toString() + ") might be interpreted as tinyint null. " + "Try explicitly using a byte parameter.");
}
// if it's in the right range, crop the value and return
if ((val <= Byte.MAX_VALUE) && (val >= Byte.MIN_VALUE))
return numberParam.byteValue();
}
} else if ((expectedClz == double.class) && (numberParam != null)) {
return numberParam.doubleValue();
} else if (expectedClz == TimestampType.class) {
// null values safe
if (inputClz == Integer.class)
return new TimestampType((Integer) param);
// null values safe
if (inputClz == Long.class)
return new TimestampType((Long) param);
if (inputClz == TimestampType.class)
return param;
if (inputClz == Date.class)
return new TimestampType((Date) param);
// if a string is given for a date, use java's JDBC parsing
if (inputClz == String.class) {
String timestring = ((String) param).trim();
try {
return new TimestampType(Long.parseLong(timestring));
} catch (IllegalArgumentException e) {
// Defer errors to the generic Exception throw below, if it's not the right format
}
try {
return SQLParser.parseDate(timestring);
} catch (IllegalArgumentException e) {
// Defer errors to the generic Exception throw below, if it's not the right format
}
}
} else if (expectedClz == java.sql.Timestamp.class) {
if (param instanceof java.sql.Timestamp)
return param;
if (param instanceof java.util.Date)
return new java.sql.Timestamp(((java.util.Date) param).getTime());
if (param instanceof TimestampType)
return ((TimestampType) param).asJavaTimestamp();
// If a string is given for a date, use java's JDBC parsing.
if (inputClz == String.class) {
String longtime = ((String) param).trim();
try {
return new java.sql.Timestamp(Long.parseLong(longtime));
} catch (IllegalArgumentException e) {
// Defer errors to the generic Exception throw below, if it's not the right format
}
try {
return java.sql.Timestamp.valueOf(longtime);
} catch (IllegalArgumentException e) {
// Defer errors to the generic Exception throw below, if it's not the right format
}
}
} else if (expectedClz == java.sql.Date.class) {
// covers java.sql.Date and java.sql.Timestamp
if (param instanceof java.sql.Date)
return param;
if (param instanceof java.util.Date)
return new java.sql.Date(((java.util.Date) param).getTime());
if (param instanceof TimestampType)
return ((TimestampType) param).asExactJavaSqlDate();
// If a string is given for a date, use java's JDBC parsing.
if (inputClz == String.class) {
try {
return new java.sql.Date(TimestampType.millisFromJDBCformat((String) param));
} catch (IllegalArgumentException e) {
// Defer errors to the generic Exception throw below, if it's not the right format
}
}
} else if (expectedClz == java.util.Date.class) {
// covers java.sql.Date and java.sql.Timestamp
if (param instanceof java.util.Date)
return param;
if (param instanceof TimestampType)
return ((TimestampType) param).asExactJavaDate();
// If a string is given for a date, use the default format parser for the default locale.
if (inputClz == String.class) {
try {
return new java.util.Date(TimestampType.millisFromJDBCformat((String) param));
} catch (IllegalArgumentException e) {
// Defer errors to the generic Exception throw below, if it's not the right format
}
}
} else if (expectedClz == BigDecimal.class) {
if (numberParam != null) {
BigDecimal bd = VoltDecimalHelper.stringToDecimal(param.toString());
return bd;
}
if (inputClz == BigDecimal.class) {
BigDecimal bd = (BigDecimal) param;
bd = VoltDecimalHelper.setDefaultScale(bd);
return bd;
}
if (inputClz == Float.class || inputClz == Double.class) {
try {
return VoltDecimalHelper.deserializeBigDecimalFromString(String.format("%.12f", param));
} catch (IOException ex) {
throw new VoltTypeException(String.format("deserialize Float from string failed. (%s to %s)", inputClz.getName(), expectedClz.getName()));
}
}
try {
return VoltDecimalHelper.deserializeBigDecimalFromString(String.valueOf(param));
} catch (IOException ex) {
throw new VoltTypeException(String.format("deserialize BigDecimal from string failed. (%s to %s)", inputClz.getName(), expectedClz.getName()));
}
} else if (expectedClz == GeographyPointValue.class) {
// Is it a point already? If so, just return it.
if (inputClz == GeographyPointValue.class) {
return param;
}
// If so, return the newly constructed point.
if (inputClz == String.class) {
try {
GeographyPointValue pt = GeographyPointValue.fromWKT((String) param);
return pt;
} catch (IllegalArgumentException e) {
throw new VoltTypeException(String.format("deserialize GeographyPointValue from string failed (string %s)", (String) param));
}
}
} else if (expectedClz == GeographyValue.class) {
if (inputClz == GeographyValue.class) {
return param;
}
if (inputClz == String.class) {
String paramStr = (String) param;
try {
GeographyValue gv = GeographyValue.fromWKT(paramStr);
return gv;
} catch (IllegalArgumentException e) {
throw new VoltTypeException(String.format("deserialize GeographyValue from string failed (string %s)", paramStr));
}
}
} else if (expectedClz == VoltTable.class && inputClz == VoltTable.class) {
return param;
} else if (expectedClz == String.class) {
//For VARCHAR columns if not null or not an array send toString value.
if (!param.getClass().isArray()) {
return String.valueOf(param);
}
} else // they do their own validation
if (expectedClz == ParameterSet.class && inputClz == ParameterSet.class) {
return param;
}
// these are used by system procedures and are ignored here
if (expectedClz.getSimpleName().equals("SystemProcedureExecutionContext")) {
if (expectedClz.isAssignableFrom(inputClz)) {
return param;
}
}
throw new VoltTypeException("tryToMakeCompatible: The provided value: (" + param.toString() + ") of type: " + inputClz.getName() + " is not a match or is out of range for the target parameter type: " + expectedClz.getName());
}
use of org.voltdb.types.GeographyPointValue in project voltdb by VoltDB.
the class ParameterSet method flattenToBuffer.
public void flattenToBuffer(ByteBuffer buf) throws IOException {
buf.putShort((short) m_params.length);
for (int i = 0; i < m_params.length; i++) {
Object obj = m_params[i];
if ((obj == null) || (obj == JSONObject.NULL)) {
VoltType type = VoltType.NULL;
buf.put(type.getValue());
continue;
}
Class<?> cls = obj.getClass();
if (cls.isArray()) {
// special case them as the VARBINARY type.
if (obj instanceof byte[]) {
final byte[] b = (byte[]) obj;
// commented out this bit... presumably the EE will do this check upon recipt
/*if (b.length > VoltType.MAX_VALUE_LENGTH) {
throw new IOException("Value of byte[] larger than allowed max string or varbinary " + VoltType.MAX_VALUE_LENGTH_STR);
}*/
buf.put(VoltType.VARBINARY.getValue());
buf.putInt(b.length);
buf.put(b);
continue;
}
//Same as before, but deal with the fact it is coming in as a unmanaged bytebuffer
if (obj instanceof BBContainer) {
final BBContainer cont = (BBContainer) obj;
final ByteBuffer paramBuf = cont.b();
buf.put(VoltType.VARBINARY.getValue());
buf.putInt(paramBuf.remaining());
buf.put(paramBuf);
continue;
}
buf.put(ARRAY);
VoltType type;
try {
type = VoltType.typeFromClass(cls.getComponentType());
} catch (VoltTypeException e) {
obj = getAKosherArray((Object[]) obj);
cls = obj.getClass();
type = VoltType.typeFromClass(cls.getComponentType());
}
buf.put(type.getValue());
switch(type) {
case SMALLINT:
SerializationHelper.writeArray((short[]) obj, buf);
break;
case INTEGER:
SerializationHelper.writeArray((int[]) obj, buf);
break;
case BIGINT:
SerializationHelper.writeArray((long[]) obj, buf);
break;
case FLOAT:
SerializationHelper.writeArray((double[]) obj, buf);
break;
case STRING:
if (m_encodedStringArrays[i] == null) {
// should not happen
throw new IOException("String array not encoded");
}
// This check used to be done by FastSerializer.writeArray(), but things changed?
if (m_encodedStringArrays[i].length > Short.MAX_VALUE) {
throw new IOException("Array exceeds maximum length of " + Short.MAX_VALUE + " bytes");
}
buf.putShort((short) m_encodedStringArrays[i].length);
for (int zz = 0; zz < m_encodedStringArrays[i].length; zz++) {
SerializationHelper.writeVarbinary(m_encodedStringArrays[i][zz], buf);
}
break;
case TIMESTAMP:
SerializationHelper.writeArray((TimestampType[]) obj, buf);
break;
case DECIMAL:
// converted long128 in serializer api
SerializationHelper.writeArray((BigDecimal[]) obj, buf);
break;
case VOLTTABLE:
SerializationHelper.writeArray((VoltTable[]) obj, buf);
break;
case VARBINARY:
SerializationHelper.writeArray((byte[][]) obj, buf);
break;
case GEOGRAPHY_POINT:
SerializationHelper.writeArray((GeographyPointValue[]) obj, buf);
break;
case GEOGRAPHY:
SerializationHelper.writeArray((GeographyValue[]) obj, buf);
break;
default:
throw new RuntimeException("FIXME: Unsupported type " + type);
}
continue;
}
// Handle NULL mappings not encoded by type.min_value convention
if (obj == VoltType.NULL_TIMESTAMP) {
buf.put(VoltType.TIMESTAMP.getValue());
// corresponds to EE value.h isNull()
buf.putLong(VoltType.NULL_BIGINT);
continue;
} else if (obj == VoltType.NULL_STRING_OR_VARBINARY) {
buf.put(VoltType.STRING.getValue());
buf.putInt(VoltType.NULL_STRING_LENGTH);
continue;
} else if (obj == VoltType.NULL_DECIMAL) {
buf.put(VoltType.DECIMAL.getValue());
VoltDecimalHelper.serializeNull(buf);
continue;
} else if (obj == VoltType.NULL_POINT) {
buf.put(VoltType.GEOGRAPHY_POINT.getValue());
GeographyPointValue.serializeNull(buf);
continue;
} else if (obj == VoltType.NULL_GEOGRAPHY) {
buf.put(VoltType.GEOGRAPHY.getValue());
buf.putInt(VoltType.NULL_STRING_LENGTH);
continue;
} else if (obj instanceof BBContainer) {
final BBContainer cont = (BBContainer) obj;
final ByteBuffer paramBuf = cont.b();
buf.put(VoltType.VARBINARY.getValue());
buf.putInt(paramBuf.remaining());
buf.put(paramBuf);
continue;
}
VoltType type = VoltType.typeFromClass(cls);
buf.put(type.getValue());
switch(type) {
case TINYINT:
buf.put((Byte) obj);
break;
case SMALLINT:
buf.putShort((Short) obj);
break;
case INTEGER:
buf.putInt((Integer) obj);
break;
case BIGINT:
buf.putLong((Long) obj);
break;
case FLOAT:
if (cls == Float.class)
buf.putDouble(((Float) obj).doubleValue());
else if (cls == Double.class)
buf.putDouble(((Double) obj).doubleValue());
else
throw new RuntimeException("Can't cast parameter type to Double");
break;
case STRING:
if (m_encodedStrings[i] == null) {
// should not happen
throw new IOException("String not encoded: " + (String) obj);
}
SerializationHelper.writeVarbinary(m_encodedStrings[i], buf);
break;
case TIMESTAMP:
long micros = timestampToMicroseconds(obj);
buf.putLong(micros);
break;
case DECIMAL:
VoltDecimalHelper.serializeBigDecimal((BigDecimal) obj, buf);
break;
case VOLTTABLE:
((VoltTable) obj).flattenToBuffer(buf);
break;
case GEOGRAPHY_POINT:
((GeographyPointValue) obj).flattenToBuffer(buf);
break;
case GEOGRAPHY:
GeographyValue gv = (GeographyValue) obj;
buf.putInt(gv.getLengthInBytes());
gv.flattenToBuffer(buf);
break;
default:
throw new RuntimeException("FIXME: Unsupported type " + type);
}
}
}
Aggregations