phantom icon indicating copy to clipboard operation
phantom copied to clipboard

Probably inconsistency in work with static columns

Open ogorun opened this issue 11 years ago • 6 comments

Hello phantom team. There is problematic behaviour of phantom on work with static columns. I'd like to hear your feedback regarding it.

Cassandra static columns are used for one-to-many relation modelling. When column family has static column, delete of last child (last primary key for given partition key) leads not to record deletion, but to record with primary key and non-static columns set to null. In phantom it leads to problems with deserialisation. Consider following simple modelling:

/**

CREATE TABLE sampletable(
parentid text,
parentname text static,
childid text,
PRIMARY KEY ((parentid), childid)
);

*/

case class SampleObj(parentId: String, parentName: String, childId: String)

class SampleTable extends CassandraTable[SampleTable, SampleObj] with ExampleConnectorr {
    object parentId   extends StringColumn(this) with PartitionKey[String]
    object parentName extends StringColumn(this) with StaticColumn[String]
    object childId    extends StringColumn(this) with PrimaryKey[String]

   override def fromRow(r: Row): SampleObj = {
      SampleObj(parentId(r), parentName(r), childId(r))
   }

   def insertObj(obj: SampleObj) : Future[ResultSet] = {
     insert.value(_.parentId, obj.parentId).value(_.parentName, obj.parentName).value(_.childId, obj.childId).future()
   }

 def deleteObj(obj: SampleObj) : Future[ResultSet] = {
    delete.where(_.parentId eqs obj.parentId).and(_.childId eqs obj.childId).future()
 }

def getParentChildren(parentId: String) : Future[Seq[SampleObj]] = {
   select.where(_.parentId eqs parentId).fetch()
 }
}

object SampleTableTest extends App  {
   val table = new SampleTable
   val interval = 40 seconds

   var list = Await.result(table.getParentChildren("sample"), interval)
   println(list)

   Await.result(table.insertObj(SampleObj("parent1", "sample", "child1")), interval)
   Await.result(table.insertObj(SampleObj("parent1", "sample", "child2")), interval)
   list = Await.result(table.getParentChildren("parent1"), interval)
   println(list)

   Await.result(table.deleteObj(SampleObj("parent1", "sample", "child2")), interval)
   list = Await.result(table.getParentChildren("parent1"), interval)
   println(list)

   Await.result(table.deleteObj(SampleObj("parent1", "sample", "child1")), interval)
  list = Await.result(table.getParentChildren("parent1"), interval)
  println(list)
}

Output:

....
Queue()
Queue(SampleObj(parent1,sample,child1), SampleObj(parent1,sample,child2))
Queue(SampleObj(parent1,sample,child1))
Exception in thread "main" java.lang.Exception: can't extract required value for column 'childId'
at com.websudos.phantom.column.Column$$anonfun$apply$1.apply(Column.scala:25)
at com.websudos.phantom.column.Column$$anonfun$apply$1.apply(Column.scala:25)
at scala.Option.getOrElse(Option.scala:121)
at com.websudos.phantom.column.Column.apply(Column.scala:25)
at com.pheed.backend.content.data.concrete.cassandra.SampleTable.fromRow(SampleTable.scala:32)
at com.pheed.backend.content.data.concrete.cassandra.SampleTable.fromRow(SampleTable.scala:26)
at com.websudos.phantom.SelectTable$$anonfun$select$1.apply(SelectTable.scala:26)
at com.websudos.phantom.SelectTable$$anonfun$select$1.apply(SelectTable.scala:26)
at com.websudos.phantom.query.SelectQuery.fromRow(Select.scala:31)
at com.websudos.phantom.query.SelectQuery$$anonfun$where$1.apply(Select.scala:64)
at com.websudos.phantom.query.SelectQuery$$anonfun$where$1.apply(Select.scala:64)
at com.websudos.phantom.query.SelectWhere.fromRow(Select.scala:134)
at com.websudos.phantom.query.ExecutableQuery$$anonfun$1$$anonfun$apply$1.apply(ExecutableQuery.scala:60)
at com.websudos.phantom.query.ExecutableQuery$$anonfun$1$$anonfun$apply$1.apply(ExecutableQuery.scala:60)
at play.api.libs.iteratee.Input$class.map(Iteratee.scala:236)
at play.api.libs.iteratee.Input$El.map(Iteratee.scala:247)
at play.api.libs.iteratee.Enumeratee$$anon$45$$anonfun$apply$23.apply(Enumeratee.scala:377)
at play.api.libs.iteratee.Enumeratee$$anon$45$$anonfun$apply$23.apply(Enumeratee.scala:377)
at play.api.libs.iteratee.Enumeratee$$anon$38$$anon$1$$anonfun$play$api$libs$iteratee$Enumeratee$$anon$$anon$$step$1$$anonfun$apply$11.apply(Enumeratee.scala:207)
at play.api.libs.iteratee.Enumeratee$$anon$38$$anon$1$$anonfun$play$api$libs$iteratee$Enumeratee$$anon$$anon$$step$1$$anonfun$apply$11.apply(Enumeratee.scala:207)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:121)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

The only record with parentId now includes null for childId, so phantom fails to construct object. From my point of view phantom should omit such records because query purpose is to get existing child objects. I'll be also glad to see your propositions for better modelling in such case.

ogorun avatar May 20 '15 08:05 ogorun

Hi @ogorun,

It is indeed the modelling scenario I would attack under the circumstances, or alternatively consider making the childId column an OptionalStringColumn. Normally under such events you would relate data with a separate table for parents as what you are using here is not very flexible.

You are eliminating two queries at runtime perhaps, but you are introducing complexity as every time a new field is needed a new static will need to be inserted into the SampleTable.

Also, what version of phantom are you using? Upgrading to later versions might come with a nice performance boost as we've removed Enumerators from fetch calls and they are now only used for very large result sets.

We will consider what changes can be made on our end, but it's quite difficult to track state changes in Cassandra in static Scala code, so it looks like an Optional column might be the easy way out, just as a separate table is.

Regards.

alexflav23 avatar May 20 '15 08:05 alexflav23

Hi @alexflav23,

Thank you for your answer.

I don't think that separate tables is a good idea in this case. Actually this answer for such type of question means that phantom doesn't support Cassandra static columns.

Here is my try to work with optional column:

case class SampleObj(parentId: String, parentName: String, childId: String)

class SampleTable extends CassandraTable[SampleTable, SampleObj] with ExampleConnectorr {
  object parentId   extends StringColumn(this) with PartitionKey[String]
  object parentName extends StringColumn(this) with StaticColumn[String]
  object childId    extends OptionalStringColumn(this) with PrimaryKey[Option[String]]

  override def fromRow(r: Row): SampleObj = {
     childId(r) match {
        case Some(childId) => SampleObj(parentId(r), parentName(r), childId)
       case None => null
    }
  }

 def insertObj(obj: SampleObj) : Future[ResultSet] = {
   insert.value(_.parentId, obj.parentId).value(_.parentName, obj.parentName).value(_.childId, Some(obj.childId)).future()
}

def deleteObj(obj: SampleObj) : Future[ResultSet] = {
  delete.where(_.parentId eqs obj.parentId).and(_.childId eqs Some(obj.childId)).future()
}

def getParentChildren(parentId: String) : Future[Seq[SampleObj]] = {
  select.where(_.parentId eqs parentId).fetch().map(seq => seq.filter(_ != null))
}
}

Result: compile time error:

Error:(43, 61) type mismatch;
 found   : com.websudos.phantom.query.SecondaryQueryCondition
 required: com.websudos.phantom.query.QueryCondition
   delete.where(_.parentId eqs obj.parentId).and(_.childId eqs Some(obj.childId)).future()
                                                        ^

Why is this considered secondary query condition?

P.S. I work with version 1.5. I'll try to move to 1.8 a bit later

ogorun avatar May 20 '15 12:05 ogorun

Hi @ogorun,

I think that's because Option[String] doesn't have a primitive. Primary keys can not be null in Cassandra which is why we didn't add support for eqs clauses in optional columns. I think there's something strange going on here, either in phantom or in Cassandra itself.

Having a null child id in your case doesn't make much sense as there would be no way in which you can actually use that right?

In here you state: Cassandra static columns are used for one-to-many relation modelling and this is I admit the first time I've heard that sentence. Maybe you could point to a resource indicating that, other than the one here. The one to many query enabled is still the partition key, even if the data happens to relate that way.

I think you are choosing the wrong solution data modelling wise and that static columns are specifically designed for shared data, NOT one-to-many. Have a look here.

Regards,

alexflav23 avatar May 20 '15 12:05 alexflav23

Well, I think that data sharing and one-to-many relation concept are distinguished more semantically. I'd say that Cassandra approach is closer to one-to-many. In just sharing case deleting of last primary key row for this partition key would delete partition key row instead of leaving row with null values (because there is no rows that share these static columns values). Actually in my case I would want to get data sharing behaviour. If all SampleObj objects for given primary key are deleted, I want to see empty list for such partition key and not an object of parent with nulls. Please give me an example of data modelling suitable for work with static columns

ogorun avatar May 20 '15 13:05 ogorun

Hi @ogorun ,

I've already linked to an example in the Cassandra docs. User bills with shared account balance is an ideal case. I think the fact that deletion leaves behind nulls should be addresses in Cassandra not phantom, as it's not phantom that breaches the contract here.

By Cassandra's own rules, Primary Keys or any part of the primary key can never be null so what you are getting is a bad false positive. It's Cassandra's fault its giving you a result list in the first place and it should itself return an empty collection if all it gets for a one-to-many query or partition slice is a series of nulls.

I will take this up with the Cassandra team and see how they feel about it, but I don't doubt it would already be a bug they are aware of. I would stick to normal one-to-many relationship mapping done in a separate table, denormalized like NoSQL is designed. That article that may have mislead you is ranking very highly in Google but that's about all the merit it has unfortunately and should probably be removed.

Regards, Flavian

alexflav23 avatar May 21 '15 01:05 alexflav23

HI @alexflav23 ,

Thank you for response and for time you spent to this problem. I see the problem. If you get to some conclusions with Cassandra team, please link this here.

Regards, Olga Gorun

ogorun avatar May 21 '15 06:05 ogorun