PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ql/src/java/org/apache/hadoop/hive/ql/exec/persistence/UnwrapRowContainer.java

https://github.com/apache/hive
Java | 165 lines | 118 code | 26 blank | 21 comment | 15 complexity | ba133fa25e81f216c0fd14e5120907aa MD5 | raw file
Possible License(s): Apache-2.0
  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. package org.apache.hadoop.hive.ql.exec.persistence;
  19. import java.io.IOException;
  20. import java.io.ObjectOutputStream;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import org.apache.hadoop.hive.ql.metadata.HiveException;
  25. import org.apache.hadoop.hive.serde2.SerDeException;
  26. import org.apache.hadoop.hive.serde2.io.ShortWritable;
  27. import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter;
  28. /**
  29. * Unwraps values from current key with valueIndex in mapjoin desc
  30. */
  31. public class UnwrapRowContainer
  32. implements MapJoinRowContainer, AbstractRowContainer.RowIterator<List<Object>> {
  33. private final byte alias;
  34. private final int[] valueIndex;
  35. private final Converter[] converters;
  36. private final boolean tagged;
  37. private final List<Object> unwrapped;
  38. private transient Object[] currentKey;
  39. private transient MapJoinRowContainer internal;
  40. private transient RowIterator<List<Object>> iterator;
  41. public UnwrapRowContainer(byte alias, int[] valueIndex, Converter[] converters, boolean tagged) {
  42. this.alias = alias;
  43. this.valueIndex = valueIndex;
  44. this.converters = converters;
  45. this.tagged = tagged;
  46. this.unwrapped = new ArrayList<Object>();
  47. }
  48. public MapJoinRowContainer setInternal(MapJoinRowContainer internal, Object[] currentKey) {
  49. this.internal = internal;
  50. this.currentKey = currentKey;
  51. return this;
  52. }
  53. @Override
  54. public List<Object> first() throws HiveException {
  55. iterator = internal.rowIter();
  56. return unwrap(iterator.first());
  57. }
  58. @Override
  59. public List<Object> next() throws HiveException {
  60. return unwrap(iterator.next());
  61. }
  62. private static final ShortWritable ALL_ALIAS_FILTER_SHORT_WRITABLE = new ShortWritable((byte) 0xff);
  63. private List<Object> unwrap(List<Object> values) {
  64. if (values == null) {
  65. return null;
  66. }
  67. unwrapped.clear();
  68. for (int pos = 0; pos < valueIndex.length; pos++) {
  69. int index = valueIndex[pos];
  70. if (index >= 0) {
  71. if (currentKey == null) {
  72. unwrapped.add(null);
  73. } else if (converters[pos] != null) {
  74. unwrapped.add(converters[pos].convert(currentKey[index]));
  75. } else {
  76. unwrapped.add(currentKey[index]);
  77. }
  78. } else {
  79. unwrapped.add(values.get(-index - 1));
  80. }
  81. }
  82. if (tagged) {
  83. // Append filter tag.
  84. final int size = values.size();
  85. if (size == 0) {
  86. unwrapped.add(ALL_ALIAS_FILTER_SHORT_WRITABLE);
  87. } else {
  88. unwrapped.add(values.get(size - 1));
  89. }
  90. }
  91. return unwrapped;
  92. }
  93. @Override
  94. public RowIterator<List<Object>> rowIter() throws HiveException {
  95. return this;
  96. }
  97. @Override
  98. public void addRow(List<Object> t) throws HiveException {
  99. internal.addRow(t);
  100. }
  101. @Override
  102. public boolean hasRows() throws HiveException {
  103. return internal.hasRows();
  104. }
  105. @Override
  106. public boolean isSingleRow() throws HiveException {
  107. return internal.isSingleRow();
  108. }
  109. @Override
  110. public int rowCount() throws HiveException {
  111. return internal.rowCount();
  112. }
  113. @Override
  114. public void clearRows() throws HiveException {
  115. internal.clearRows();
  116. }
  117. @Override
  118. public byte getAliasFilter() throws HiveException {
  119. return internal.getAliasFilter();
  120. }
  121. @Override
  122. public MapJoinRowContainer copy() throws HiveException {
  123. internal = internal.copy();
  124. return this;
  125. }
  126. @Override
  127. public void addRow(Object[] value) throws HiveException {
  128. internal.addRow(value);
  129. }
  130. @Override
  131. public void write(MapJoinObjectSerDeContext valueContext, ObjectOutputStream out)
  132. throws IOException, SerDeException {
  133. internal.write(valueContext, out);
  134. }
  135. @Override
  136. public String toString() {
  137. return alias + (tagged ? ":TAGGED" : "") + Arrays.toString(valueIndex);
  138. }
  139. }