Bringing tree up to date.
[galago.git] / java / pig-galago / src / com / yahoo / pig / impl / eval / ColEvalItem.java
blob926c91cf344f472edad6c3302d7186718b1cd80c
1 /*
2 * Copyright (c) 2007 Yahoo! Inc. All rights reserved.
3 * See accompanying LICENSE file.
4 */
5 package com.yahoo.pig.impl.eval;
7 import java.io.IOException;
8 import java.util.*;
10 import com.yahoo.pig.data.*;
11 import com.yahoo.pig.impl.PigContext;
12 import com.yahoo.pig.impl.logicalLayer.schema.SchemaItem;
13 import com.yahoo.pig.impl.logicalLayer.schema.SchemaItemList;
16 final public class ColEvalItem extends NestableEvalItem {
17 public String alias = null;
18 public int colNum = -1;
20 public ColEvalItem(PigContext pigContext, int colNum) {
21 super(pigContext);
22 this.colNum = colNum;
25 public ColEvalItem(PigContext pigContext, String alias) {
26 super(pigContext);
27 this.alias = alias;
30 public String toString() {
31 StringBuffer sb = new StringBuffer();
32 if (colNum != -1) sb.append("$" + colNum);
33 else sb.append(alias);
34 if (nestedEval != null) sb.append(nestedEval);
36 if (subColSpec != null) sb.append(".(" + subColSpec + ")");
38 return sb.toString();
41 public List<String> getFuncs() {
42 List<String> funcs = new ArrayList<String>();
44 if (nestedEval != null) funcs.addAll(nestedEval.getFuncs());
46 if (subColSpec != null) funcs.addAll(subColSpec.getFuncs());
48 return funcs;
51 public String stringVal(Tuple tup) throws IOException {
52 return tup.getAtomField(colNum).strval();
56 @Override
57 public Double doubleVal(Tuple tup) throws IOException {
58 return tup.getAtomField(colNum).numval();
62 @Override
63 public SchemaItem mapInputSchemaInitial(SchemaItem input){
64 if (colNum == -1 || input == null)
65 return new SchemaItemList();
66 SchemaItem output = input.schemaFor(colNum);
67 if (output == null)
68 output = new SchemaItemList();
69 return output;
72 @Override
73 public void eval(Tuple input, DataCollector output) throws IOException {
75 Datum d = null;
76 try {
77 d = input.getField(colNum);
78 } catch (IOException e) {
79 System.out.println("Warning: tuple does not contain field #" + colNum + "; substituting empty string.");
80 output.add(new Tuple(new DataAtom())); // insert default value
81 return;
84 Datum amendKey = null;
85 if (input instanceof AmendableTuple) amendKey = ((AmendableTuple) input).getAmendKey();
87 if (d instanceof DataAtom) atomEval((DataAtom) d, output);
88 else if (d instanceof Tuple) tupleEval((Tuple) d, output);
89 else bagEval((DataBag) d, amendKey, output);
92 @Override
93 public Datum simpleEval(Tuple input) throws IOException {
95 Datum d = null;
96 try {
97 d = input.getField(colNum);
98 }catch(ArrayIndexOutOfBoundsException exp) {
99 throw new RuntimeException("ArrayException: |" + input + "| --> |" + colNum + "| |" + alias + "|");
100 } catch (IOException e) {
101 System.out.println("Warning: tuple does not contain field #" + colNum + "; substituting empty string.");
102 return new DataAtom(); // insert default value
105 if (nestedEval == null) return d;
106 else return nestedEval.simpleEval(d);