kunal642 commented on a change in pull request #3834: URL: https://github.com/apache/carbondata/pull/3834#discussion_r475244586
########## File path: sdk/sdk/src/main/java/org/apache/carbondata/sdk/file/CarbonIUD.java ########## @@ -0,0 +1,275 @@ +package org.apache.carbondata.sdk.file; + +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.Field; +import org.apache.carbondata.core.scan.expression.ColumnExpression; +import org.apache.carbondata.core.scan.expression.Expression; +import org.apache.carbondata.core.scan.expression.LiteralExpression; +import org.apache.carbondata.core.scan.expression.conditional.EqualToExpression; +import org.apache.carbondata.core.scan.expression.logical.AndExpression; +import org.apache.carbondata.hadoop.api.CarbonTableOutputFormat; +import org.apache.carbondata.hadoop.internal.ObjectArrayWritable; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.jcraft.jsch.IO; +import org.apache.hadoop.io.NullWritable; +import org.apache.hadoop.mapred.JobConf; +import org.apache.hadoop.mapreduce.Job; +import org.apache.hadoop.mapreduce.RecordWriter; +import org.apache.hadoop.mapreduce.TaskAttemptID; +import org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl; + +public class CarbonIUD { + + private final Map<String, Map<String, String>> filterColumnToValueMapping; + private final Map<String, Map<String, String>> updateColumnToValueMapping; + + private CarbonIUD() { + filterColumnToValueMapping = new HashMap<>(); + updateColumnToValueMapping = new HashMap<>(); + } + + /** + * @return CarbonIUD object + */ + public static CarbonIUD getInstance() { + return new CarbonIUD(); + } + + /** + * @param path is the segment path on which delete is performed + * @param column is the columeName on which records have to be deleted + * @param value of column on which the records have to be deleted + * @return CarbonIUD object + * <p> + * for eg: DELETE WHERE column = value + */ + public CarbonIUD delete(String path, String column, String value) { + prepareDelete(path, column, value); + return this; + } + + /** + * @param path is the segment path on which delete is performed + * @param filterExpression is the expression to delete the records + * @throws IOException + * @throws InterruptedException + */ + public void delete(String path, Expression filterExpression) + throws IOException, InterruptedException { + CarbonReader reader = CarbonReader.builder(path, "_temp") + .projection(new String[] { CarbonCommonConstants.CARBON_IMPLICIT_COLUMN_TUPLEID }) + .filter(filterExpression).build(); + + RecordWriter<NullWritable, ObjectArrayWritable> deleteDeltaWriter = + CarbonTableOutputFormat.getDeleteDeltaRecordWriter(path); + ObjectArrayWritable writable = new ObjectArrayWritable(); + while (reader.hasNext()) { + Object[] row = (Object[]) reader.readNextRow(); + writable.set(row); + deleteDeltaWriter.write(NullWritable.get(), writable); + } + deleteDeltaWriter.close(null); + reader.close(); + } + + /** + * Calling this method will start the execution of delete process + * @throws IOException + * @throws InterruptedException + */ + public void closeDelete() throws IOException, InterruptedException { + for (Map.Entry<String, Map<String, String>> path : this.filterColumnToValueMapping.entrySet()) { + deleteExecution(path.getKey()); + } + this.filterColumnToValueMapping.clear(); + } + + /** + * @param path is the segment path on which update is performed + * @param column is the columeName on which records have to be updated + * @param value of column on which the records have to be updated + * @param updColumn is the name of updatedColumn + * @param updValue is the value of updatedColumn + * @return CarbonUID + * <p> + * for eg: UPDATE updColumn = updValue WHERE column = value + */ + public CarbonIUD update(String path, String column, String value, String updColumn, + String updValue) { + prepareUpdate(path, column, value, updColumn, updValue); + return this; + } + + /** + * @param path is the segment path on which update is performed. + * @param filterExpression is the expression object to update the records + * @param updatedColumnToValueMapping contains the mapping of updatedColumns to updatedValues + * @throws IOException + * @throws InterruptedException + * @throws InvalidLoadOptionException + */ + public void update(String path, Expression filterExpression, + Map<String, String> updatedColumnToValueMapping) + throws IOException, InterruptedException, InvalidLoadOptionException { + File[] indexFiles = getCarbonIndexFile(path); + Schema schema = CarbonSchemaReader.readSchema(indexFiles[0].getAbsolutePath()).asOriginOrder(); + Field[] fields = schema.getFields(); + String[] projectionColumns = new String[fields.length + 1]; + for (int i = 0; i < fields.length; i++) { + projectionColumns[i] = (fields[i].getFieldName()); + } + projectionColumns[projectionColumns.length - 1] = + CarbonCommonConstants.CARBON_IMPLICIT_COLUMN_TUPLEID; + + CarbonWriter writer = + CarbonWriter.builder().outputPath(path).withCsvInput(schema).writtenBy("CarbonIUD").build(); + + CarbonReader reader = + CarbonReader.builder(path, "_temp").projection(projectionColumns).filter(filterExpression) + .build(); + + RecordWriter<NullWritable, ObjectArrayWritable> deleteDeltaWriter = + CarbonTableOutputFormat.getDeleteDeltaRecordWriter(path); + ObjectArrayWritable writable = new ObjectArrayWritable(); + + while (reader.hasNext()) { + Object[] row = (Object[]) reader.readNextRow(); + writable.set(Arrays.copyOfRange(row, row.length - 1, row.length)); + for (Map.Entry<String, String> column : updatedColumnToValueMapping.entrySet()) { + row[getColumnIndex(fields, column.getKey())] = column.getValue(); + } + writer.write(Arrays.copyOfRange(row, 0, row.length - 1)); + deleteDeltaWriter.write(NullWritable.get(), writable); + } + deleteDeltaWriter.close(null); + writer.close(); + reader.close(); + } + + /** + * Calling this method will start the execution of update process + * @throws IOException + * @throws InterruptedException + * @throws InvalidLoadOptionException + */ + public void closeUpdate() throws IOException, InterruptedException, InvalidLoadOptionException { + for (Map.Entry<String, Map<String, String>> path : this.filterColumnToValueMapping.entrySet()) { + if (this.updateColumnToValueMapping.containsKey(path.getKey())) { + updateExecution(path.getKey()); + } + } + this.filterColumnToValueMapping.clear(); + this.updateColumnToValueMapping.clear(); + } + + private void updateExecution(String path) + throws IOException, InterruptedException, InvalidLoadOptionException { + Expression filterExpression = getExpression(path); + update(path, filterExpression, this.updateColumnToValueMapping.get(path)); + } + + private void deleteExecution(String path) throws IOException, InterruptedException { + Expression filterExpression = getExpression(path); + delete(path, filterExpression); + } + + private void prepareUpdate(String path, String column, String value, String updColumn, + String updValue) { + prepareDelete(path, column, value); + updColumn = updColumn.toLowerCase().trim(); + if (this.updateColumnToValueMapping.containsKey(path)) { + this.updateColumnToValueMapping.get(path).put(updColumn, updValue); + } else { + Map<String, String> columnToValue = new HashMap<>(); + columnToValue.put(updColumn, updValue); + this.updateColumnToValueMapping.put(path, columnToValue); + } + } + + private void prepareDelete(String path, String column, String value) { + column = column.toLowerCase().trim(); + if (this.filterColumnToValueMapping.containsKey(path)) { + this.filterColumnToValueMapping.get(path).put(column, value); + } else { + Map<String, String> columnToValueMapping = new HashMap<>(); + columnToValueMapping.put(column, value); + this.filterColumnToValueMapping.put(path, columnToValueMapping); + } + } + + private Expression getExpression(String path) throws IOException { + File[] indexFiles = getCarbonIndexFile(path); + Schema schema = CarbonSchemaReader.readSchema(indexFiles[0].getAbsolutePath()).asOriginOrder(); + Field[] fields = schema.getFields(); + List<EqualToExpression> listOfExpressions = new ArrayList<>(); + for (Map.Entry<String, String> column : this.filterColumnToValueMapping.get(path).entrySet()) { + DataType dataType = getColumnDataType(fields, column.getKey()); + listOfExpressions.add(new EqualToExpression(new ColumnExpression(column.getKey(), dataType), + new LiteralExpression(column.getValue(), dataType))); + } + Expression filterExpression = null; + if (listOfExpressions.size() > 0) { + filterExpression = listOfExpressions.get(0); + } + for (int i = 1; i < listOfExpressions.size(); i++) { + filterExpression = new AndExpression(filterExpression, listOfExpressions.get(i)); + } + return filterExpression; + } + + private int getColumnIndex(Field[] fields, String column) { + int index = -1; + for (Field field : fields) { + if (field.getFieldName().equals(column)) { + index = field.getSchemaOrdinal(); + break; + } + } + if (index == -1) { Review comment: @Karan980 Please check this and add test cases for all the missing types as well ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org