forked from apache/carbondata
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CarbonWriterBuilder.java
744 lines (703 loc) · 28.8 KB
/
CarbonWriterBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.carbondata.sdk.file;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.carbondata.common.annotations.InterfaceAudience;
import org.apache.carbondata.common.annotations.InterfaceStability;
import org.apache.carbondata.common.exceptions.sql.InvalidLoadOptionException;
import org.apache.carbondata.core.constants.CarbonCommonConstants;
import org.apache.carbondata.core.datastore.impl.FileFactory;
import org.apache.carbondata.core.metadata.datatype.DataType;
import org.apache.carbondata.core.metadata.datatype.DataTypes;
import org.apache.carbondata.core.metadata.datatype.MapType;
import org.apache.carbondata.core.metadata.datatype.StructField;
import org.apache.carbondata.core.metadata.schema.table.CarbonTable;
import org.apache.carbondata.core.metadata.schema.table.TableSchema;
import org.apache.carbondata.core.metadata.schema.table.TableSchemaBuilder;
import org.apache.carbondata.core.metadata.schema.table.column.ColumnSchema;
import org.apache.carbondata.core.util.CarbonProperties;
import org.apache.carbondata.core.util.CarbonUtil;
import org.apache.carbondata.processing.loading.model.CarbonLoadModel;
import org.apache.carbondata.processing.loading.model.CarbonLoadModelBuilder;
import org.apache.hadoop.conf.Configuration;
/**
* Builder for {@link CarbonWriter}
*/
@InterfaceAudience.User
@InterfaceStability.Unstable
public class CarbonWriterBuilder {
private Schema schema;
private String path;
private String[] sortColumns;
private int blockletSize;
private int blockSize;
private long timestamp;
private Map<String, String> options;
private String taskNo;
private int localDictionaryThreshold;
private boolean isLocalDictionaryEnabled;
private short numOfThreads;
private Configuration hadoopConf;
private String writtenByApp;
private String[] invertedIndexColumns;
private enum WRITER_TYPE {
CSV, AVRO, JSON
}
private WRITER_TYPE writerType;
/**
* Sets the output path of the writer builder
* @param path is the absolute path where output files are written
* This method must be called when building CarbonWriterBuilder
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder outputPath(String path) {
Objects.requireNonNull(path, "path should not be null");
this.path = path;
return this;
}
/**
* sets the list of columns that needs to be in sorted order
* @param sortColumns is a string array of columns that needs to be sorted.
* If it is null or by default all dimensions are selected for sorting
* If it is empty array, no columns are sorted
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder sortBy(String[] sortColumns) {
if (sortColumns != null) {
for (int i = 0; i < sortColumns.length; i++) {
sortColumns[i] = sortColumns[i].toLowerCase();
}
}
this.sortColumns = sortColumns;
return this;
}
/**
* sets the list of columns for which inverted index needs to generated
* @param invertedIndexColumns is a string array of columns for which inverted index needs to
* generated.
* If it is null or an empty array, inverted index will be generated for none of the columns
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder invertedIndexFor(String[] invertedIndexColumns) {
if (invertedIndexColumns != null) {
for (int i = 0; i < invertedIndexColumns.length; i++) {
invertedIndexColumns[i] = invertedIndexColumns[i].toLowerCase();
}
}
this.invertedIndexColumns = invertedIndexColumns;
return this;
}
/**
* sets the taskNo for the writer. SDKs concurrently running
* will set taskNo in order to avoid conflicts in file's name during write.
* @param taskNo is the TaskNo user wants to specify.
* by default it is system time in nano seconds.
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder taskNo(long taskNo) {
this.taskNo = String.valueOf(taskNo);
return this;
}
/**
* to set the timestamp in the carbondata and carbonindex index files
* @param timestamp is a timestamp to be used in the carbondata and carbonindex index files.
* By default set to zero.
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder uniqueIdentifier(long timestamp) {
Objects.requireNonNull(timestamp, "Unique Identifier should not be null");
this.timestamp = timestamp;
return this;
}
/**
* To support the load options for sdk writer
* @param options key,value pair of load options.
* supported keys values are
* a. bad_records_logger_enable -- true (write into separate logs), false
* b. bad_records_action -- FAIL, FORCE, IGNORE, REDIRECT
* c. bad_record_path -- path
* d. dateformat -- same as JAVA SimpleDateFormat
* e. timestampformat -- same as JAVA SimpleDateFormat
* f. complex_delimiter_level_1 -- value to Split the complexTypeData
* g. complex_delimiter_level_2 -- value to Split the nested complexTypeData
* h. quotechar
* i. escapechar
*
* Default values are as follows.
*
* a. bad_records_logger_enable -- "false"
* b. bad_records_action -- "FAIL"
* c. bad_record_path -- ""
* d. dateformat -- "" , uses from carbon.properties file
* e. timestampformat -- "", uses from carbon.properties file
* f. complex_delimiter_level_1 -- "$"
* g. complex_delimiter_level_2 -- ":"
* h. quotechar -- "\""
* i. escapechar -- "\\"
*
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder withLoadOptions(Map<String, String> options) {
Objects.requireNonNull(options, "Load options should not be null");
//validate the options.
for (String option: options.keySet()) {
if (!option.equalsIgnoreCase("bad_records_logger_enable") &&
!option.equalsIgnoreCase("bad_records_action") &&
!option.equalsIgnoreCase("bad_record_path") &&
!option.equalsIgnoreCase("dateformat") &&
!option.equalsIgnoreCase("timestampformat") &&
!option.equalsIgnoreCase("complex_delimiter_level_1") &&
!option.equalsIgnoreCase("complex_delimiter_level_2") &&
!option.equalsIgnoreCase("quotechar") &&
!option.equalsIgnoreCase("escapechar")) {
throw new IllegalArgumentException("Unsupported option:" + option
+ ". Refer method header or documentation");
}
}
if (this.options == null) {
// convert it to treeMap as keys need to be case insensitive
this.options = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
this.options.putAll(options);
return this;
}
/**
* To support the table properties for sdk writer
*
* @param options key,value pair of create table properties.
* supported keys values are
* a. table_blocksize -- [1-2048] values in MB. Default value is 1024
* b. table_blocklet_size -- values in MB. Default value is 64 MB
* c. local_dictionary_threshold -- positive value, default is 10000
* d. local_dictionary_enable -- true / false. Default is false
* e. sort_columns -- comma separated column. "c1,c2". Default all dimensions are sorted.
* If empty string "" is passed. No columns are sorted
* j. sort_scope -- "local_sort", "no_sort", "batch_sort". default value is "local_sort"
* k. long_string_columns -- comma separated string columns which are more than 32k length.
* default value is null.
* l. inverted_index -- comma separated string columns for which inverted index needs to be
* generated
*
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder withTableProperties(Map<String, String> options) {
Objects.requireNonNull(options, "Table properties should not be null");
Set<String> supportedOptions = new HashSet<>(Arrays
.asList("table_blocksize", "table_blocklet_size", "local_dictionary_threshold",
"local_dictionary_enable", "sort_columns", "sort_scope", "long_string_columns",
"inverted_index"));
for (String key : options.keySet()) {
if (!supportedOptions.contains(key.toLowerCase())) {
throw new IllegalArgumentException(
"Unsupported options. " + "Refer method header or documentation");
}
}
for (Map.Entry<String, String> entry : options.entrySet()) {
if (entry.getKey().equalsIgnoreCase("table_blocksize")) {
this.withBlockSize(Integer.parseInt(entry.getValue()));
} else if (entry.getKey().equalsIgnoreCase("table_blocklet_size")) {
this.withBlockletSize(Integer.parseInt(entry.getValue()));
} else if (entry.getKey().equalsIgnoreCase("local_dictionary_threshold")) {
this.localDictionaryThreshold(Integer.parseInt(entry.getValue()));
} else if (entry.getKey().equalsIgnoreCase("local_dictionary_enable")) {
this.enableLocalDictionary((entry.getValue().equalsIgnoreCase("true")));
} else if (entry.getKey().equalsIgnoreCase("sort_columns")) {
//sort columns
String[] sortColumns;
if (entry.getValue().trim().isEmpty()) {
sortColumns = new String[0];
} else {
sortColumns = entry.getValue().split(",");
}
this.sortBy(sortColumns);
} else if (entry.getKey().equalsIgnoreCase("sort_scope")) {
this.withSortScope(entry);
} else if (entry.getKey().equalsIgnoreCase("long_string_columns")) {
updateToLoadOptions(entry);
} else if (entry.getKey().equalsIgnoreCase("inverted_index")) {
//inverted index columns
String[] invertedIndexColumns;
if (entry.getValue().trim().isEmpty()) {
invertedIndexColumns = new String[0];
} else {
invertedIndexColumns = entry.getValue().split(",");
}
this.invertedIndexFor(invertedIndexColumns);
}
}
return this;
}
/**
* To make sdk writer thread safe.
*
* @param numOfThreads should number of threads in which writer is called in multi-thread scenario
* default sdk writer is not thread safe.
* can use one writer instance in one thread only.
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder withThreadSafe(short numOfThreads) {
if (numOfThreads < 1) {
throw new IllegalArgumentException("number of threads cannot be lesser than 1. "
+ "suggest to keep two times the number of cores available");
}
this.numOfThreads = numOfThreads;
return this;
}
/**
* To support hadoop configuration
*
* @param conf hadoop configuration support, can set s3a AK,SK,end point and other conf with this
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder withHadoopConf(Configuration conf) {
if (conf != null) {
this.hadoopConf = conf;
}
return this;
}
/**
* configure hadoop configuration with key value
*
* @param key key word
* @param value value
* @return this object
*/
public CarbonWriterBuilder withHadoopConf(String key, String value) {
if (this.hadoopConf == null) {
this.hadoopConf = new Configuration(true);
}
this.hadoopConf.set(key, value);
return this;
}
/**
* To set the carbondata file size in MB between 1MB-2048MB
* @param blockSize is size in MB between 1MB to 2048 MB
* default value is 1024 MB
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder withBlockSize(int blockSize) {
if (blockSize <= 0 || blockSize > 2048) {
throw new IllegalArgumentException("blockSize should be between 1 MB to 2048 MB");
}
this.blockSize = blockSize;
return this;
}
/**
* @param localDictionaryThreshold is localDictionaryThreshold,default is 10000
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder localDictionaryThreshold(int localDictionaryThreshold) {
if (localDictionaryThreshold <= 0) {
throw new IllegalArgumentException(
"Local Dictionary Threshold should be greater than 0");
}
this.localDictionaryThreshold = localDictionaryThreshold;
return this;
}
/**
* @param appName appName which is writing the carbondata files
* @return
*/
public CarbonWriterBuilder writtenBy(String appName) {
this.writtenByApp = appName;
return this;
}
/**
* @param enableLocalDictionary enable local dictionary , default is false
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder enableLocalDictionary(boolean enableLocalDictionary) {
this.isLocalDictionaryEnabled = enableLocalDictionary;
return this;
}
/**
* To set the blocklet size of CarbonData file
* @param blockletSize is blocklet size in MB
* default value is 64 MB
* @return updated CarbonWriterBuilder
*/
public CarbonWriterBuilder withBlockletSize(int blockletSize) {
if (blockletSize <= 0) {
throw new IllegalArgumentException("blockletSize should be greater than zero");
}
this.blockletSize = blockletSize;
return this;
}
/**
* to build a {@link CarbonWriter}, which accepts row in CSV format
*
* @param schema carbon Schema object {org.apache.carbondata.sdk.file.Schema}
* @return CarbonWriterBuilder
*/
public CarbonWriterBuilder withCsvInput(Schema schema) {
Objects.requireNonNull(schema, "schema should not be null");
this.schema = schema;
this.writerType = WRITER_TYPE.CSV;
return this;
}
/**
* to build a {@link CarbonWriter}, which accepts row in CSV format
*
* @param jsonSchema json Schema string
* @return CarbonWriterBuilder
*/
public CarbonWriterBuilder withCsvInput(String jsonSchema) {
Objects.requireNonNull(jsonSchema, "schema should not be null");
this.schema = Schema.parseJson(jsonSchema);
this.writerType = WRITER_TYPE.CSV;
return this;
}
/**
* to build a {@link CarbonWriter}, which accepts Avro object
*
* @param avroSchema avro Schema object {org.apache.avro.Schema}
* @return CarbonWriterBuilder
*/
public CarbonWriterBuilder withAvroInput(org.apache.avro.Schema avroSchema) {
Objects.requireNonNull(avroSchema, "Avro schema should not be null");
this.schema = AvroCarbonWriter.getCarbonSchemaFromAvroSchema(avroSchema);
this.writerType = WRITER_TYPE.AVRO;
return this;
}
/**
* to build a {@link CarbonWriter}, which accepts Json object
*
* @param carbonSchema carbon Schema object
* @return CarbonWriterBuilder
*/
public CarbonWriterBuilder withJsonInput(Schema carbonSchema) {
Objects.requireNonNull(carbonSchema, "schema should not be null");
this.schema = carbonSchema;
this.writerType = WRITER_TYPE.JSON;
return this;
}
/**
* Build a {@link CarbonWriter}
* This writer is not thread safe,
* use withThreadSafe() configuration in multi thread environment
*
* @return CarbonWriter {AvroCarbonWriter/CSVCarbonWriter/JsonCarbonWriter based on Input Type }
* @throws IOException
* @throws InvalidLoadOptionException
*/
public CarbonWriter build() throws IOException, InvalidLoadOptionException {
Objects.requireNonNull(path, "path should not be null");
if (this.writerType == null) {
throw new IOException(
"'writerType' must be set, use withCsvInput() or withAvroInput() or withJsonInput() "
+ "API based on input");
}
if (this.writtenByApp == null || this.writtenByApp.isEmpty()) {
throw new RuntimeException(
"'writtenBy' must be set when writting carbon files, use writtenBy() API to "
+ "set it, it can be the name of the application which is using the SDK");
}
CarbonLoadModel loadModel = buildLoadModel(schema);
loadModel.setSdkWriterCores(numOfThreads);
CarbonProperties.getInstance()
.addProperty(CarbonCommonConstants.CARBON_WRITTEN_BY_APPNAME, writtenByApp);
if (hadoopConf == null) {
hadoopConf = FileFactory.getConfiguration();
}
if (this.writerType == WRITER_TYPE.AVRO) {
// AVRO records are pushed to Carbon as Object not as Strings. This was done in order to
// handle multi level complex type support. As there are no conversion converter step is
// removed from the load. LoadWithoutConverter flag is going to point to the Loader Builder
// which will skip Conversion Step.
loadModel.setLoadWithoutConverterStep(true);
return new AvroCarbonWriter(loadModel, hadoopConf);
} else if (this.writerType == WRITER_TYPE.JSON) {
loadModel.setJsonFileLoad(true);
return new JsonCarbonWriter(loadModel, hadoopConf);
} else {
// CSV
return new CSVCarbonWriter(loadModel, hadoopConf);
}
}
private void setCsvHeader(CarbonLoadModel model) {
Field[] fields = schema.getFields();
StringBuilder builder = new StringBuilder();
String[] columns = new String[fields.length];
int i = 0;
for (Field field : fields) {
if (null != field) {
builder.append(field.getFieldName());
builder.append(",");
columns[i++] = field.getFieldName();
}
}
String header = builder.toString();
model.setCsvHeader(header.substring(0, header.length() - 1));
model.setCsvHeaderColumns(columns);
}
public CarbonLoadModel buildLoadModel(Schema carbonSchema)
throws IOException, InvalidLoadOptionException {
timestamp = System.nanoTime();
// validate long_string_column
Set<String> longStringColumns = new HashSet<>();
if (options != null && options.get(CarbonCommonConstants.LONG_STRING_COLUMNS) != null) {
String[] specifiedLongStrings =
options.get(CarbonCommonConstants.LONG_STRING_COLUMNS).toLowerCase().split(",");
for (String str : specifiedLongStrings) {
longStringColumns.add(str.trim());
}
validateLongStringColumns(carbonSchema, longStringColumns);
}
// for the longstring field, change the datatype from string to varchar
this.schema = updateSchemaFields(carbonSchema, longStringColumns);
// build CarbonTable using schema
CarbonTable table = buildCarbonTable();
// build LoadModel
return buildLoadModel(table, timestamp, taskNo, options);
}
private void validateLongStringColumns(Schema carbonSchema, Set<String> longStringColumns) {
// long string columns must be string or varchar type
for (Field field :carbonSchema.getFields()) {
if (longStringColumns.contains(field.getFieldName().toLowerCase()) && (
(field.getDataType() != DataTypes.STRING) && field.getDataType() != DataTypes.VARCHAR)) {
throw new RuntimeException(
"long string column : " + field.getFieldName() + "is not supported for data type: "
+ field.getDataType());
}
}
// long string columns must not be present in sort columns
if (sortColumns != null) {
for (String col : sortColumns) {
// already will be in lower case
if (longStringColumns.contains(col)) {
throw new RuntimeException(
"long string column : " + col + "must not be present in sort columns");
}
}
}
}
/**
* Build a {@link CarbonTable}
*/
private CarbonTable buildCarbonTable() {
TableSchemaBuilder tableSchemaBuilder = TableSchema.builder();
if (blockSize > 0) {
tableSchemaBuilder = tableSchemaBuilder.blockSize(blockSize);
}
if (blockletSize > 0) {
tableSchemaBuilder = tableSchemaBuilder.blockletSize(blockletSize);
}
tableSchemaBuilder.enableLocalDictionary(isLocalDictionaryEnabled);
tableSchemaBuilder.localDictionaryThreshold(localDictionaryThreshold);
List<String> sortColumnsList = new ArrayList<>();
if (sortColumns == null) {
// If sort columns are not specified, default set all dimensions to sort column.
// When dimensions are default set to sort column,
// Inverted index will be supported by default for sort columns.
//Null check for field to handle hole in field[] ex.
// user passed size 4 but supplied only 2 fileds
for (Field field : schema.getFields()) {
if (null != field) {
if (field.getDataType() == DataTypes.STRING ||
field.getDataType() == DataTypes.DATE ||
field.getDataType() == DataTypes.TIMESTAMP) {
sortColumnsList.add(field.getFieldName());
}
}
}
sortColumns = new String[sortColumnsList.size()];
sortColumns = sortColumnsList.toArray(sortColumns);
} else {
sortColumnsList = Arrays.asList(sortColumns);
}
ColumnSchema[] sortColumnsSchemaList = new ColumnSchema[sortColumnsList.size()];
List<String> invertedIdxColumnsList = new ArrayList<>();
if (null != invertedIndexColumns) {
invertedIdxColumnsList = Arrays.asList(invertedIndexColumns);
}
Field[] fields = schema.getFields();
buildTableSchema(fields, tableSchemaBuilder, sortColumnsList, sortColumnsSchemaList,
invertedIdxColumnsList);
tableSchemaBuilder.setSortColumns(Arrays.asList(sortColumnsSchemaList));
String tableName;
String dbName;
dbName = "";
tableName = "_tempTable_" + String.valueOf(timestamp);
TableSchema schema = tableSchemaBuilder.build();
schema.setTableName(tableName);
CarbonTable table =
CarbonTable.builder().tableName(schema.getTableName()).databaseName(dbName).tablePath(path)
.tableSchema(schema).isTransactionalTable(false).build();
return table;
}
private void buildTableSchema(Field[] fields, TableSchemaBuilder tableSchemaBuilder,
List<String> sortColumnsList, ColumnSchema[] sortColumnsSchemaList,
List<String> invertedIdxColumnsList) {
Set<String> uniqueFields = new HashSet<>();
// a counter which will be used in case of complex array type. This valIndex will be assigned
// to child of complex array type in the order val1, val2 so that each array type child is
// differentiated to any level
AtomicInteger valIndex = new AtomicInteger(0);
// Check if any of the columns specified in sort columns are missing from schema.
for (String sortColumn: sortColumnsList) {
boolean exists = false;
for (Field field : fields) {
if (field.getFieldName().equalsIgnoreCase(sortColumn)) {
exists = true;
break;
}
}
if (!exists) {
throw new RuntimeException(
"column: " + sortColumn + " specified in sort columns does not exist in schema");
}
}
// Check if any of the columns specified in inverted index are missing from schema.
for (String invertedIdxColumn: invertedIdxColumnsList) {
boolean exists = false;
for (Field field : fields) {
if (field.getFieldName().equalsIgnoreCase(invertedIdxColumn)) {
exists = true;
break;
}
}
if (!exists) {
throw new RuntimeException(
"column: " + invertedIdxColumn + " specified in sort columns does not exist in schema");
}
}
int i = 0;
for (Field field : fields) {
if (null != field) {
if (!uniqueFields.add(field.getFieldName())) {
throw new RuntimeException(
"Duplicate column " + field.getFieldName() + " found in table schema");
}
int isSortColumn = sortColumnsList.indexOf(field.getFieldName());
int isInvertedIdxColumn = invertedIdxColumnsList.indexOf(field.getFieldName());
if (isSortColumn > -1) {
// unsupported types for ("array", "struct", "double", "float", "decimal")
if (field.getDataType() == DataTypes.DOUBLE || field.getDataType() == DataTypes.FLOAT
|| DataTypes.isDecimal(field.getDataType()) || field.getDataType().isComplexType()
|| field.getDataType() == DataTypes.VARCHAR) {
String errorMsg =
"sort columns not supported for array, struct, map, double, float, decimal,"
+ "varchar";
throw new RuntimeException(errorMsg);
}
}
if (field.getChildren() != null && field.getChildren().size() > 0) {
if (field.getDataType().getName().equalsIgnoreCase("ARRAY")) {
// Loop through the inner columns and for a StructData
DataType complexType =
DataTypes.createArrayType(field.getChildren().get(0).getDataType());
tableSchemaBuilder
.addColumn(new StructField(field.getFieldName(), complexType), valIndex, false,
isInvertedIdxColumn > -1);
} else if (field.getDataType().getName().equalsIgnoreCase("STRUCT")) {
// Loop through the inner columns and for a StructData
List<StructField> structFieldsArray =
new ArrayList<StructField>(field.getChildren().size());
for (StructField childFld : field.getChildren()) {
structFieldsArray
.add(new StructField(childFld.getFieldName(), childFld.getDataType()));
}
DataType complexType = DataTypes.createStructType(structFieldsArray);
tableSchemaBuilder
.addColumn(new StructField(field.getFieldName(), complexType), valIndex, false,
isInvertedIdxColumn > -1);
} else if (field.getDataType().getName().equalsIgnoreCase("MAP")) {
// Loop through the inner columns for MapType
DataType mapType = DataTypes.createMapType(((MapType) field.getDataType()).getKeyType(),
field.getChildren().get(0).getDataType());
tableSchemaBuilder
.addColumn(new StructField(field.getFieldName(), mapType), valIndex, false,
isInvertedIdxColumn > -1);
}
} else {
ColumnSchema columnSchema = tableSchemaBuilder
.addColumn(new StructField(field.getFieldName(), field.getDataType()), valIndex,
isSortColumn > -1, isInvertedIdxColumn > -1);
if (isSortColumn > -1) {
columnSchema.setSortColumn(true);
sortColumnsSchemaList[isSortColumn] = columnSchema;
}
}
}
}
}
/**
* Build a {@link CarbonLoadModel}
*/
private CarbonLoadModel buildLoadModel(CarbonTable table, long timestamp, String taskNo,
Map<String, String> options) throws InvalidLoadOptionException, IOException {
if (options == null) {
options = new HashMap<>();
}
CarbonLoadModelBuilder builder = new CarbonLoadModelBuilder(table);
CarbonLoadModel build = builder.build(options, timestamp, taskNo);
setCsvHeader(build);
return build;
}
/* loop through all the parent column and
a) change fields name to lower case.
this is to match with sort column case.
b) change string fields to varchar type */
private Schema updateSchemaFields(Schema schema, Set<String> longStringColumns) {
if (schema == null) {
return null;
}
Field[] fields = schema.getFields();
for (int i = 0; i < fields.length; i++) {
if (fields[i] != null) {
fields[i].updateNameToLowerCase();
if (longStringColumns != null) {
/* Also update the string type to varchar */
if (longStringColumns.contains(fields[i].getFieldName())) {
fields[i].updateDataTypeToVarchar();
}
}
}
}
return new Schema(fields);
}
private void updateToLoadOptions(Map.Entry<String, String> entry) {
if (this.options == null) {
// convert it to treeMap as keys need to be case insensitive
this.options = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
// update it to load options
this.options.put(entry.getKey(), entry.getValue());
}
private void withSortScope(Map.Entry<String, String> entry) {
String sortScope = entry.getValue();
if (sortScope != null) {
if ((!CarbonUtil.isValidSortOption(sortScope))) {
throw new IllegalArgumentException("Invalid Sort Scope Option: " + sortScope);
} else if (sortScope.equalsIgnoreCase("global_sort")) {
throw new IllegalArgumentException("global sort is not supported");
}
}
// update it to load options
updateToLoadOptions(entry);
}
}