luis4a0 commented on code in PR #12963:
URL: https://github.com/apache/gluten/pull/12963#discussion_r4360674861


##########
gluten-substrait/src/main/java/org/apache/gluten/substrait/rel/VirtualTableRelNode.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.gluten.substrait.rel;
+
+import org.apache.gluten.substrait.type.TypeNode;
+import org.apache.gluten.utils.SubstraitUtil;
+
+import io.substrait.proto.Expression;
+import io.substrait.proto.NamedStruct;
+import io.substrait.proto.ReadRel;
+import io.substrait.proto.Rel;
+import io.substrait.proto.RelCommon;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+public class VirtualTableRelNode implements RelNode, Serializable {
+  private final List<TypeNode> types;
+  private final List<String> names;
+  private final List<List<Expression.Literal>> rows;
+
+  VirtualTableRelNode(
+      List<TypeNode> types, List<String> names, List<List<Expression.Literal>> 
rows) {
+    Objects.requireNonNull(types, "types");
+    Objects.requireNonNull(names, "names");
+    Objects.requireNonNull(rows, "rows");
+    if (types.size() != names.size()) {
+      throw new IllegalArgumentException(
+          "Virtual table schema has " + types.size() + " types but " + 
names.size() + " names.");
+    }
+    if (rows.isEmpty()) {
+      throw new IllegalArgumentException("Virtual table must contain at least 
one row.");
+    }
+
+    this.types = new ArrayList<>(types.size());
+    this.names = new ArrayList<>(names.size());
+    for (int index = 0; index < types.size(); ++index) {
+      this.types.add(Objects.requireNonNull(types.get(index), "types[" + index 
+ "]"));
+      this.names.add(Objects.requireNonNull(names.get(index), "names[" + index 
+ "]"));
+    }
+    this.rows = new ArrayList<>(rows.size());
+    for (List<Expression.Literal> row : rows) {
+      Objects.requireNonNull(row, "row");
+      if (row.size() != types.size()) {
+        throw new IllegalArgumentException(
+            "Virtual table row has "
+                + row.size()
+                + " fields but the schema has "
+                + types.size()
+                + ".");
+      }
+      ArrayList<Expression.Literal> rowCopy = new ArrayList<>(row.size());
+      for (Expression.Literal literal : row) {
+        rowCopy.add(Objects.requireNonNull(literal, "literal"));
+      }
+      this.rows.add(rowCopy);
+    }
+  }
+
+  @Override
+  public Rel toProtobuf() {
+    NamedStruct baseSchema =
+        SubstraitUtil.createNameStructBuilder(types, names, 
Collections.emptyList()).build();
+
+    ReadRel.VirtualTable.Builder virtualTableBuilder = 
ReadRel.VirtualTable.newBuilder();
+    if (types.isEmpty()) {
+      // Empty structs carry the row cardinality when there are no fields.
+      for (int row = 0; row < rows.size(); ++row) {
+        
virtualTableBuilder.addExpressions(Expression.Nested.Struct.getDefaultInstance());
+      }
+    } else {
+      Expression.Nested.Struct.Builder rowGroupBuilder = 
Expression.Nested.Struct.newBuilder();
+      for (int column = 0; column < types.size(); ++column) {
+        for (List<Expression.Literal> row : rows) {
+          
rowGroupBuilder.addFields(Expression.newBuilder().setLiteral(row.get(column)).build());
+        }
+      }
+      virtualTableBuilder.addExpressions(rowGroupBuilder.build());

Review Comment:
   You're right: the non-empty-schema branch was unnecessary for this PR. I 
checked the callers; OneRowRelationExecTransformer is the only production 
caller and always supplies one empty row.
   
   I replaced the configurable JVM serializer with OneRowRelNode and 
makeOneRowReadRel(context, operatorId). It now emits exactly one empty nested 
struct with an empty schema, with no configurable rows, literal batching or 
schema-copying branch. The native generic virtual-table consumer is unchanged. 
Focused tests cover the exact wire shape, repeatable serialization and operator 
registration; the existing seven OneRow integration cases pass on Spark 4.1.1 
with JDK 17 and 21.
   
   Commit: 
https://github.com/apache/gluten/commit/92ae892e9f4cd49d450efdc9435f9d887ec7635f



-- 
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]

Reply via email to