-
Notifications
You must be signed in to change notification settings - Fork 70
Add query describe endpoint #5126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,26 @@ | ||
| package compiler | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/brimdata/zed/compiler/data" | ||
| "github.com/brimdata/zed/compiler/describe" | ||
| "github.com/brimdata/zed/compiler/parser" | ||
| "github.com/brimdata/zed/compiler/semantic" | ||
| "github.com/brimdata/zed/lakeparse" | ||
| ) | ||
|
|
||
| func Describe(ctx context.Context, query string, src *data.Source, head *lakeparse.Commitish) (*describe.Info, error) { | ||
| seq, sset, err := Parse(query) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| entry, err := semantic.AnalyzeAddSource(ctx, seq, src, head) | ||
| if err != nil { | ||
| if list, ok := err.(parser.ErrorList); ok { | ||
| list.SetSourceSet(sset) | ||
| } | ||
| return nil, err | ||
| } | ||
| return describe.Analyze(ctx, src, entry) | ||
| } |
This file contains hidden or 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,156 @@ | ||
| package describe | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/brimdata/zed/compiler/ast/dag" | ||
| "github.com/brimdata/zed/compiler/data" | ||
| "github.com/brimdata/zed/compiler/optimizer" | ||
| "github.com/brimdata/zed/lake" | ||
| "github.com/brimdata/zed/order" | ||
| "github.com/brimdata/zed/pkg/field" | ||
| "github.com/segmentio/ksuid" | ||
| ) | ||
|
|
||
| type Info struct { | ||
| Sources []Source `json:"sources"` | ||
| Channels []Channel `json:"channels"` | ||
| } | ||
|
|
||
| type Source interface { | ||
| Source() | ||
| } | ||
|
|
||
| type ( | ||
| LakeMeta struct { | ||
| Kind string `json:"kind"` | ||
| Meta string `json:"meta"` | ||
| } | ||
| Pool struct { | ||
| Kind string `json:"kind"` | ||
| Name string `json:"name"` | ||
| ID ksuid.KSUID `json:"id"` | ||
| } | ||
| Path struct { | ||
| Kind string `json:"kind"` | ||
| URI string `json:"uri"` | ||
| } | ||
| ) | ||
|
|
||
| func (*LakeMeta) Source() {} | ||
| func (*Pool) Source() {} | ||
| func (*Path) Source() {} | ||
|
|
||
| type Channel struct { | ||
| AggregationKeys field.List `json:"aggregation_keys"` | ||
| Sort *order.SortKey `json:"sort"` | ||
| } | ||
|
|
||
| func Analyze(ctx context.Context, source *data.Source, seq dag.Seq) (*Info, error) { | ||
| var info Info | ||
| var err error | ||
| if info.Sources, err = describeSources(ctx, source.Lake(), seq[0]); err != nil { | ||
| return nil, err | ||
| } | ||
| sortKeys, err := optimizer.New(ctx, source).SortKeys(seq) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| aggKeys := describeAggs(seq, []field.List{nil}) | ||
| for i := range sortKeys { | ||
| // Convert SortKey to a pointer so a nil sort is encoded as null for | ||
| // JSON/ZSON. | ||
| var s *order.SortKey | ||
| if !sortKeys[i].IsNil() { | ||
| s = &sortKeys[i] | ||
| } | ||
| info.Channels = append(info.Channels, Channel{ | ||
| Sort: s, | ||
| AggregationKeys: aggKeys[i], | ||
| }) | ||
| } | ||
| return &info, nil | ||
| } | ||
|
|
||
| func describeSources(ctx context.Context, lk *lake.Root, o dag.Op) ([]Source, error) { | ||
| switch o := o.(type) { | ||
| case *dag.Fork: | ||
| var s []Source | ||
| for _, p := range o.Paths { | ||
| out, err := describeSources(ctx, lk, p[0]) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| s = append(s, out...) | ||
| } | ||
| return s, nil | ||
| case *dag.DefaultScan: | ||
| return []Source{&Path{Kind: "Path", URI: "stdio://stdin"}}, nil | ||
| case *dag.FileScan: | ||
| return []Source{&Path{Kind: "Path", URI: o.Path}}, nil | ||
| case *dag.HTTPScan: | ||
| return []Source{&Path{Kind: "Path", URI: o.URL}}, nil | ||
| case *dag.PoolScan: | ||
| return sourceOfPool(ctx, lk, o.ID) | ||
| case *dag.Lister: | ||
| return sourceOfPool(ctx, lk, o.Pool) | ||
| case *dag.SeqScan: | ||
| return sourceOfPool(ctx, lk, o.Pool) | ||
| case *dag.CommitMetaScan: | ||
| return sourceOfPool(ctx, lk, o.Pool) | ||
| case *dag.LakeMetaScan: | ||
| return []Source{&LakeMeta{Kind: "LakeMeta", Meta: o.Meta}}, nil | ||
| default: | ||
| return nil, fmt.Errorf("unsupported source type %T", o) | ||
| } | ||
| } | ||
|
|
||
| func sourceOfPool(ctx context.Context, lk *lake.Root, id ksuid.KSUID) ([]Source, error) { | ||
| p, err := lk.OpenPool(ctx, id) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return []Source{&Pool{ | ||
| Kind: "Pool", | ||
| ID: id, | ||
| Name: p.Name, | ||
| }}, nil | ||
| } | ||
|
|
||
| func describeAggs(seq dag.Seq, parents []field.List) []field.List { | ||
| for _, op := range seq { | ||
| parents = describeOpAggs(op, parents) | ||
| } | ||
| return parents | ||
| } | ||
|
|
||
| func describeOpAggs(op dag.Op, parents []field.List) []field.List { | ||
| switch op := op.(type) { | ||
| case *dag.Fork: | ||
| var aggs []field.List | ||
| for _, p := range op.Paths { | ||
| aggs = append(aggs, describeAggs(p, []field.List{nil})...) | ||
| } | ||
| return aggs | ||
| case *dag.Scatter: | ||
| var aggs []field.List | ||
| for _, p := range op.Paths { | ||
| aggs = append(aggs, describeAggs(p, []field.List{nil})...) | ||
| } | ||
| return aggs | ||
| case *dag.Summarize: | ||
| // The field list for aggregation with no keys is an empty slice and | ||
| // not nil. | ||
| keys := field.List{} | ||
| for _, k := range op.Keys { | ||
| keys = append(keys, k.LHS.(*dag.This).Path) | ||
| } | ||
| return []field.List{keys} | ||
| } | ||
| // If more than one parent reset to nil aggregation. | ||
| if len(parents) > 1 { | ||
| return []field.List{nil} | ||
| } | ||
| return parents | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.