2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-15 06:30:19 -04:00
|
|
|
|
|
|
|
package s3select
|
|
|
|
|
|
|
|
import (
|
2019-08-31 03:07:40 -04:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2020-03-05 22:34:04 -05:00
|
|
|
"compress/bzip2"
|
2019-01-08 19:53:04 -05:00
|
|
|
"encoding/xml"
|
2020-03-05 22:34:04 -05:00
|
|
|
"errors"
|
2019-01-08 19:53:04 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-08-31 03:07:40 -04:00
|
|
|
"io/ioutil"
|
2019-01-08 19:53:04 -05:00
|
|
|
"net/http"
|
2020-08-18 13:23:28 -04:00
|
|
|
"os"
|
2018-08-15 06:30:19 -04:00
|
|
|
"strings"
|
2019-08-31 03:07:40 -04:00
|
|
|
"sync"
|
2018-08-15 06:30:19 -04:00
|
|
|
|
2021-09-06 12:09:53 -04:00
|
|
|
"github.com/klauspost/compress/s2"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
|
|
gzip "github.com/klauspost/pgzip"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/s3select/csv"
|
|
|
|
"github.com/minio/minio/internal/s3select/json"
|
|
|
|
"github.com/minio/minio/internal/s3select/parquet"
|
|
|
|
"github.com/minio/minio/internal/s3select/simdj"
|
|
|
|
"github.com/minio/minio/internal/s3select/sql"
|
2020-02-13 17:03:52 -05:00
|
|
|
"github.com/minio/simdjson-go"
|
2021-09-06 12:09:53 -04:00
|
|
|
"github.com/pierrec/lz4"
|
2018-08-15 06:30:19 -04:00
|
|
|
)
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
type recordReader interface {
|
2019-09-13 17:18:35 -04:00
|
|
|
// Read a record.
|
|
|
|
// dst is optional but will be used if valid.
|
|
|
|
Read(dst sql.Record) (sql.Record, error)
|
2019-01-08 19:53:04 -05:00
|
|
|
Close() error
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
const (
|
|
|
|
csvFormat = "csv"
|
|
|
|
jsonFormat = "json"
|
|
|
|
parquetFormat = "parquet"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CompressionType - represents value inside <CompressionType/> in request XML.
|
|
|
|
type CompressionType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
noneType CompressionType = "none"
|
2021-09-06 12:09:53 -04:00
|
|
|
gzipType CompressionType = "GZIP"
|
|
|
|
bzip2Type CompressionType = "BZIP2"
|
|
|
|
|
|
|
|
zstdType CompressionType = "ZSTD"
|
|
|
|
lz4Type CompressionType = "LZ4"
|
|
|
|
s2Type CompressionType = "S2"
|
|
|
|
snappyType CompressionType = "SNAPPY"
|
2019-01-08 19:53:04 -05:00
|
|
|
)
|
|
|
|
|
2019-01-28 23:00:18 -05:00
|
|
|
const (
|
|
|
|
maxRecordSize = 1 << 20 // 1 MiB
|
|
|
|
)
|
|
|
|
|
2019-08-31 03:07:40 -04:00
|
|
|
var bufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
2019-09-16 20:26:27 -04:00
|
|
|
// make a buffer with a reasonable capacity.
|
|
|
|
return bytes.NewBuffer(make([]byte, 0, maxRecordSize))
|
2019-08-31 03:07:40 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var bufioWriterPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
// ioutil.Discard is just used to create the writer. Actual destination
|
|
|
|
// writer is set later by Reset() before using it.
|
|
|
|
return bufio.NewWriter(ioutil.Discard)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// UnmarshalXML - decodes XML data.
|
|
|
|
func (c *CompressionType) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
var s string
|
|
|
|
if err := d.DecodeElement(&s, &start); err != nil {
|
|
|
|
return errMalformedXML(err)
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:09:53 -04:00
|
|
|
parsedType := CompressionType(strings.ToUpper(s))
|
|
|
|
if s == "" || parsedType == "NONE" {
|
2019-01-08 19:53:04 -05:00
|
|
|
parsedType = noneType
|
|
|
|
}
|
|
|
|
|
|
|
|
switch parsedType {
|
2021-09-06 12:09:53 -04:00
|
|
|
case noneType, gzipType, bzip2Type, snappyType, s2Type, zstdType, lz4Type:
|
2019-01-08 19:53:04 -05:00
|
|
|
default:
|
|
|
|
return errInvalidCompressionFormat(fmt.Errorf("invalid compression format '%v'", s))
|
|
|
|
}
|
|
|
|
|
|
|
|
*c = parsedType
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputSerialization - represents elements inside <InputSerialization/> in request XML.
|
|
|
|
type InputSerialization struct {
|
|
|
|
CompressionType CompressionType `xml:"CompressionType"`
|
|
|
|
CSVArgs csv.ReaderArgs `xml:"CSV"`
|
|
|
|
JSONArgs json.ReaderArgs `xml:"JSON"`
|
|
|
|
ParquetArgs parquet.ReaderArgs `xml:"Parquet"`
|
|
|
|
unmarshaled bool
|
|
|
|
format string
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty - returns whether input serialization is empty or not.
|
|
|
|
func (input *InputSerialization) IsEmpty() bool {
|
|
|
|
return !input.unmarshaled
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalXML - decodes XML data.
|
|
|
|
func (input *InputSerialization) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
// Make subtype to avoid recursive UnmarshalXML().
|
|
|
|
type subInputSerialization InputSerialization
|
|
|
|
parsedInput := subInputSerialization{}
|
|
|
|
if err := d.DecodeElement(&parsedInput, &start); err != nil {
|
|
|
|
return errMalformedXML(err)
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:25:52 -05:00
|
|
|
// If no compression is specified, set to noneType
|
2021-09-06 12:09:53 -04:00
|
|
|
if parsedInput.CompressionType == "" {
|
2019-02-06 16:25:52 -05:00
|
|
|
parsedInput.CompressionType = noneType
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
found := 0
|
|
|
|
if !parsedInput.CSVArgs.IsEmpty() {
|
|
|
|
parsedInput.format = csvFormat
|
|
|
|
found++
|
|
|
|
}
|
|
|
|
if !parsedInput.JSONArgs.IsEmpty() {
|
|
|
|
parsedInput.format = jsonFormat
|
|
|
|
found++
|
|
|
|
}
|
|
|
|
if !parsedInput.ParquetArgs.IsEmpty() {
|
2019-01-28 20:59:48 -05:00
|
|
|
if parsedInput.CompressionType != "" && parsedInput.CompressionType != noneType {
|
2019-01-08 19:53:04 -05:00
|
|
|
return errInvalidRequestParameter(fmt.Errorf("CompressionType must be NONE for Parquet format"))
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
parsedInput.format = parquetFormat
|
|
|
|
found++
|
|
|
|
}
|
|
|
|
|
|
|
|
if found != 1 {
|
|
|
|
return errInvalidDataSource(nil)
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
*input = InputSerialization(parsedInput)
|
|
|
|
input.unmarshaled = true
|
|
|
|
return nil
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// OutputSerialization - represents elements inside <OutputSerialization/> in request XML.
|
|
|
|
type OutputSerialization struct {
|
|
|
|
CSVArgs csv.WriterArgs `xml:"CSV"`
|
|
|
|
JSONArgs json.WriterArgs `xml:"JSON"`
|
|
|
|
unmarshaled bool
|
|
|
|
format string
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty - returns whether output serialization is empty or not.
|
|
|
|
func (output *OutputSerialization) IsEmpty() bool {
|
|
|
|
return !output.unmarshaled
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalXML - decodes XML data.
|
|
|
|
func (output *OutputSerialization) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
// Make subtype to avoid recursive UnmarshalXML().
|
|
|
|
type subOutputSerialization OutputSerialization
|
|
|
|
parsedOutput := subOutputSerialization{}
|
|
|
|
if err := d.DecodeElement(&parsedOutput, &start); err != nil {
|
|
|
|
return errMalformedXML(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
found := 0
|
|
|
|
if !parsedOutput.CSVArgs.IsEmpty() {
|
|
|
|
parsedOutput.format = csvFormat
|
|
|
|
found++
|
|
|
|
}
|
|
|
|
if !parsedOutput.JSONArgs.IsEmpty() {
|
|
|
|
parsedOutput.format = jsonFormat
|
|
|
|
found++
|
|
|
|
}
|
|
|
|
if found != 1 {
|
|
|
|
return errObjectSerializationConflict(fmt.Errorf("either CSV or JSON should be present in OutputSerialization"))
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
*output = OutputSerialization(parsedOutput)
|
|
|
|
output.unmarshaled = true
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-15 06:30:19 -04:00
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// RequestProgress - represents elements inside <RequestProgress/> in request XML.
|
|
|
|
type RequestProgress struct {
|
|
|
|
Enabled bool `xml:"Enabled"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// S3Select - filters the contents on a simple structured query language (SQL) statement. It
|
2019-02-06 16:25:52 -05:00
|
|
|
// represents elements inside <SelectRequest/> in request XML specified in detail at
|
2019-01-08 19:53:04 -05:00
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectSELECTContent.html.
|
|
|
|
type S3Select struct {
|
2019-02-06 16:25:52 -05:00
|
|
|
XMLName xml.Name `xml:"SelectRequest"`
|
2019-01-08 19:53:04 -05:00
|
|
|
Expression string `xml:"Expression"`
|
|
|
|
ExpressionType string `xml:"ExpressionType"`
|
|
|
|
Input InputSerialization `xml:"InputSerialization"`
|
|
|
|
Output OutputSerialization `xml:"OutputSerialization"`
|
|
|
|
Progress RequestProgress `xml:"RequestProgress"`
|
|
|
|
|
2019-01-28 20:59:48 -05:00
|
|
|
statement *sql.SelectStatement
|
2019-01-08 19:53:04 -05:00
|
|
|
progressReader *progressReader
|
|
|
|
recordReader recordReader
|
2021-04-07 16:29:27 -04:00
|
|
|
close func() error
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
2019-02-06 16:25:52 -05:00
|
|
|
var (
|
|
|
|
legacyXMLName = "SelectObjectContentRequest"
|
|
|
|
)
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// UnmarshalXML - decodes XML data.
|
|
|
|
func (s3Select *S3Select) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
2019-02-06 16:25:52 -05:00
|
|
|
// S3 also supports the older SelectObjectContentRequest tag,
|
|
|
|
// though it is no longer found in documentation. This is
|
|
|
|
// checked and renamed below to allow older clients to also
|
|
|
|
// work.
|
|
|
|
if start.Name.Local == legacyXMLName {
|
|
|
|
start.Name = xml.Name{Space: "", Local: "SelectRequest"}
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// Make subtype to avoid recursive UnmarshalXML().
|
|
|
|
type subS3Select S3Select
|
|
|
|
parsedS3Select := subS3Select{}
|
|
|
|
if err := d.DecodeElement(&parsedS3Select, &start); err != nil {
|
|
|
|
if _, ok := err.(*s3Error); ok {
|
|
|
|
return err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
return errMalformedXML(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedS3Select.ExpressionType = strings.ToLower(parsedS3Select.ExpressionType)
|
|
|
|
if parsedS3Select.ExpressionType != "sql" {
|
|
|
|
return errInvalidExpressionType(fmt.Errorf("invalid expression type '%v'", parsedS3Select.ExpressionType))
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedS3Select.Input.IsEmpty() {
|
|
|
|
return errMissingRequiredParameter(fmt.Errorf("InputSerialization must be provided"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedS3Select.Output.IsEmpty() {
|
|
|
|
return errMissingRequiredParameter(fmt.Errorf("OutputSerialization must be provided"))
|
|
|
|
}
|
|
|
|
|
2019-01-28 20:59:48 -05:00
|
|
|
statement, err := sql.ParseSelectStatement(parsedS3Select.Expression)
|
2019-01-08 19:53:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-28 20:59:48 -05:00
|
|
|
parsedS3Select.statement = &statement
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
*s3Select = S3Select(parsedS3Select)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3Select *S3Select) outputRecord() sql.Record {
|
|
|
|
switch s3Select.Output.format {
|
|
|
|
case csvFormat:
|
|
|
|
return csv.NewRecord()
|
|
|
|
case jsonFormat:
|
2019-03-09 11:13:37 -05:00
|
|
|
return json.NewRecord(sql.SelectFmtJSON)
|
2019-01-08 19:53:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Errorf("unknown output format '%v'", s3Select.Output.format))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3Select *S3Select) getProgress() (bytesScanned, bytesProcessed int64) {
|
|
|
|
if s3Select.progressReader != nil {
|
|
|
|
return s3Select.progressReader.Stats()
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open - opens S3 object by using callback for SQL selection query.
|
|
|
|
// Currently CSV, JSON and Apache Parquet formats are supported.
|
|
|
|
func (s3Select *S3Select) Open(getReader func(offset, length int64) (io.ReadCloser, error)) error {
|
|
|
|
switch s3Select.Input.format {
|
|
|
|
case csvFormat:
|
|
|
|
rc, err := getReader(0, -1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
s3Select.progressReader, err = newProgressReader(rc, s3Select.Input.CompressionType)
|
|
|
|
if err != nil {
|
2019-06-25 16:30:48 -04:00
|
|
|
rc.Close()
|
2019-01-08 19:53:04 -05:00
|
|
|
return err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
s3Select.recordReader, err = csv.NewReader(s3Select.progressReader, &s3Select.Input.CSVArgs)
|
|
|
|
if err != nil {
|
2019-06-25 16:30:48 -04:00
|
|
|
rc.Close()
|
2020-03-05 22:34:04 -05:00
|
|
|
var stErr bzip2.StructuralError
|
|
|
|
if errors.As(err, &stErr) {
|
2021-09-06 12:09:53 -04:00
|
|
|
return errInvalidCompression(err, s3Select.Input.CompressionType)
|
|
|
|
}
|
|
|
|
// Test these compressor errors
|
|
|
|
errs := []error{
|
|
|
|
gzip.ErrHeader, gzip.ErrChecksum,
|
|
|
|
s2.ErrCorrupt, s2.ErrUnsupported, s2.ErrCRC,
|
|
|
|
zstd.ErrBlockTooSmall, zstd.ErrMagicMismatch, zstd.ErrWindowSizeExceeded, zstd.ErrUnknownDictionary, zstd.ErrWindowSizeTooSmall,
|
|
|
|
lz4.ErrInvalid, lz4.ErrBlockDependency,
|
|
|
|
}
|
|
|
|
for _, e := range errs {
|
|
|
|
if errors.Is(err, e) {
|
|
|
|
return errInvalidCompression(err, s3Select.Input.CompressionType)
|
|
|
|
}
|
2020-03-05 22:34:04 -05:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
return err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2021-04-07 16:29:27 -04:00
|
|
|
s3Select.close = rc.Close
|
2019-01-08 19:53:04 -05:00
|
|
|
return nil
|
|
|
|
case jsonFormat:
|
|
|
|
rc, err := getReader(0, -1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
s3Select.progressReader, err = newProgressReader(rc, s3Select.Input.CompressionType)
|
|
|
|
if err != nil {
|
2019-06-25 16:30:48 -04:00
|
|
|
rc.Close()
|
2019-01-08 19:53:04 -05:00
|
|
|
return err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
2019-12-09 09:55:31 -05:00
|
|
|
if strings.EqualFold(s3Select.Input.JSONArgs.ContentType, "lines") {
|
2020-02-13 17:03:52 -05:00
|
|
|
if simdjson.SupportedCPU() {
|
|
|
|
s3Select.recordReader = simdj.NewReader(s3Select.progressReader, &s3Select.Input.JSONArgs)
|
|
|
|
} else {
|
|
|
|
s3Select.recordReader = json.NewPReader(s3Select.progressReader, &s3Select.Input.JSONArgs)
|
|
|
|
}
|
2019-12-09 09:55:31 -05:00
|
|
|
} else {
|
|
|
|
s3Select.recordReader = json.NewReader(s3Select.progressReader, &s3Select.Input.JSONArgs)
|
|
|
|
}
|
2021-04-07 16:29:27 -04:00
|
|
|
|
|
|
|
s3Select.close = rc.Close
|
2019-01-08 19:53:04 -05:00
|
|
|
return nil
|
|
|
|
case parquetFormat:
|
2020-08-18 13:23:28 -04:00
|
|
|
if !strings.EqualFold(os.Getenv("MINIO_API_SELECT_PARQUET"), "on") {
|
|
|
|
return errors.New("parquet format parsing not enabled on server")
|
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
var err error
|
|
|
|
s3Select.recordReader, err = parquet.NewReader(getReader, &s3Select.Input.ParquetArgs)
|
|
|
|
return err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
panic(fmt.Errorf("unknown input format '%v'", s3Select.Input.format))
|
2018-11-14 18:55:10 -05:00
|
|
|
}
|
|
|
|
|
2019-08-31 03:07:40 -04:00
|
|
|
func (s3Select *S3Select) marshal(buf *bytes.Buffer, record sql.Record) error {
|
2019-01-08 19:53:04 -05:00
|
|
|
switch s3Select.Output.format {
|
|
|
|
case csvFormat:
|
2019-08-31 03:07:40 -04:00
|
|
|
// Use bufio Writer to prevent csv.Writer from allocating a new buffer.
|
|
|
|
bufioWriter := bufioWriterPool.Get().(*bufio.Writer)
|
|
|
|
defer func() {
|
|
|
|
bufioWriter.Reset(ioutil.Discard)
|
|
|
|
bufioWriterPool.Put(bufioWriter)
|
|
|
|
}()
|
|
|
|
|
|
|
|
bufioWriter.Reset(buf)
|
2020-04-01 18:39:34 -04:00
|
|
|
opts := sql.WriteCSVOpts{
|
|
|
|
FieldDelimiter: []rune(s3Select.Output.CSVArgs.FieldDelimiter)[0],
|
|
|
|
Quote: []rune(s3Select.Output.CSVArgs.QuoteCharacter)[0],
|
|
|
|
QuoteEscape: []rune(s3Select.Output.CSVArgs.QuoteEscapeCharacter)[0],
|
|
|
|
AlwaysQuote: strings.ToLower(s3Select.Output.CSVArgs.QuoteFields) == "always",
|
|
|
|
}
|
|
|
|
err := record.WriteCSV(bufioWriter, opts)
|
2018-11-14 18:55:10 -05:00
|
|
|
if err != nil {
|
2019-08-31 03:07:40 -04:00
|
|
|
return err
|
2018-11-14 18:55:10 -05:00
|
|
|
}
|
2019-09-16 20:26:27 -04:00
|
|
|
err = bufioWriter.Flush()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-13 17:03:52 -05:00
|
|
|
if buf.Bytes()[buf.Len()-1] == '\n' {
|
|
|
|
buf.Truncate(buf.Len() - 1)
|
|
|
|
}
|
2019-08-31 03:07:40 -04:00
|
|
|
buf.WriteString(s3Select.Output.CSVArgs.RecordDelimiter)
|
|
|
|
|
|
|
|
return nil
|
2019-01-08 19:53:04 -05:00
|
|
|
case jsonFormat:
|
2019-08-31 03:07:40 -04:00
|
|
|
err := record.WriteJSON(buf)
|
2019-01-08 19:53:04 -05:00
|
|
|
if err != nil {
|
2019-08-31 03:07:40 -04:00
|
|
|
return err
|
2018-11-14 18:55:10 -05:00
|
|
|
}
|
2020-02-13 17:03:52 -05:00
|
|
|
// Trim trailing newline from non-simd output
|
|
|
|
if buf.Bytes()[buf.Len()-1] == '\n' {
|
|
|
|
buf.Truncate(buf.Len() - 1)
|
|
|
|
}
|
2019-08-31 03:07:40 -04:00
|
|
|
buf.WriteString(s3Select.Output.JSONArgs.RecordDelimiter)
|
|
|
|
|
|
|
|
return nil
|
2018-11-14 18:55:10 -05:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
panic(fmt.Errorf("unknown output format '%v'", s3Select.Output.format))
|
2018-11-14 18:55:10 -05:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// Evaluate - filters and sends records read from opened reader as per select statement to http response writer.
|
|
|
|
func (s3Select *S3Select) Evaluate(w http.ResponseWriter) {
|
2021-04-07 16:29:27 -04:00
|
|
|
defer func() {
|
|
|
|
if s3Select.close != nil {
|
|
|
|
s3Select.close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
getProgressFunc := s3Select.getProgress
|
|
|
|
if !s3Select.Progress.Enabled {
|
|
|
|
getProgressFunc = nil
|
2018-11-14 18:55:10 -05:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
writer := newMessageWriter(w, getProgressFunc)
|
2018-11-14 18:55:10 -05:00
|
|
|
|
2019-09-16 20:26:27 -04:00
|
|
|
var outputQueue []sql.Record
|
|
|
|
|
|
|
|
// Create queue based on the type.
|
|
|
|
if s3Select.statement.IsAggregated() {
|
|
|
|
outputQueue = make([]sql.Record, 0, 1)
|
|
|
|
} else {
|
|
|
|
outputQueue = make([]sql.Record, 0, 100)
|
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
var err error
|
|
|
|
sendRecord := func() bool {
|
2019-08-31 03:07:40 -04:00
|
|
|
buf := bufPool.Get().(*bytes.Buffer)
|
|
|
|
buf.Reset()
|
|
|
|
|
2019-09-16 20:26:27 -04:00
|
|
|
for _, outputRecord := range outputQueue {
|
|
|
|
if outputRecord == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
before := buf.Len()
|
|
|
|
if err = s3Select.marshal(buf, outputRecord); err != nil {
|
|
|
|
bufPool.Put(buf)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if buf.Len()-before > maxRecordSize {
|
|
|
|
writer.FinishWithError("OverMaxRecordSize", "The length of a record in the input or result is greater than maxCharsPerRecord of 1 MB.")
|
|
|
|
bufPool.Put(buf)
|
|
|
|
return false
|
|
|
|
}
|
2019-01-28 23:00:18 -05:00
|
|
|
}
|
|
|
|
|
2019-08-31 03:07:40 -04:00
|
|
|
if err = writer.SendRecord(buf); err != nil {
|
2019-01-08 19:53:04 -05:00
|
|
|
// FIXME: log this error.
|
|
|
|
err = nil
|
2019-08-31 03:07:40 -04:00
|
|
|
bufPool.Put(buf)
|
2019-01-08 19:53:04 -05:00
|
|
|
return false
|
2018-11-14 18:55:10 -05:00
|
|
|
}
|
2019-09-16 20:26:27 -04:00
|
|
|
outputQueue = outputQueue[:0]
|
2019-01-08 19:53:04 -05:00
|
|
|
return true
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
|
2019-09-13 17:18:35 -04:00
|
|
|
var rec sql.Record
|
2020-03-11 01:34:58 -04:00
|
|
|
OuterLoop:
|
2018-08-15 06:30:19 -04:00
|
|
|
for {
|
2019-01-28 20:59:48 -05:00
|
|
|
if s3Select.statement.LimitReached() {
|
2019-09-20 14:00:17 -04:00
|
|
|
if !sendRecord() {
|
|
|
|
break
|
|
|
|
}
|
2019-02-06 05:33:05 -05:00
|
|
|
if err = writer.Finish(s3Select.getProgress()); err != nil {
|
2019-01-28 20:59:48 -05:00
|
|
|
// FIXME: log this error.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-09-13 17:18:35 -04:00
|
|
|
if rec, err = s3Select.recordReader.Read(rec); err != nil {
|
2019-01-08 19:53:04 -05:00
|
|
|
if err != io.EOF {
|
|
|
|
break
|
2018-10-22 15:12:22 -04:00
|
|
|
}
|
2019-01-08 19:53:04 -05:00
|
|
|
|
|
|
|
if s3Select.statement.IsAggregated() {
|
2019-09-16 20:26:27 -04:00
|
|
|
outputRecord := s3Select.outputRecord()
|
2019-01-08 19:53:04 -05:00
|
|
|
if err = s3Select.statement.AggregateResult(outputRecord); err != nil {
|
|
|
|
break
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2019-09-16 20:26:27 -04:00
|
|
|
outputQueue = append(outputQueue, outputRecord)
|
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2019-09-16 20:26:27 -04:00
|
|
|
if !sendRecord() {
|
|
|
|
break
|
2018-12-07 17:55:32 -05:00
|
|
|
}
|
|
|
|
|
2019-02-06 05:33:05 -05:00
|
|
|
if err = writer.Finish(s3Select.getProgress()); err != nil {
|
2019-01-08 19:53:04 -05:00
|
|
|
// FIXME: log this error.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
break
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
2018-10-22 15:12:22 -04:00
|
|
|
|
2020-03-11 01:34:58 -04:00
|
|
|
var inputRecords []*sql.Record
|
|
|
|
if inputRecords, err = s3Select.statement.EvalFrom(s3Select.Input.format, rec); err != nil {
|
2019-03-09 11:13:37 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:34:58 -04:00
|
|
|
for _, inputRecord := range inputRecords {
|
|
|
|
if s3Select.statement.IsAggregated() {
|
|
|
|
if err = s3Select.statement.AggregateRow(*inputRecord); err != nil {
|
|
|
|
break OuterLoop
|
|
|
|
}
|
2019-09-16 20:26:27 -04:00
|
|
|
} else {
|
2020-03-11 01:34:58 -04:00
|
|
|
var outputRecord sql.Record
|
|
|
|
// We will attempt to reuse the records in the table.
|
|
|
|
// The type of these should not change.
|
|
|
|
// The queue should always have at least one entry left for this to work.
|
|
|
|
outputQueue = outputQueue[:len(outputQueue)+1]
|
|
|
|
if t := outputQueue[len(outputQueue)-1]; t != nil {
|
|
|
|
// If the output record is already set, we reuse it.
|
|
|
|
outputRecord = t
|
|
|
|
outputRecord.Reset()
|
|
|
|
} else {
|
|
|
|
// Create new one
|
|
|
|
outputRecord = s3Select.outputRecord()
|
|
|
|
outputQueue[len(outputQueue)-1] = outputRecord
|
|
|
|
}
|
|
|
|
outputRecord, err = s3Select.statement.Eval(*inputRecord, outputRecord)
|
|
|
|
if outputRecord == nil || err != nil {
|
|
|
|
// This should not be written.
|
|
|
|
// Remove it from the queue.
|
|
|
|
outputQueue = outputQueue[:len(outputQueue)-1]
|
|
|
|
if err != nil {
|
|
|
|
break OuterLoop
|
|
|
|
}
|
|
|
|
continue
|
2019-09-17 20:21:23 -04:00
|
|
|
}
|
2019-09-16 20:26:27 -04:00
|
|
|
|
2020-03-11 01:34:58 -04:00
|
|
|
outputQueue[len(outputQueue)-1] = outputRecord
|
2021-11-02 22:14:46 -04:00
|
|
|
if s3Select.statement.LimitReached() {
|
|
|
|
if !sendRecord() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err = writer.Finish(s3Select.getProgress()); err != nil {
|
|
|
|
// FIXME: log this error.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:34:58 -04:00
|
|
|
if len(outputQueue) < cap(outputQueue) {
|
|
|
|
continue
|
|
|
|
}
|
2018-11-14 18:55:10 -05:00
|
|
|
|
2020-03-11 01:34:58 -04:00
|
|
|
if !sendRecord() {
|
|
|
|
break OuterLoop
|
|
|
|
}
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
if err != nil {
|
2019-02-13 07:59:36 -05:00
|
|
|
_ = writer.FinishWithError("InternalError", err.Error())
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// Close - closes opened S3 object.
|
|
|
|
func (s3Select *S3Select) Close() error {
|
|
|
|
return s3Select.recordReader.Close()
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
// NewS3Select - creates new S3Select by given request XML reader.
|
|
|
|
func NewS3Select(r io.Reader) (*S3Select, error) {
|
|
|
|
s3Select := &S3Select{}
|
|
|
|
if err := xml.NewDecoder(r).Decode(s3Select); err != nil {
|
|
|
|
return nil, err
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:53:04 -05:00
|
|
|
return s3Select, nil
|
2018-08-15 06:30:19 -04:00
|
|
|
}
|