package parquet.cascading;

import java.util.Map;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapred.JobConf;

import cascading.tuple.Tuple;
import cascading.tuple.Fields;
import cascading.flow.hadoop.util.HadoopUtil;

import parquet.hadoop.api.ReadSupport;
import parquet.io.api.RecordMaterializer;
import parquet.schema.MessageType;
import parquet.cascading.convert.TupleRecordMaterializer;


public class TupleReadSupport extends ReadSupport<Tuple> {
  static final String PARQUET_CASCADING_REQUESTED_FIELDS = "parquet.cascading.requested.fields";

  static protected Fields getRequestedFields(Configuration configuration) {
    try {
      String fieldsString = configuration.get(PARQUET_CASCADING_REQUESTED_FIELDS);
      return HadoopUtil.deserializeBase64(fieldsString, configuration, Fields.class);
    } catch(IOException e) {
      throw new RuntimeException(e.toString());
    }
  }

  static protected void setRequestedFields(JobConf configuration, Fields fields) {
    try {
      String fieldsString = HadoopUtil.serializeBase64(fields, configuration);
      configuration.set(PARQUET_CASCADING_REQUESTED_FIELDS, fieldsString);
    }
    catch(IOException e) {
      throw new RuntimeException(e.toString());
    }
  }

  @Override
  public ReadContext init(Configuration configuration, Map<String, String> keyValueMetaData, MessageType fileSchema) {
    Fields requestedFields = getRequestedFields(configuration);
    if (requestedFields == null) {
      return new ReadContext(fileSchema);
    } else {
      SchemaIntersection intersection = new SchemaIntersection(fileSchema, requestedFields);
      return new ReadContext(intersection.getRequestedSchema());
    }
  }

  @Override
  public RecordMaterializer<Tuple> prepareForRead(
      Configuration configuration,
      Map<String, String> keyValueMetaData,
      MessageType fileSchema,
      ReadContext readContext) {
    MessageType requestedSchema = readContext.getRequestedSchema();
    return new TupleRecordMaterializer(requestedSchema);
  }

}

@@ -157,16 +157,60 @@
 JobConf;
+%0Aimport org.apache.commons.lang.StringUtils;
 %0A%0Aimport
@@ -716,36 +716,24 @@
 guration) %7B%0A
-    try %7B%0A  
     String f
@@ -809,172 +809,154 @@
     
-  return HadoopUtil.deserializeBase64(fieldsString, configuration, Fields.class);%0A    %7D catch(IOException e) %7B%0A      throw new RuntimeException(e.toString());%0A    %7D
+String%5B%5D parts = StringUtils.split(fieldsString, %22:%22);%0A    if(parts.length == 0)%0A      return Fields.ALL;%0A    else%0A      return new Fields(parts);
 %0A  %7D
@@ -1044,20 +1044,8 @@
 ) %7B%0A
-    try %7B%0A  
     
@@ -1070,61 +1070,50 @@
 g = 
-HadoopUtil.serializeBase64(fields, configuration
+StringUtils.join(fields.iterator(), %22:%22
 );%0A
-  
     
@@ -1185,95 +1185,8 @@
 g);%0A
-    %7D%0A    catch(IOException e) %7B%0A      throw new RuntimeException(e.toString());%0A    %7D%0A
   %7D%0A

Use a simpler serialization for cascading Fields to be compatible with older cascading versions