Skip to content

Commit 483e693

Browse files
committed
Adds DatumWriter for most types
1 parent 0d123c2 commit 483e693

File tree

11 files changed

+1838
-39
lines changed

11 files changed

+1838
-39
lines changed

docs/wiki/v1/streams.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# PartiQL Data Streams
2+
3+
*This document defines the PSink and PSource in relation to Datum and Java primitives*
4+
5+
* * *
6+
7+
### Background
8+
9+
We have defined
10+
an [encoding of PartiQL values using the Ion data format](https://quip-amazon.com/5Su8AQhKG5xA/PartiQL-Values-in-Ion),
11+
but how does this fit in? Let’s look at two questions.
12+
13+
1. How does PartiQL represent values in memory?
14+
2. How does PartiQL read values from a stream into memory?
15+
16+
An in-memory PartiQL value has a layer of indirection between the Java primitive and its view to the rest of the
17+
program. This is called the “Datum” and is a fat interface which allows the partiql-lang-kotlin engine to not worry
18+
about a value’s Java type, and instead switch on an int tag (ptype) to then pull out a value. Effectively the fat
19+
interface removes checking classes and casting with tag checking then accessing without a cast. It’s effectively a
20+
unifying interface over the old values, so how does the variant fit in?
21+
22+
A variant is an implementation of a Datum whose value is opaque to the rest of the system. When the system checks the
23+
tag, it simply gets back “variant<T>” where T might tell us a set of capabilities (or traits) this type system / value
24+
has. This value is not lowered into a PartiQL value such as an INT or ARRAY, but is kept in its “container” or “box”.
25+
Think of the variant types of other engines or jsonb of PostgreSQL.
26+
27+
So how does PartiQL read values from a stream into Datums, and how does it handle variants? It depends because an
28+
encoding may include a data type or it may not. Also, the reader itself may expect a type (or not). Consider that a
29+
PartiQL value carries a type with it along with the value itself.
30+
31+
## Writing Data
32+
33+
### PSink
34+
35+
The PSink interface is used to write PartiQL data. It has APIs just like the IonWriter, and similarly, it has different
36+
implementations for the actual encoding like how Ion has both a text and a binary encoding. A PSink is used without any
37+
assumptions about the actual encoding.
38+
39+
### DatumWriter
40+
41+
The DatumWriter is a class which facilitates writing datums via a PSink implementation; it is handles materializing a
42+
datum and calling the appropriate sink methods.
43+
44+
**Example**
45+
46+
```kotlin
47+
val writer = DatumWriter.standard(sink)
48+
writer.write(datum1)
49+
writer.write(datum2)
50+
writer.write(datum3)
51+
writer.close()
52+
```
53+
54+
### IonSink
55+
56+
This example shows how to encode a datum as Ion; type decorations are omitted where possible.
57+
58+
```kotlin
59+
val sink = IonSink(System.out) // printing
60+
val writer = DatumWriter(sink)
61+
62+
// bool
63+
writer.write(Datum.bool(true)) // >> true
64+
65+
// ints
66+
writer.write(Datum.smallint(1)) // >> smallint::1
67+
writer.write(Datum.int(2)) // >> int::2
68+
writer.write(Datum.bigint(3)) // >> 3
69+
70+
// exact and approx numeric
71+
writer.write(Datum.decimal(BigDecimal("3.14"), 3, 2)) // >> ((decimal 3 2) 3.14)
72+
writer.write(Datum.real(3.14f)) // >> real::3.14e0
73+
writer.write(Datum.doublePrecision(3.14)) // >> 3.14e0
74+
75+
// char strings
76+
writer.write(Datum.char("abc", 3)) // >> ((char 3) "abc")
77+
writer.write(Datum.varchar("abc", 3)) // >> ((varchar 3) "abc")
78+
writer.write(Datum.string("abc")) // >> "abc"
79+
80+
// lobs
81+
writer.write(Datum.clob("hello".toByteArray()), 5) // >> {{ "hello" }}
82+
writer.write(Datum.blob("hello".toByteArray()), 5) // >> {{ aGVsbG8= }}
83+
84+
// datetime
85+
// TODO blocked on https://github.com/partiql/partiql-lang-kotlin/pull/1656
86+
87+
// ion
88+
89+
```
90+
91+
## Reading Data
92+
93+
### DatumReader
94+
95+
### PSource
96+
97+
PLACEHOLDER

0 commit comments

Comments
 (0)