cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
30.21k stars 3.82k forks source link

builtins: export builtin that returns a string between 2 other strings #49486

Open jordanlewis opened 4 years ago

jordanlewis commented 4 years ago

A user in community slack had a question about how to implement a reorderable list in the database. This seems like a pretty common schema design problem, with no easy answer.

The enum datatype has a nice algorithm for creating byte strings that sort between other byte strings. I think we should consider exporting this algorithm as a builtin to help users who need to make a reorderable list.

Proposed API:

bisect_strings(a string, b string) string

If both are NULL, return an arbitrary middle string (or maybe just return NULL?). If a is NULL, return a string before b. If b is NULL, return a string after a. Otherwise, return a string between a and b.

@rohany, what do you think about this idea?

Jira issue: CRDB-4222

rohany commented 4 years ago

This is an interesting use case!

This API already lines up pretty much with how the enum generator works re nulls and such. I'm just worried about how it could be used given the restrictions around the generator:

I guess if we document these then it would be ok.