Search in sources :

Example 6 with QueryException

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);
}
Also used : QueryException(com.hazelcast.sql.impl.QueryException) RelDataType(org.apache.calcite.rel.type.RelDataType)

Example 7 with QueryException

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());
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) QueryException(com.hazelcast.sql.impl.QueryException) LazyTarget(com.hazelcast.sql.impl.LazyTarget) Data(com.hazelcast.internal.serialization.Data)

Aggregations

QueryException (com.hazelcast.sql.impl.QueryException)7 HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)2 AbstractSqlResult (com.hazelcast.sql.impl.AbstractSqlResult)2 ArrayList (java.util.ArrayList)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 Data (com.hazelcast.internal.serialization.Data)1 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)1 HazelcastJsonValueFunction (com.hazelcast.jet.sql.impl.validate.operators.json.HazelcastJsonValueFunction)1 SqlColumnMetadata (com.hazelcast.sql.SqlColumnMetadata)1 SqlColumnType (com.hazelcast.sql.SqlColumnType)1 SqlRow (com.hazelcast.sql.SqlRow)1 LazyTarget (com.hazelcast.sql.impl.LazyTarget)1 QueryId (com.hazelcast.sql.impl.QueryId)1 SqlPage (com.hazelcast.sql.impl.client.SqlPage)1 IndexFilterValue (com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue)1 ExpressionEvalContext (com.hazelcast.sql.impl.expression.ExpressionEvalContext)1 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Test (org.junit.Test)1