Skip to content

Commit b0a6c8e

Browse files
committed
implement --schema option on load-csv
1 parent f8252a3 commit b0a6c8e

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

rai/cmds.go

+25
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,30 @@ func listEdbs(cmd *cobra.Command, args []string) {
583583
action.Exit(rsp, err)
584584
}
585585

586+
// Parse the schema option string into the schema definition map that is
587+
// expected by the golang client.
588+
//
589+
// The schema definition consists of a sequence of semicolon delimited
590+
// <column>:<type> pairs that are parsed into a column => type map, eg:
591+
//
592+
// --schema='cocktail:string;quantity:int;price:decimal(64,2);date:date'
593+
func parseSchema(a *Action) map[string]string {
594+
schema := a.getString("schema")
595+
if schema == "" {
596+
return nil
597+
}
598+
result := map[string]string{}
599+
parts := strings.Split(schema, ";")
600+
for _, part := range parts {
601+
item := strings.Split(part, ":")
602+
if len(item) != 2 {
603+
fatal("bad schema definition '%s', expected '<column>:<type>'", item)
604+
}
605+
result[item[0]] = item[1]
606+
}
607+
return result
608+
}
609+
586610
// Returns load-csv options specified on command
587611
func getCSVOptions(a *Action) *rai.CSVOptions {
588612
opts := &rai.CSVOptions{}
@@ -602,6 +626,7 @@ func getCSVOptions(a *Action) *rai.CSVOptions {
602626
if c != 0 {
603627
opts.QuoteChar = c
604628
}
629+
opts.Schema = parseSchema(a)
605630
return opts
606631
}
607632

rai/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ func addCommands(root *cobra.Command) {
205205
cmd.Flags().String("delim", "", "field delimiter")
206206
cmd.Flags().String("escapechar", "", "character used to escape quotes")
207207
cmd.Flags().String("quotechar", "", "quoted field character")
208+
cmd.Flags().String("schema", "", "schema definition")
208209
cmd.Flags().StringP("relation", "r", "", "relation name (default: file name)")
209210
root.AddCommand(cmd)
210211

test/run-all

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ $RAI list-edbs $DATABASE -e $ENGINE
4949
# load-csv
5050
$RAI load-csv $DATABASE -e $ENGINE sample.csv -r sample_csv
5151
$RAI exec $DATABASE -e $ENGINE -c sample_csv
52+
$RAI load-csv $DATABASE -e $ENGINE sample.csv -r sample_with_schema_csv --schema='cocktail:string;quantity:int;price:decimal(64,2);date:date'
53+
$RAI exec $DATABASE -e $ENGINE -c sample_with_schema_csv
5254
$RAI load-csv $DATABASE -e $ENGINE sample_no_header.csv --header-row=0 -r sample_no_header_csv
5355
$RAI exec $DATABASE -e $ENGINE -c sample_no_header_csv
5456
$RAI load-csv $DATABASE -e $ENGINE sample_alt_syntax.csv --delim="|" --quotechar="'" -r sample_alt_syntax_csv

0 commit comments

Comments
 (0)