Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions type-class-schema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,20 @@ We'd like to support the following operations:
- projection
- cartesian product
- inner join

## Record representation

We have a few options of how to store record values:
- tuples;
- `JsonObject`;
- `Map[String, Any]`;
- ...

In order to generalize storage we might use a type class `RecordRepr` that captures the operations that are required to compose record value and extract column values.

```scala
trait RecordRepr[A]:
type Prepend[HS, HV, S <:Tuple, A]
inline def prepend[HS, HV, S <:Tuple](h: HV, value: ValueWithSchema[S, A])(using SchemaValueType[HS *: S, A]): Prepend[HS, HV, S, A]
inline def get
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ru.primetalk.typed.ontology.typeclass.rschema

import ru.primetalk.typed.ontology.typeclass.schema.{SchemaValueType, ValueWithSchema}
trait RecordRepr[Base]:
/**
* Type after prepending column of schema HS with value type HV to
* an existing schema S with value representation A
*/
type Prepend[HS, HV, S <: Tuple, A <: Base]
inline def prepend[HS, HV, S <:Tuple, A <: Base](h: HV, value: ValueWithSchema[S, A])(using SchemaValueType[S, A]): Prepend[HS, HV, S, A]

/**
* Type result of getting value from schema. Usually it's HV | Nothing
*/
type Get[HS, HV, S <: Tuple, A <: Base] <: HV | Nothing
inline def get[HS, HV, S <: Tuple, A <: Base](value: ValueWithSchema[S, A]): Get[HS, HV, S, A]

inline def getOpt[HS, HV, S <: Tuple, A <: Base](value: ValueWithSchema[S, A]): Option[HV]


object TupleReprs:
type Get0[HS, HV, S <: Tuple] <: HV | Nothing = S match
case HS *: tail => HV
case _ *: tail => Get0[HS, HV, tail]
case EmptyTuple => Nothing

type Get0Opt[HS, HV, S <: Tuple] <: Option[HV] = S match
case HS *: tail => Some[HV]
case _ *: tail => Get0Opt[HS, HV, tail]
case EmptyTuple => None.type

given RecordRepr[Tuple] = new RecordRepr[Tuple]:
type Prepend[HS, HV, S <: Tuple, A <: Tuple] =
HV *: A
inline def prepend[HS, HV, S <: Tuple, A <: Tuple](h: HV, value: ValueWithSchema[S, A])(using SchemaValueType[S, A]): Prepend[HS, HV, S, A] =
h *: value.value

type Get[HS, HV, S <: Tuple, A <: Tuple] = Get0[HS, HV, S]
inline def get[HS, HV, S <: Tuple, A <: Tuple](value: ValueWithSchema[S, A]): Get[HS, HV, S, A] =
inline scala.compiletime.erasedValue[S] match
case _: (HS *: tail) => (value.value.head).asInstanceOf[HV]
case _: (_ *: tail) => get(value.value.tail)
case _: EmptyTuple => ???


inline def getOpt[HS, HV, S <: Tuple, A <: Tuple](value: ValueWithSchema[S, A]): Option[HV] =
inline scala.compiletime.erasedValue[S] match
case _: (HS *: tail) => Some((value.value.head).asInstanceOf[HV])
case _: (_ *: tail) => getOpt(value.value.tail)
case _: EmptyTuple => None
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ object ValueWithSchema:
new IdentityConversion[ValueWithSchema[Schema, Value]]

extension [Schema, Value](vws: ValueWithSchema[Schema, Value])
def value: Value = vws
inline def value: Value = vws

end ValueWithSchema
Loading