void validateDataFrameWithBeans(Bean bean, DataFrame df) { StructType schema = df.schema(); Assert.assertEquals( new StructField("a", DoubleType$.MODULE$, false, Metadata.empty()), schema.apply("a")); Assert.assertEquals( new StructField("b", new ArrayType(IntegerType$.MODULE$, true), true, Metadata.empty()), schema.apply("b")); ArrayType valueType = new ArrayType(DataTypes.IntegerType, false); MapType mapType = new MapType(DataTypes.StringType, valueType, true); Assert.assertEquals(new StructField("c", mapType, true, Metadata.empty()), schema.apply("c")); Assert.assertEquals( new StructField("d", new ArrayType(DataTypes.StringType, true), true, Metadata.empty()), schema.apply("d")); Row first = df.select("a", "b", "c", "d").first(); Assert.assertEquals(bean.getA(), first.getDouble(0), 0.0); // Now Java lists and maps are converted to Scala Seq's and Map's. Once we get a Seq below, // verify that it has the expected length, and contains expected elements. Seq<Integer> result = first.getAs(1); Assert.assertEquals(bean.getB().length, result.length()); for (int i = 0; i < result.length(); i++) { Assert.assertEquals(bean.getB()[i], result.apply(i)); } @SuppressWarnings("unchecked") Seq<Integer> outputBuffer = (Seq<Integer>) first.getJavaMap(2).get("hello"); Assert.assertArrayEquals( bean.getC().get("hello"), Ints.toArray(JavaConverters.seqAsJavaListConverter(outputBuffer).asJava())); Seq<String> d = first.getAs(3); Assert.assertEquals(bean.getD().size(), d.length()); for (int i = 0; i < d.length(); i++) { Assert.assertEquals(bean.getD().get(i), d.apply(i)); } }
@Test public void testGetDouble() throws Exception { try (Cursor cursor = new SimpleCursor( QueryResult.newBuilder() .addFields(Field.newBuilder().setName("col1").setType(Query.Type.FLOAT64).build()) .addFields(Field.newBuilder().setName("null").setType(Query.Type.FLOAT64).build()) .addRows( Query.Row.newBuilder() .addLengths("2.5".length()) .addLengths(-1) // SQL NULL .setValues(ByteString.copyFromUtf8("2.5"))) .build())) { Row row = cursor.next(); Assert.assertNotNull(row); Assert.assertEquals(2.5, row.getDouble("col1"), 0.01); Assert.assertFalse(row.wasNull()); Assert.assertEquals(0.0, row.getDouble("null"), 0.0); Assert.assertTrue(row.wasNull()); Assert.assertEquals(null, row.getObject("null", Double.class)); Assert.assertTrue(row.wasNull()); } }