Search in sources :

Example 1 with Warning

use of com.mysql.cj.xdevapi.Warning in project aws-mysql-jdbc by awslabs.

the class XProtocolTest method testWarnings.

@Test
public void testWarnings() {
    assumeTrue(this.isSetForXTests, PropertyDefinitions.SYSP_testsuite_url_mysqlx + " must be set to run this test.");
    this.protocol.send(this.messageBuilder.buildSqlStatement("explain select 1"), 0);
    this.protocol.readMetadata();
    this.protocol.drainRows();
    SqlResult res = this.protocol.readQueryResult(new SqlResultBuilder(this.protocol.getServerSession().getDefaultTimeZone(), this.protocol.getPropertySet()));
    Iterable<Warning> iterable = () -> res.getWarnings();
    List<Warning> warnings = StreamSupport.stream(iterable.spliterator(), false).map(w -> new WarningImpl(w)).collect(Collectors.toList());
    assertEquals(1, warnings.size());
    Warning w = warnings.get(0);
    assertEquals(1, w.getLevel());
    assertEquals(1003, w.getCode());
    // this message format might change over time and have to be loosened up
    assertEquals("/* select#1 */ select 1 AS `1`", w.getMessage());
}
Also used : MysqlErrorNumbers(com.mysql.cj.exceptions.MysqlErrorNumbers) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) XProtocolRowInputStream(com.mysql.cj.protocol.x.XProtocolRowInputStream) MysqlType(com.mysql.cj.MysqlType) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) BiConsumer(java.util.function.BiConsumer) StreamSupport(java.util.stream.StreamSupport) XServerCapabilities(com.mysql.cj.protocol.x.XServerCapabilities) DocFilterParams(com.mysql.cj.xdevapi.DocFilterParams) SqlResultBuilder(com.mysql.cj.xdevapi.SqlResultBuilder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) StringValueFactory(com.mysql.cj.result.StringValueFactory) UpdateSpec(com.mysql.cj.xdevapi.UpdateSpec) UpdateType(com.mysql.cj.xdevapi.UpdateType) Warning(com.mysql.cj.xdevapi.Warning) Iterator(java.util.Iterator) ColumnDefinition(com.mysql.cj.protocol.ColumnDefinition) XMessageBuilder(com.mysql.cj.protocol.x.XMessageBuilder) Field(com.mysql.cj.result.Field) InsertParams(com.mysql.cj.xdevapi.InsertParams) IOException(java.io.IOException) TableFilterParams(com.mysql.cj.xdevapi.TableFilterParams) WarningImpl(com.mysql.cj.xdevapi.WarningImpl) Collectors(java.util.stream.Collectors) PropertyDefinitions(com.mysql.cj.conf.PropertyDefinitions) StatementExecuteOkBuilder(com.mysql.cj.protocol.x.StatementExecuteOkBuilder) XProtocolError(com.mysql.cj.protocol.x.XProtocolError) Test(org.junit.jupiter.api.Test) SqlResult(com.mysql.cj.xdevapi.SqlResult) Row(com.mysql.cj.result.Row) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) FilterParams(com.mysql.cj.xdevapi.FilterParams) XProtocol(com.mysql.cj.protocol.x.XProtocol) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Warning(com.mysql.cj.xdevapi.Warning) SqlResult(com.mysql.cj.xdevapi.SqlResult) SqlResultBuilder(com.mysql.cj.xdevapi.SqlResultBuilder) WarningImpl(com.mysql.cj.xdevapi.WarningImpl) Test(org.junit.jupiter.api.Test)

Example 2 with Warning

use of com.mysql.cj.xdevapi.Warning in project aws-mysql-jdbc by awslabs.

the class SessionTest method testBug97269.

/**
 * Test fix for Bug#97269 (30438500), POSSIBLE BUG IN COM.MYSQL.CJ.XDEVAPI.STREAMINGDOCRESULTBUILDER.
 *
 * @throws Exception
 */
@Test
public void testBug97269() throws Exception {
    Session sess = null;
    try {
        String message1 = "W1";
        String message2 = "W2";
        // create notice message buffers
        Frame.Builder notice1 = Frame.newBuilder().setScope(Frame.Scope.LOCAL).setType(Frame.Type.WARNING_VALUE).setPayload(com.mysql.cj.x.protobuf.MysqlxNotice.Warning.newBuilder().setCode(MysqlErrorNumbers.ER_BAD_DB_ERROR).setMsg(message1).build().toByteString());
        Frame.Builder notice2 = Frame.newBuilder().setScope(Frame.Scope.GLOBAL).setType(Frame.Type.WARNING_VALUE).setPayload(com.mysql.cj.x.protobuf.MysqlxNotice.Warning.newBuilder().setCode(MysqlErrorNumbers.ER_BAD_DB_ERROR).setMsg(message2).build().toByteString());
        byte[] notice1Bytes = makeNoticeBytes(notice1.build());
        byte[] notice2Bytes = makeNoticeBytes(notice2.build());
        int size = notice1Bytes.length + notice2Bytes.length;
        byte[] noticesBytes = new byte[size];
        System.arraycopy(notice1Bytes, 0, noticesBytes, 0, notice1Bytes.length);
        System.arraycopy(notice2Bytes, 0, noticesBytes, notice1Bytes.length, notice2Bytes.length);
        InjectedSocketFactory.flushAllStaticData();
        String url = this.baseUrl + (this.baseUrl.contains("?") ? "" : "?") + makeParam(PropertyKey.socketFactory, InjectedSocketFactory.class.getName(), !this.baseUrl.contains("?") || this.baseUrl.endsWith("?")) + makeParam(PropertyKey.xdevapiSslMode, XdevapiSslMode.DISABLED.toString()) + makeParam(PropertyKey.xdevapiCompression, Compression.DISABLED.toString()) + // to allow injection between result rows
        makeParam(PropertyKey.useReadAheadInput, "false");
        sess = this.fact.getSession(url);
        SocketFactory sf = ((SessionImpl) sess).getSession().getProtocol().getSocketConnection().getSocketFactory();
        assertTrue(InjectedSocketFactory.class.isAssignableFrom(sf.getClass()));
        Collection collection = sess.getDefaultSchema().createCollection("testBug97269");
        collection.add("{\"_id\":\"the_id\",\"g\":1}").execute();
        // StreamingDocResultBuilder
        InjectedSocketFactory.injectedBuffer = noticesBytes;
        DocResult docs = collection.find().fields("$._id as _id, $.g as g, 1 + 1 as q").execute();
        DbDoc doc = docs.next();
        assertEquals("the_id", ((JsonString) doc.get("_id")).getString());
        assertEquals(new Integer(1), ((JsonNumber) doc.get("g")).getInteger());
        assertEquals(new Integer(2), ((JsonNumber) doc.get("q")).getInteger());
        int cnt = 0;
        for (Iterator<Warning> warn = docs.getWarnings(); warn.hasNext(); ) {
            Warning w = warn.next();
            if (w.getMessage().equals(message1) || w.getMessage().equals(message2)) {
                cnt++;
            }
        }
        assertEquals(2, cnt);
        InjectedSocketFactory.flushAllStaticData();
        InjectedSocketFactory.injectedBuffer = noticesBytes;
        SqlResult rs1 = sess.sql("select 1").execute();
        assertEquals(1, rs1.fetchOne().getInt(0));
        cnt = 0;
        for (Iterator<Warning> warn = rs1.getWarnings(); warn.hasNext(); ) {
            Warning w = warn.next();
            if (w.getMessage().equals(message1) || w.getMessage().equals(message2)) {
                cnt++;
            }
        }
        assertEquals(2, cnt);
    } finally {
        InjectedSocketFactory.flushAllStaticData();
        dropCollection("testBug97269");
        if (sess != null) {
            sess.close();
        }
    }
}
Also used : Frame(com.mysql.cj.x.protobuf.MysqlxNotice.Frame) Warning(com.mysql.cj.xdevapi.Warning) SqlResult(com.mysql.cj.xdevapi.SqlResult) SocketFactory(com.mysql.cj.protocol.SocketFactory) InjectedSocketFactory(testsuite.InjectedSocketFactory) UnreliableSocketFactory(testsuite.UnreliableSocketFactory) JsonString(com.mysql.cj.xdevapi.JsonString) DbDoc(com.mysql.cj.xdevapi.DbDoc) InjectedSocketFactory(testsuite.InjectedSocketFactory) Collection(com.mysql.cj.xdevapi.Collection) DocResult(com.mysql.cj.xdevapi.DocResult) CoreSession(com.mysql.cj.CoreSession) Session(com.mysql.cj.xdevapi.Session) Test(org.junit.jupiter.api.Test)

Example 3 with Warning

use of com.mysql.cj.xdevapi.Warning in project aws-mysql-jdbc by awslabs.

the class CollectionFindTest method testGetWarningsFromCollection.

/**
 * Checks getWarningsCount and getWarnings APIs
 *
 * @throws Exception
 */
@Test
public void testGetWarningsFromCollection() throws Exception {
    assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
    String collname = "coll1";
    Collection coll = null;
    Warning w = null;
    DocResult docs = null;
    int i = 0;
    try {
        this.session.sql("set  max_error_count=20000").execute();
        dropCollection(collname);
        coll = this.schema.createCollection(collname, true);
        for (i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                coll.add("{\"X\":" + i + ",\"Y\":" + (i + 1000) + "}").execute();
            } else {
                coll.add("{\"X\":0,\"Y\":0}").execute();
            }
        }
        docs = coll.find().fields("1/$.X as col1,1/$.Y as col2").execute();
        assertEquals(10, docs.getWarningsCount());
        i = 0;
        for (Iterator<Warning> warn = docs.getWarnings(); warn.hasNext(); ) {
            w = warn.next();
            assertEquals("Division by 0", w.getMessage());
            assertEquals(2, w.getLevel());
            assertEquals(1365, w.getCode());
            i++;
        }
        this.schema.dropCollection(collname);
        coll = this.schema.createCollection(collname, true);
        coll.add("{\"X\":1}").execute();
        String s = "";
        for (i = 1; i <= 10000; i++) {
            if (i > 1) {
                s = s + ",";
            }
            if (i % 2 == 0) {
                s = s + "1/$.X as col" + i;
            } else {
                s = s + "$.X/0 as col1" + i;
            }
        }
        docs = coll.find().fields(s).execute();
        assertEquals(5000, docs.getWarningsCount());
        i = 0;
        for (Iterator<Warning> warn = docs.getWarnings(); warn.hasNext(); ) {
            w = warn.next();
            assertEquals("Division by 0", w.getMessage());
            assertEquals(2, w.getLevel());
            assertEquals(1365, w.getCode());
            i++;
        }
        this.schema.dropCollection(collname);
    } finally {
        if (this.session != null) {
            this.session.close();
        }
    }
}
Also used : Warning(com.mysql.cj.xdevapi.Warning) Collection(com.mysql.cj.xdevapi.Collection) JsonString(com.mysql.cj.xdevapi.JsonString) DocResult(com.mysql.cj.xdevapi.DocResult) Test(org.junit.jupiter.api.Test)

Example 4 with Warning

use of com.mysql.cj.xdevapi.Warning in project aws-mysql-jdbc by awslabs.

the class ResultTest method testForceBuffering.

@Test
public void testForceBuffering() {
    try {
        sqlUpdate("drop table if exists testx");
        sqlUpdate("create table testx (x int)");
        sqlUpdate("insert into testx values (1), (2), (3)");
        Table table = this.schema.getTable("testx");
        RowResult rows = table.select("x/0 as bad_x").execute();
        // get warnings IMMEDIATELY
        assertEquals(3, rows.getWarningsCount());
        Iterator<Warning> warnings = rows.getWarnings();
        assertEquals(1365, warnings.next().getCode());
        assertEquals(1365, warnings.next().getCode());
        assertEquals(1365, warnings.next().getCode());
        Row r = rows.next();
        assertEquals(null, r.getString("bad_x"));
        r = rows.next();
        assertEquals(null, r.getString("bad_x"));
        r = rows.next();
        assertEquals(null, r.getString("bad_x"));
        assertThrows(NoSuchElementException.class, () -> rows.next());
    } finally {
        sqlUpdate("drop table if exists testx");
    }
}
Also used : RowResult(com.mysql.cj.xdevapi.RowResult) Warning(com.mysql.cj.xdevapi.Warning) Table(com.mysql.cj.xdevapi.Table) Row(com.mysql.cj.xdevapi.Row) Test(org.junit.jupiter.api.Test)

Example 5 with Warning

use of com.mysql.cj.xdevapi.Warning in project aws-mysql-jdbc by awslabs.

the class SessionTest method testExecAsyncNegative.

/**
 * Few Negative Scenarios
 *
 * @throws Exception
 */
@Test
public void testExecAsyncNegative() throws Exception {
    int i = 0;
    SqlResult sqlRes = null;
    Row r = null;
    try {
        assertThrows(ExecutionException.class, ".*Unknown table '" + this.schema.getName() + ".non_existing'.*", () -> {
            CompletableFuture<SqlResult> res = this.session.sql("drop table non_existing").executeAsync();
            res.get();
            return null;
        });
        assertThrows(ExecutionException.class, ".* BIGINT value is out of range .*", () -> {
            CompletableFuture<SqlResult> res = this.session.sql("select 123456*123456722323289").executeAsync();
            res.get();
            return null;
        });
        sqlUpdate("drop table if exists testExecAsyncNegative");
        sqlUpdate("create table testExecAsyncNegative(a int,b bigint ,c bigint  GENERATED ALWAYS AS (b*1000) VIRTUAL COMMENT '1',d bigint  GENERATED ALWAYS AS (c*100000) STOred  COMMENT '2')");
        sqlUpdate("Insert into  testExecAsyncNegative (a,b) values(1,100)");
        sqlUpdate("create index id on testExecAsyncNegative(d)");
        sqlUpdate("create unique index id2 on testExecAsyncNegative(a)");
        int NUMBER_OF_QUERIES = 5000;
        List<CompletableFuture<SqlResult>> futures = new ArrayList<>();
        for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
            if (i % 6 == 0) {
                futures.add(this.session.sql("replace into testExecAsyncNegative (a,b) values(?,?)").bind(1).bind(1555666000000L).executeAsync());
            } else if (i % 6 == 1) {
                futures.add(this.session.sql("insert into testExecAsyncNegative (a,b) values (?,?) ON DUPLICATE KEY UPDATE b= ?").bind(1).bind(2).bind(1555666009990L).executeAsync());
            } else if (i % 6 == 2) {
                futures.add(this.session.sql("alter table testExecAsyncNegative add d point").executeAsync());
            } else if (i % 6 == 3) {
                futures.add(this.session.sql("insert into testExecAsyncNegative (a,b) values (?,?) ON DUPLICATE KEY UPDATE b=b/?").bind(1).bind(2).bind(0).executeAsync());
            } else if (i % 6 == 4) {
                futures.add(this.session.sql("SELECT /*+ max_execution_time (100) bad_hint */ SLEEP(0.5)").executeAsync());
            } else {
                futures.add(this.session.sql("select /*+*/ * from testExecAsyncNegative").executeAsync());
            }
        }
        for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
            int i1 = i;
            if (i % 6 == 0) {
                assertThrows(ExecutionException.class, ".* BIGINT value is out of range .*", () -> futures.get(i1).get());
            } else if (i % 6 == 1) {
                assertThrows(ExecutionException.class, ".* BIGINT value is out of range .*", () -> futures.get(i1).get());
            } else if (i % 6 == 2) {
                assertThrows(ExecutionException.class, ".*Duplicate column name 'd'.*", () -> futures.get(i1).get());
            } else if (i % 6 == 3) {
                assertThrows(ExecutionException.class, ".*Division by 0.*", () -> futures.get(i1).get());
            } else {
                sqlRes = futures.get(i).get();
                r = sqlRes.next();
                assertEquals(1, r.getInt(0));
                assertFalse(sqlRes.hasNext());
                Iterator<Warning> w = sqlRes.getWarnings();
                while (w.hasNext()) {
                    Warning element = w.next();
                    assertTrue(element.getMessage().contains("Optimizer hint syntax error"));
                }
            }
        }
    } finally {
        sqlUpdate("drop table if exists testExecAsyncNegative");
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Warning(com.mysql.cj.xdevapi.Warning) SqlResult(com.mysql.cj.xdevapi.SqlResult) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Row(com.mysql.cj.xdevapi.Row) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

Warning (com.mysql.cj.xdevapi.Warning)5 Test (org.junit.jupiter.api.Test)5 SqlResult (com.mysql.cj.xdevapi.SqlResult)3 Collection (com.mysql.cj.xdevapi.Collection)2 DocResult (com.mysql.cj.xdevapi.DocResult)2 JsonString (com.mysql.cj.xdevapi.JsonString)2 Row (com.mysql.cj.xdevapi.Row)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 CoreSession (com.mysql.cj.CoreSession)1 MysqlType (com.mysql.cj.MysqlType)1 PropertyDefinitions (com.mysql.cj.conf.PropertyDefinitions)1 MysqlErrorNumbers (com.mysql.cj.exceptions.MysqlErrorNumbers)1 ColumnDefinition (com.mysql.cj.protocol.ColumnDefinition)1 SocketFactory (com.mysql.cj.protocol.SocketFactory)1 StatementExecuteOkBuilder (com.mysql.cj.protocol.x.StatementExecuteOkBuilder)1 XMessageBuilder (com.mysql.cj.protocol.x.XMessageBuilder)1 XProtocol (com.mysql.cj.protocol.x.XProtocol)1 XProtocolError (com.mysql.cj.protocol.x.XProtocolError)1 XProtocolRowInputStream (com.mysql.cj.protocol.x.XProtocolRowInputStream)1