MCPcopy Create free account
hub / github.com/brimdata/super / mapColumns

Function mapColumns

compiler/semantic/sql.go:400–427  ·  view source on GitHub ↗
(sctx *super.Context, in *super.TypeRecord, alias *ast.TableAlias, seq sem.Seq)

Source from the content-addressed store, hash-verified

398}
399
400func mapColumns(sctx *super.Context, in *super.TypeRecord, alias *ast.TableAlias, seq sem.Seq) (sem.Seq, *staticTable, error) {
401 if len(alias.Columns) > len(in.Fields) {
402 return nil, nil, fmt.Errorf("cannot apply %d column aliases in table alias %q to table with %d columns", len(alias.Columns), alias.Name, len(in.Fields))
403 }
404 typ := in
405 out := idsToStrings(alias.Columns)
406 if !slices.EqualFunc(in.Fields, out, func(f super.Field, name string) bool {
407 return f.Name == name
408 }) {
409 // Make a record expression...
410 elems := make([]sem.RecordElem, 0, len(in.Fields))
411 fields := make([]super.Field, 0, len(in.Fields))
412 for k := range out {
413 elems = append(elems, &sem.FieldElem{
414 Node: alias.Columns[k],
415 Name: out[k],
416 Value: sem.NewThis(alias.Columns[k], []string{in.Fields[k].Name}),
417 })
418 fields = append(fields, super.NewField(out[k], in.Fields[k].Type))
419 }
420 seq = valuesExpr(&sem.RecordExpr{
421 Node: alias,
422 Elems: elems,
423 }, seq)
424 typ = sctx.MustLookupTypeRecord(fields)
425 }
426 return seq, &staticTable{table: alias.Name, typ: typ}, nil
427}
428
429func idsToStrings(ids []*ast.ID) []string {
430 out := make([]string, 0, len(ids))

Callers 1

applyAliasFunction · 0.85

Calls 5

NewThisFunction · 0.92
NewFieldFunction · 0.92
idsToStringsFunction · 0.85
valuesExprFunction · 0.85
MustLookupTypeRecordMethod · 0.80

Tested by

no test coverage detected