Skip to content

Do not skip headers for DDL #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql/driver"
"errors"
"regexp"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -47,9 +48,12 @@ func (c *conn) runQuery(ctx context.Context, query string) (driver.Rows, error)
return nil, err
}

skipHeaders := !isDDL(query)

return newRows(rowsConfig{
Athena: c.athena,
QueryID: queryID,
Athena: c.athena,
QueryID: queryID,
SkipHeaders: skipHeaders,
})
}

Expand Down Expand Up @@ -134,3 +138,9 @@ func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {

var _ driver.Queryer = (*conn)(nil)
var _ driver.Execer = (*conn)(nil)

var ddlQueryRegex = regexp.MustCompile(`^(ALTER|CREATE|DESCRIBE|DROP|MSCK|SHOW)`)

func isDDL(query string) bool {
return ddlQueryRegex.Match([]byte(query))
}
31 changes: 19 additions & 12 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@ import (
)

type rows struct {
athena athenaiface.AthenaAPI
queryID string

done bool
out *athena.GetQueryResultsOutput
athena athenaiface.AthenaAPI
queryID string
skipHeaders bool
done bool
out *athena.GetQueryResultsOutput
}

type rowsConfig struct {
Athena athenaiface.AthenaAPI
QueryID string
Athena athenaiface.AthenaAPI
QueryID string
SkipHeaders bool
}

func newRows(cfg rowsConfig) (*rows, error) {
r := rows{
athena: cfg.Athena,
queryID: cfg.QueryID,
athena: cfg.Athena,
queryID: cfg.QueryID,
skipHeaders: cfg.SkipHeaders,
}

shouldContinue, err := r.fetchNextPage(nil)
Expand Down Expand Up @@ -97,13 +99,18 @@ func (r *rows) fetchNextPage(token *string) (bool, error) {
return false, err
}

// First row of an Athena response contains headers.
// First row of an Athena response (except of DDL queries) contains headers.
// These are also available in *athena.Row.ResultSetMetadata.
if len(r.out.ResultSet.Rows) < 2 {
minRows := 2
if !r.skipHeaders {
minRows = 1
}

if len(r.out.ResultSet.Rows) < minRows {
return false, nil
}

r.out.ResultSet.Rows = r.out.ResultSet.Rows[1:]
r.out.ResultSet.Rows = r.out.ResultSet.Rows[(minRows - 1):]
return true, nil
}

Expand Down