-
Notifications
You must be signed in to change notification settings - Fork 100
/
table_columns.go
150 lines (135 loc) · 4.19 KB
/
table_columns.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
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
package queries
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"strings"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/drivers"
)
type TableColumns struct {
Connector string
Database string
DatabaseSchema string
TableName string
Result *runtimev1.TableColumnsResponse
}
var _ runtime.Query = &TableColumns{}
func (q *TableColumns) Key() string {
return fmt.Sprintf("TableColumns:%s", q.TableName)
}
func (q *TableColumns) Deps() []*runtimev1.ResourceName {
return []*runtimev1.ResourceName{
{Kind: runtime.ResourceKindSource, Name: q.TableName},
{Kind: runtime.ResourceKindModel, Name: q.TableName},
}
}
func (q *TableColumns) MarshalResult() *runtime.QueryResult {
var size int64
if len(q.Result.ProfileColumns) > 0 {
// approx
size = sizeProtoMessage(q.Result.ProfileColumns[0]) * int64(len(q.Result.ProfileColumns))
}
if len(q.Result.UnsupportedColumns) > 0 {
r, err := json.Marshal(q.Result.UnsupportedColumns)
if err == nil { // ignore error
size += int64(len(r))
}
}
return &runtime.QueryResult{
Value: q.Result,
Bytes: size,
}
}
func (q *TableColumns) UnmarshalResult(v any) error {
res, ok := v.(*runtimev1.TableColumnsResponse)
if !ok {
return fmt.Errorf("TableColumns: mismatched unmarshal input")
}
q.Result = res
return nil
}
func (q *TableColumns) Resolve(ctx context.Context, rt *runtime.Runtime, instanceID string, priority int) error {
olap, release, err := rt.OLAP(ctx, instanceID, q.Connector)
if err != nil {
return err
}
defer release()
switch olap.Dialect() {
case drivers.DialectDuckDB:
return olap.WithConnection(ctx, priority, false, false, func(ctx context.Context, ensuredCtx context.Context, _ *sql.Conn) error {
// views return duplicate column names, so we need to create a temporary table
temporaryTableName := tempName("profile_columns_")
err = olap.Exec(ctx, &drivers.Statement{
Query: fmt.Sprintf(`CREATE TEMPORARY TABLE "%s" AS (SELECT * FROM %s LIMIT 1)`, temporaryTableName, olap.Dialect().EscapeTable(q.Database, q.DatabaseSchema, q.TableName)),
Priority: priority,
ExecutionTimeout: defaultExecutionTimeout,
})
if err != nil {
return err
}
defer func() {
// NOTE: Using ensuredCtx
_ = olap.Exec(ensuredCtx, &drivers.Statement{
Query: `DROP TABLE "` + temporaryTableName + `"`,
Priority: priority,
ExecutionTimeout: defaultExecutionTimeout,
})
}()
rows, err := olap.Execute(ctx, &drivers.Statement{
Query: fmt.Sprintf(`
SELECT column_name AS name, data_type AS type
FROM information_schema.columns
WHERE table_catalog = 'temp' AND table_name = '%s'`, temporaryTableName),
Priority: priority,
ExecutionTimeout: defaultExecutionTimeout,
})
if err != nil {
return err
}
defer rows.Close()
var pcs []*runtimev1.ProfileColumn
i := 0
for rows.Next() {
pc := runtimev1.ProfileColumn{}
if err := rows.StructScan(&pc); err != nil {
return err
}
// TODO: Find a better way to handle this, this is ugly
if strings.Contains(pc.Type, "ENUM") {
pc.Type = "VARCHAR"
}
pcs = append(pcs, &pc)
i++
}
q.Result = &runtimev1.TableColumnsResponse{
ProfileColumns: pcs[0:i],
}
return nil
})
case drivers.DialectClickHouse, drivers.DialectDruid, drivers.DialectPinot:
tbl, err := olap.InformationSchema().Lookup(ctx, q.Database, q.DatabaseSchema, q.TableName)
if err != nil {
return err
}
q.Result = &runtimev1.TableColumnsResponse{
ProfileColumns: make([]*runtimev1.ProfileColumn, len(tbl.Schema.Fields)),
UnsupportedColumns: tbl.UnsupportedCols,
}
for i := 0; i < len(tbl.Schema.Fields); i++ {
q.Result.ProfileColumns[i] = &runtimev1.ProfileColumn{
Name: tbl.Schema.Fields[i].Name,
Type: tbl.Schema.Fields[i].Type.Code.String(),
}
}
return nil
default:
return fmt.Errorf("not available for dialect '%s'", olap.Dialect())
}
}
func (q *TableColumns) Export(ctx context.Context, rt *runtime.Runtime, instanceID string, w io.Writer, opts *runtime.ExportOptions) error {
return ErrExportNotSupported
}