-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dsq binary for running SQL queries against files (#127)
* Start on dsq * Continue sketching out dsq * More sketching * Refactoring to support dsq * Working with hardcoded shape * Clean up * Fix for shape guesser * Fix for format * Go doesnt support graph or table yet * Fix build scripts for new binary * Fixes for build * More fixes
- Loading branch information
Showing
28 changed files
with
493 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
runner | ||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/multiprocessio/datastation/runner" | ||
) | ||
|
||
func isinpipe() bool { | ||
fi, _ := os.Stdin.Stat() | ||
return (fi.Mode() & os.ModeCharDevice) == 0 | ||
} | ||
|
||
func resolveContentType(fileExtensionOrContentType string) string { | ||
if strings.Contains(fileExtensionOrContentType, "/") { | ||
return fileExtensionOrContentType | ||
} | ||
|
||
return runner.GetMimeType("x."+fileExtensionOrContentType, runner.ContentTypeInfo{}) | ||
} | ||
|
||
var firstNonFlagArg = "" | ||
|
||
func getResult(res interface{}) error { | ||
out := bytes.NewBuffer(nil) | ||
arg := firstNonFlagArg | ||
|
||
var internalErr error | ||
if isinpipe() { | ||
mimetype := resolveContentType(arg) | ||
if mimetype == "" { | ||
return fmt.Errorf(`First argument when used in a pipe should be file extension or content type. e.g. 'cat test.csv | dsq csv "SELECT * FROM {}"'`) | ||
} | ||
|
||
cti := runner.ContentTypeInfo{Type: mimetype} | ||
internalErr = runner.TransformReader(os.Stdin, "", cti, out) | ||
} else { | ||
internalErr = runner.TransformFile(arg, runner.ContentTypeInfo{}, out) | ||
} | ||
if internalErr != nil { | ||
return internalErr | ||
} | ||
|
||
decoder := json.NewDecoder(out) | ||
return decoder.Decode(res) | ||
} | ||
|
||
func main() { | ||
if len(os.Args) < 3 { | ||
log.Fatal(`Expected data source and query. e.g. 'dsq names.csv "SELECT name FROM {}"'`) | ||
} | ||
|
||
runner.Verbose = false | ||
inputTable := "{}" | ||
lastNonFlagArg := "" | ||
for i, arg := range os.Args[1:] { | ||
if arg == "-i" || arg == "--input-table-alias" { | ||
if i > len(os.Args)-2 { | ||
log.Fatal(`Expected input table alias after flag. e.g. 'dsq -i XX names.csv "SELECT * FROM XX"'`) | ||
} | ||
|
||
inputTable = os.Args[i+1] | ||
continue | ||
} | ||
|
||
if arg == "-v" || arg == "--verbose" { | ||
runner.Verbose = true | ||
continue | ||
} | ||
|
||
if firstNonFlagArg == "" { | ||
firstNonFlagArg = arg | ||
} | ||
|
||
lastNonFlagArg = arg | ||
} | ||
|
||
var res []map[string]interface{} | ||
err := getResult(&res) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
sampleSize := 50 | ||
shape, err := runner.GetArrayShape(firstNonFlagArg, res, sampleSize) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
p0 := runner.PanelInfo{ | ||
ResultMeta: runner.PanelResult{ | ||
Shape: *shape, | ||
}, | ||
} | ||
project := &runner.ProjectState{ | ||
Pages: []runner.ProjectPage{ | ||
{ | ||
Panels: []runner.PanelInfo{p0}, | ||
}, | ||
}, | ||
} | ||
connector, tmp, err := runner.MakeTmpSQLiteConnector() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer os.Remove(tmp.Name()) | ||
project.Connectors = append(project.Connectors, *connector) | ||
|
||
query := lastNonFlagArg | ||
query = strings.ReplaceAll(query, inputTable, "DM_getPanel(0)") | ||
panel := &runner.PanelInfo{ | ||
Type: runner.DatabasePanel, | ||
Content: query, | ||
DatabasePanelInfo: &runner.DatabasePanelInfo{ | ||
Database: runner.DatabasePanelInfoDatabase{ | ||
ConnectorId: connector.Id, | ||
}, | ||
}, | ||
} | ||
|
||
panelResultLoader := func(_, _ string, out interface{}) error { | ||
r := out.(*[]map[string]interface{}) | ||
*r = res | ||
return nil | ||
} | ||
err = runner.EvalDatabasePanel(project, 0, panel, panelResultLoader) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Dump the result to stdout | ||
fd, err := os.Open(runner.GetPanelResultsFile(project.Id, panel.Id)) | ||
if err != nil { | ||
log.Fatalf("Could not open results file: %s", err) | ||
} | ||
|
||
io.Copy(os.Stdout, fd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/multiprocessio/datastation/runner" | ||
) | ||
|
||
const VERSION = "development" | ||
const APP_NAME = "DataStation Runner (Go)" | ||
|
||
func main() { | ||
runner.Verbose = true | ||
runner.Logln(APP_NAME + " " + VERSION) | ||
projectId := "" | ||
panelId := "" | ||
panelMetaOut := "" | ||
|
||
args := os.Args | ||
for i := 0; i < len(args)-1; i++ { | ||
if args[i] == "--dsproj" { | ||
projectId = args[i+1] | ||
i++ | ||
continue | ||
} | ||
|
||
if args[i] == "--evalPanel" { | ||
panelId = args[i+1] | ||
i++ | ||
continue | ||
} | ||
|
||
if args[i] == "--metaFile" { | ||
panelMetaOut = args[i+1] | ||
i++ | ||
continue | ||
} | ||
} | ||
|
||
if projectId == "" { | ||
runner.Fatalln("No project id given.") | ||
} | ||
|
||
if panelId == "" { | ||
runner.Fatalln("No panel id given.") | ||
} | ||
|
||
if panelMetaOut == "" { | ||
runner.Fatalln("No panel meta out given.") | ||
} | ||
|
||
settings, err := runner.LoadSettings() | ||
if err != nil { | ||
runner.Logln("Could not load settings, assuming defaults.") | ||
settings = runner.DefaultSettings | ||
} | ||
|
||
ec := runner.NewEvalContext(*settings) | ||
|
||
err = ec.Eval(projectId, panelId) | ||
if err != nil { | ||
runner.Logln("Failed to eval: %s", err) | ||
|
||
if _, ok := err.(*runner.DSError); !ok { | ||
err = runner.Edse(err) | ||
err.(*runner.DSError).Stack = "Unknown" | ||
} | ||
|
||
err := runner.WriteJSONFile(panelMetaOut, map[string]interface{}{ | ||
"exception": err, | ||
}) | ||
if err != nil { | ||
runner.Fatalln("Could not write panel meta out: %s", err) | ||
} | ||
|
||
// Explicitly don't fail here so that the parent can read the exception from disk | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package runner | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.