Guosmilesmile commented on code in PR #18144: URL: https://github.com/apache/iceberg/pull/18144#discussion_r4338286102
########## flink/v2.3/flink/src/test/java/org/apache/iceberg/flink/source/TestIcebergLookupJoinSql.java: ########## @@ -0,0 +1,362 @@ +/* + * 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; + +import static org.apache.iceberg.types.Types.NestedField.required; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.table.api.TableEnvironment; +import org.apache.flink.table.api.config.TableConfigOptions; +import org.apache.flink.types.Row; +import org.apache.iceberg.DataFile; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.data.GenericAppenderHelper; +import org.apache.iceberg.data.GenericRecord; +import org.apache.iceberg.data.Record; +import org.apache.iceberg.flink.FlinkConfigOptions; +import org.apache.iceberg.flink.TestFixtures; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.types.Types; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Tests SQL lookup join with Iceberg table source. */ +class TestIcebergLookupJoinSql extends TestSqlBase { + private static final Schema DIM_SCHEMA = + new Schema( + required(1, "user_id", Types.LongType.get()), + required(2, "name", Types.StringType.get()), + required(3, "city", Types.StringType.get())); + + @BeforeEach + @Override + public void before() throws IOException { + setUpTableEnv(getStreamingTableEnv()); + } + + @Test + void lookupJoinReturnsMatchedAndUnmatchedRows() throws Exception { + createDefaultDimTable(); + + TableEnvironment streamEnv = getStreamingTableEnv(); + createOrdersTable(streamEnv, "orders", 5); + + String joinSql = lookupJoinSql("o.order_id, o.user_id, u.name, u.city", "orders"); + + assertThat(SqlHelpers.sql(streamEnv, joinSql)) + .containsExactlyInAnyOrder( + Row.of(1L, 1L, "alice", "beijing"), + Row.of(2L, 2L, "bob", "shanghai"), + Row.of(3L, 3L, "carol", "guangzhou"), + Row.of(4L, 4L, null, null), + Row.of(5L, 5L, null, null)); + } + + @Test + void lookupJoinWithFilterConditionInJoin() throws Exception { Review Comment: Yes, you're right. I updated the UT to verify whether the corresponding data file was actually filtered out. ########## flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/source/lookup/IcebergFullCachingLookupFunction.java: ########## @@ -0,0 +1,232 @@ +/* + * 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 java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import org.apache.flink.annotation.Internal; +import org.apache.flink.metrics.Counter; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.binary.BinaryRowData; +import org.apache.flink.table.functions.FunctionContext; +import org.apache.flink.table.functions.LookupFunction; +import org.apache.flink.table.runtime.typeutils.RowDataSerializer; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SnapshotSummary; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableProperties; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.flink.FlinkRowData; +import org.apache.iceberg.flink.FlinkSchemaUtil; +import org.apache.iceberg.flink.TableLoader; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.util.PropertyUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A lookup function that serves every lookup from an in-memory cache of the rows of an Iceberg + * table, after projection and pushed-down filters are applied. + * + * <p>The cache is loaded from the current snapshot either when the function is opened or on the + * first lookup, depending on {@link IcebergLookupOptions#FULL_CACHE_EAGER_LOAD}. It is never + * refreshed, so later changes to the table are not visible. + */ +@Internal +public class IcebergFullCachingLookupFunction extends LookupFunction { + + private static final Logger LOG = LoggerFactory.getLogger(IcebergFullCachingLookupFunction.class); + + private static final String METRIC_GROUP = "icebergLookupCache"; + + private final TableLoader tableLoader; + private final RowType projectedRowType; + private final int[] lookupKeyIndexes; + private final List<Expression> pushedFilters; + private final boolean caseSensitive; + private final boolean eagerLoad; + + private transient Table table; + private transient IcebergLookupReader reader; + private transient RowDataSerializer rowSerializer; + private transient RowDataSerializer keySerializer; + private transient RowData.FieldGetter[] keyGetters; + private transient InMemoryLookupCache cache; + + private transient Counter lookupMissCounter; + private transient volatile long currentSnapshotId; + private transient volatile int cachedRows; + + public IcebergFullCachingLookupFunction( + TableLoader tableLoader, + RowType projectedRowType, + int[] lookupKeyIndexes, + List<Expression> pushedFilters, + boolean caseSensitive, + boolean eagerLoad) { + this.tableLoader = tableLoader; + this.projectedRowType = projectedRowType; + this.lookupKeyIndexes = lookupKeyIndexes; + this.pushedFilters = pushedFilters; + this.caseSensitive = caseSensitive; + this.eagerLoad = eagerLoad; + } + + @Override + public void open(FunctionContext context) throws Exception { + super.open(context); + LOG.info( + "IcebergFullCachingLookupFunction opening, projected fields={}, lookupKeyIndexes={}", + projectedRowType.getFieldNames(), + Arrays.toString(lookupKeyIndexes)); + + MetricGroup metricGroup = context.getMetricGroup().addGroup(METRIC_GROUP); + this.lookupMissCounter = metricGroup.counter("lookupMiss"); Review Comment: Make sense .Add metric 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]
