Skip to content

ReQL driver proposal: r.registerType #1016

Open

Description

@coffeemug
r.register_type(CustomType,
    # serializer
    lambda obj: r.json(json.dumps(obj)),
    # deserializer
    lambda dict: CustomType.from_dict(dict))
)

Then:

obj = CustomType()
r.table('foo').insert(obj)
r.table('foo').get(some_id) # returns CustomType

It's very easy to do the serializer, but harder to do the deserializer because it's unclear how to represent type tags properly. I'd suggest doing the serializer first and leaving the deserializer for later.

The use case for this is outlined in #241.

Activity

  1. jdoliner commented on Jun 15, 2013

    @jdoliner
    Contributor

    I remember talking with @mlucy about this a while ago and the way that we
    both really wanted to see this work is that registered types would
    serialized to an object that looked something like:

    {reql_type : "pickle", data: ...}

    This has some nice properties

    • it's incredibly easy, it takes 0 server side support and is as simple as
      looking up deserialization functions in a map in the drivers.
    • it's very easy to do the deserialization and I really think this feature
      is awfully half baked if you can't get out what you put in.
    • it degrades somewhat gracefully in drivers that don't support the type.
      Pickling is such a python specific thing I doubt that one will ever truly
      be usable but for a date type it works great in a language that doesn't
      have a native date type. (Which may not exist but somewhere is a good
      example)
    • this paves the way for the server understanding these types where it's
      useful. One great place would be with a date type and this we could
      overload subtraction on it so we don't need a special datedifference
      function.
    • typeof can give a meaningful result when called on such a value.

    On Friday, June 14, 2013, coffeemug wrote:

    r.register_type(CustomType,
    # serializer
    lambda obj: r.json(json.dumps(obj)),
    # deserializer
    lambda dict: CustomType.from_dict(dict)))

    Then:

    obj = CustomType()r.table('foo').insert(obj)r.table('foo').get(some_id) # returns CustomType

    It's very easy to do the serializer, but harder to do the deserializer
    because it's unclear how to represent type tags properly. I'd suggest doing
    the serializer first and leaving the deserializer for later.

    The use case for this is outlined in #241#241
    .

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/1016
    .

  2. alexbw commented on Jun 16, 2013

    @alexbw

    I agree with @jdoliner, the depickling and pickling should occur at the same time, otherwise it's a bit crippled. Inserting a dictionary that has type information and the pickled data together is a technique that has worked well for me in the past, in similar situations. +1

  3. mlucy commented on Jun 16, 2013

    @mlucy
    Member

    I like this idea as well. We should bikeshed for a bit about the magic keyword once this is moved into a milestone, though -- I would prefer __type__ to __rql_type__.

  4. jdoliner commented on Jun 17, 2013

    @jdoliner
    Contributor

    I went with reql_type over type mostly because it seemed like a
    .001% chance of collision instead of a .1% chance of collision. Also in the
    case that someone does get the raw dict version of the object back there's
    a chance they could not know that type is a reql thing rather than
    something another developer added. Especially considering we have an
    example in this thread of a user who's done something similar himself.
    On Jun 16, 2013 4:53 PM, "Michael Lucy" [email protected] wrote:

    I like this idea as well. We should bikeshed for a bit about the magic
    keyword once this is moved into a milestone, though -- I would prefer
    type to reql_type.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/1016#issuecomment-19518912
    .

  5. coffeemug commented on Jun 17, 2013

    @coffeemug
    ContributorAuthor

    Ok, I'm moving to backlog for now, but we'll try to get this into 1.8.

  6. neumino commented on Jun 30, 2013

    @neumino
    Member

    We have talked a lot about Dates, nested updates, triggers but not custom types for 1.8
    It came up twice this morning (for integers and number with bigger precision).

    Moving the issue into 1.8 just to make sure we don't forget it.

  7. jdoliner commented on Jun 30, 2013

    @jdoliner
    Contributor

    @neumino I'd really prefer if you left scheduling issues to @coffeemug it's not that I want to impose a hierarchy with this process but I think scheduling things really works a lot better if one person coordinates it. Right now when an issue is in a release milestone I take this to mean 1.8 isn't shipping without this. Which just isn't the case here.

    On top of this I think your reasoning for moving this is completely wrong. Adding integers, numbers with bigger precision and dates is a great feature but we certainly don't want those to be types that users have to register for themselves. We'd want those to be builtin types. They'd probably wind up using the reql_type field method but we can easily do that without giving users the ability to register their own types. Registering your own types is both a fairly big api discussion and some non trivial backend work which means I think it's higher cost and lower benefit than everything else we want to do in 1.8.

  8. neumino commented on Jun 30, 2013

    @neumino
    Member

    @jdoliner no problem.
    It wasn't an attempt to schedule anything, I was just worried that with the release coming next week, we would somehow forget to take a look at this one.

  9. jdoliner commented on Jun 30, 2013

    @jdoliner
    Contributor

    No worries. If it was indeed the case that this wasn't considered I think there's a decent argument for not scheduling it. But @coffeemug knows better if he did indeed consider this.

  10. coffeemug commented on Jun 30, 2013

    @coffeemug
    ContributorAuthor

    My plan was to get together with everyone next week after 1.7 is out and discuss 1.8, but roughly I think the most important things for 1.8 (wrt ReQL) are as follows:

    • .group.map.reduce
    • dates
    • nested objects (extending pluck syntax to other commands, and implementing r.literal)

    After these three are done, everything else in ReQL strikes me as a "nice to have" category (in as much as ReQL functionality post these changes will be sufficient to satisfy a huge number of users and will cease to be a bottleneck for product adoption, possibly with triggers and very minor ReQL improvements being notable exceptions). So we can move on to performance and scalability after 1.8 is out.

    It seems to me that extending ReQL with more types, and supporting a custom registerType functionality on the drivers are a) mostly orthogonal, and b) not a bottleneck relative to performance/scale issues. One possibility is that we might want to support client-side date types, and can implement it on top of registerType functionality. Since it's entirely a driver-side thing, I wouldn't be opposed to doing it in 1.8 if we have the time.

    Also FYI: the 1.8 milestone contains way more than we need for 1.8 as of now. I'll pare it down soon.

  11. srh commented on Jul 10, 2013

    @srh
    Contributor

    The time and maybe duration type will have a custom comparison operation that does not map to object comparison.

    #977 (comment)

    We need a general rule for how objects with different reql_type and no reql_type fields should be ordered.

  12. wmrowan commented on Jul 11, 2013

    @wmrowan
    Contributor

    I think the correct way to do this would be to have each driver implement a ReQLType abstract class (or equivalent concept) from which the user may subclass to create a new type. Instances of this class could then be passed to and stored in rethinkdb (using any mechanism we want) and retrieving values of the custom type would result in new instances of the class. The details of serialization and deserialization could be hidden from the user rendering arguments like #1149 unnecessary as this would be purely an implementation detail internal to the server.

    Most of the hard work would be done within the parent class in the driver but the user would be able to provide fine grained guidance by choosing to overload comparison methods etc.

    I think that functionality like this would feel much more like magic than r.register_type and thus get us a little bit closer to closing #1000.

  13. coffeemug commented on Jul 11, 2013

    @coffeemug
    ContributorAuthor

    I think the correct way to do this would be to have each driver implement a ReQLType abstract class (or equivalent concept) from which the user may subclass to create a new type.

    There are a couple of downsides to this approach:

    • It wouldn't work well with existing types users might want to store (for example, NumPy types) because they'd have to create wrapper objects which would get rather messy.
    • It would be very difficult for the abstract type to magically serialize the derived type correctly. For example, many types have transient fields that don't need to be serialized, and the driver wouldn't know that. Supporting this kind of stuff would get really complicated because we'd have to build a declarative serialization framework in each driver, which is hard.

    Overall, I think register_type would be much easier to use, and would work for more use cases.

  14. wmrowan commented on Jul 11, 2013

    @wmrowan
    Contributor

    Actually, what I have in mind is mostly an enhancement on top of r.register_type to make it more useful in the common case. Here's how I imagine it might would work:

    from time import time
    
    class MyTime(r.CustomType):
       def __init__(self, utc_time):
          self.utc_time = utc_time
    
       @staticmethod
       def now():
          return MyTime(time.now())
    
       # Serialization is handled by the parent class so the user
       # doesn't have to even think about it.
       # For more complex cases than this though these methods
       # can be overloaded.
       # With just `r.register_type` this is mandatory, not an option
       def serialize(self):
          return json.dumps(self)
    
       def parse(self, serialized)
          self.update(json.parse(serialized))
    
       # We could use the same trick that we do with lambda
       # functions to serialize a user provided comparison
       # function. I'm not totally sold on this though.
       def less_than(self, other):
          self.utc_time < other.utc_time
    
    # For existing types we basically default to the register type proposal
    r.CusumType.register(crazy_type,
       serialize=lambda self: ...,
       parse=lambda self: ...,
       less_than=lambda self, other: ...
    )
    
    # Again, the serialize and deserialize methods are optional, defaulting
    # to json serialization of all fields. The inherited method throws on
    # non serializable fields.
    r.CustomType.register(complex)
  15. alexbw commented on Aug 15, 2013

    @alexbw

    With 1.8 out, and supporting dates, is there any chance this enhancement will get some love?

  16. coffeemug commented on Aug 19, 2013

    @coffeemug
    ContributorAuthor

    @alexbw -- with dates out, RethinkDB now has a notion of pseudotypes (i.e. types that aren't specified by json, dates being an example). That should make this enhancement easier, but there are still some questions we need to answer about how to structured user-defined types (what's allowed, what's not allowed, how it integrates with the rest of the system, etc.)

    Our main priority now is to get Rethink to production-readiness, which means mainly performance, stability, and scalability fixes. I think we won't be able to get to this enhancement until 2.0 is out, but we'll almost certainly do it almost immediately after that.

  17. alexbw commented on Aug 19, 2013

    @alexbw

    Good to know, thanks.

    On Mon, Aug 19, 2013 at 3:23 AM, coffeemug [email protected] wrote:

    @alexbw https://github.com/alexbw -- with dates out, RethinkDB now has
    a notion of pseudotypes (i.e. types that aren't specified by json, dates
    being an example). That should make this enhancement easier, but there are
    still some questions we need to answer about how to structured user-defined
    types (what's allowed, what's not allowed, how it integrates with the rest
    of the system, etc.)

    Our main priority now is to get Rethink to production-readiness, which
    means mainly performance, stability, and scalability fixes. I think we
    won't be able to get to this enhancement until 2.0 is out, but we'll almost
    certainly do it almost immediately after that.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/1016#issuecomment-22854688
    .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions