-
Notifications
You must be signed in to change notification settings - Fork 0
/
TupleDesc.java
177 lines (162 loc) · 5.35 KB
/
TupleDesc.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
package simpledb;
import java.util.*;
/**
* TupleDesc describes the schema of a tuple.
*/
public class TupleDesc {
/**
* The Types defined in this TupleDesc.
*/
protected Type types[];
/**
* The names defined in this TupleDesc.
*/
protected String names[];
/**
* Merge two TupleDescs into one, with td1.numFields + td2.numFields fields, with the first td1.numFields coming
* from td1 and the remaining from td2.
*
* @param td1
* The TupleDesc with the first fields of the new TupleDesc
* @param td2
* The TupleDesc with the last fields of the TupleDesc
* @return the new TupleDesc
*/
public static TupleDesc combine(TupleDesc td1, TupleDesc td2) {
Type[] newTypes = new Type[td1.numFields() + td2.numFields()];
String[] newNames = new String[td1.numFields() + td2.numFields()];
for (int i = 0; i < td1.numFields(); i++) {
newTypes[i] = td1.getType(i);
newNames[i] = td1.getFieldName(i);
}
for (int i = 0; i < td2.numFields(); i++) {
newTypes[i + td1.numFields()] = td2.getType(i);
newNames[i + td1.numFields()] = td2.getFieldName(i);
}
return new TupleDesc(newTypes, newNames);
}
/**
* Create a new TupleDesc with typeAr.length fields with fields of the specified types, with associated named
* fields.
*
* @param typeAr
* array specifying the number of and types of fields in this TupleDesc. It must contain at least one
* entry.
* @param fieldAr
* array specifying the names of the fields. Note that names may be null.
*/
public TupleDesc(Type[] typeAr, String[] fieldAr) {
types = typeAr;
names = fieldAr;
}
/**
* Constructor. Create a new tuple desc with typeAr.length fields with fields of the specified types, with anonymous
* (unnamed) fields.
*
* @param typeAr
* array specifying the number of and types of fields in this TupleDesc. It must contain at least one
* entry.
*/
public TupleDesc(Type[] typeAr) {
types = typeAr;
names = new String[typeAr.length];
}
/**
* @return the number of fields in this TupleDesc
*/
public int numFields() {
return types.length;
}
/**
* Gets the (possibly null) field name of the ith field of this TupleDesc.
*
* @param i
* index of the field name to return. It must be a valid index.
* @return the name of the ith field
* @throws NoSuchElementException
* if i is not a valid field reference.
*/
public String getFieldName(int i) throws NoSuchElementException {
if (i >= 0 && i < names.length)
return names[i];
throw new NoSuchElementException("invalid field index");
}
/**
* Find the index of the field with a given name.
*
* @param name
* name of the field.
* @return the index of the field that is first to have the given name.
* @throws NoSuchElementException
* if no field with a matching name is found.
*/
public int nameToId(String name) throws NoSuchElementException {
if (names == null)
throw new NoSuchElementException("no named fields");
for (int i = 0; i < names.length; i++) {
if (names[i] != null && names[i].equals(name)) {
return i;
}
}
throw new NoSuchElementException("invalid field name: " + name);
}
/**
* Gets the type of the ith field of this TupleDesc.
*
* @param i
* The index of the field to get the type of. It must be a valid index.
* @return the type of the ith field
* @throws NoSuchElementException
* if i is not a valid field reference.
*/
public Type getType(int i) throws NoSuchElementException {
// some code goes here
if(i >= 0 && i < types.length)//checking so i is valid
return types[i];//returning the wanted type
throw new NoSuchElementException("Invalid type index");
//throw new UnsupportedOperationException("Implement this");
}
/**
* Compares the specified object with this TupleDesc for equality. Two TupleDescs are considered equal if they are
* the same size and if the n-th type in this TupleDesc is equal to the n-th type in td.
*
* @param o
* the Object to be compared for equality with this TupleDesc.
* @return true if the object is equal to this TupleDesc.
*/
public boolean equals(Object o) {
if (!(o instanceof TupleDesc))
return false;
return equals((TupleDesc) o);
}
/**
* Compares the specified object with this TupleDesc for equality. Two TupleDescs are considered equal if they are
* the same size and if the n-th type in this TupleDesc is equal to the n-th type in td.
*
* @param o
* the TupleDesc to be compared for equality with this TupleDesc.
* @return true if the TupleDesc is equal to this TupleDesc.
*/
public boolean equals(TupleDesc o) {
if (types.length != o.types.length)
return false;
for (int i = 0; i < types.length; i++)
if (!types[i].equals(o.types[i]))
return false;
return true;
}
/**
* Returns a String describing this descriptor. It should be of the form
* "fieldType[0](fieldName[0])|fieldType[1](fieldName[1])|...|fieldType[M](fieldName[M])".
*
* @return String describing this descriptor.
*/
public String toString() {
// some code goes here
System.out.print(types[0]+ "(" +names[0]+ ")" );
for(int i = 1; i < types.length; i++){
System.out.print("|" + types[i] + "(" + names[i] + ")" );
}
throw new UnsupportedOperationException("Implement this");
}
}