yyanyy commented on a change in pull request #1857:
URL: https://github.com/apache/iceberg/pull/1857#discussion_r539690798
##########
File path: spark3/src/test/java/org/apache/iceberg/spark/sql/TestSelect.java
##########
@@ -63,11 +63,25 @@ public void removeTables() {
@Test
public void testSelect() {
- List<Object[]> expected = ImmutableList.of(row(1L, "a"), row(2L, "b"),
row(3L, "c"));
+ List<Object[]> expected = ImmutableList.of(
+ row(1L, "a", 1.0F), row(2L, "b", 2.0F), row(3L, "c", Float.NaN));
assertEquals("Should return all expected rows", expected, sql("SELECT *
FROM %s", tableName));
}
+ @Test
+ public void testSelectRewrite() {
+ List<Object[]> expected = ImmutableList.of(row(3L, "c", Float.NaN));
+
+ assertEquals("Should return all expected rows", expected,
+ sql("SELECT * FROM %s where float = float('NaN')", tableName));
+
+ Assert.assertEquals("Should create only one scan", 1, scanEventCount);
+ Assert.assertEquals("Should push down expected filter",
+ "(float IS NOT NULL AND float = NaN)",
Review comment:
This is because in `DescribeExpressionVisitor ` we translate is_nan to
`= NaN` in
[here](https://github.com/apache/iceberg/blob/master/spark3/src/main/java/org/apache/iceberg/spark/Spark3Util.java#L536).
Do you want me to change this to `is_nan(float)`?
##########
File path: spark2/src/test/java/org/apache/iceberg/spark/source/TestSelect.java
##########
@@ -0,0 +1,230 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg.spark.source;
+
+import java.io.File;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.events.Listeners;
+import org.apache.iceberg.events.ScanEvent;
+import org.apache.iceberg.expressions.Expressions;
+import org.apache.iceberg.hadoop.HadoopTables;
+import org.apache.iceberg.relocated.com.google.common.base.Objects;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Types;
+import org.apache.spark.sql.Dataset;
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.SparkSession;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.apache.iceberg.types.Types.NestedField.optional;
+
+public class TestSelect {
+ private static final HadoopTables TABLES = new HadoopTables(new
Configuration());
+ private static final Schema SCHEMA = new Schema(
+ optional(1, "id", Types.IntegerType.get()),
+ optional(2, "data", Types.StringType.get()),
+ optional(3, "doubleVal", Types.DoubleType.get())
+ );
+
+ private static SparkSession spark;
+
+ private static int scanEventCount = 0;
+ private static ScanEvent lastScanEvent = null;
+
+ private Table table;
+
+ static {
+ Listeners.register(event -> {
+ scanEventCount += 1;
+ lastScanEvent = event;
+ }, ScanEvent.class);
+ }
+
+ @BeforeClass
+ public static void startSpark() {
+ spark = SparkSession.builder()
+ .master("local[2]")
+ .getOrCreate();
+ }
+
+ @AfterClass
+ public static void stopSpark() {
+ SparkSession currentSpark = spark;
+ spark = null;
+ currentSpark.stop();
+ }
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private String tableLocation = null;
+
+ @Before
+ public void init() throws Exception {
+ File tableDir = temp.newFolder();
+ this.tableLocation = tableDir.toURI().toString();
+
+ table = TABLES.create(SCHEMA, tableLocation);
+
+ List<Record> rows = Lists.newArrayList(
+ new Record(1, "a", 1.0),
+ new Record(2, "b", 2.0),
+ new Record(3, "c", Double.NaN)
+ );
+
+ Dataset<Row> df = spark.createDataFrame(rows, Record.class);
+
+ df.select("id", "data", "doubleVal").write()
+ .format("iceberg")
+ .mode("append")
+ .save(tableLocation);
+
+ table.refresh();
+
+ Dataset<Row> results = spark.read()
+ .format("iceberg")
+ .load(tableLocation);
+ results.createOrReplaceTempView("table");
+
+ scanEventCount = 0;
+ lastScanEvent = null;
+ }
+
+ @Test
+ public void testSelect() {
+ List<Record> expected = ImmutableList.of(
+ new Record(1, "a", 1.0), new Record(2, "b", 2.0), new Record(3, "c",
Double.NaN));
+
+ Assert.assertEquals("Should return all expected rows", expected,
sql("select * from table"));
+ }
+
+ @Test
+ public void testSelectRewrite() {
+ List<Record> expected = ImmutableList.of(new Record(3, "c", Double.NaN));
+
+ Assert.assertEquals("Should return all expected rows", expected,
+ sql("SELECT * FROM table where doubleVal = double('NaN')"));
+ Assert.assertEquals("Should create only one scan", 1, scanEventCount);
Review comment:
Yes, sorry I forgot to revisit this after cleaning up other changes.
Since in spark2 we don't have `Spark3Util.describe()` I wasn't sure to which
level we want to assert the expression, so that we can still have test coverage
without being too coupled with internal implementation. Let me know how you
think the updated test is!
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]