-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
381 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package inspect | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/stoewer/parquet-cli/pkg/output" | ||
|
||
"github.com/parquet-go/parquet-go" | ||
) | ||
|
||
var headers = []any{ | ||
"Index", | ||
"Name", | ||
"Optional", | ||
"Repeated", | ||
"Required", | ||
"Is Leaf", | ||
"Type", | ||
"Go Type", | ||
"Encoding", | ||
"Compression", | ||
"Path", | ||
} | ||
|
||
type Schema struct { | ||
pf *parquet.File | ||
|
||
fields []fieldWithPath | ||
next int | ||
} | ||
|
||
func NewSchema(pf *parquet.File) *Schema { | ||
return &Schema{pf: pf} | ||
} | ||
|
||
func (s *Schema) Text() (string, error) { | ||
textRaw := s.pf.Schema().String() | ||
|
||
var text strings.Builder | ||
for _, r := range textRaw { | ||
if r == '\t' { | ||
text.WriteString(" ") | ||
} else { | ||
text.WriteRune(r) | ||
} | ||
} | ||
|
||
return text.String(), nil | ||
} | ||
|
||
func (s *Schema) Header() []any { | ||
return headers | ||
} | ||
|
||
func (s *Schema) NextRow() (output.TableRow, error) { | ||
if s.fields == nil { | ||
s.fields = fieldsFromSchema(s.pf.Schema()) | ||
} | ||
if s.next >= len(s.fields) { | ||
return nil, fmt.Errorf("no more fields: %w", io.EOF) | ||
} | ||
|
||
nextField := s.fields[s.next] | ||
s.next++ | ||
return toSchemaNode(&nextField), nil | ||
} | ||
|
||
func (s *Schema) NextSerializable() (any, error) { | ||
return s.NextRow() | ||
} | ||
|
||
func toSchemaNode(n *fieldWithPath) *schemaNode { | ||
sn := &schemaNode{ | ||
Index: n.Index, | ||
Name: n.Name(), | ||
Optional: n.Optional(), | ||
Repeated: n.Repeated(), | ||
Required: n.Required(), | ||
IsLeaf: n.Leaf(), | ||
} | ||
|
||
if n.Leaf() { | ||
sn.IsLeaf = true | ||
sn.Type = n.Type().String() | ||
sn.GoType = n.GoType().String() | ||
if n.Encoding() != nil { | ||
sn.Encoding = n.Encoding().String() | ||
} | ||
if n.Compression() != nil { | ||
sn.Compression = n.Compression().String() | ||
} | ||
} | ||
|
||
if len(n.Path) > 0 { | ||
sn.Path = strings.Join(n.Path, ".") | ||
sn.Name = PathToDisplayName(n.Path) | ||
} | ||
|
||
return sn | ||
} | ||
|
||
type schemaNode struct { | ||
Index int `json:"index,omitempty"` | ||
Name string `json:"name"` | ||
Optional bool `json:"optional"` | ||
Repeated bool `json:"repeated"` | ||
Required bool `json:"required"` | ||
IsLeaf bool `json:"is_leaf"` | ||
Type string `json:"type,omitempty"` | ||
GoType string `json:"go_type,omitempty"` | ||
Encoding string `json:"encoding,omitempty"` | ||
Compression string `json:"compression,omitempty"` | ||
Path string `json:"path,omitempty"` | ||
} | ||
|
||
func (sn *schemaNode) Cells() []any { | ||
return []any{ | ||
sn.Index, | ||
sn.Name, | ||
sn.Optional, | ||
sn.Repeated, | ||
sn.Required, | ||
sn.IsLeaf, | ||
sn.Type, | ||
sn.GoType, | ||
sn.Encoding, | ||
sn.Compression, | ||
sn.Path, | ||
} | ||
} | ||
|
||
type fieldWithPath struct { | ||
parquet.Field | ||
Path []string | ||
Index int | ||
} | ||
|
||
func fieldsFromSchema(schema *parquet.Schema) []fieldWithPath { | ||
result := make([]fieldWithPath, 0) | ||
for _, field := range schema.Fields() { | ||
result = fieldsFromPathRecursive(field, []string{}, result) | ||
} | ||
return result | ||
} | ||
|
||
func fieldsFromPathRecursive(field parquet.Field, path []string, result []fieldWithPath) []fieldWithPath { | ||
path = append(path, field.Name()) | ||
|
||
result = append(result, fieldWithPath{Field: field, Path: path}) | ||
for _, child := range field.Fields() { | ||
result = fieldsFromPathRecursive(child, path, result) | ||
} | ||
|
||
colIndex := 0 | ||
for i := range result { | ||
if result[i].Leaf() { | ||
result[i].Index = colIndex | ||
colIndex++ | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.