Guosmilesmile commented on code in PR #18144: URL: https://github.com/apache/iceberg/pull/18144#discussion_r4071246223
########## flink/v2.3/flink/src/test/java/org/apache/iceberg/flink/source/lookup/TestIcebergLookup.java: ########## @@ -0,0 +1,283 @@ +/* + * 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.flink.source.lookup; + +import static org.apache.iceberg.types.Types.NestedField.optional; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Collection; +import java.util.List; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.functions.FunctionContext; +import org.apache.flink.table.types.logical.RowType; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; +import org.apache.iceberg.data.GenericAppenderHelper; +import org.apache.iceberg.data.GenericRecord; +import org.apache.iceberg.data.Record; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.expressions.Expressions; +import org.apache.iceberg.flink.FlinkSchemaUtil; +import org.apache.iceberg.flink.HadoopCatalogExtension; +import org.apache.iceberg.flink.TestFixtures; +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.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; + +public class TestIcebergLookup { + private static final Schema SCHEMA = + new Schema( + optional(1, "id", Types.LongType.get()), + optional(2, "data", Types.StringType.get()), + optional(3, "category", Types.StringType.get())); + + private static final String[] PROJECTED_COLUMNS = {"id", "data", "category"}; + private static final RowType ROW_TYPE = FlinkSchemaUtil.convert(SCHEMA); + private static final int[] ID_KEY_INDICES = {0}; + + @TempDir private Path temporaryFolder; + + @RegisterExtension + protected static final HadoopCatalogExtension CATALOG_EXTENSION = + new HadoopCatalogExtension(TestFixtures.DATABASE, TestFixtures.TABLE); + + private IcebergFullCachingLookupFunction lookupFunction; + + @AfterEach + public void after() throws Exception { + if (lookupFunction != null) { + lookupFunction.close(); + } + } + + @Test + public void lookupReaderReadsWithBaseFilters() throws Exception { + Table table = createTableWithRecords(); + IcebergLookupReader reader = + lookupReader(table, ImmutableList.of(Expressions.equal("category", "B")), false); + + List<List<Object>> rows = Lists.newArrayList(); + reader.read( + IcebergLookupReader.CURRENT_SNAPSHOT, + row -> + rows.add( + ImmutableList.of( + row.getLong(0), row.getString(1).toString(), row.getString(2).toString()))); + + assertThat(rows).containsExactly(ImmutableList.of(2L, "bob", "B")); + } + + @Test + public void lookupReaderRespectsCaseSensitivity() throws Exception { + Table table = createTableWithRecords(); + List<Expression> filters = ImmutableList.of(Expressions.equal("CATEGORY", "B")); + + IcebergLookupReader caseSensitiveReader = lookupReader(table, filters, true); + assertThatThrownBy( + () -> caseSensitiveReader.read(IcebergLookupReader.CURRENT_SNAPSHOT, row -> {})) + .as("Case sensitive lookup should reject a filter that doesn't match the column case") + .isInstanceOf(ValidationException.class) + .hasMessageContaining("CATEGORY"); + + assertThat(readIds(lookupReader(table, filters, false), IcebergLookupReader.CURRENT_SNAPSHOT)) + .containsExactly(2L); + } + + @Test + public void lookupReaderReadsPinnedSnapshot() throws Exception { + Table table = createTableWithRecords(); + long pinnedSnapshot = table.currentSnapshot().snapshotId(); + + appendRecords(table, ImmutableList.of(record(6L, "frank", "D"))); + + IcebergLookupReader reader = lookupReader(table, ImmutableList.of(), false); + + assertThat(readIds(reader, pinnedSnapshot)) + .as("Pinned snapshot should not contain the appended row") + .containsExactlyInAnyOrder(1L, 2L, 3L, null); + assertThat(readIds(reader, IcebergLookupReader.CURRENT_SNAPSHOT)) + .as("Current snapshot should contain the appended row") + .containsExactlyInAnyOrder(1L, 2L, 3L, null, 6L); + } + + @Test + public void lookupFunctionReturnsRowsFromCache() throws Exception { + Table table = createTableWithRecords(); + + lookupFunction = newLookupFunction(); + lookupFunction.open(new FunctionContext(null)); + + Collection<RowData> rows = lookupFunction.lookup(keyRow(1L)); Review Comment: Add it now. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
