PageRenderTime 40ms CodeModel.GetById 12ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/exec/KeyWrapperFactory.java

#
Java | 238 lines | 184 code | 33 blank | 21 comment | 22 complexity | 21abe04d3a0f005d4a6b7f5cad461e62 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.ql.exec;
 20
 21import java.util.Arrays;
 22
 23import org.apache.hadoop.hive.ql.metadata.HiveException;
 24import org.apache.hadoop.hive.serde2.objectinspector.ListObjectsEqualComparer;
 25import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 26import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
 27import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils.ObjectInspectorCopyOption;
 28import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
 29import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
 30import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
 31import org.apache.hadoop.io.Text;
 32
 33public class KeyWrapperFactory {
 34  public KeyWrapperFactory(ExprNodeEvaluator[] keyFields, ObjectInspector[] keyObjectInspectors,
 35      ObjectInspector[] currentKeyObjectInspectors) {
 36    this.keyFields = keyFields;
 37    this.keyObjectInspectors = keyObjectInspectors;
 38    this.currentKeyObjectInspectors = currentKeyObjectInspectors;
 39
 40  }
 41
 42  public KeyWrapper getKeyWrapper() {
 43    if (keyFields.length == 1
 44        && TypeInfoUtils.getTypeInfoFromObjectInspector(keyObjectInspectors[0]).equals(
 45            TypeInfoFactory.stringTypeInfo)) {
 46      assert(TypeInfoUtils.getTypeInfoFromObjectInspector(currentKeyObjectInspectors[0]).equals(
 47            TypeInfoFactory.stringTypeInfo));
 48      soi_new = (StringObjectInspector) keyObjectInspectors[0];
 49      soi_copy = (StringObjectInspector) currentKeyObjectInspectors[0];
 50      return new TextKeyWrapper(false);
 51    } else {
 52      currentStructEqualComparer = new ListObjectsEqualComparer(currentKeyObjectInspectors, currentKeyObjectInspectors);
 53      newKeyStructEqualComparer = new ListObjectsEqualComparer(currentKeyObjectInspectors, keyObjectInspectors);
 54      return new ListKeyWrapper(false);
 55    }
 56  }
 57
 58  transient ExprNodeEvaluator[] keyFields;
 59  transient ObjectInspector[] keyObjectInspectors;
 60  transient ObjectInspector[] currentKeyObjectInspectors;
 61
 62
 63  transient ListObjectsEqualComparer currentStructEqualComparer;
 64  transient ListObjectsEqualComparer newKeyStructEqualComparer;
 65
 66  class ListKeyWrapper extends KeyWrapper {
 67    int hashcode;
 68    Object[] keys;
 69    // decide whether this is already in hashmap (keys in hashmap are deepcopied
 70    // version, and we need to use 'currentKeyObjectInspector').
 71    ListObjectsEqualComparer equalComparer;
 72
 73    public ListKeyWrapper(boolean isCopy) {
 74      this(-1, new Object[keyFields.length], isCopy);
 75    }
 76
 77    private ListKeyWrapper(int hashcode, Object[] copiedKeys,
 78        boolean isCopy) {
 79      super();
 80      this.hashcode = hashcode;
 81      keys = copiedKeys;
 82      setEqualComparer(isCopy);
 83    }
 84
 85    private void setEqualComparer(boolean copy) {
 86      if (!copy) {
 87        equalComparer = newKeyStructEqualComparer;
 88      } else {
 89        equalComparer = currentStructEqualComparer;
 90      }
 91    }
 92
 93    @Override
 94    public int hashCode() {
 95      return hashcode;
 96    }
 97
 98    @Override
 99    public boolean equals(Object obj) {
100      Object[] copied_in_hashmap = ((ListKeyWrapper) obj).keys;
101      return equalComparer.areEqual(copied_in_hashmap, keys);
102    }
103
104    @Override
105    public void setHashKey() {
106      hashcode = Arrays.hashCode(keys);
107    }
108
109    @Override
110    public void getNewKey(Object row, ObjectInspector rowInspector) throws HiveException {
111      // Compute the keys
112      for (int i = 0; i < keyFields.length; i++) {
113        keys[i]  = keyFields[i].evaluate(row);
114      }
115    }
116
117    @Override
118    public KeyWrapper copyKey() {
119      Object[] newDefaultKeys = deepCopyElements(keys, keyObjectInspectors,
120          ObjectInspectorCopyOption.WRITABLE);
121      return new ListKeyWrapper(hashcode, newDefaultKeys, true);
122    }
123
124    @Override
125    public void copyKey(KeyWrapper oldWrapper) {
126      ListKeyWrapper listWrapper = (ListKeyWrapper) oldWrapper;
127      hashcode = listWrapper.hashcode;
128      equalComparer = currentStructEqualComparer;
129      deepCopyElements(listWrapper.keys, keyObjectInspectors, keys,
130          ObjectInspectorCopyOption.WRITABLE);
131    }
132
133    @Override
134    public Object[] getKeyArray() {
135      return keys;
136    }
137
138    private Object[] deepCopyElements(Object[] keys,
139        ObjectInspector[] keyObjectInspectors,
140        ObjectInspectorCopyOption copyOption) {
141      Object[] result = new Object[keys.length];
142      deepCopyElements(keys, keyObjectInspectors, result, copyOption);
143      return result;
144    }
145
146    private void deepCopyElements(Object[] keys,
147        ObjectInspector[] keyObjectInspectors, Object[] result,
148        ObjectInspectorCopyOption copyOption) {
149      for (int i = 0; i < keys.length; i++) {
150        result[i] = ObjectInspectorUtils.copyToStandardObject(keys[i],
151            keyObjectInspectors[i], copyOption);
152      }
153    }
154  }
155
156  transient Object[] singleEleArray = new Object[1];
157  transient StringObjectInspector soi_new, soi_copy;
158
159  class TextKeyWrapper extends KeyWrapper {
160    int hashcode;
161    Object key;
162    boolean isCopy;
163
164    public TextKeyWrapper(boolean isCopy) {
165      this(-1, null, isCopy);
166    }
167
168    private TextKeyWrapper(int hashcode, Object key,
169        boolean isCopy) {
170      super();
171      this.hashcode = hashcode;
172      this.key = key;
173      this.isCopy = isCopy;
174    }
175
176    @Override
177    public int hashCode() {
178      return hashcode;
179    }
180
181    @Override
182    public boolean equals(Object other) {
183      Object obj = ((TextKeyWrapper) other).key;
184      Text t1;
185      Text t2;
186      if (isCopy) {
187        t1 =  soi_copy.getPrimitiveWritableObject(key);
188        t2 =  soi_copy.getPrimitiveWritableObject(obj);
189      } else {
190        t1 = soi_new.getPrimitiveWritableObject(key);
191        t2 = soi_copy.getPrimitiveWritableObject(obj);
192      }
193      if (t1 == null && t2 == null) {
194        return true;
195      } else if (t1 == null || t2 == null) {
196        return false;
197      } else {
198        return t1.equals(t2);
199      }
200    }
201
202    @Override
203    public void setHashKey() {
204      if (key == null) {
205        hashcode = 0;
206      } else{
207        hashcode = key.hashCode();
208      }
209    }
210
211    @Override
212    public void getNewKey(Object row, ObjectInspector rowInspector) throws HiveException {
213      // Compute the keys
214      key = keyFields[0].evaluate(row);
215    }
216
217    @Override
218    public KeyWrapper copyKey() {
219      return new TextKeyWrapper(hashcode, ObjectInspectorUtils.copyToStandardObject(key,
220          soi_new, ObjectInspectorCopyOption.WRITABLE), true);
221    }
222
223    @Override
224    public void copyKey(KeyWrapper oldWrapper) {
225      TextKeyWrapper textWrapper = (TextKeyWrapper) oldWrapper;
226      hashcode = textWrapper.hashcode;
227      isCopy = true;
228      key = ObjectInspectorUtils.copyToStandardObject(textWrapper.key,
229          soi_new, ObjectInspectorCopyOption.WRITABLE);
230    }
231
232    @Override
233    public Object[] getKeyArray() {
234      singleEleArray[0] = key;
235      return singleEleArray;
236    }
237  }
238}