title: Apache Spark + Java: "java.lang.AssertionError: assertion failed" in ExpressionEncoder
body: <p>I try to call groupByKey on a Dataset using below code:</p>
  
  <pre><code>    SparkSession SPARK_SESSION = new SparkSession(new SparkContext("local", "app"));
      JavaSparkContext JAVA_SPARK_CONTEXT = new JavaSparkContext(SPARK_SESSION.sparkContext());
  
      @Data
      @NoArgsConstructor
      @AllArgsConstructor
      class Chunk implements Serializable {
          private Integer id;
          private String letters;
      }
  
      class JavaAggregator extends Aggregator&lt;Chunk, String, String&gt; {
  
          @Override
          public String zero() {
              return "";
          }
  
          @Override
          public String reduce(String b, Chunk a) {
              return b + a.getLetters();
          }
  
          @Override
          public String merge(String b1, String b2) {
              return b1 + b2;
          }
  
          @Override
          public String finish(String reduction) {
              return reduction;
          }
  
          @Override
          public Encoder&lt;String&gt; bufferEncoder() {
              return Encoders.bean(String.class);
          }
  
          @Override
          public Encoder&lt;String&gt; outputEncoder() {
              return Encoders.bean(String.class);
          }
      }
  
      List&lt;Chunk&gt; chunkList = List.of(
              new Chunk(1, "a"), new Chunk(2, "1"), new Chunk(3, "-*-"),
              new Chunk(1, "b"), new Chunk(2, "2"), new Chunk(3, "-**-"),
              new Chunk(1, "c"), new Chunk(2, "3"), new Chunk(3, "-***-"));
  
      Dataset&lt;Row&gt; df = SPARK_SESSION.createDataFrame(JAVA_SPARK_CONTEXT.parallelize(chunkList), Chunk.class);
      Dataset&lt;Chunk&gt; ds = df.as(Encoders.bean(Chunk.class));
  
      KeyValueGroupedDataset&lt;Integer, Chunk&gt; grouped = ds.groupByKey((Function1&lt;Chunk, Integer&gt;)  v -&gt; v.getId(), Encoders.bean(Integer.class));
  </code></pre>
  
  <p>But I get Exception which says 
  java.lang.AssertionError: assertion failed</p>
  
  <pre><code>at scala.Predef$.assert(Predef.scala:208)
  at org.apache.spark.sql.catalyst.encoders.ExpressionEncoder$.javaBean(ExpressionEncoder.scala:87)
  at org.apache.spark.sql.Encoders$.bean(Encoders.scala:142)
  at org.apache.spark.sql.Encoders.bean(Encoders.scala)
  </code></pre>
  
  <p>I am not an expert in scala internals and it is hard for me to say what is wrong with the code as the exception is thrown by some compiled code and assertion message "assertion failed" is not very helpful. But maybe there is something fundamentally wrong what I have done in my code that causes this exception?</p>