-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend_spark.go
More file actions
162 lines (147 loc) · 4.34 KB
/
Copy pathbackend_spark.go
File metadata and controls
162 lines (147 loc) · 4.34 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//go:build spark
// sparkBackend is the MobilitySpark (Apache Spark) engine over Spark Connect,
// selected by a spark:// DSN and built only under -tags spark. The same Go
// response assembly and the same canonical SQL serve it; per-engine the SQL is
// translated to the Spark idiom (dialect_spark.go): canonical function names
// remap to their MobilitySpark camelCase names and $N placeholders inline,
// since Spark Connect's Sql takes no bind parameters. Spark Connect has no
// transactions, so Begin returns a pass-through.
package main
import (
"context"
"reflect"
"strings"
"github.com/apache/spark-connect-go/v35/spark/sql"
"github.com/apache/spark-connect-go/v35/spark/sql/types"
)
func openSpark(dsn string) (Backend, error) {
remote := "sc://" + strings.TrimPrefix(dsn, "spark://")
s, err := sql.NewSessionBuilder().Remote(remote).Build(context.Background())
if err != nil {
return nil, err
}
return &sparkBackend{s}, nil
}
type sparkBackend struct{ s sql.SparkSession }
func (b *sparkBackend) prep(q string, a []any) string { return inlineParams(rewriteSparkSQL(q), a) }
func (b *sparkBackend) Query(ctx context.Context, q string, a ...any) (Rows, error) {
df, err := b.s.Sql(ctx, b.prep(q, a))
if err != nil {
return nil, err
}
rs, err := df.Collect(ctx)
if err != nil {
return nil, err
}
return &sparkRows{rows: rs, i: -1}, nil
}
func (b *sparkBackend) QueryRow(ctx context.Context, q string, a ...any) Row {
rows, err := b.Query(ctx, q, a...)
return sparkRow{rows: rows, err: err}
}
func (b *sparkBackend) Exec(ctx context.Context, q string, a ...any) (int64, error) {
df, err := b.s.Sql(ctx, b.prep(q, a))
if err != nil {
return 0, err
}
_, err = df.Collect(ctx) // force execution
return 0, err
}
func (b *sparkBackend) Begin(ctx context.Context) (Tx, error) { return sparkTx{b}, nil }
func (b *sparkBackend) Ping(ctx context.Context) error {
_, err := b.s.Sql(ctx, "SELECT 1")
return err
}
func (b *sparkBackend) Close() { b.s.Stop() }
type sparkRows struct {
rows []types.Row
i int
}
func (r *sparkRows) Next() bool { r.i++; return r.i < len(r.rows) }
func (r *sparkRows) Scan(d ...any) error { return scanSparkRow(r.rows[r.i], d) }
func (r *sparkRows) Close() {}
func (r *sparkRows) Err() error { return nil }
type sparkRow struct {
rows Rows
err error
}
func (r sparkRow) Scan(dest ...any) error {
if r.err != nil {
return r.err
}
if !r.rows.Next() {
return ErrNoRows
}
return r.rows.Scan(dest...)
}
// sparkTx is a pass-through: Spark Connect has no transactions.
type sparkTx struct{ b *sparkBackend }
func (t sparkTx) Exec(ctx context.Context, q string, a ...any) (int64, error) {
return t.b.Exec(ctx, q, a...)
}
func (t sparkTx) Query(ctx context.Context, q string, a ...any) (Rows, error) {
return t.b.Query(ctx, q, a...)
}
func (t sparkTx) QueryRow(ctx context.Context, q string, a ...any) Row {
return t.b.QueryRow(ctx, q, a...)
}
func (t sparkTx) Commit(ctx context.Context) error { return nil }
func (t sparkTx) Rollback(ctx context.Context) error { return nil }
func scanSparkRow(row types.Row, dest []any) error {
vals := row.Values()
for i, d := range dest {
var v any
if i < len(vals) {
v = vals[i]
}
assignSpark(d, v)
}
return nil
}
// assignSpark sets the scan destination *T (or **T for nullable columns) from a
// Spark value, converting across the numeric/string widths Spark returns.
func assignSpark(dest, v any) {
rv := reflect.ValueOf(dest)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return
}
elem := rv.Elem()
if elem.Kind() == reflect.Ptr { // **T nullable
if v == nil {
elem.Set(reflect.Zero(elem.Type()))
return
}
p := reflect.New(elem.Type().Elem())
assignSpark(p.Interface(), v)
elem.Set(p)
return
}
if v == nil {
return
}
src := reflect.ValueOf(v)
switch elem.Kind() {
case reflect.String:
if src.Kind() == reflect.String {
elem.SetString(src.String())
}
case reflect.Int, reflect.Int32, reflect.Int64:
if src.CanInt() {
elem.SetInt(src.Int())
} else if src.CanFloat() {
elem.SetInt(int64(src.Float()))
}
case reflect.Float32, reflect.Float64:
if src.CanFloat() {
elem.SetFloat(src.Float())
} else if src.CanInt() {
elem.SetFloat(float64(src.Int()))
}
case reflect.Slice: // []byte
if b, ok := v.([]byte); ok {
elem.SetBytes(b)
} else if s, ok := v.(string); ok {
elem.SetBytes([]byte(s))
}
}
}