2 * Copyright (c) 2007 Yahoo! Inc. All rights reserved.
3 * See accompanying LICENSE file.
5 package com
.yahoo
.pig
.impl
.eval
;
7 import java
.io
.IOException
;
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
) {
25 public ColEvalItem(PigContext pigContext
, String 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
+ ")");
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());
51 public String
stringVal(Tuple tup
) throws IOException
{
52 return tup
.getAtomField(colNum
).strval();
57 public Double
doubleVal(Tuple tup
) throws IOException
{
58 return tup
.getAtomField(colNum
).numval();
63 public SchemaItem
mapInputSchemaInitial(SchemaItem input
){
64 if (colNum
== -1 || input
== null)
65 return new SchemaItemList();
66 SchemaItem output
= input
.schemaFor(colNum
);
68 output
= new SchemaItemList();
73 public void eval(Tuple input
, DataCollector output
) throws IOException
{
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
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
);
93 public Datum
simpleEval(Tuple input
) throws IOException
{
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
);