Skip to content

Commit

Permalink
select: Return null for non-exiting column indexes (minio#13196)
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost authored Sep 13, 2021
1 parent 3117183 commit 5a64003
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/s3select/csv/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"errors"
"fmt"
"io"
"strconv"
"strings"

"github.com/bcicen/jstream"
csv "github.com/minio/csvparser"
Expand All @@ -43,6 +45,20 @@ type Record struct {
func (r *Record) Get(name string) (*sql.Value, error) {
index, found := r.nameIndexMap[name]
if !found {
// Check if index.
if strings.HasPrefix(name, "_") {
idx, err := strconv.Atoi(strings.TrimPrefix(name, "_"))
if err != nil {
return nil, fmt.Errorf("column %v not found", name)
}
// The position count starts at 1.
idx--
if idx >= len(r.csvRecord) || idx < 0 {
// If field index > number of columns, return null
return sql.FromNull(), nil
}
return sql.FromBytes([]byte(r.csvRecord[idx])), nil
}
return nil, fmt.Errorf("column %v not found", name)
}

Expand Down
5 changes: 5 additions & 0 deletions internal/s3select/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,11 @@ func TestCSVQueries2(t *testing.T) {
query: `SELECT num2 from s3object s WHERE num2 = 0.765111`,
wantResult: `{"num2":" 0.765111"}`,
},
{
name: "select-non_exiting_values",
query: `SELECT _1 as first, s._100 from s3object s LIMIT 1`,
wantResult: `{"first":"1","_100":null}`,
},
}

defRequest := `<?xml version="1.0" encoding="UTF-8"?>
Expand Down

0 comments on commit 5a64003

Please sign in to comment.