Search in sources :

Example 6 with SqlColumnType

use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.

the class BetweenOperatorIntegrationTest method checkSuccessOrFailure.

protected void checkSuccessOrFailure(String sql, Tuple2<List<SqlRow>, HazelcastSqlException> expectedOutcome, Object... params) {
    try {
        List<SqlRow> rows = execute(sql, params);
        assertNull(expectedOutcome.f1());
        assertEquals(expectedOutcome.f0().size(), rows.size());
        List<SqlRow> expectedResultsList = expectedOutcome.f0();
        for (int i = 0; i < rows.size(); i++) {
            SqlColumnType expectedType = expectedResultsList.get(i).getMetadata().getColumn(0).getType();
            SqlColumnType actualType = rows.get(i).getMetadata().getColumn(0).getType();
            assertEquals(expectedType, actualType);
            Object actualObject = rows.get(i).getObject(0);
            Object expectedObject = expectedResultsList.get(i).getObject(0);
            assertEquals(expectedObject, actualObject);
        }
    } catch (HazelcastSqlException e) {
        assertNotNull(expectedOutcome.f1());
        // Expected :At line 1, column [5]5: ...
        // Actual   :At line 1, column [6]5: ...
        // To overcome             this ^ we are comparing substrings like
        // "Parameter at position 1 must be of $1 type, but $2 was found (consider adding an explicit CAST)"
        // 
        // Expected :The Jet SQL job failed: Execution on a member failed: com.hazelcast.jet.JetException: Exception in ProcessorTasklet{06bd-fcd0-9e82-0001/Project(IMap[public.map])#1}: com.hazelcast.sql.impl.QueryException: ...
        // Actual   :The Jet SQL job failed: Execution on a member failed: com.hazelcast.jet.JetException: Exception in ProcessorTasklet{06bd-fcd0-9e83-0001/Project(IMap[public.map])#1}: com.hazelcast.sql.impl.QueryException: ...
        // To overcome                                                                                                                           this ^ we are comparing substrings like
        // "Cannot compare two OBJECT values, because left operand has class com.hazelcast.jet.sql.impl.support.expressions.ExpressionType$ObjectHolder type and right operand has class java.lang.String type
        int startIndex = e.getMessage().indexOf("Parameter");
        if (startIndex == -1) {
            startIndex = e.getMessage().indexOf("Cannot compare");
        }
        assertEquals(expectedOutcome.f1().getMessage().substring(startIndex), e.getMessage().substring(startIndex));
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlColumnType(com.hazelcast.sql.SqlColumnType) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException)

Example 7 with SqlColumnType

use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.

the class BetweenOperatorIntegrationTest method checkValues.

protected void checkValues(String sql, SqlColumnType expectedType, Object[] expectedResults, Object... params) {
    List<SqlRow> rows = execute(sql, params);
    assertEquals(expectedResults.length, rows.size());
    if (rows.size() == 0) {
        return;
    }
    if (rows.get(0).getObject(0) instanceof Integer) {
        rows.sort(Comparator.comparingInt(a -> a.getObject(0)));
    } else if (rows.get(0).getObject(0) instanceof Comparable) {
        rows.sort((a, b) -> ((Comparable<?>) a.getObject(0)).compareTo(b.getObject(0)));
    }
    for (int i = 0; i < expectedResults.length; i++) {
        SqlRow row = rows.get(i);
        assertEquals(expectedType, row.getMetadata().getColumn(0).getType());
        assertEquals(expectedResults[i], row.getObject(0));
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) INTEGER(com.hazelcast.sql.SqlColumnType.INTEGER) TIME(com.hazelcast.sql.SqlColumnType.TIME) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) LocalDateTime(java.time.LocalDateTime) DOUBLE(com.hazelcast.sql.SqlColumnType.DOUBLE) ExpressionBiValue(com.hazelcast.jet.sql.impl.support.expressions.ExpressionBiValue) HazelcastSerialClassRunner(com.hazelcast.test.HazelcastSerialClassRunner) ArrayList(java.util.ArrayList) ExpressionTypes(com.hazelcast.jet.sql.impl.support.expressions.ExpressionTypes) LocalTime(java.time.LocalTime) VARCHAR(com.hazelcast.sql.SqlColumnType.VARCHAR) Tuple2(com.hazelcast.jet.datamodel.Tuple2) SqlRow(com.hazelcast.sql.SqlRow) SqlColumnType(com.hazelcast.sql.SqlColumnType) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) OBJECT(com.hazelcast.sql.SqlColumnType.OBJECT) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) BIGINT(com.hazelcast.sql.SqlColumnType.BIGINT) Category(org.junit.experimental.categories.Category) Serializable(java.io.Serializable) ExpressionTestSupport(com.hazelcast.jet.sql.impl.expression.ExpressionTestSupport) List(java.util.List) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) LocalDate(java.time.LocalDate) SqlResult(com.hazelcast.sql.SqlResult) ExpressionType(com.hazelcast.jet.sql.impl.support.expressions.ExpressionType) Comparator(java.util.Comparator) DATE(com.hazelcast.sql.SqlColumnType.DATE) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals)

Example 8 with SqlColumnType

use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.

the class ComparisonPredicateIntegrationTest method checkNumeric.

private void checkNumeric(Object value1, Object value2) {
    int res = compareNumeric(value1, value2);
    if (res == RES_EQ && (value1 instanceof Float || value1 instanceof Double || value2 instanceof Float || value2 instanceof Double)) {
        return;
    }
    ExpressionType<?> valueType1 = ExpressionTypes.resolve(value1);
    ExpressionType<?> valueType2 = ExpressionTypes.resolve(value2);
    Class<? extends ExpressionValue> class1 = ExpressionValue.createClass(valueType1);
    Class<? extends ExpressionValue> class2 = ExpressionValue.createClass(valueType2);
    Class<? extends ExpressionBiValue> biClass = ExpressionBiValue.createBiClass(valueType1, valueType2);
    String literal1 = value1.toString();
    String literal2 = value2.toString();
    QueryDataType type1 = QueryDataTypeUtils.resolveTypeForClass(value1.getClass());
    QueryDataType type2 = QueryDataTypeUtils.resolveTypeForClass(value2.getClass());
    SqlColumnType publicType1 = type1.getTypeFamily().getPublicType();
    SqlColumnType publicType2 = type2.getTypeFamily().getPublicType();
    int precedence1 = type1.getTypeFamily().getPrecedence();
    int precedence2 = type2.getTypeFamily().getPrecedence();
    // Column/column
    putCheckCommute(ExpressionBiValue.createBiValue(biClass, value1, value2), "field1", "field2", res);
    putCheckCommute(ExpressionBiValue.createBiValue(biClass, value1, null), "field1", "field2", RES_NULL);
    putCheckCommute(ExpressionBiValue.createBiValue(biClass, null, value2), "field1", "field2", RES_NULL);
    // Column/literal
    putCheckCommute(ExpressionValue.create(class1, value1), "field1", literal2, res);
    putCheckCommute(ExpressionValue.create(class2, value2), literal1, "field1", res);
    // Column/parameter
    if (precedence1 >= precedence2) {
        putCheckCommute(ExpressionValue.create(class1, value1), "field1", "?", res, value2);
    } else if (precedence1 >= QueryDataType.BIGINT.getTypeFamily().getPrecedence()) {
        putAndCheckFailure(ExpressionValue.create(class1, value1), sql(mode.token(), "field1", "?"), SqlErrorCode.DATA_EXCEPTION, parameterError(0, publicType1, publicType2), value2);
    }
    if (precedence2 >= precedence1) {
        putCheckCommute(ExpressionValue.create(class2, value2), "?", "field1", res, value1);
    } else if (precedence2 >= QueryDataType.BIGINT.getTypeFamily().getPrecedence()) {
        putAndCheckFailure(ExpressionValue.create(class2, value2), sql(mode.token(), "?", "field1"), SqlErrorCode.DATA_EXCEPTION, parameterError(0, publicType2, publicType1), value1);
    }
    // Literal/literal
    checkCommute(literal1, literal2, res);
}
Also used : QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) SqlColumnType(com.hazelcast.sql.SqlColumnType)

Example 9 with SqlColumnType

use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.

the class ComparisonPredicateIntegrationTest method testNumeric.

@Test(timeout = 600_000)
public void testNumeric() {
    Object[] values = getNumericValues();
    for (int i = 0; i < values.length; i++) {
        for (int j = i; j < values.length; j++) {
            checkNumeric(values[i], values[j]);
        }
    }
    for (ExpressionType type : ExpressionTypes.numeric()) {
        // Column/column
        checkUnsupportedColumnColumn(type, ExpressionTypes.allExcept(ExpressionTypes.numeric()));
        // Column/literal
        SqlColumnType columnType = type.getFieldConverterType().getTypeFamily().getPublicType();
        checkUnsupportedColumnLiteral(type.valueFrom(), columnType, LITERAL_VARCHAR, LITERAL_BOOLEAN);
        // Column/parameter
        if (type.getFieldConverterType().getTypeFamily().getPrecedence() >= QueryDataType.BIGINT.getTypeFamily().getPrecedence()) {
            checkUnsupportedColumnParameter(type.valueFrom(), columnType, 0, ExpressionTypes.allExcept(ExpressionTypes.numeric()));
        }
    }
}
Also used : ExpressionType(com.hazelcast.jet.sql.impl.support.expressions.ExpressionType) SqlColumnType(com.hazelcast.sql.SqlColumnType) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 10 with SqlColumnType

use of com.hazelcast.sql.SqlColumnType in project hazelcast by hazelcast.

the class SqlConsole method determineAlignments.

private static Alignment[] determineAlignments(SqlRowMetadata metadata) {
    int colCount = metadata.getColumnCount();
    Alignment[] alignments = new Alignment[colCount];
    for (int i = 0; i < colCount; i++) {
        SqlColumnMetadata colMetadata = metadata.getColumn(i);
        SqlColumnType type = colMetadata.getType();
        switch(type) {
            case BIGINT:
            case DECIMAL:
            case DOUBLE:
            case INTEGER:
            case REAL:
            case SMALLINT:
            case TINYINT:
                alignments[i] = Alignment.RIGHT;
                break;
            case BOOLEAN:
            case DATE:
            case NULL:
            case OBJECT:
            case TIMESTAMP:
            case VARCHAR:
            case TIMESTAMP_WITH_TIME_ZONE:
            default:
                alignments[i] = Alignment.LEFT;
        }
    }
    return alignments;
}
Also used : SqlColumnMetadata(com.hazelcast.sql.SqlColumnMetadata) SqlColumnType(com.hazelcast.sql.SqlColumnType)

Aggregations

SqlColumnType (com.hazelcast.sql.SqlColumnType)12 SqlColumnMetadata (com.hazelcast.sql.SqlColumnMetadata)4 ArrayList (java.util.ArrayList)4 HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)3 SqlRow (com.hazelcast.sql.SqlRow)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 Test (org.junit.Test)3 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)2 ExpressionType (com.hazelcast.jet.sql.impl.support.expressions.ExpressionType)2 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2 LocalTime (java.time.LocalTime)2 List (java.util.List)2 HazelcastJsonValueCodec (com.hazelcast.client.impl.protocol.codec.custom.HazelcastJsonValueCodec)1 HazelcastJsonValue (com.hazelcast.core.HazelcastJsonValue)1 Data (com.hazelcast.internal.serialization.Data)1 Tuple2 (com.hazelcast.jet.datamodel.Tuple2)1 Tuple2.tuple2 (com.hazelcast.jet.datamodel.Tuple2.tuple2)1 ExpressionTestSupport (com.hazelcast.jet.sql.impl.expression.ExpressionTestSupport)1