PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java

#
Java | 174 lines | 81 code | 13 blank | 80 comment | 21 complexity | f8b9ecfd0593e01f8fda2db707968804 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.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.udf;
  19. import org.apache.hadoop.hive.ql.exec.UDF;
  20. import org.apache.hadoop.hive.serde2.io.ByteWritable;
  21. import org.apache.hadoop.hive.serde2.io.DoubleWritable;
  22. import org.apache.hadoop.hive.serde2.io.ShortWritable;
  23. import org.apache.hadoop.hive.serde2.lazy.LazyInteger;
  24. import org.apache.hadoop.io.BooleanWritable;
  25. import org.apache.hadoop.io.FloatWritable;
  26. import org.apache.hadoop.io.IntWritable;
  27. import org.apache.hadoop.io.LongWritable;
  28. import org.apache.hadoop.io.NullWritable;
  29. import org.apache.hadoop.io.Text;
  30. /**
  31. * UDFToInteger.
  32. *
  33. */
  34. public class UDFToInteger extends UDF {
  35. private IntWritable intWritable = new IntWritable();
  36. public UDFToInteger() {
  37. }
  38. /**
  39. * Convert from void to an integer. This is called for CAST(... AS INT)
  40. *
  41. * @param i
  42. * The void value to convert
  43. * @return Integer
  44. */
  45. public IntWritable evaluate(NullWritable i) {
  46. return null;
  47. }
  48. /**
  49. * Convert from boolean to an integer. This is called for CAST(... AS INT)
  50. *
  51. * @param i
  52. * The boolean value to convert
  53. * @return IntWritable
  54. */
  55. public IntWritable evaluate(BooleanWritable i) {
  56. if (i == null) {
  57. return null;
  58. } else {
  59. intWritable.set(i.get() ? 1 : 0);
  60. return intWritable;
  61. }
  62. }
  63. /**
  64. * Convert from byte to an integer. This is called for CAST(... AS INT)
  65. *
  66. * @param i
  67. * The byte value to convert
  68. * @return IntWritable
  69. */
  70. public IntWritable evaluate(ByteWritable i) {
  71. if (i == null) {
  72. return null;
  73. } else {
  74. intWritable.set(i.get());
  75. return intWritable;
  76. }
  77. }
  78. /**
  79. * Convert from short to an integer. This is called for CAST(... AS INT)
  80. *
  81. * @param i
  82. * The short value to convert
  83. * @return IntWritable
  84. */
  85. public IntWritable evaluate(ShortWritable i) {
  86. if (i == null) {
  87. return null;
  88. } else {
  89. intWritable.set(i.get());
  90. return intWritable;
  91. }
  92. }
  93. /**
  94. * Convert from long to an integer. This is called for CAST(... AS INT)
  95. *
  96. * @param i
  97. * The long value to convert
  98. * @return IntWritable
  99. */
  100. public IntWritable evaluate(LongWritable i) {
  101. if (i == null) {
  102. return null;
  103. } else {
  104. intWritable.set((int) i.get());
  105. return intWritable;
  106. }
  107. }
  108. /**
  109. * Convert from float to an integer. This is called for CAST(... AS INT)
  110. *
  111. * @param i
  112. * The float value to convert
  113. * @return IntWritable
  114. */
  115. public IntWritable evaluate(FloatWritable i) {
  116. if (i == null) {
  117. return null;
  118. } else {
  119. intWritable.set((int) i.get());
  120. return intWritable;
  121. }
  122. }
  123. /**
  124. * Convert from double to an integer. This is called for CAST(... AS INT)
  125. *
  126. * @param i
  127. * The double value to convert
  128. * @return IntWritable
  129. */
  130. public IntWritable evaluate(DoubleWritable i) {
  131. if (i == null) {
  132. return null;
  133. } else {
  134. intWritable.set((int) i.get());
  135. return intWritable;
  136. }
  137. }
  138. /**
  139. * Convert from string to an integer. This is called for CAST(... AS INT)
  140. *
  141. * @param i
  142. * The string value to convert
  143. * @return IntWritable
  144. */
  145. public IntWritable evaluate(Text i) {
  146. if (i == null) {
  147. return null;
  148. } else {
  149. try {
  150. intWritable.set(LazyInteger
  151. .parseInt(i.getBytes(), 0, i.getLength(), 10));
  152. return intWritable;
  153. } catch (NumberFormatException e) {
  154. // MySQL returns 0 if the string is not a well-formed numeric value.
  155. // return IntWritable.valueOf(0);
  156. // But we decided to return NULL instead, which is more conservative.
  157. return null;
  158. }
  159. }
  160. }
  161. }