PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToString.java

#
Java | 125 lines | 90 code | 14 blank | 21 comment | 21 complexity | a1d5be28bd44edbf67e108521c8c0d08 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.ByteStream;
  21. import org.apache.hadoop.hive.serde2.io.ByteWritable;
  22. import org.apache.hadoop.hive.serde2.io.DoubleWritable;
  23. import org.apache.hadoop.hive.serde2.io.ShortWritable;
  24. import org.apache.hadoop.hive.serde2.lazy.LazyInteger;
  25. import org.apache.hadoop.hive.serde2.lazy.LazyLong;
  26. import org.apache.hadoop.io.BooleanWritable;
  27. import org.apache.hadoop.io.FloatWritable;
  28. import org.apache.hadoop.io.IntWritable;
  29. import org.apache.hadoop.io.LongWritable;
  30. import org.apache.hadoop.io.NullWritable;
  31. import org.apache.hadoop.io.Text;
  32. /**
  33. * UDFToString.
  34. *
  35. */
  36. public class UDFToString extends UDF {
  37. private Text t = new Text();
  38. private ByteStream.Output out = new ByteStream.Output();
  39. public UDFToString() {
  40. }
  41. public Text evaluate(NullWritable i) {
  42. return null;
  43. }
  44. private byte[] trueBytes = {'T', 'R', 'U', 'E'};
  45. private byte[] falseBytes = {'F', 'A', 'L', 'S', 'E'};
  46. public Text evaluate(BooleanWritable i) {
  47. if (i == null) {
  48. return null;
  49. } else {
  50. t.clear();
  51. t.set(i.get() ? trueBytes : falseBytes);
  52. return t;
  53. }
  54. }
  55. public Text evaluate(ByteWritable i) {
  56. if (i == null) {
  57. return null;
  58. } else {
  59. out.reset();
  60. LazyInteger.writeUTF8NoException(out, i.get());
  61. t.set(out.getData(), 0, out.getCount());
  62. return t;
  63. }
  64. }
  65. public Text evaluate(ShortWritable i) {
  66. if (i == null) {
  67. return null;
  68. } else {
  69. out.reset();
  70. LazyInteger.writeUTF8NoException(out, i.get());
  71. t.set(out.getData(), 0, out.getCount());
  72. return t;
  73. }
  74. }
  75. public Text evaluate(IntWritable i) {
  76. if (i == null) {
  77. return null;
  78. } else {
  79. out.reset();
  80. LazyInteger.writeUTF8NoException(out, i.get());
  81. t.set(out.getData(), 0, out.getCount());
  82. return t;
  83. }
  84. }
  85. public Text evaluate(LongWritable i) {
  86. if (i == null) {
  87. return null;
  88. } else {
  89. out.reset();
  90. LazyLong.writeUTF8NoException(out, i.get());
  91. t.set(out.getData(), 0, out.getCount());
  92. return t;
  93. }
  94. }
  95. public Text evaluate(FloatWritable i) {
  96. if (i == null) {
  97. return null;
  98. } else {
  99. t.set(i.toString());
  100. return t;
  101. }
  102. }
  103. public Text evaluate(DoubleWritable i) {
  104. if (i == null) {
  105. return null;
  106. } else {
  107. t.set(i.toString());
  108. return t;
  109. }
  110. }
  111. }