Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.hadoop.hive.ql.io.sarg.ExpressionTree;
Expand Down Expand Up @@ -173,11 +172,11 @@ private static BigDecimal hiveDecimalToBigDecimal(HiveDecimalWritable hiveDecima
}

private static int daysFromDate(Date date) {
return DateTimeUtil.daysFromInstant(Instant.ofEpochMilli(date.getTime()));
return DateTimeUtil.daysFromDate(date.toLocalDate());
Comment thread
pvary marked this conversation as resolved.
}

private static int daysFromTimestamp(Timestamp timestamp) {
return DateTimeUtil.daysFromInstant(timestamp.toInstant());
return DateTimeUtil.daysFromDate(timestamp.toLocalDateTime().toLocalDate());
}

private static long microsFromTimestamp(Timestamp timestamp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.iceberg.data.DeleteFilter;
import org.apache.iceberg.data.GenericDeleteFilter;
import org.apache.iceberg.data.IdentityPartitionConverters;
import org.apache.iceberg.data.InternalRecordWrapper;
import org.apache.iceberg.data.avro.DataReader;
import org.apache.iceberg.data.orc.GenericOrcReader;
import org.apache.iceberg.data.parquet.GenericParquetReaders;
Expand Down Expand Up @@ -284,8 +285,11 @@ private CloseableIterable<T> applyResidualFiltering(CloseableIterable<T> iter, E
boolean applyResidual = !context.getConfiguration().getBoolean(InputFormatConfig.SKIP_RESIDUAL_FILTERING, false);

if (applyResidual && residual != null && residual != Expressions.alwaysTrue()) {
// Date and timestamp values are not the correct type for Evaluator.
// Wrapping to return the expected type.
InternalRecordWrapper wrapper = new InternalRecordWrapper(readSchema.asStruct());
Comment thread
pvary marked this conversation as resolved.
Evaluator filter = new Evaluator(readSchema.asStruct(), residual, caseSensitive);
return CloseableIterable.filter(iter, record -> filter.eval((StructLike) record));
return CloseableIterable.filter(iter, record -> filter.eval(wrapper.wrap((StructLike) record)));
} else {
return iter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void testBooleanType() {
@Test
public void testDateType() {
SearchArgument.Builder builder = SearchArgumentFactory.newBuilder();
Date gmtDate = new Date(LocalDate.of(2015, 11, 12).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli());
Date gmtDate = Date.valueOf(LocalDate.of(2015, 11, 12));
SearchArgument arg = builder.startAnd().equals("date", PredicateLeaf.Type.DATE, gmtDate).end().build();

UnboundPredicate expected = Expressions.equal("date", Literal.of("2015-11-12").to(Types.DateType.get()).value());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -54,6 +56,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.apache.iceberg.types.Types.NestedField.optional;
import static org.apache.iceberg.types.Types.NestedField.required;
import static org.junit.runners.Parameterized.Parameter;
import static org.junit.runners.Parameterized.Parameters;
Expand Down Expand Up @@ -563,6 +566,61 @@ public void testStructOfStructsInTable() throws IOException {
}
}

@Test
public void testDateQuery() throws IOException {
Schema dateSchema = new Schema(optional(1, "d_date", Types.DateType.get()));

List<Record> records = TestHelper.RecordsBuilder.newInstance(dateSchema)
.add(LocalDate.of(2020, 1, 21))
.add(LocalDate.of(2020, 1, 24))
.build();

testTables.createTable(shell, "date_test", dateSchema, fileFormat, records);

List<Object[]> result = shell.executeStatement("SELECT * from date_test WHERE d_date='2020-01-21'");
Assert.assertEquals(1, result.size());
Assert.assertEquals("2020-01-21", result.get(0)[0]);

result = shell.executeStatement("SELECT * from date_test WHERE d_date in ('2020-01-21', '2020-01-22')");
Assert.assertEquals(1, result.size());
Assert.assertEquals("2020-01-21", result.get(0)[0]);

result = shell.executeStatement("SELECT * from date_test WHERE d_date > '2020-01-21'");
Assert.assertEquals(1, result.size());
Assert.assertEquals("2020-01-24", result.get(0)[0]);

result = shell.executeStatement("SELECT * from date_test WHERE d_date='2020-01-20'");
Assert.assertEquals(0, result.size());
Comment thread
pvary marked this conversation as resolved.
Outdated
}

@Test
public void testTimestampQuery() throws IOException {
Comment thread
pvary marked this conversation as resolved.
Outdated
Schema timestampSchema = new Schema(optional(1, "d_ts", Types.TimestampType.withoutZone()));
Comment thread
pvary marked this conversation as resolved.
Outdated

List<Record> records = TestHelper.RecordsBuilder.newInstance(timestampSchema)
.add(LocalDateTime.of(2019, 1, 22, 9, 44, 54, 100000000))
.add(LocalDateTime.of(2019, 2, 22, 9, 44, 54, 200000000))
.build();

testTables.createTable(shell, "ts_test", timestampSchema, fileFormat, records);

List<Object[]> result = shell.executeStatement("SELECT d_ts FROM ts_test WHERE d_ts='2019-02-22 09:44:54.2'");
Assert.assertEquals(1, result.size());
Assert.assertEquals("2019-02-22 09:44:54.2", result.get(0)[0]);

result = shell.executeStatement(
"SELECT * FROM ts_test WHERE d_ts in ('2017-01-01 22:30:57.1', '2019-02-22 09:44:54.2')");
Assert.assertEquals(1, result.size());
Assert.assertEquals("2019-02-22 09:44:54.2", result.get(0)[0]);

result = shell.executeStatement("SELECT d_ts FROM ts_test WHERE d_ts < '2019-02-22 09:44:54.2'");
Assert.assertEquals(1, result.size());
Assert.assertEquals("2019-01-22 09:44:54.1", result.get(0)[0]);

result = shell.executeStatement("SELECT * FROM ts_test WHERE d_ts='2017-01-01 22:30:57.3'");
Assert.assertEquals(0, result.size());
}

private void runCreateAndReadTest(TableIdentifier identifier, String createSQL, Schema expectedSchema,
PartitionSpec expectedSpec, Map<StructLike, List<Record>> data) throws IOException {
shell.executeStatement(createSQL);
Expand Down