Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package test.org.apache.spark.sql;
0019 
0020 import java.math.BigDecimal;
0021 import java.nio.charset.StandardCharsets;
0022 import java.sql.Date;
0023 import java.sql.Timestamp;
0024 import java.util.Arrays;
0025 import java.util.HashMap;
0026 import java.util.List;
0027 import java.util.Map;
0028 
0029 import org.junit.Assert;
0030 import org.junit.Before;
0031 import org.junit.Test;
0032 
0033 import org.apache.spark.sql.Row;
0034 import org.apache.spark.sql.RowFactory;
0035 
0036 public class JavaRowSuite {
0037   private byte byteValue;
0038   private short shortValue;
0039   private int intValue;
0040   private long longValue;
0041   private float floatValue;
0042   private double doubleValue;
0043   private BigDecimal decimalValue;
0044   private boolean booleanValue;
0045   private String stringValue;
0046   private byte[] binaryValue;
0047   private Date dateValue;
0048   private Timestamp timestampValue;
0049 
0050   @Before
0051   public void setUp() {
0052     byteValue = (byte)127;
0053     shortValue = (short)32767;
0054     intValue = 2147483647;
0055     longValue = 9223372036854775807L;
0056     floatValue = 3.4028235E38f;
0057     doubleValue = 1.7976931348623157E308;
0058     decimalValue = new BigDecimal("1.7976931348623157E328");
0059     booleanValue = true;
0060     stringValue = "this is a string";
0061     binaryValue = stringValue.getBytes(StandardCharsets.UTF_8);
0062     dateValue = Date.valueOf("2014-06-30");
0063     timestampValue = Timestamp.valueOf("2014-06-30 09:20:00.0");
0064   }
0065 
0066   @Test
0067   public void constructSimpleRow() {
0068     Row simpleRow = RowFactory.create(
0069       byteValue,                 // ByteType
0070       Byte.valueOf(byteValue),
0071       shortValue,                // ShortType
0072       Short.valueOf(shortValue),
0073       intValue,                  // IntegerType
0074       Integer.valueOf(intValue),
0075       longValue,                 // LongType
0076       Long.valueOf(longValue),
0077       floatValue,                // FloatType
0078       Float.valueOf(floatValue),
0079       doubleValue,               // DoubleType
0080       Double.valueOf(doubleValue),
0081       decimalValue,              // DecimalType
0082       booleanValue,              // BooleanType
0083       Boolean.valueOf(booleanValue),
0084       stringValue,               // StringType
0085       binaryValue,               // BinaryType
0086       dateValue,                 // DateType
0087       timestampValue,            // TimestampType
0088       null                       // null
0089     );
0090 
0091     Assert.assertEquals(byteValue, simpleRow.getByte(0));
0092     Assert.assertEquals(byteValue, simpleRow.get(0));
0093     Assert.assertEquals(byteValue, simpleRow.getByte(1));
0094     Assert.assertEquals(byteValue, simpleRow.get(1));
0095     Assert.assertEquals(shortValue, simpleRow.getShort(2));
0096     Assert.assertEquals(shortValue, simpleRow.get(2));
0097     Assert.assertEquals(shortValue, simpleRow.getShort(3));
0098     Assert.assertEquals(shortValue, simpleRow.get(3));
0099     Assert.assertEquals(intValue, simpleRow.getInt(4));
0100     Assert.assertEquals(intValue, simpleRow.get(4));
0101     Assert.assertEquals(intValue, simpleRow.getInt(5));
0102     Assert.assertEquals(intValue, simpleRow.get(5));
0103     Assert.assertEquals(longValue, simpleRow.getLong(6));
0104     Assert.assertEquals(longValue, simpleRow.get(6));
0105     Assert.assertEquals(longValue, simpleRow.getLong(7));
0106     Assert.assertEquals(longValue, simpleRow.get(7));
0107     // When we create the row, we do not do any conversion
0108     // for a float/double value, so we just set the delta to 0.
0109     Assert.assertEquals(floatValue, simpleRow.getFloat(8), 0);
0110     Assert.assertEquals(floatValue, simpleRow.get(8));
0111     Assert.assertEquals(floatValue, simpleRow.getFloat(9), 0);
0112     Assert.assertEquals(floatValue, simpleRow.get(9));
0113     Assert.assertEquals(doubleValue, simpleRow.getDouble(10), 0);
0114     Assert.assertEquals(doubleValue, simpleRow.get(10));
0115     Assert.assertEquals(doubleValue, simpleRow.getDouble(11), 0);
0116     Assert.assertEquals(doubleValue, simpleRow.get(11));
0117     Assert.assertEquals(decimalValue, simpleRow.get(12));
0118     Assert.assertEquals(booleanValue, simpleRow.getBoolean(13));
0119     Assert.assertEquals(booleanValue, simpleRow.get(13));
0120     Assert.assertEquals(booleanValue, simpleRow.getBoolean(14));
0121     Assert.assertEquals(booleanValue, simpleRow.get(14));
0122     Assert.assertEquals(stringValue, simpleRow.getString(15));
0123     Assert.assertEquals(stringValue, simpleRow.get(15));
0124     Assert.assertEquals(binaryValue, simpleRow.get(16));
0125     Assert.assertEquals(dateValue, simpleRow.get(17));
0126     Assert.assertEquals(timestampValue, simpleRow.get(18));
0127     Assert.assertTrue(simpleRow.isNullAt(19));
0128     Assert.assertNull(simpleRow.get(19));
0129   }
0130 
0131   @Test
0132   public void constructComplexRow() {
0133     // Simple array
0134     List<String> simpleStringArray = Arrays.asList(
0135       stringValue + " (1)", stringValue + " (2)", stringValue + "(3)");
0136 
0137     // Simple map
0138     Map<String, Long> simpleMap = new HashMap<>();
0139     simpleMap.put(stringValue + " (1)", longValue);
0140     simpleMap.put(stringValue + " (2)", longValue - 1);
0141     simpleMap.put(stringValue + " (3)", longValue - 2);
0142 
0143     // Simple struct
0144     Row simpleStruct = RowFactory.create(
0145       doubleValue, stringValue, timestampValue, null);
0146 
0147     // Complex array
0148     @SuppressWarnings("unchecked")
0149     List<Map<String, Long>> arrayOfMaps = Arrays.asList(simpleMap);
0150     List<Row> arrayOfRows = Arrays.asList(simpleStruct);
0151 
0152     // Complex map
0153     Map<List<Row>, Row> complexMap = new HashMap<>();
0154     complexMap.put(arrayOfRows, simpleStruct);
0155 
0156     // Complex struct
0157     Row complexStruct = RowFactory.create(
0158       simpleStringArray,
0159       simpleMap,
0160       simpleStruct,
0161       arrayOfMaps,
0162       arrayOfRows,
0163       complexMap,
0164       null);
0165     Assert.assertEquals(simpleStringArray, complexStruct.get(0));
0166     Assert.assertEquals(simpleMap, complexStruct.get(1));
0167     Assert.assertEquals(simpleStruct, complexStruct.get(2));
0168     Assert.assertEquals(arrayOfMaps, complexStruct.get(3));
0169     Assert.assertEquals(arrayOfRows, complexStruct.get(4));
0170     Assert.assertEquals(complexMap, complexStruct.get(5));
0171     Assert.assertNull(complexStruct.get(6));
0172 
0173     // A very complex row
0174     Row complexRow = RowFactory.create(arrayOfMaps, arrayOfRows, complexMap, complexStruct);
0175     Assert.assertEquals(arrayOfMaps, complexRow.get(0));
0176     Assert.assertEquals(arrayOfRows, complexRow.get(1));
0177     Assert.assertEquals(complexMap, complexRow.get(2));
0178     Assert.assertEquals(complexStruct, complexRow.get(3));
0179   }
0180 }