2018-08-15 06:30:19 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2018 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package s3select
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-10-30 23:18:01 -04:00
|
|
|
"compress/bzip2"
|
|
|
|
"compress/gzip"
|
2018-08-15 06:30:19 -04:00
|
|
|
"io"
|
2018-10-22 15:12:22 -04:00
|
|
|
"net/http"
|
2018-08-15 06:30:19 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
"github.com/minio/minio/pkg/s3select/format"
|
|
|
|
"github.com/minio/minio/pkg/s3select/format/csv"
|
|
|
|
"github.com/minio/minio/pkg/s3select/format/json"
|
2018-10-30 23:18:01 -04:00
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
2018-08-15 06:30:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// progressTime is the time interval for which a progress message is sent.
|
|
|
|
progressTime time.Duration = 60 * time.Second
|
|
|
|
// continuationTime is the time interval for which a continuation message is
|
|
|
|
// sent.
|
|
|
|
continuationTime time.Duration = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
// ParseSelectTokens tokenizes the select query into required Columns, Alias, limit value
|
|
|
|
// where clause, aggregate functions, myFunctions, error.
|
|
|
|
type ParseSelectTokens struct {
|
|
|
|
reqCols []string
|
|
|
|
alias string
|
|
|
|
myLimit int64
|
|
|
|
whereClause interface{}
|
|
|
|
aggFunctionNames []string
|
|
|
|
myFuncs *SelectFuncs
|
|
|
|
myErr error
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
// Row is a Struct for keeping track of key aspects of a row.
|
|
|
|
type Row struct {
|
|
|
|
record string
|
|
|
|
err error
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
// This function replaces "",'' with `` for the select parser
|
|
|
|
func cleanExpr(expr string) string {
|
|
|
|
r := strings.NewReplacer("\"", "`", "'", "`")
|
|
|
|
return r.Replace(expr)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
// New - initialize new select format
|
2018-10-30 23:18:01 -04:00
|
|
|
func New(reader io.Reader, size int64, req ObjectSelectRequest) (s3s format.Select, err error) {
|
|
|
|
switch req.InputSerialization.CompressionType {
|
|
|
|
case SelectCompressionGZIP:
|
|
|
|
if reader, err = gzip.NewReader(reader); err != nil {
|
|
|
|
return nil, format.ErrTruncatedInput
|
|
|
|
}
|
|
|
|
case SelectCompressionBZIP:
|
|
|
|
reader = bzip2.NewReader(reader)
|
|
|
|
}
|
|
|
|
|
2018-10-22 15:12:22 -04:00
|
|
|
// Initializating options for CSV
|
|
|
|
if req.InputSerialization.CSV != nil {
|
|
|
|
if req.OutputSerialization.CSV.FieldDelimiter == "" {
|
|
|
|
req.OutputSerialization.CSV.FieldDelimiter = ","
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
if req.InputSerialization.CSV.FileHeaderInfo == "" {
|
|
|
|
req.InputSerialization.CSV.FileHeaderInfo = CSVFileHeaderInfoNone
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
if req.InputSerialization.CSV.RecordDelimiter == "" {
|
|
|
|
req.InputSerialization.CSV.RecordDelimiter = "\n"
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
s3s, err = csv.New(&csv.Options{
|
|
|
|
HasHeader: req.InputSerialization.CSV.FileHeaderInfo != CSVFileHeaderInfoNone,
|
|
|
|
RecordDelimiter: req.InputSerialization.CSV.RecordDelimiter,
|
|
|
|
FieldDelimiter: req.InputSerialization.CSV.FieldDelimiter,
|
|
|
|
Comments: req.InputSerialization.CSV.Comments,
|
|
|
|
Name: "S3Object", // Default table name for all objects
|
2018-10-30 23:18:01 -04:00
|
|
|
ReadFrom: reader,
|
2018-10-22 15:12:22 -04:00
|
|
|
Compressed: string(req.InputSerialization.CompressionType),
|
|
|
|
Expression: cleanExpr(req.Expression),
|
|
|
|
OutputFieldDelimiter: req.OutputSerialization.CSV.FieldDelimiter,
|
|
|
|
StreamSize: size,
|
|
|
|
HeaderOpt: req.InputSerialization.CSV.FileHeaderInfo == CSVFileHeaderInfoUse,
|
|
|
|
Progress: req.RequestProgress.Enabled,
|
|
|
|
})
|
|
|
|
} else if req.InputSerialization.JSON != nil {
|
|
|
|
// Initializating options for JSON
|
|
|
|
s3s, err = json.New(&json.Options{
|
|
|
|
Name: "S3Object", // Default table name for all objects
|
2018-10-30 23:18:01 -04:00
|
|
|
ReadFrom: reader,
|
2018-10-22 15:12:22 -04:00
|
|
|
Compressed: string(req.InputSerialization.CompressionType),
|
|
|
|
Expression: cleanExpr(req.Expression),
|
|
|
|
StreamSize: size,
|
|
|
|
Type: req.InputSerialization.JSON.Type == JSONTypeDocument,
|
|
|
|
Progress: req.RequestProgress.Enabled,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return s3s, err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute is the function where all the blocking occurs, It writes to the HTTP
|
|
|
|
// response writer in a streaming fashion so that the client can actively use
|
|
|
|
// the results before the query is finally finished executing. The
|
2018-10-22 15:12:22 -04:00
|
|
|
func Execute(writer io.Writer, f format.Select) error {
|
2018-10-30 23:18:01 -04:00
|
|
|
myRow := make(chan Row, 1000)
|
|
|
|
curBuf := bytes.NewBuffer(make([]byte, humanize.MiByte))
|
2018-08-15 06:30:19 -04:00
|
|
|
curBuf.Reset()
|
|
|
|
progressTicker := time.NewTicker(progressTime)
|
|
|
|
continuationTimer := time.NewTimer(continuationTime)
|
|
|
|
defer progressTicker.Stop()
|
|
|
|
defer continuationTimer.Stop()
|
2018-10-22 15:12:22 -04:00
|
|
|
|
|
|
|
go runSelectParser(f, myRow)
|
2018-08-15 06:30:19 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case row, ok := <-myRow:
|
|
|
|
if ok && row.err != nil {
|
2018-10-30 23:18:01 -04:00
|
|
|
_, err := writeErrorMessage(row.err, curBuf).WriteTo(writer)
|
2018-08-15 06:30:19 -04:00
|
|
|
flusher, okFlush := writer.(http.Flusher)
|
|
|
|
if okFlush {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
curBuf.Reset()
|
|
|
|
close(myRow)
|
|
|
|
return nil
|
|
|
|
} else if ok {
|
2018-10-30 23:18:01 -04:00
|
|
|
_, err := writeRecordMessage(row.record, curBuf).WriteTo(writer)
|
2018-08-15 06:30:19 -04:00
|
|
|
flusher, okFlush := writer.(http.Flusher)
|
|
|
|
if okFlush {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
curBuf.Reset()
|
2018-10-22 15:12:22 -04:00
|
|
|
f.UpdateBytesReturned(int64(len(row.record)))
|
2018-08-15 06:30:19 -04:00
|
|
|
if !continuationTimer.Stop() {
|
|
|
|
<-continuationTimer.C
|
|
|
|
}
|
|
|
|
continuationTimer.Reset(continuationTime)
|
|
|
|
} else if !ok {
|
2018-10-22 15:12:22 -04:00
|
|
|
statPayload, err := f.CreateStatXML()
|
2018-08-15 06:30:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-30 23:18:01 -04:00
|
|
|
_, err = writeStatMessage(statPayload, curBuf).WriteTo(writer)
|
2018-08-15 06:30:19 -04:00
|
|
|
flusher, ok := writer.(http.Flusher)
|
|
|
|
if ok {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
curBuf.Reset()
|
2018-10-30 23:18:01 -04:00
|
|
|
_, err = writeEndMessage(curBuf).WriteTo(writer)
|
2018-08-15 06:30:19 -04:00
|
|
|
flusher, ok = writer.(http.Flusher)
|
|
|
|
if ok {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-progressTicker.C:
|
2018-09-17 02:22:46 -04:00
|
|
|
// Send progress messages only if requested by client.
|
2018-10-22 15:12:22 -04:00
|
|
|
if f.Progress() {
|
|
|
|
progressPayload, err := f.CreateProgressXML()
|
2018-09-17 02:22:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-30 23:18:01 -04:00
|
|
|
_, err = writeProgressMessage(progressPayload, curBuf).WriteTo(writer)
|
2018-09-17 02:22:46 -04:00
|
|
|
flusher, ok := writer.(http.Flusher)
|
|
|
|
if ok {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
curBuf.Reset()
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
case <-continuationTimer.C:
|
2018-10-30 23:18:01 -04:00
|
|
|
_, err := writeContinuationMessage(curBuf).WriteTo(writer)
|
2018-08-15 06:30:19 -04:00
|
|
|
flusher, ok := writer.(http.Flusher)
|
|
|
|
if ok {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
curBuf.Reset()
|
|
|
|
continuationTimer.Reset(continuationTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|