Skip to content

Commit fd64ee8

Browse files
authored
compiler/semantic: refactor sem.FileScan.Type initialization (#6291)
Initializing this field in translator.fileScanColumns doesn't make much sense. Remove that method and initialize the field in translator.semFile along with the rest of sem.FileScan.
1 parent dd65b93 commit fd64ee8

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

compiler/semantic/op.go

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package semantic
22

33
import (
4-
"context"
54
"errors"
65
"fmt"
76
"net/url"
@@ -101,8 +100,12 @@ func (t *translator) semFromEntity(entity ast.FromEntity, alias *ast.TableAlias,
101100
}
102101
op, def := t.semFromName(entity, entity.Text, args)
103102
if op, ok := op.(*sem.FileScan); ok {
104-
if cols, ok := t.fileScanColumns(op); ok {
105-
schema := schema(&staticSchema{def, cols})
103+
if rec := super.TypeRecordOf(op.Type); rec != nil {
104+
var columns []string
105+
for _, f := range rec.Fields {
106+
columns = append(columns, f.Name)
107+
}
108+
schema := schema(&staticSchema{def, columns})
106109
seq, schema, err := derefSchemaWithAlias(op, schema, alias, sem.Seq{op})
107110
if err != nil {
108111
t.error(alias, err)
@@ -145,33 +148,6 @@ func (t *translator) fromCTE(entity ast.FromEntity, c *ast.SQLCTE, alias *ast.Ta
145148
return seq, schema
146149
}
147150

148-
// XXX this should find a type from the schema rather than the columns
149-
func (t *translator) fileScanColumns(op *sem.FileScan) ([]string, bool) {
150-
if op.Format != "parquet" {
151-
return nil, false
152-
}
153-
uri, err := storage.ParseURI(op.Path)
154-
if err != nil {
155-
return nil, false
156-
}
157-
ctx, cancel := context.WithCancel(t.ctx)
158-
defer cancel()
159-
sr, err := t.env.Engine().Get(ctx, uri)
160-
if err != nil {
161-
return nil, false
162-
}
163-
defer sr.Close()
164-
op.Type = parquetio.Type(t.sctx, sr)
165-
if op.Type == nil {
166-
return nil, false
167-
}
168-
var cols []string
169-
for _, f := range op.Type.(*super.TypeRecord).Fields {
170-
cols = append(cols, f.Name)
171-
}
172-
return cols, true
173-
}
174-
175151
func (t *translator) semFromExpr(entity *ast.ExprEntity, args []ast.OpArg, seq sem.Seq) (sem.Seq, string) {
176152
expr := t.semExpr(entity.Expr)
177153
val, ok := t.maybeEval(expr)
@@ -260,13 +236,34 @@ func (t *translator) semFile(n ast.Node, name string, args []ast.OpArg) sem.Op {
260236
if format == "" {
261237
format = sio.FormatFromPath(name)
262238
}
239+
typ, err := t.fileType(name, format)
240+
if err != nil {
241+
t.error(n, err)
242+
}
263243
return &sem.FileScan{
264244
Node: n,
245+
Type: typ,
265246
Path: name,
266247
Format: format,
267248
}
268249
}
269250

251+
func (t *translator) fileType(path, format string) (super.Type, error) {
252+
if format != "parquet" {
253+
return nil, nil
254+
}
255+
uri, err := storage.ParseURI(path)
256+
if err != nil {
257+
return nil, err
258+
}
259+
r, err := t.env.Engine().Get(t.ctx, uri)
260+
if err != nil {
261+
return nil, err
262+
}
263+
defer r.Close()
264+
return parquetio.Type(t.sctx, r), nil
265+
}
266+
270267
func (t *translator) semFromFileGlob(globLoc ast.Node, pattern string, args []ast.OpArg) sem.Op {
271268
names, err := filepath.Glob(pattern)
272269
if err != nil {

0 commit comments

Comments
 (0)