Skip to content
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

Add support for prefiltering and limited field import in SQLiteWriter #76

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module github.com/multiprocessio/dsq
go 1.18

// Uncomment for local development (and re-run `go mod tidy`)
// replace github.com/multiprocessio/datastation/runner => ../datastation/runner
replace github.com/multiprocessio/datastation/runner => ../datastation/runner
eatonphil marked this conversation as resolved.
Show resolved Hide resolved

require (
github.com/chzyer/readline v1.5.0
github.com/google/uuid v1.3.0
github.com/multiprocessio/datastation/runner v0.0.0-20220616090857-d42388371862
github.com/olekukonko/tablewriter v0.0.5
github.com/pganalyze/pg_query_go/v2 v2.1.0
github.com/stretchr/testify v1.7.1
)

require (
Expand Down Expand Up @@ -37,6 +39,7 @@ require (
github.com/aws/smithy-go v1.9.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
github.com/denisenkom/go-mssqldb v0.12.0 // indirect
github.com/flosch/pongo2 v0.0.0-20200913210552-0d938eb266f3 // indirect
Expand Down Expand Up @@ -80,6 +83,7 @@ require (
github.com/pierrec/lz4/v4 v4.1.14 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.34.0 // indirect
Expand Down Expand Up @@ -114,4 +118,5 @@ require (
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ github.com/paulmach/orb v0.5.0 h1:sNhJV5ML+mv1F077ljOck/9inorF4ahDO8iNNpHbKHY=
github.com/paulmach/orb v0.5.0/go.mod h1:FWRlTgl88VI1RBx/MkrwWDRhQ96ctqMCh8boXhmqB/A=
github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKfDlhJCDw2gY=
github.com/pborman/getopt v0.0.0-20180729010549-6fdd0a2c7117/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pganalyze/pg_query_go/v2 v2.1.0 h1:donwPZ4G/X+kMs7j5eYtKjdziqyOLVp3pkUrzb9lDl8=
github.com/pganalyze/pg_query_go/v2 v2.1.0/go.mod h1:XAxmVqz1tEGqizcQ3YSdN90vCOHBWjJi8URL1er5+cA=
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
Expand Down
69 changes: 57 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"strings"
"time"

"github.com/chzyer/readline"
"github.com/multiprocessio/datastation/runner"

"github.com/chzyer/readline"
"github.com/google/uuid"
"github.com/olekukonko/tablewriter"
)
Expand Down Expand Up @@ -343,6 +343,8 @@ type args struct {
isInteractive bool
convertNumbers bool
noSQLiteWriter bool
noFieldsGuess bool
noPrefilter bool
}

func getArgs() (*args, error) {
Expand All @@ -351,6 +353,8 @@ func getArgs() (*args, error) {
args.noSQLiteWriter = strings.ToLower(os.Getenv("DSQ_NO_SQLITE_WRITER")) == "true"
args.convertNumbers = strings.ToLower(os.Getenv("DSQ_CONVERT_NUMBERS")) == "true"
args.cacheSettings.Enabled = strings.ToLower(os.Getenv("DSQ_CACHE")) == "true"
args.noFieldsGuess = strings.ToLower(os.Getenv("DSQ_NO_FIELDS_GUESS")) == "true"
args.noPrefilter = strings.ToLower(os.Getenv("DSQ_NO_PREFILTER")) == "true"

osArgs := os.Args[1:]
for i := 0; i < len(osArgs); i++ {
Expand Down Expand Up @@ -427,11 +431,21 @@ func getArgs() (*args, error) {
continue
}

if arg == "--no-sqlite-writer" {
if arg == "-nsw" || arg == "--no-sqlite-writer" {
args.noSQLiteWriter = true
continue
}

if arg == "-np" || arg == "--no-prefilter" {
args.noPrefilter = true
continue
}

if arg == "-nfg" || arg == "--no-fields-guess" {
args.noFieldsGuess = true
continue
}

args.nonFlagArgs = append(args.nonFlagArgs, arg)
}

Expand Down Expand Up @@ -482,7 +496,7 @@ func _main() error {
return nil
}

lastNonFlagArg := ""
query := ""
files := args.nonFlagArgs

// Grab from stdin into local file
Expand All @@ -507,8 +521,8 @@ func _main() error {
// If -f|--file not present, query is the last argument
if args.sqlFile == "" {
if len(files) > 1 {
lastNonFlagArg = files[len(files)-1]
if strings.Contains(lastNonFlagArg, " ") {
query = files[len(files)-1]
if strings.Contains(query, " ") {
files = files[:len(files)-1]
}
}
Expand All @@ -519,8 +533,8 @@ func _main() error {
return errors.New("Error opening sql file: " + err.Error())
}

lastNonFlagArg = string(content)
if lastNonFlagArg == "" {
query = string(content)
if query == "" {
return errors.New("SQL file is empty.")
}
}
Expand Down Expand Up @@ -582,7 +596,7 @@ func _main() error {
connector.DatabaseConnectorInfo.Database.Database = cachedPath
}

justDumpResults := lastNonFlagArg == "" && !args.isInteractive
justDumpResults := query == "" && !args.isInteractive

// Check if we can use direct SQLite writer
useSQLiteWriter := !args.noSQLiteWriter && !args.schema && !justDumpResults
Expand All @@ -605,6 +619,7 @@ func _main() error {
mtm := runner.MimeType(mt)
useSQLiteWriter = useSQLiteWriter && (mtm == runner.CSVMimeType ||
mtm == runner.TSVMimeType ||
mtm == runner.JSONLinesMimeType ||
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to drop this or actually implement support for nested fields correctly.

mtm == runner.RegexpLinesMimeType)
if !useSQLiteWriter {
break
Expand All @@ -621,8 +636,24 @@ func _main() error {
}
}

var fieldsGuess []string
var prefilter func(map[string]any) bool
if query != "" && (!args.noFieldsGuess || !args.noPrefilter) {
a, ok := parse(rewriteQuery(query, &map[string]string{"0": "t_0"}))
if ok && !args.noFieldsGuess {
fieldsGuess, ok = identifiers(a)
if !ok {
fieldsGuess = nil
}
}

if ok && !args.noPrefilter {
prefilter = filter(a)
}
}

// When dumping schema, need to injest even if cache is on.
if !args.cacheSettings.CachePresent || !args.cacheSettings.Enabled || lastNonFlagArg == "" {
if !args.cacheSettings.CachePresent || !args.cacheSettings.Enabled || query == "" {
for i, file := range files {
panelId := uuid.New().String()

Expand All @@ -631,7 +662,14 @@ func _main() error {
var w *runner.ResultWriter
if useSQLiteWriter {
tableName := fmt.Sprintf("t_%d", i)
sw, err := openSQLiteResultItemWriter(connector.DatabaseConnectorInfo.Database.Database, tableName, convertNumbers)
sw, err := openSQLiteResultItemWriter(
connector.DatabaseConnectorInfo.Database.Database,
tableName,
SQLiteResultItemWriterOptions{
convertNumbers: convertNumbers,
prefilter: prefilter,
fieldsOverride: fieldsGuess,
})
if err != nil {
return err
}
Expand All @@ -651,7 +689,14 @@ func _main() error {
}
}

panel, err := importFile(project.Id, panelId, file, mimetypeOverride[file], convertNumbers, w, !useSQLiteWriter)
panel, err := importFile(
project.Id,
panelId,
file,
mimetypeOverride[file],
convertNumbers,
w,
!useSQLiteWriter)
if err != nil {
return err
}
Expand Down Expand Up @@ -685,7 +730,7 @@ func _main() error {
return repl(project, &ec, args, files, resolveDM_getPanelToId)
}

return runQuery(lastNonFlagArg, project, &ec, args, files, resolveDM_getPanelToId)
return runQuery(query, project, &ec, args, files, resolveDM_getPanelToId)
}

func main() {
Expand Down
Loading