Search in sources :

Example 11 with HazelcastSqlException

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

the class SqlErrorTest method testMemberLeaveDuringQueryAfterImmediateShutdown.

@Test
public void testMemberLeaveDuringQueryAfterImmediateShutdown() {
    // Start two instances
    instance1 = newHazelcastInstance(false);
    instance2 = newHazelcastInstance(true);
    SqlStatement streamingQuery = new SqlStatement("SELECT * FROM TABLE(GENERATE_STREAM(1000))");
    // Start query with immediate shutdown afterwards
    HazelcastSqlException error = assertSqlExceptionWithShutdown(instance1, streamingQuery);
    assertInstanceOf(HazelcastInstanceNotActiveException.class, findRootCause(error));
}
Also used : SqlStatement(com.hazelcast.sql.SqlStatement) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with HazelcastSqlException

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

the class SqlErrorTest method testExecuteOnLiteMember.

@Test
public void testExecuteOnLiteMember() {
    // Start one normal member and one local member.
    newHazelcastInstance(true);
    HazelcastInstance liteMember = factory.newHazelcastInstance(getConfig().setLiteMember(true));
    // Insert data
    populate(liteMember);
    // Try query from the lite member.
    HazelcastSqlException error = assertSqlException(liteMember, query());
    assertErrorCode(SqlErrorCode.GENERIC, error);
    assertEquals("SQL queries cannot be executed on lite members", error.getMessage());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 13 with HazelcastSqlException

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

the class SqlConsole method executeSqlCmd.

private static void executeSqlCmd(HazelcastInstance hz, String command, Terminal terminal, AtomicReference<SqlResult> activeSqlResult) {
    PrintWriter out = terminal.writer();
    try (SqlResult sqlResult = hz.getSql().execute(command)) {
        activeSqlResult.set(sqlResult);
        // if it's a result with an update count, just print it
        if (sqlResult.updateCount() != -1) {
            String message = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append("OK").toAnsi();
            out.println(message);
            return;
        }
        SqlRowMetadata rowMetadata = sqlResult.getRowMetadata();
        int[] colWidths = determineColumnWidths(rowMetadata);
        Alignment[] alignments = determineAlignments(rowMetadata);
        // this is a result with rows. Print the header and rows, watch for concurrent cancellation
        printMetadataInfo(rowMetadata, colWidths, alignments, out);
        int rowCount = 0;
        for (SqlRow row : sqlResult) {
            rowCount++;
            printRow(row, colWidths, alignments, out);
        }
        // bottom line after all the rows
        printSeparatorLine(sqlResult.getRowMetadata().getColumnCount(), colWidths, out);
        String message = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append(String.valueOf(rowCount)).append(" row(s) selected").toAnsi();
        out.println(message);
    } catch (HazelcastSqlException e) {
        // the query failed to execute with HazelcastSqlException
        String errorPrompt = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append(e.getMessage()).toAnsi();
        out.println(errorPrompt);
    } catch (Exception e) {
        // the query failed to execute with an unexpected exception
        String unexpectedErrorPrompt = new AttributedStringBuilder().style(AttributedStyle.BOLD.foreground(PRIMARY_COLOR)).append("Encountered an unexpected exception while executing the query:\n").append(e.getMessage()).toAnsi();
        out.println(unexpectedErrorPrompt);
        e.printStackTrace(out);
    }
}
Also used : SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) SqlRowMetadata(com.hazelcast.sql.SqlRowMetadata) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) EndOfFileException(org.jline.reader.EndOfFileException) UserInterruptException(org.jline.reader.UserInterruptException) PrintWriter(java.io.PrintWriter) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException)

Example 14 with HazelcastSqlException

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

the class SqlIndexConverterMismatchTest method testMismatch.

/**
 * @see MapIndexScanExecIterator#getIndexEntries
 */
@SuppressWarnings({ "StatementWithEmptyBody", "JavadocReference" })
@Ignore("https://github.com/hazelcast/hazelcast/issues/19287")
@Test
public void testMismatch() {
    ExpressionBiValue value1 = new ExpressionBiValue.IntegerIntegerVal();
    value1.field1(10);
    value1.field2(10);
    ExpressionBiValue value2 = new ExpressionBiValue.StringIntegerVal();
    value2.field1("10");
    value2.field2(10);
    map.put(getLocalKey(member1, key -> key), value1);
    map.put(getLocalKey(member2, key -> key), value2);
    try {
        try (SqlResult result = member1.getSql().execute("SELECT key FROM " + MAP_NAME + " WHERE field1=1")) {
            for (SqlRow ignore : result) {
            // No-op.
            }
        }
        fail("Must fail!");
    } catch (HazelcastSqlException e) {
        assertEquals(SqlErrorCode.INDEX_INVALID, e.getCode());
        Throwable ex = findRootQueryException(e);
        assertEquals("Cannot use the index \"index\" of the IMap \"map\" because it has component \"field1\" of type VARCHAR, but INTEGER was expected", ex.getMessage());
    }
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) ExpressionBiValue(com.hazelcast.jet.sql.impl.support.expressions.ExpressionBiValue) ArrayList(java.util.ArrayList) MapConfig(com.hazelcast.config.MapConfig) IndexType(com.hazelcast.config.IndexType) MapIndexScanExecIterator(com.hazelcast.sql.impl.exec.scan.index.MapIndexScanExecIterator) After(org.junit.After) Assert.fail(org.junit.Assert.fail) SqlRow(com.hazelcast.sql.SqlRow) Parameterized(org.junit.runners.Parameterized) QueryException(com.hazelcast.sql.impl.QueryException) Before(org.junit.Before) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) Collection(java.util.Collection) HazelcastParallelParametersRunnerFactory(com.hazelcast.test.HazelcastParallelParametersRunnerFactory) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) IndexConfig(com.hazelcast.config.IndexConfig) List(java.util.List) Ignore(org.junit.Ignore) SqlResult(com.hazelcast.sql.SqlResult) SqlErrorCode(com.hazelcast.sql.impl.SqlErrorCode) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) SqlRow(com.hazelcast.sql.SqlRow) SqlResult(com.hazelcast.sql.SqlResult) ExpressionBiValue(com.hazelcast.jet.sql.impl.support.expressions.ExpressionBiValue) HazelcastSqlException(com.hazelcast.sql.HazelcastSqlException) Ignore(org.junit.Ignore) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 15 with HazelcastSqlException

use of com.hazelcast.sql.HazelcastSqlException 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)

Aggregations

HazelcastSqlException (com.hazelcast.sql.HazelcastSqlException)23 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)11 QuickTest (com.hazelcast.test.annotation.QuickTest)11 Test (org.junit.Test)11 SqlRow (com.hazelcast.sql.SqlRow)8 SqlStatement (com.hazelcast.sql.SqlStatement)7 SqlResult (com.hazelcast.sql.SqlResult)6 ArrayList (java.util.ArrayList)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 ExpressionBiValue (com.hazelcast.jet.sql.impl.support.expressions.ExpressionBiValue)3 List (java.util.List)3 SqlColumnType (com.hazelcast.sql.SqlColumnType)2 QueryException (com.hazelcast.sql.impl.QueryException)2 SqlFetchCodec (com.hazelcast.client.impl.protocol.codec.SqlFetchCodec)1 Config (com.hazelcast.config.Config)1 IndexConfig (com.hazelcast.config.IndexConfig)1 IndexType (com.hazelcast.config.IndexType)1 MapConfig (com.hazelcast.config.MapConfig)1 IMap (com.hazelcast.map.IMap)1 SqlColumnMetadata (com.hazelcast.sql.SqlColumnMetadata)1