|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.aliyun.odps.local.common.utils; |
| 21 | + |
| 22 | +import com.aliyun.odps.data.Binary; |
| 23 | +import com.aliyun.odps.data.Char; |
| 24 | +import com.aliyun.odps.data.IntervalDayTime; |
| 25 | +import com.aliyun.odps.data.IntervalYearMonth; |
| 26 | +import com.aliyun.odps.data.SimpleStruct; |
| 27 | +import com.aliyun.odps.data.Varchar; |
| 28 | +import com.aliyun.odps.io.ArrayWritable; |
| 29 | +import com.aliyun.odps.io.BigDecimalWritable; |
| 30 | +import com.aliyun.odps.io.ByteWritable; |
| 31 | +import com.aliyun.odps.io.BytesWritable; |
| 32 | +import com.aliyun.odps.io.CharWritable; |
| 33 | +import com.aliyun.odps.io.DateWritable; |
| 34 | +import com.aliyun.odps.io.DatetimeWritable; |
| 35 | +import com.aliyun.odps.io.FloatWritable; |
| 36 | +import com.aliyun.odps.io.IntervalDayTimeWritable; |
| 37 | +import com.aliyun.odps.io.IntervalYearMonthWritable; |
| 38 | +import com.aliyun.odps.io.MapWritable; |
| 39 | +import com.aliyun.odps.io.ShortWritable; |
| 40 | +import com.aliyun.odps.io.StructWritable; |
| 41 | +import com.aliyun.odps.io.TimestampWritable; |
| 42 | +import com.aliyun.odps.io.VarcharWritable; |
| 43 | +import com.aliyun.odps.type.ArrayTypeInfo; |
| 44 | +import com.aliyun.odps.type.CharTypeInfo; |
| 45 | +import com.aliyun.odps.type.MapTypeInfo; |
| 46 | +import com.aliyun.odps.type.StructTypeInfo; |
| 47 | +import com.aliyun.odps.type.TypeInfo; |
| 48 | +import com.aliyun.odps.type.TypeInfoFactory; |
| 49 | +import com.aliyun.odps.type.VarcharTypeInfo; |
| 50 | +import java.math.BigDecimal; |
| 51 | +import java.sql.Timestamp; |
| 52 | +import java.util.ArrayList; |
| 53 | +import java.util.Arrays; |
| 54 | + |
| 55 | +import com.aliyun.odps.io.BooleanWritable; |
| 56 | +import com.aliyun.odps.io.DoubleWritable; |
| 57 | +import com.aliyun.odps.io.IntWritable; |
| 58 | +import com.aliyun.odps.io.LongWritable; |
| 59 | +import com.aliyun.odps.io.Text; |
| 60 | +import com.aliyun.odps.io.Writable; |
| 61 | +import java.util.HashMap; |
| 62 | +import java.util.List; |
| 63 | +import java.util.Map; |
| 64 | + |
| 65 | +public class LocalWritableUtils { |
| 66 | + |
| 67 | + public static Writable[] convert(Object[] args, List<TypeInfo> types) { |
| 68 | + if (args == null) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + Writable[] result = new Writable[args.length]; |
| 72 | + for (int i = 0; i < args.length; i++) { |
| 73 | + TypeInfo typeInfo = types != null ? types.get(i) : null; |
| 74 | + result[i] = convert(args[i], typeInfo); |
| 75 | + } |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + public static Writable convert(Object arg0, TypeInfo typeInfo) { |
| 80 | + if (arg0 == null) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + if (typeInfo == TypeInfoFactory.STRING || arg0 instanceof String) { |
| 84 | + if (arg0 instanceof String) { |
| 85 | + return new Text((String) arg0); |
| 86 | + } else { |
| 87 | + byte[] bytes = (byte[]) arg0; |
| 88 | + return new Text(bytes); |
| 89 | + } |
| 90 | + } |
| 91 | + if (typeInfo == TypeInfoFactory.TINYINT || arg0 instanceof Byte) { |
| 92 | + return new ByteWritable((Byte) arg0); |
| 93 | + } |
| 94 | + if (typeInfo == TypeInfoFactory.SMALLINT || arg0 instanceof Short) { |
| 95 | + return new ShortWritable((Short) arg0); |
| 96 | + } |
| 97 | + if (typeInfo == TypeInfoFactory.INT || arg0 instanceof Integer) { |
| 98 | + return new IntWritable((Integer) arg0); |
| 99 | + } |
| 100 | + if (typeInfo == TypeInfoFactory.BIGINT || arg0 instanceof Long) { |
| 101 | + return new LongWritable((Long) arg0); |
| 102 | + } |
| 103 | + if (typeInfo == TypeInfoFactory.BOOLEAN || arg0 instanceof Boolean) { |
| 104 | + return new BooleanWritable((Boolean) arg0); |
| 105 | + } |
| 106 | + if (typeInfo == TypeInfoFactory.FLOAT || arg0 instanceof Float) { |
| 107 | + return new FloatWritable((Float) arg0); |
| 108 | + } |
| 109 | + if (typeInfo == TypeInfoFactory.DOUBLE || arg0 instanceof Double) { |
| 110 | + return new DoubleWritable((Double) arg0); |
| 111 | + } |
| 112 | + if (typeInfo == TypeInfoFactory.DECIMAL || arg0 instanceof BigDecimal) { |
| 113 | + return new BigDecimalWritable((BigDecimal) arg0); |
| 114 | + } |
| 115 | + if (typeInfo == TypeInfoFactory.DATETIME || arg0 instanceof java.util.Date) { |
| 116 | + return new DatetimeWritable(((java.util.Date)arg0).getTime()); |
| 117 | + } |
| 118 | + if (typeInfo == TypeInfoFactory.TIMESTAMP || arg0 instanceof Timestamp) { |
| 119 | + TimestampWritable temp = new TimestampWritable(); |
| 120 | + temp.setTimestamp((Timestamp) arg0); |
| 121 | + return temp; |
| 122 | + } |
| 123 | + if (typeInfo == TypeInfoFactory.DATE || arg0 instanceof java.sql.Date) { |
| 124 | + return new DateWritable(((java.sql.Date) arg0).getTime()/1000); |
| 125 | + } |
| 126 | + if (typeInfo instanceof CharTypeInfo || arg0 instanceof Char) { |
| 127 | + return new CharWritable((Char) arg0); |
| 128 | + } |
| 129 | + if (typeInfo instanceof VarcharTypeInfo || arg0 instanceof Varchar) { |
| 130 | + return new VarcharWritable((Varchar) arg0); |
| 131 | + } |
| 132 | + if (typeInfo == TypeInfoFactory.BINARY || arg0 instanceof Binary) { |
| 133 | + return new BytesWritable(((Binary) arg0).data()); |
| 134 | + } |
| 135 | + if (typeInfo == TypeInfoFactory.INTERVAL_DAY_TIME || arg0 instanceof IntervalDayTime) { |
| 136 | + return new IntervalDayTimeWritable((IntervalDayTime) arg0); |
| 137 | + } |
| 138 | + if (typeInfo == TypeInfoFactory.INTERVAL_YEAR_MONTH || arg0 instanceof IntervalYearMonth) { |
| 139 | + return new IntervalYearMonthWritable((IntervalYearMonth) arg0); |
| 140 | + } |
| 141 | + if (typeInfo instanceof ArrayTypeInfo) { |
| 142 | + TypeInfo subType = ((ArrayTypeInfo) typeInfo).getElementTypeInfo(); |
| 143 | + List list = (List) arg0; |
| 144 | + Writable[] writables = new Writable[list.size()]; |
| 145 | + for (int i = 0; i < writables.length; i++) { |
| 146 | + Object ele = list.get(i); |
| 147 | + writables[i] = convert(ele, subType); |
| 148 | + } |
| 149 | + Class subClazz = getWritableClass(subType); |
| 150 | + return new ArrayWritable(subClazz, writables); |
| 151 | + } |
| 152 | + if (typeInfo instanceof MapTypeInfo) { |
| 153 | + TypeInfo keyType = ((MapTypeInfo) typeInfo).getKeyTypeInfo(); |
| 154 | + TypeInfo valueType = ((MapTypeInfo) typeInfo).getValueTypeInfo(); |
| 155 | + Map map = (Map) arg0; |
| 156 | + MapWritable result = new MapWritable(); |
| 157 | + for (Object entry : map.entrySet()) { |
| 158 | + Map.Entry mapEntry = (Map.Entry) entry; |
| 159 | + Writable key = convert(mapEntry.getKey(), keyType); |
| 160 | + Writable value = null; |
| 161 | + if (mapEntry.getValue() != null) { |
| 162 | + value = convert(mapEntry.getValue(), valueType); |
| 163 | + } |
| 164 | + result.put(key, value); |
| 165 | + } |
| 166 | + return result; |
| 167 | + } |
| 168 | + if (typeInfo instanceof StructTypeInfo) { |
| 169 | + List<TypeInfo> fieldTypes = ((StructTypeInfo)typeInfo).getFieldTypeInfos(); |
| 170 | + List<Object> fieldValues = ((SimpleStruct) arg0).getFieldValues(); |
| 171 | + assert fieldTypes.size() == fieldValues.size(); |
| 172 | + List<Writable> writables = new ArrayList<>(); |
| 173 | + for (int i = 0; i < fieldValues.size(); i++) { |
| 174 | + Object val = fieldValues.get(i); |
| 175 | + TypeInfo type = fieldTypes.get(i); |
| 176 | + writables.add(convert(val, type)); |
| 177 | + } |
| 178 | + return new StructWritable(writables); |
| 179 | + } |
| 180 | + |
| 181 | + throw new IllegalArgumentException("unsupported data type:" + arg0.getClass().getName()); |
| 182 | + } |
| 183 | + |
| 184 | + public static Object convert(Writable arg0, TypeInfo typeInfo) { |
| 185 | + if (arg0 == null) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + if (typeInfo == TypeInfoFactory.STRING || arg0 instanceof Text) { |
| 189 | + Text text = (Text) arg0; |
| 190 | + return new String(text.getBytes(), 0, text.getLength(), TypeConvertUtils.UTF8); |
| 191 | + } |
| 192 | + if (typeInfo == TypeInfoFactory.TINYINT || arg0 instanceof ByteWritable) { |
| 193 | + return ((ByteWritable) arg0).get(); |
| 194 | + } |
| 195 | + if (typeInfo == TypeInfoFactory.SMALLINT || arg0 instanceof ShortWritable) { |
| 196 | + return ((ShortWritable) arg0).get(); |
| 197 | + } |
| 198 | + if (typeInfo == TypeInfoFactory.INT || arg0 instanceof IntWritable) { |
| 199 | + return ((IntWritable) arg0).get(); |
| 200 | + } |
| 201 | + if (typeInfo == TypeInfoFactory.BIGINT || arg0 instanceof LongWritable) { |
| 202 | + return ((LongWritable) arg0).get(); |
| 203 | + } |
| 204 | + if (typeInfo == TypeInfoFactory.BOOLEAN || arg0 instanceof BooleanWritable) { |
| 205 | + return ((BooleanWritable) arg0).get(); |
| 206 | + } |
| 207 | + if (typeInfo == TypeInfoFactory.FLOAT || arg0 instanceof FloatWritable) { |
| 208 | + return ((FloatWritable) arg0).get(); |
| 209 | + } |
| 210 | + if (typeInfo == TypeInfoFactory.DOUBLE || arg0 instanceof DoubleWritable) { |
| 211 | + return ((DoubleWritable) arg0).get(); |
| 212 | + } |
| 213 | + if (typeInfo == TypeInfoFactory.DECIMAL || arg0 instanceof BigDecimalWritable) { |
| 214 | + return ((BigDecimalWritable) arg0).get(); |
| 215 | + } |
| 216 | + if (typeInfo == TypeInfoFactory.DATETIME || arg0 instanceof DatetimeWritable) { |
| 217 | + return ((DatetimeWritable) arg0).getDatetime(); |
| 218 | + } |
| 219 | + if (typeInfo == TypeInfoFactory.TIMESTAMP || arg0 instanceof TimestampWritable) { |
| 220 | + return ((TimestampWritable)arg0).getTimestamp(); |
| 221 | + } |
| 222 | + if (typeInfo == TypeInfoFactory.DATE || arg0 instanceof DateWritable) { |
| 223 | + return ((DateWritable)arg0).getDate(); |
| 224 | + } |
| 225 | + if (typeInfo instanceof CharTypeInfo || arg0 instanceof CharWritable) { |
| 226 | + return ((CharWritable) arg0).get(); |
| 227 | + } |
| 228 | + if (typeInfo instanceof VarcharTypeInfo || arg0 instanceof VarcharWritable) { |
| 229 | + return ((VarcharWritable) arg0).get(); |
| 230 | + } |
| 231 | + if (typeInfo == TypeInfoFactory.BINARY || arg0 instanceof BytesWritable) { |
| 232 | + BytesWritable tmp = (BytesWritable) arg0; |
| 233 | + byte[] bytes = Arrays.copyOfRange(tmp.getBytes(), 0, tmp.getLength()); |
| 234 | + return new Binary(bytes); |
| 235 | + } |
| 236 | + if (typeInfo == TypeInfoFactory.INTERVAL_DAY_TIME || arg0 instanceof IntervalDayTimeWritable) { |
| 237 | + return ((IntervalDayTimeWritable) arg0).get(); |
| 238 | + } |
| 239 | + if (typeInfo == TypeInfoFactory.INTERVAL_YEAR_MONTH || arg0 instanceof IntervalYearMonthWritable) { |
| 240 | + return ((IntervalYearMonthWritable) arg0).get(); |
| 241 | + } |
| 242 | + if (typeInfo instanceof ArrayTypeInfo) { |
| 243 | + TypeInfo subType = ((ArrayTypeInfo) typeInfo).getElementTypeInfo(); |
| 244 | + List<Object> res = new ArrayList<>(); |
| 245 | + for (Writable writable : ((ArrayWritable) arg0).get()) { |
| 246 | + res.add(convert(writable, subType)); |
| 247 | + } |
| 248 | + return res; |
| 249 | + } |
| 250 | + if (typeInfo instanceof MapTypeInfo) { |
| 251 | + TypeInfo keyType = ((MapTypeInfo) typeInfo).getKeyTypeInfo(); |
| 252 | + TypeInfo valueType = ((MapTypeInfo) typeInfo).getValueTypeInfo(); |
| 253 | + Map map = new HashMap(); |
| 254 | + for (Map.Entry<Writable, Writable> entry : ((MapWritable) arg0).entrySet()) { |
| 255 | + Object key = convert(entry.getKey(), keyType); |
| 256 | + Object value = convert(entry.getValue(), valueType); |
| 257 | + map.put(key, value); |
| 258 | + } |
| 259 | + return map; |
| 260 | + } |
| 261 | + if (typeInfo instanceof StructTypeInfo) { |
| 262 | + List<TypeInfo> fieldTypes = ((StructTypeInfo)typeInfo).getFieldTypeInfos(); |
| 263 | + List<Writable> writables = ((StructWritable) arg0).get(); |
| 264 | + assert fieldTypes.size() == writables.size(); |
| 265 | + List<Object> elements = new ArrayList<>(writables.size()); |
| 266 | + for (int i = 0; i < writables.size(); i++) { |
| 267 | + Writable val = writables.get(i); |
| 268 | + TypeInfo type = fieldTypes.get(i); |
| 269 | + elements.add(convert(val, type)); |
| 270 | + } |
| 271 | + return new SimpleStruct((StructTypeInfo)typeInfo, elements); |
| 272 | + } |
| 273 | + |
| 274 | + throw new IllegalArgumentException("unsupported data type:" + arg0.getClass().getName()); |
| 275 | + } |
| 276 | + |
| 277 | + public static Class getWritableClass(TypeInfo typeInfo) { |
| 278 | + if (typeInfo == TypeInfoFactory.STRING) { |
| 279 | + return Text.class; |
| 280 | + } |
| 281 | + if (typeInfo == TypeInfoFactory.TINYINT) { |
| 282 | + return ByteWritable.class; |
| 283 | + } |
| 284 | + if (typeInfo == TypeInfoFactory.SMALLINT) { |
| 285 | + return ShortWritable.class; |
| 286 | + } |
| 287 | + if (typeInfo == TypeInfoFactory.INT) { |
| 288 | + return IntWritable.class; |
| 289 | + } |
| 290 | + if (typeInfo == TypeInfoFactory.BIGINT) { |
| 291 | + return LongWritable.class; |
| 292 | + } |
| 293 | + if (typeInfo == TypeInfoFactory.BOOLEAN) { |
| 294 | + return BooleanWritable.class; |
| 295 | + } |
| 296 | + if (typeInfo == TypeInfoFactory.FLOAT) { |
| 297 | + return FloatWritable.class; |
| 298 | + } |
| 299 | + if (typeInfo == TypeInfoFactory.DOUBLE) { |
| 300 | + return DoubleWritable.class; |
| 301 | + } |
| 302 | + if (typeInfo == TypeInfoFactory.DECIMAL) { |
| 303 | + return BigDecimalWritable.class; |
| 304 | + } |
| 305 | + if (typeInfo == TypeInfoFactory.DATETIME) { |
| 306 | + return DatetimeWritable.class; |
| 307 | + } |
| 308 | + if (typeInfo == TypeInfoFactory.TIMESTAMP) { |
| 309 | + return TimestampWritable.class; |
| 310 | + } |
| 311 | + if (typeInfo == TypeInfoFactory.DATE) { |
| 312 | + return DateWritable.class; |
| 313 | + } |
| 314 | + if (typeInfo instanceof CharTypeInfo) { |
| 315 | + return CharWritable.class; |
| 316 | + } |
| 317 | + if (typeInfo instanceof VarcharTypeInfo) { |
| 318 | + return VarcharWritable.class; |
| 319 | + } |
| 320 | + if (typeInfo == TypeInfoFactory.BINARY) { |
| 321 | + return BytesWritable.class; |
| 322 | + } |
| 323 | + if (typeInfo == TypeInfoFactory.INTERVAL_DAY_TIME) { |
| 324 | + return IntervalDayTimeWritable.class; |
| 325 | + } |
| 326 | + if (typeInfo == TypeInfoFactory.INTERVAL_YEAR_MONTH) { |
| 327 | + return IntervalYearMonth.class; |
| 328 | + } |
| 329 | + if (typeInfo instanceof ArrayTypeInfo) { |
| 330 | + return ArrayWritable.class; |
| 331 | + } |
| 332 | + if (typeInfo instanceof MapTypeInfo) { |
| 333 | + return MapWritable.class; |
| 334 | + } |
| 335 | + if (typeInfo instanceof StructTypeInfo) { |
| 336 | + return StructWritable.class; |
| 337 | + } |
| 338 | + throw new IllegalArgumentException("unsupported data type:" + typeInfo.getTypeName()); |
| 339 | + } |
| 340 | + |
| 341 | +} |
0 commit comments