amogh-jahagirdar commented on code in PR #13310:
URL: https://github.com/apache/iceberg/pull/13310#discussion_r2205641072


##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/ExtractRowLineage.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.util.List;
+import java.util.function.Function;
+import org.apache.iceberg.MetadataColumns;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.catalyst.ProjectingInternalRow;
+import org.apache.spark.sql.catalyst.expressions.GenericInternalRow;
+import org.apache.spark.sql.types.LongType$;
+import org.apache.spark.sql.types.StructType;
+import scala.collection.JavaConverters;
+
+class ExtractRowLineage implements Function<InternalRow, InternalRow> {
+  private static final StructType ROW_LINEAGE_SCHEMA =
+      new StructType()
+          .add(MetadataColumns.ROW_ID.name(), LongType$.MODULE$, true)
+          .add(MetadataColumns.LAST_UPDATED_SEQUENCE_NUMBER.name(), 
LongType$.MODULE$, true);
+  private static final InternalRow EMPTY_LINEAGE_ROW = new 
GenericInternalRow(2);
+
+  private final Schema writeSchema;
+
+  private ProjectingInternalRow cachedRowLineageProjection;
+
+  ExtractRowLineage(Schema writeSchema) {
+    Preconditions.checkArgument(writeSchema != null, "Write schema cannot be 
null");
+    this.writeSchema = writeSchema;
+  }
+
+  @Override
+  public InternalRow apply(InternalRow meta) {
+    // If row lineage is not required on write return a null row
+    if (writeSchema.findField(MetadataColumns.ROW_ID.name()) == null) {

Review Comment:
   I updated to have a boolean `rowLineageRequiredField` which we only set once 
in the constructor and then it's just a boolean check on every record



##########
spark/v4.0/spark/src/main/java/org/apache/iceberg/spark/source/ExtractRowLineage.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.util.List;
+import java.util.function.Function;
+import org.apache.iceberg.MetadataColumns;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.catalyst.ProjectingInternalRow;
+import org.apache.spark.sql.catalyst.expressions.GenericInternalRow;
+import org.apache.spark.sql.types.LongType$;
+import org.apache.spark.sql.types.StructType;
+import scala.collection.JavaConverters;
+
+class ExtractRowLineage implements Function<InternalRow, InternalRow> {
+  private static final StructType ROW_LINEAGE_SCHEMA =
+      new StructType()
+          .add(MetadataColumns.ROW_ID.name(), LongType$.MODULE$, true)
+          .add(MetadataColumns.LAST_UPDATED_SEQUENCE_NUMBER.name(), 
LongType$.MODULE$, true);
+  private static final InternalRow EMPTY_LINEAGE_ROW = new 
GenericInternalRow(2);
+
+  private final Schema writeSchema;
+
+  private ProjectingInternalRow cachedRowLineageProjection;
+
+  ExtractRowLineage(Schema writeSchema) {
+    Preconditions.checkArgument(writeSchema != null, "Write schema cannot be 
null");
+    this.writeSchema = writeSchema;
+  }
+
+  @Override
+  public InternalRow apply(InternalRow meta) {
+    // If row lineage is not required on write return a null row
+    if (writeSchema.findField(MetadataColumns.ROW_ID.name()) == null) {
+      return null;
+    }
+
+    // If metadata row is null but the write schema requires lineage, return 
an empty lineage row
+    if (meta == null) {
+      return EMPTY_LINEAGE_ROW;
+    }
+
+    ProjectingInternalRow metaProj = (ProjectingInternalRow) meta;
+    // Use cached ordinals if they exist
+    if (cachedRowLineageProjection != null) {

Review Comment:
   Done!



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to