Reputation: 48694
Do Cassandra lightweight transactions use an implied SET, and so can not have an IF part in terms of a primary key?
I ask because the Datastax Cassandra Java driver (version 2.1.5) throws an InvalidQueryException
while I'm preparing a statement. The exception message is clear enough:
PRIMARY KEY part name found in SET part
Except that the query it is preparing does not attempt to SET the primary key. The query is
UPDATE operator SET password = :password WHERE name = :name IF name = :name;
As you can see, the query does SET password
and does not set name
; name
is the primary key for the operator
table. The exception message would make sense only if the IF name = :name
of the lightweight transaction was implemented using a SET name = ...
.
If I remove the IF name = :name
part from the query, the driver does not complain about trying to SET the primry key.
Here is the full stacktrace:
org.springframework.dao.InvalidDataAccessResourceUsageException: PRIMARY KEY part name found in SET part; nested exception is com.datastax.driver.core.exceptions.InvalidQueryException: PRIMARY KEY part name found in SET part at com.example.application.datalayer.DriverExceptionTranslator.translateExceptionIfPossible(DriverExceptionTranslator.java:200) ~[application-user.jar:1.0.0-SNAPSHOT] at com.example.application.datalayer.DriverExceptionTranslator$ExceptionTranslationAdvice.afterThrowing(DriverExceptionTranslator.java:50) ~[application-user.jar:1.0.0-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_75] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_75] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_75] at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_75] at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invokeHandlerMethod(ThrowsAdviceInterceptor.java:145) ~[application-user.jar:1.0.0-SNAPSHOT] at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:130) ~[application-user.jar:1.0.0-SNAPSHOT] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[application-user.jar:1.0.0-SNAPSHOT] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) ~[application-user.jar:1.0.0-SNAPSHOT] at com.example.application.user.$Proxy0.addUser(Unknown Source) ~[na:1.0.0-SNAPSHOT] at com.example.application.user.Add.tryToAddUser(Add.java:294) [application-user.jar:1.0.0-SNAPSHOT] at com.example.application.user.Add.performOperation(Add.java:266) [application-user.jar:1.0.0-SNAPSHOT] at com.example.application.user.Mode.run(Mode.java:812) [application-user.jar:1.0.0-SNAPSHOT] at com.example.application.user.Mode.runProgram(Mode.java:79) [application-user.jar:1.0.0-SNAPSHOT] at com.example.application.user.Add.main(Add.java:63) [application-user.jar:1.0.0-SNAPSHOT] Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: PRIMARY KEY part name found in SET part at com.datastax.driver.core.exceptions.InvalidQueryException.copy(InvalidQueryException.java:35) ~[application-user.jar:1.0.0-SNAPSHOT] at com.datastax.driver.core.DefaultResultSetFuture.extractCauseFromExecutionException(DefaultResultSetFuture.java:289) ~[application-user.jar:1.0.0-SNAPSHOT] at com.datastax.driver.core.AbstractSession.prepare(AbstractSession.java:79) ~[application-user.jar:1.0.0-SNAPSHOT] at com.example.application.user.CassandraUserRepository$SessionAdapter.(CassandraUserRepository.java:80) ~[application-user.jar:1.0.0-SNAPSHOT] at com.example.application.user.CassandraUserRepository.getSessionAdapter(CassandraUserRepository.java:447) ~[application-user.jar:1.0.0-SNAPSHOT] at com.example.application.user.CassandraUserRepository.addUser(CassandraUserRepository.java:317) ~[application-user.jar:1.0.0-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_75] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_75] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_75] at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_75] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[application-user.jar:1.0.0-SNAPSHOT] at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[application-user.jar:1.0.0-SNAPSHOT] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[application-user.jar:1.0.0-SNAPSHOT] at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:125) ~[application-user.jar:1.0.0-SNAPSHOT] ... 8 common frames omitted Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: PRIMARY KEY part name found in SET part at com.datastax.driver.core.Responses$Error.asException(Responses.java:103) ~[application-user.jar:1.0.0-SNAPSHOT] at com.datastax.driver.core.SessionManager$1.apply(SessionManager.java:185) ~[application-user.jar:1.0.0-SNAPSHOT] at com.datastax.driver.core.SessionManager$1.apply(SessionManager.java:160) ~[application-user.jar:1.0.0-SNAPSHOT] at com.google.common.util.concurrent.Futures$1.apply(Futures.java:720) ~[application-user.jar:1.0.0-SNAPSHOT] at com.google.common.util.concurrent.Futures$ChainingListenableFuture.run(Futures.java:859) ~[application-user.jar:1.0.0-SNAPSHOT] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_75] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_75] at java.lang.Thread.run(Thread.java:745) ~[na:1.7.0_75]
Upvotes: 1
Views: 619
Reputation: 2312
You can't set a condition on a partition key column (IF name = 'foo'
), because that column is already restricted in the where clause (WHERE name = 'foo'
).
There is indeed a problem with the error message. This has apparently been fixed, with Cassandra 2.1.4 I get: PRIMARY KEY column 'name' cannot have IF conditions
.
Upvotes: 4