-
Notifications
You must be signed in to change notification settings - Fork 2
/
execution.go
111 lines (102 loc) · 3.33 KB
/
execution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"encoding/base64"
"fmt"
"log"
"math"
"time"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
. "github.com/cube2222/octosql/execution"
"github.com/cube2222/octosql/octosql"
"github.com/cube2222/octosql/physical"
)
type DatasourceExecuting struct {
fields []physical.SchemaField
table string
placeholderExprTypes []octosql.Type
placeholderExprs []Expression
db *pgx.ConnPool
stmt *pgx.PreparedStatement
}
func (d *DatasourceExecuting) Run(ctx ExecutionContext, produce ProduceFn, metaSend MetaSendFn) error {
placeholderValues := make([]interface{}, len(d.placeholderExprs))
for i := range d.placeholderExprs {
value, err := d.placeholderExprs[i].Evaluate(ctx)
if err != nil {
return fmt.Errorf("couldn't evaluate pushed-down predicate placeholder expression: %w", err)
}
// TODO: Use internal function for this.
placeholderValues[i] = value.ToRawGoValue(d.placeholderExprTypes[i])
}
rows, err := d.db.QueryEx(ctx, d.stmt.SQL, nil, placeholderValues...)
if err != nil {
return fmt.Errorf("couldn't execute database query: %w", err)
}
for rows.Next() {
values, err := rows.Values()
if err != nil {
return fmt.Errorf("couldn't get row values: %w", err)
}
recordValues := make([]octosql.Value, len(values))
for i, value := range values {
switch value := value.(type) {
case int:
recordValues[i] = octosql.NewInt(value)
case int8:
recordValues[i] = octosql.NewInt(int(value))
case int16:
recordValues[i] = octosql.NewInt(int(value))
case int32:
recordValues[i] = octosql.NewInt(int(value))
case int64:
recordValues[i] = octosql.NewInt(int(value))
case uint8:
recordValues[i] = octosql.NewInt(int(value))
case uint16:
recordValues[i] = octosql.NewInt(int(value))
case uint32:
recordValues[i] = octosql.NewInt(int(value))
case uint64:
recordValues[i] = octosql.NewInt(int(value))
case bool:
recordValues[i] = octosql.NewBoolean(value)
case float32:
recordValues[i] = octosql.NewFloat(float64(value))
case float64:
recordValues[i] = octosql.NewFloat(value)
case string:
recordValues[i] = octosql.NewString(value)
case time.Time:
recordValues[i] = octosql.NewTime(value)
case nil:
recordValues[i] = octosql.NewNull()
case *pgtype.Numeric:
recordValues[i] = octosql.NewFloat(float64(value.Int.Int64()) * math.Pow10(int(value.Exp)))
case *pgtype.VarcharArray:
var strings []string
if err := value.AssignTo(&strings); err != nil {
log.Printf("couldn't decode varchar array: %s, setting null", err)
recordValues[i] = octosql.NewNull()
} else {
octoValues := make([]octosql.Value, len(strings))
for j := range strings {
octoValues[j] = octosql.NewString(strings[j])
}
recordValues[i] = octosql.NewList(octoValues)
}
case []byte:
// TODO: Create new datatype byte blob.
recordValues[i] = octosql.NewString(base64.StdEncoding.EncodeToString(value))
default:
log.Printf("unknown postgres value type, setting null: %T, %+v", value, value)
recordValues[i] = octosql.NewNull()
// TODO: Handle more types.
}
}
if err := produce(ProduceFromExecutionContext(ctx), NewRecord(recordValues, false, time.Time{})); err != nil {
return fmt.Errorf("couldn't produce record: %w", err)
}
}
return nil
}