Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ object StructType extends AbstractDataType {
def apply(fields: Seq[StructField]): StructType = StructType(fields.toArray)

def apply(fields: java.util.List[StructField]): StructType = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related, we should just deprecate this. Its weird to have an apply for java.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Yes, a java friendly API is preferred.

StructType(fields.toArray.asInstanceOf[Array[StructField]])
import scala.collection.JavaConverters._
StructType(fields.asScala)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this change, was there any issue when we use org.apache.spark.sql.types.DataTypes.createStructType? Or, the problem only appear when we use this apply method directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DataTypes.createStructType works well because it use fields.toArray(new StructField[0]). However, I think it's bad to create an empty StructField array every time we call that method.
You remind me that we should also update DataTypes.createStructType to use this apply method.

}

protected[sql] def fromAttributes(attributes: Seq[Attribute]): StructType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import scala.collection.JavaConverters;
import scala.collection.Seq;
Expand Down Expand Up @@ -209,6 +210,18 @@ public void testCreateDataFromFromList() {
Assert.assertEquals(1, result.length);
}

@Test
public void testCreateStructTypeFromList(){
List<StructField> fields1 = new ArrayList<>();
fields1.add(new StructField("id", DataTypes.StringType, true, Metadata.empty()));
StructType schema1 = StructType$.MODULE$.apply(fields1);
Assert.assertEquals(0, schema1.fieldIndex("id"));

List<StructField> fields2 = Arrays.asList(new StructField("id", DataTypes.StringType, true, Metadata.empty()));
StructType schema2 = StructType$.MODULE$.apply(fields2);
Assert.assertEquals(0, schema2.fieldIndex("id"));
}

private static final Comparator<Row> crosstabRowComparator = new Comparator<Row>() {
@Override
public int compare(Row row1, Row row2) {
Expand Down