flyrain commented on code in PR #7326: URL: https://github.com/apache/iceberg/pull/7326#discussion_r1167296762
########## spark/v3.3/spark/src/main/java/org/apache/iceberg/spark/NetChangelogIterator.java: ########## @@ -0,0 +1,138 @@ +/* + * 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; + +import java.util.Iterator; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.types.StructType; + +public class NetChangelogIterator extends ChangelogIterator { + + private Row firstRow = null; + private Row postImage = null; + + protected NetChangelogIterator( + Iterator<Row> rowIterator, StructType rowType, String[] identifierFields) { + super(rowIterator, rowType, identifierFields); + } + + @Override + public boolean hasNext() { + if (postImage != null || firstRow != null) { + return true; + } + return getRowIterator().hasNext(); + } + + /** + * This method iterates all versions of the same logical row across multiple snapshots. It caches + * the row's first version, and removes every version in between, then compares the last version + * with the first version to get the net change. Please note that it also removes any invalid + * data. For example, if there are two identical records, it will remove one of them. Review Comment: That's a good point. The essential problem is that whether we accept the idea of primary key(here is identifier fields), which means for a particular key, there should be only one record. I understand Iceberg doesn't enforce it strictly at the write time. But for this use case, it's going to be crazy to not have a primary key or allowing multiple rows with the same primary key. For example, assuming we have the following pair of rows with the same key. ``` s1: delete s2: insert ``` There is no way we can connect these two actions together. `s1` can delete a row completely unrelated to the row inserted in `s2`. So computing the pre/post images or getting net changes is impossible. What's the point of identifier field then? There is no difference between with and without identifier fields. Our previous discussion/solution is to allow user to input the identifier fields as a primary key, since they know their data. There may be duplicated rows in the table, but users may not care. So the pre/post images and net change results are still useful and acceptable for users. To provide better UX, this procedure can log warn message, or provide an option to failed the query if duplicated rows is there. WDYT? -- 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]
