Skip to content

Commit a3f7263

Browse files
NRHelmiNHDaly
andauthored
show result for v2 protocol (#21)
* show result for TransactionAsyncResult * enhance v2 show * enhance v2 show result * Update rai/show.go Co-authored-by: Nathan Daly <nathan.daly@relational.ai> * Update rai/show.go Co-authored-by: Nathan Daly <nathan.daly@relational.ai> * update * make arrowRelation unique by relation id * fix arrow relation test * Simplification to readArrowFiles to construct the columns in one pass (#22) heh, sorry i have one more suggestion. I guess you can do this in a single-pass. (Sorry, github wouldn't let me leave a code suggestion over this large section of the code) * adding ShowIO and v2 show test Co-authored-by: Nathan Daly <nathan.daly@relational.ai>
1 parent 6ac387b commit a3f7263

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

rai/client.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,14 @@ func readArrowFiles(files []TransactionAsyncFile) ([]ArrowRelation, error) {
349349
defer reader.Release()
350350
for reader.Next() {
351351
rec := reader.Record()
352+
var columns [][]interface{}
352353
for i := 0; i < int(rec.NumCols()); i++ {
353354
data, _ := rec.Column(i).MarshalJSON()
354-
var values []interface{}
355-
json.Unmarshal(data, &values)
356-
out = append(out, ArrowRelation{file.Name, values})
355+
var column []interface{}
356+
json.Unmarshal(data, &column)
357+
columns = append(columns, column)
357358
}
359+
out = append(out, ArrowRelation{file.Name, columns})
358360

359361
rec.Retain()
360362
}

rai/client_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package rai
1616

1717
import (
18+
"bytes"
1819
"context"
1920
"fmt"
2021
"net/http"
@@ -326,10 +327,12 @@ func TestExecuteAsync(t *testing.T) {
326327
assert.Nil(t, err)
327328

328329
expectedResults := []ArrowRelation{
329-
ArrowRelation{"/:output/Int64/Int64/Int64/Int64", []interface{}{1., 2., 3., 4., 5.}},
330-
ArrowRelation{"/:output/Int64/Int64/Int64/Int64", []interface{}{1., 4., 9., 16., 25.}},
331-
ArrowRelation{"/:output/Int64/Int64/Int64/Int64", []interface{}{1., 8., 27., 64., 125.}},
332-
ArrowRelation{"/:output/Int64/Int64/Int64/Int64", []interface{}{1., 16., 81., 256., 625.}},
330+
ArrowRelation{"/:output/Int64/Int64/Int64/Int64", [][]interface{}{
331+
{1., 2., 3., 4., 5.},
332+
{1., 4., 9., 16., 25.},
333+
{1., 8., 27., 64., 125.},
334+
{1., 16., 81., 256., 625.},
335+
}},
333336
}
334337

335338
assert.Equal(t, rsp.Results[0].Table, expectedResults[0].Table)
@@ -343,6 +346,13 @@ func TestExecuteAsync(t *testing.T) {
343346
expectedProblems := []interface{}{}
344347

345348
assert.Equal(t, rsp.Problems, expectedProblems)
349+
350+
// also testing Show v2 result format
351+
var io bytes.Buffer
352+
rsp.ShowIO(&io)
353+
expectedOutput := "/:output/Int64/Int64/Int64/Int64\n1, 1, 1, 1\n2, 4, 8, 16\n3, 9, 27, 81\n4, 16, 64, 256\n5, 25, 125, 625\n\n"
354+
355+
assert.Equal(t, io.String(), expectedOutput)
346356
}
347357

348358
func findRelation(relations []Relation, colName string) *Relation {

rai/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ type TransactionAsyncFile struct {
258258

259259
type ArrowRelation struct {
260260
RelationID string
261-
Table []interface{}
261+
Table [][]interface{}
262262
}
263263

264264
type TransactionAsyncSingleResponse struct {

rai/show.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,42 @@ func (tx *TransactionResult) Show() {
127127
}
128128
}
129129
}
130+
131+
func zip(lists ...[]interface{}) func() []interface{} {
132+
zip := make([]interface{}, len(lists))
133+
i := 0
134+
return func() []interface{} {
135+
for j := range lists {
136+
if i >= len(lists[j]) {
137+
return nil
138+
}
139+
zip[j] = lists[j][i]
140+
}
141+
i++
142+
return zip
143+
}
144+
}
145+
146+
func (tx *TransactionAsyncResult) ShowIO(io io.Writer) {
147+
for _, r := range tx.Results {
148+
k := r.RelationID
149+
v := r.Table
150+
fmt.Fprintf(io, "%s\n", k)
151+
iter := zip(v...)
152+
for tuple := iter(); tuple != nil; tuple = iter() {
153+
for i, element := range tuple {
154+
if i > 0 {
155+
fmt.Fprint(io, ", ")
156+
}
157+
158+
fmt.Fprintf(io, "%v", element)
159+
}
160+
fmt.Fprintln(io)
161+
}
162+
fmt.Fprintln(io)
163+
}
164+
}
165+
166+
func (tx *TransactionAsyncResult) Show() {
167+
tx.ShowIO(os.Stdout)
168+
}

0 commit comments

Comments
 (0)