You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/08/05 07:53:14 UTC

[GitHub] [shardingsphere] stephen-gao opened a new issue #6637: execute “replact into xxx” throw “InsertStatement cannot be cast to ReplaceStatement” exception.

stephen-gao opened a new issue #6637:
URL: https://github.com/apache/shardingsphere/issues/6637


   ## Bug Report
   version: 4.1.1
   project: ShardingSphere-JDBC
   sql: REPLACE INTO test(id, user_key, username)VALUES(#{id,jdbcType=VARCHAR}, #{userKey,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR});
   
   There may be a bug.
   when execute `replace into table-xxx` insert a data to mysql, throw exception `java.lang.ClassCastException: org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement cannot be cast to org.apache.shardingsphere.sql.parser.sql.statement.dml.ReplaceStatement`
   
   location problem:org.apache.shardingsphere.sql.parser.mysql.visitor.impl.MySQLDMLVisitor.visitReplace()
   >public ASTNode visitReplace(final ReplaceContext ctx) {
   // TODO :FIXME, since there is no segment for insertValuesClause, InsertStatement is created by sub rule.
   ReplaceStatement result;
   if (null != ctx.insertValuesClause()) {
   result = (ReplaceStatement) visit(ctx.insertValuesClause());
   } else {
   result = new ReplaceStatement();
   result.setSetAssignment((SetAssignmentSegment) visit(ctx.setAssignmentsClause()));
   }
   result.setTable((SimpleTableSegment) visit(ctx.tableName()));
   result.setParameterCount(getCurrentParameterIndex());
   return result;
   }
   
   My solution is use spi override visitReplace()
   
   >public ASTNode visitReplace(final MySQLStatementParser.ReplaceContext ctx) {
   InsertStatement result;
   if (null != ctx.insertValuesClause()) {
   result = (InsertStatement) visit(ctx.insertValuesClause());
   } else {
   result = new InsertStatement();
   result.setSetAssignment((SetAssignmentSegment) visit(ctx.setAssignmentsClause()));
   }
   result.setTable((SimpleTableSegment) visit(ctx.tableName()));
   result.setParameterCount(getCurrentParameterIndex());
   return result;
   }
   
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] stephen-gao commented on issue #6637: execute “replact into xxx” throw “InsertStatement cannot be cast to ReplaceStatement” exception.

Posted by GitBox <gi...@apache.org>.
stephen-gao commented on issue #6637:
URL: https://github.com/apache/shardingsphere/issues/6637#issuecomment-669074815


   all stack info
   ```
   
   org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
   ### Error updating database.  Cause: java.lang.ClassCastException: org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement cannot be cast to org.apache.shardingsphere.sql.parser.sql.statement.dml.ReplaceStatement
   ### The error may exist in file [D:\gitProject\myProject\shard-jdbc\target\classes\com\gs\shardjdbc\mapper\TestDOMapper.xml]
   ### The error may involve defaultParameterMap
   ### The error occurred while setting parameters
   ### SQL: REPLACE INTO test(                 id                 ,user_key                 ,username             )VALUES(                  ?                 , ?                 , ?             )
   ### Cause: java.lang.ClassCastException: org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement cannot be cast to org.apache.shardingsphere.sql.parser.sql.statement.dml.ReplaceStatement
   
   	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:78)
   	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
   	at com.sun.proxy.$Proxy68.update(Unknown Source)
   	at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:287)
   	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:67)
   	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57)
   	at com.sun.proxy.$Proxy69.replace(Unknown Source)
   	at com.gs.shardjdbc.dao.TestDAO.insert(TestDAO.java:27)
   	at com.gs.shardjdbc.dao.TestDAO$$FastClassBySpringCGLIB$$76ccecda.invoke(<generated>)
   	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
   	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
   	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
   	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
   	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
   	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
   	at com.gs.shardjdbc.dao.TestDAO$$EnhancerBySpringCGLIB$$a7f04240.insert(<generated>)
   	at com.gs.shardjdbc.dao.TestDAOTest.insertTest(TestDAOTest.java:46)
   	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   	at java.lang.reflect.Method.invoke(Method.java:498)
   	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
   	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
   	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
   	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
   	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
   	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
   	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
   	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
   	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
   	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
   	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
   	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
   	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
   	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
   	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
   	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
   	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
   	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
   	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
   	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
   	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
   	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
   	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
   	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
   	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
   	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
   Caused by: org.apache.ibatis.exceptions.PersistenceException: 
   ### Error updating database.  Cause: java.lang.ClassCastException: org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement cannot be cast to org.apache.shardingsphere.sql.parser.sql.statement.dml.ReplaceStatement
   ### The error may exist in file [D:\gitProject\myProject\shard-jdbc\target\classes\com\gs\shardjdbc\mapper\TestDOMapper.xml]
   ### The error may involve defaultParameterMap
   ### The error occurred while setting parameters
   ### SQL: REPLACE INTO test(                 id                 ,user_key                 ,username             )VALUES(                  ?                 , ?                 , ?             )
   ### Cause: java.lang.ClassCastException: org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement cannot be cast to org.apache.shardingsphere.sql.parser.sql.statement.dml.ReplaceStatement
   	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
   	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199)
   	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   	at java.lang.reflect.Method.invoke(Method.java:498)
   	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
   	... 45 more
   Caused by: java.lang.ClassCastException: org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement cannot be cast to org.apache.shardingsphere.sql.parser.sql.statement.dml.ReplaceStatement
   	at org.apache.shardingsphere.sql.parser.mysql.visitor.impl.MySQLDMLVisitor.visitReplace(MySQLDMLVisitor.java:163)
   	at org.apache.shardingsphere.sql.parser.mysql.visitor.impl.MySQLDMLVisitor.visitReplace(MySQLDMLVisitor.java:127)
   	at org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser$ReplaceContext.accept(MySQLStatementParser.java:1471)
   	at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18)
   	at org.apache.shardingsphere.sql.parser.SQLParserEngine.parse0(SQLParserEngine.java:80)
   	at org.apache.shardingsphere.sql.parser.SQLParserEngine.parse(SQLParserEngine.java:61)
   	at org.apache.shardingsphere.underlying.route.DataNodeRouter.createRouteContext(DataNodeRouter.java:97)
   	at org.apache.shardingsphere.underlying.route.DataNodeRouter.executeRoute(DataNodeRouter.java:89)
   	at org.apache.shardingsphere.underlying.route.DataNodeRouter.route(DataNodeRouter.java:76)
   	at org.apache.shardingsphere.underlying.pluggble.prepare.PreparedQueryPrepareEngine.route(PreparedQueryPrepareEngine.java:54)
   	at org.apache.shardingsphere.underlying.pluggble.prepare.BasePrepareEngine.executeRoute(BasePrepareEngine.java:96)
   	at org.apache.shardingsphere.underlying.pluggble.prepare.BasePrepareEngine.prepare(BasePrepareEngine.java:83)
   	at org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement.prepare(ShardingPreparedStatement.java:183)
   	at org.apache.shardingsphere.shardingjdbc.jdbc.core.statement.ShardingPreparedStatement.execute(ShardingPreparedStatement.java:143)
   	at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47)
   	at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
   	at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)
   	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
   	at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
   	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   	at java.lang.reflect.Method.invoke(Method.java:498)
   	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
   	at com.sun.proxy.$Proxy87.update(Unknown Source)
   	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
   	... 50 more
   
   
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] tristaZero commented on issue #6637: execute “replact into xxx” throw “InsertStatement cannot be cast to ReplaceStatement” exception.

Posted by GitBox <gi...@apache.org>.
tristaZero commented on issue #6637:
URL: https://github.com/apache/shardingsphere/issues/6637#issuecomment-669132814


   @strongduanmu The whole community is benefiting what you did, great job!


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] strongduanmu commented on issue #6637: execute “replact into xxx” throw “InsertStatement cannot be cast to ReplaceStatement” exception.

Posted by GitBox <gi...@apache.org>.
strongduanmu commented on issue #6637:
URL: https://github.com/apache/shardingsphere/issues/6637#issuecomment-669127075


   @stephen-gao This exception has been fixed in the master branch, please wait for the 5.x release version.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] tristaZero closed issue #6637: execute “replact into xxx” throw “InsertStatement cannot be cast to ReplaceStatement” exception.

Posted by GitBox <gi...@apache.org>.
tristaZero closed issue #6637:
URL: https://github.com/apache/shardingsphere/issues/6637


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] stephen-gao commented on issue #6637: execute “replact into xxx” throw “InsertStatement cannot be cast to ReplaceStatement” exception.

Posted by GitBox <gi...@apache.org>.
stephen-gao commented on issue #6637:
URL: https://github.com/apache/shardingsphere/issues/6637#issuecomment-669069305


   > @stephen-gao Can you show some stack info? such as actual sql, logical sql.
   
   stack info:
   > Error updating database.  Cause: java.lang.ClassCastException: org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement cannot be cast to org.apache.shardingsphere.sql.parser.sql.statement.dml.ReplaceStatement
   The error may exist in file [D:\gitProject\myProject\shard-jdbc\target\classes\com\gs\shardjdbc\mapper\TestDOMapper.xml]
   The error may involve defaultParameterMap
   The error occurred while setting parameters
   SQL: REPLACE INTO test(                 id                 ,user_key                 ,username             )VALUES(                  ?                 , ?                 , ?             )
   Cause: java.lang.ClassCastException: org.apache.shardingsphere.sql.parser.sql.statement.dml.InsertStatement cannot be cast to org.apache.shardingsphere.sql.parser.sql.statement.dml.ReplaceStatement at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:78)
   at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
   at com.sun.proxy.$Proxy68.update(Unknown Source)
   at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:287)
   at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:67)
   at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:57)
   at com.sun.proxy.$Proxy69.replace(Unknown Source)
   at com.gs.shardjdbc.dao.TestDAO.insert(TestDAO.java:27)
   
   my properties:
   >spring.shardingsphere.datasource.names=ds0
   spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
   spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
   spring.shardingsphere.datasource.ds0.url=xxxx
   spring.shardingsphere.datasource.ds0.username=root
   spring.shardingsphere.datasource.ds0.password=xxx
   spring.shardingsphere.sharding.binding-tables=test
   spring.shardingsphere.sharding.tables.test.actual-data-nodes=ds0.test_$->{0..7}
   spring.shardingsphere.sharding.tables.test.table-strategy.inline.sharding-column=user_key
   spring.shardingsphere.sharding.tables.test.table-strategy.inline.algorithm-expression=test_$->{user_key % 8}


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] jingshanglu commented on issue #6637: execute “replact into xxx” throw “InsertStatement cannot be cast to ReplaceStatement” exception.

Posted by GitBox <gi...@apache.org>.
jingshanglu commented on issue #6637:
URL: https://github.com/apache/shardingsphere/issues/6637#issuecomment-669055724


   @stephen-gao Can you show some stack info? such as actual sql, logical sql.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org