use of com.hazelcast.sql.impl.QueryException in project hazelcast by hazelcast.
the class NumericLiteral method tryReduceInteger.
private static Literal tryReduceInteger(BigDecimal value) {
// Dealing with integer type family
long longValue;
try {
longValue = BigDecimalConverter.INSTANCE.asBigint(value);
} catch (QueryException e) {
assert e.getMessage().contains("Numeric overflow");
return null;
}
int bitWidth = HazelcastIntegerType.bitWidthOf(longValue);
RelDataType type = HazelcastIntegerType.create(bitWidth, false);
Object adjustedValue;
switch(type.getSqlTypeName()) {
case TINYINT:
adjustedValue = (byte) longValue;
break;
case SMALLINT:
adjustedValue = (short) longValue;
break;
case INTEGER:
adjustedValue = (int) longValue;
break;
default:
assert type.getSqlTypeName() == SqlTypeName.BIGINT;
adjustedValue = longValue;
}
return new NumericLiteral(adjustedValue, type.getSqlTypeName(), Mode.INTEGER, bitWidth);
}
use of com.hazelcast.sql.impl.QueryException in project hazelcast by hazelcast.
the class GenericQueryTargetTest method checkTarget.
private void checkTarget(GenericQueryTarget target, TestObject originalObject, Object object) {
// Set target.
if (object instanceof Data) {
target.setTarget(null, (Data) object);
} else {
target.setTarget(object, null);
}
// Good top-level extractor.
QueryExtractor targetExtractor = target.createExtractor(null, QueryDataType.OBJECT);
LazyTarget lazyTarget = (LazyTarget) targetExtractor.get();
TestObject extractedObject = (TestObject) (lazyTarget.deserialize(new DefaultSerializationServiceBuilder().build()));
assertEquals(originalObject.getField(), extractedObject.getField());
assertEquals(originalObject.getField2(), extractedObject.getField2());
// Bad top-level extractor.
QueryExtractor badTargetExtractor = target.createExtractor(null, QueryDataType.INT);
QueryException error = assertThrows(QueryException.class, badTargetExtractor::get);
assertEquals(SqlErrorCode.DATA_EXCEPTION, error.getCode());
assertTrue(error.getMessage().startsWith("Failed to extract map entry " + (target.isKey() ? "key" : "value")));
// Good field extractor.
QueryExtractor fieldExtractor = target.createExtractor("field", QueryDataType.OBJECT);
int extractedField = (Integer) fieldExtractor.get();
assertEquals(originalObject.getField(), extractedField);
// Bad field extractor (type).
QueryExtractor badFieldTypeExtractor = target.createExtractor("field", QueryDataType.BIGINT);
error = assertThrows(QueryException.class, badFieldTypeExtractor::get);
assertEquals(SqlErrorCode.DATA_EXCEPTION, error.getCode());
assertTrue(error.getMessage().startsWith("Failed to extract map entry " + (target.isKey() ? "key" : "value") + " field"));
// Bad field extractor (name).
QueryExtractor badFieldNameExtractor = target.createExtractor("badField", QueryDataType.INT);
assertNull(badFieldNameExtractor.get());
}
Aggregations