File tree 3 files changed +28
-0
lines changed
3 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -583,6 +583,30 @@ func listEdbs(cmd *cobra.Command, args []string) {
583
583
action .Exit (rsp , err )
584
584
}
585
585
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
+
586
610
// Returns load-csv options specified on command
587
611
func getCSVOptions (a * Action ) * rai.CSVOptions {
588
612
opts := & rai.CSVOptions {}
@@ -602,6 +626,7 @@ func getCSVOptions(a *Action) *rai.CSVOptions {
602
626
if c != 0 {
603
627
opts .QuoteChar = c
604
628
}
629
+ opts .Schema = parseSchema (a )
605
630
return opts
606
631
}
607
632
Original file line number Diff line number Diff line change @@ -205,6 +205,7 @@ func addCommands(root *cobra.Command) {
205
205
cmd .Flags ().String ("delim" , "" , "field delimiter" )
206
206
cmd .Flags ().String ("escapechar" , "" , "character used to escape quotes" )
207
207
cmd .Flags ().String ("quotechar" , "" , "quoted field character" )
208
+ cmd .Flags ().String ("schema" , "" , "schema definition" )
208
209
cmd .Flags ().StringP ("relation" , "r" , "" , "relation name (default: file name)" )
209
210
root .AddCommand (cmd )
210
211
Original file line number Diff line number Diff line change @@ -49,6 +49,8 @@ $RAI list-edbs $DATABASE -e $ENGINE
49
49
# load-csv
50
50
$RAI load-csv $DATABASE -e $ENGINE sample.csv -r sample_csv
51
51
$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
52
54
$RAI load-csv $DATABASE -e $ENGINE sample_no_header.csv --header-row=0 -r sample_no_header_csv
53
55
$RAI exec $DATABASE -e $ENGINE -c sample_no_header_csv
54
56
$RAI load-csv $DATABASE -e $ENGINE sample_alt_syntax.csv --delim=" |" --quotechar=" '" -r sample_alt_syntax_csv
You can’t perform that action at this time.
0 commit comments