mithrandie / csvq

SQL-like query language for csv
https://mithrandie.github.io/csvq
MIT License
1.49k stars 65 forks source link

index out of range error when doing GROUP BY on empty table #107

Open nijssen opened 1 year ago

nijssen commented 1 year ago

If you have an empty table, and try to group by on it, then this error will occur. For example:

touch oops.csv # Maybe a previous step gave an empty output
csvq  'select a, max(b) from oops group by a'
[Fatal Error] runtime error: index out of range [-1]
An unexpected error has occurred. Please report this problem to: https://github.com/mithrandie/csvq/issues

Stack:
  0: github.com/mithrandie/csvq/lib/query.(*Processor).execute.func1 [github.com/mithrandie/csvq/lib/query/processor.go:95]
  1: runtime.gopanic [runtime/panic.go:884]
  2: runtime.goPanicIndex [runtime/panic.go:113]
  3: github.com/mithrandie/csvq/lib/query.(*View).group [github.com/mithrandie/csvq/lib/query/view.go:240]
  4: github.com/mithrandie/csvq/lib/query.(*View).GroupBy [github.com/mithrandie/csvq/lib/query/view.go:109]
  5: github.com/mithrandie/csvq/lib/query.selectEntity [github.com/mithrandie/csvq/lib/query/query.go:206]
  6: github.com/mithrandie/csvq/lib/query.Select [github.com/mithrandie/csvq/lib/query/query.go:122]
  7: github.com/mithrandie/csvq/lib/query.(*Processor).ExecuteStatement [github.com/mithrandie/csvq/lib/query/processor.go:197]
  8: github.com/mithrandie/csvq/lib/query.(*Processor).execute [github.com/mithrandie/csvq/lib/query/processor.go:103]
  9: github.com/mithrandie/csvq/lib/query.(*Processor).Execute [github.com/mithrandie/csvq/lib/query/processor.go:83]
  10: github.com/mithrandie/csvq/lib/action.Run [github.com/mithrandie/csvq/lib/action/run.go:60]
  11: github.com/mithrandie/csvq/lib/cli.Run.func5 [github.com/mithrandie/csvq/lib/cli/app.go:297]
  12: github.com/mithrandie/csvq/lib/cli.commandAction.func1 [github.com/mithrandie/csvq/lib/cli/app.go:373]
  13: github.com/urfave/cli/v2.(*App).RunContext [github.com/urfave/cli/v2@v2.10.3/app.go:350]
  14: github.com/urfave/cli/v2.(*App).Run [github.com/urfave/cli/v2@v2.10.3/app.go:247]
  15: github.com/mithrandie/csvq/lib/cli.Run [github.com/mithrandie/csvq/lib/cli/app.go:306]
  16: main.main [github.com/mithrandie/csvq/main.go:8]
  17: runtime.main [runtime/proc.go:250]
  18: runtime.goexit [runtime/asm_amd64.s:1594]

I guess the desired behavior isn't exactly clear: since the file is empty csvq cannot know the column names; on the other hand maybe we could add a flag to just assume any unknown columns are treated as null?

(NB: although I used csv in above example, my real use case is JSON; then the file would become just [] and the same problem occurs)

mithrandie commented 1 year ago

I checked the behavior. As you reported, specifying an non-existent column name in a grouping results a fatal error.

I also wanted a way to avoid raising errors when specifying a column that does not exist. I will consider your suggestion. Thank you.