Added json output format for SQL, and fixed null printing bug in CSV output

Signed-off-by: Zach Musgrave <zach@liquidata.co>
This commit is contained in:
Zach Musgrave
2020-04-10 11:52:19 -07:00
parent 90da3c0268
commit 7ead0abbf6
5 changed files with 28 additions and 10 deletions

View File

@@ -167,7 +167,7 @@ func printConflicts(ctx context.Context, root *doltdb.RootValue, tblNames []stri
nullPrinter := nullprinter.NewNullPrinter(cnfRd.GetSchema())
fwtTr := fwt.NewAutoSizingFWTTransformer(cnfRd.GetSchema(), fwt.HashFillWhenTooLong, 1000)
transforms := pipeline.NewTransformCollection(
pipeline.NewNamedTransform(nullprinter.NULL_PRINTING_STAGE, nullPrinter.ProcessRow),
pipeline.NewNamedTransform(nullprinter.NullPrintingStage, nullPrinter.ProcessRow),
pipeline.NamedTransform{Name: "fwt", Func: fwtTr.TransformToFWT},
)

View File

@@ -710,7 +710,7 @@ func buildPipeline(dArgs *diffArgs, joiner *rowconv.Joiner, ds *diff.DiffSplitte
nullPrinter := nullprinter.NewNullPrinter(untypedUnionSch)
fwtTr := fwt.NewAutoSizingFWTTransformer(untypedUnionSch, fwt.HashFillWhenTooLong, 1000)
transforms.AppendTransforms(
pipeline.NewNamedTransform(nullprinter.NULL_PRINTING_STAGE, nullPrinter.ProcessRow),
pipeline.NewNamedTransform(nullprinter.NullPrintingStage, nullPrinter.ProcessRow),
pipeline.NamedTransform{Name: fwtStageName, Func: fwtTr.TransformToFWT},
)
}

View File

@@ -19,6 +19,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/liquidata-inc/dolt/go/libraries/doltcore/table/typed/json"
"io"
"os"
"path/filepath"
@@ -452,6 +453,8 @@ func getFormat(format string) (resultFormat, errhand.VerboseError) {
return formatTabular, nil
case "csv":
return formatCsv, nil
case "json":
return formatJson, nil
default:
return formatTabular, errhand.BuildDError("Invalid argument for --result-format. Valid values are tabular,csv").Build()
}
@@ -1051,6 +1054,7 @@ type resultFormat byte
const (
formatTabular resultFormat = iota
formatCsv
formatJson
)
type sqlEngine struct {
@@ -1180,10 +1184,15 @@ func (se *sqlEngine) prettyPrintResults(ctx context.Context, sqlSch sql.Schema,
}
}()
nullPrinter := nullprinter.NewNullPrinter(untypedSch)
p.AddStage(pipeline.NewNamedTransform(nullprinter.NULL_PRINTING_STAGE, nullPrinter.ProcessRow))
// Parts of the pipeline depend on the output format, such as how we print null values and whether we pad strings.
switch se.resultFormat {
case formatCsv:
nullPrinter := nullprinter.NewNullPrinterWithNullString(untypedSch, "")
p.AddStage(pipeline.NewNamedTransform(nullprinter.NullPrintingStage, nullPrinter.ProcessRow))
if se.resultFormat == formatTabular {
case formatTabular:
nullPrinter := nullprinter.NewNullPrinter(untypedSch)
p.AddStage(pipeline.NewNamedTransform(nullprinter.NullPrintingStage, nullPrinter.ProcessRow))
autoSizeTransform := fwt.NewAutoSizingFWTTransformer(untypedSch, fwt.PrintAllWhenTooLong, 10000)
p.AddStage(pipeline.NamedTransform{Name: fwtStageName, Func: autoSizeTransform.TransformToFWT})
}
@@ -1198,6 +1207,8 @@ func (se *sqlEngine) prettyPrintResults(ctx context.Context, sqlSch sql.Schema,
wr, err = tabular.NewTextTableWriter(cliWr, untypedSch)
case formatCsv:
wr, err = csv.NewCSVWriter(cliWr, untypedSch, csv.NewCSVInfo())
case formatJson:
wr, err = json.NewJSONWriter(cliWr, untypedSch)
default:
panic("unimplemented output format type")
}

View File

@@ -188,7 +188,7 @@ func PrintSqlTableDiffs(ctx context.Context, r1, r2 *doltdb.RootValue, wr io.Wri
transforms := pipeline.NewTransformCollection()
nullPrinter := nullprinter.NewNullPrinter(sch)
transforms.AppendTransforms(
pipeline.NewNamedTransform(nullprinter.NULL_PRINTING_STAGE, nullPrinter.ProcessRow),
pipeline.NewNamedTransform(nullprinter.NullPrintingStage, nullPrinter.ProcessRow),
)
sink, err := NewSQLDiffSink(wr, sch, tblName)
if err != nil {

View File

@@ -21,18 +21,25 @@ import (
"github.com/liquidata-inc/dolt/go/store/types"
)
const PRINTED_NULL = "<NULL>"
const PrintedNull = "<NULL>"
const NULL_PRINTING_STAGE = "null printing"
const NullPrintingStage = "null printing"
// NullPrinter is a utility to convert nil values in rows to a string representation.
type NullPrinter struct {
Sch schema.Schema
nullStr string
}
// NewNullPrinter returns a new null printer for the schema given, which must be string-typed (untyped).
func NewNullPrinter(sch schema.Schema) *NullPrinter {
return &NullPrinter{Sch: sch}
return &NullPrinter{Sch: sch, nullStr: PrintedNull}
}
// NewNullPrinterWithNullString returns a new null printer for the schema given, which must be string-typed, using the
// string given as the value to print for nulls.
func NewNullPrinterWithNullString(sch schema.Schema, nullStr string) *NullPrinter {
return &NullPrinter{Sch: sch, nullStr: nullStr}
}
// Function to convert any nil values for a row with the schema given to a string representation. Used as the transform
@@ -44,7 +51,7 @@ func (np *NullPrinter) ProcessRow(inRow row.Row, props pipeline.ReadableMap) (ro
if !types.IsNull(val) {
taggedVals[tag] = val
} else {
taggedVals[tag] = types.String(PRINTED_NULL)
taggedVals[tag] = types.String(np.nullStr)
}
return false, nil