Skip to content

Commit

Permalink
zq: Correct error message when no source is found
Browse files Browse the repository at this point in the history
This commit fixes an issue where the wrong error message was returned for a
single argument zq invocation where the argument is a valid Zed query but the
query contains no source.

Closes #5110
  • Loading branch information
mattnibs authored and nwt committed May 31, 2024
1 parent 0adda42 commit 9e91c3e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
26 changes: 17 additions & 9 deletions cli/queryflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,23 @@ func (f *Flags) ParseSourcesAndInputs(paths []string) ([]string, ast.Seq, *parse
// and appears to start with a from or yield operator.
// Otherwise, consider it a file.
query, sset, err := compiler.Parse(src, f.Includes...)
if err == nil {
if s, err := semantic.Analyze(context.Background(), query, data.NewSource(storage.NewLocalEngine(), nil), nil); err == nil {
if semantic.HasSource(s) {
return nil, query, sset, false, nil
}
if semantic.StartsWithYield(s) {
return nil, query, sset, true, nil
}
if err != nil {
return nil, nil, nil, false, singleArgError(src, err)
}
s, err := semantic.Analyze(context.Background(), query, data.NewSource(storage.NewLocalEngine(), nil), nil)
if err != nil {
if list, ok := err.(parser.ErrorList); ok {
list.SetSourceSet(sset)
}
return nil, nil, nil, false, err
}
if semantic.HasSource(s) {
return nil, query, sset, false, nil
}
if semantic.StartsWithYield(s) {
return nil, query, sset, true, nil
}
return nil, nil, nil, false, singleArgError(src, err)
return nil, nil, nil, false, singleArgError(src, nil)
}
}
query, sset, err := parser.ParseZed(f.Includes, src)
Expand Down Expand Up @@ -92,6 +98,8 @@ func singleArgError(src string, err error) error {
for _, line := range strings.Split(list.Error(), "\n") {
fmt.Fprintf(&b, "\n %s", line)
}
} else if err == nil {
b.WriteString("\n - the argument is a valid Zed query but does not begin with a source (e.g., \"file input.zson\") or yield operator")
} else {
b.WriteString("\n - the argument did not parse as a valid Zed query")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/zq/ztests/from-pool-error.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ script: |
outputs:
- name: stderr
data: |
zq: could not invoke zq with a single argument because:
- a file could not be found with the name "from ( pool a )"
- the argument did not parse as a valid Zed query
zq: "from pool" cannot be used without a lake at line 1, column 8:
from ( pool a )
~~~~~~
2 changes: 1 addition & 1 deletion cmd/zq/ztests/no-files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ outputs:
data: |
zq: could not invoke zq with a single argument because:
- a file could not be found with the name "doesnotexist"
- the argument did not parse as a valid Zed query
- the argument is a valid Zed query but does not begin with a source (e.g., "file input.zson") or yield operator
2 changes: 1 addition & 1 deletion compiler/semantic/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (a *analyzer) semSource(source ast.Source) []dag.Op {
}
case *ast.Pool:
if !a.source.IsLake() {
a.error(s, errors.New("from pool cannot be used without a lake"))
a.error(s, errors.New("\"from pool\" cannot be used without a lake"))
return []dag.Op{badOp()}
}
return a.semPool(s)
Expand Down
2 changes: 1 addition & 1 deletion compiler/ztests/from-error.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ script: |
outputs:
- name: stderr
data: |
from pool cannot be used without a lake at line 1, column 1:
"from pool" cannot be used without a lake at line 1, column 1:
from p
~~~~~~
===
Expand Down

0 comments on commit 9e91c3e

Please sign in to comment.