You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2019/08/09 20:43:52 UTC

[calcite] 03/03: [CALCITE-3237] IndexOutOfBoundsException when generating deeply nested Java code from linq4j (Sahith Nallapareddy)

This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit 996cdd0249a4e5086fadc27a15ccc23eb9defd8e
Author: Sahith Nallapareddy <sa...@spotify.com>
AuthorDate: Thu Aug 8 09:42:56 2019 -0400

    [CALCITE-3237] IndexOutOfBoundsException when generating deeply nested Java code from linq4j (Sahith Nallapareddy)
    
    The problem was that ExpressionWriter was using a list of indent
    strings but it did not resize the list for very large indents. The
    solution was to use Avatica's Spacer class.
    
    Close apache/calcite#1364
---
 linq4j/pom.xml                                     |  4 ++
 .../calcite/linq4j/tree/ExpressionWriter.java      | 43 +++++-----------------
 2 files changed, 13 insertions(+), 34 deletions(-)

diff --git a/linq4j/pom.xml b/linq4j/pom.xml
index 166514e..1c809aa 100644
--- a/linq4j/pom.xml
+++ b/linq4j/pom.xml
@@ -49,6 +49,10 @@ limitations under the License.
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.calcite.avatica</groupId>
+      <artifactId>avatica-core</artifactId>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ExpressionWriter.java b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ExpressionWriter.java
index 3ea0048..524efca 100644
--- a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ExpressionWriter.java
+++ b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ExpressionWriter.java
@@ -16,20 +16,20 @@
  */
 package org.apache.calcite.linq4j.tree;
 
+import org.apache.calcite.avatica.util.Spacer;
+
 import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Iterator;
 
 /**
  * Converts an expression to Java code.
  */
 class ExpressionWriter {
-  static final Indent INDENT = new Indent(20);
+  /** How many spaces to indent Java code. */
+  private static final int INDENT = 2;
 
+  private final Spacer spacer = new Spacer(0);
   private final StringBuilder buf = new StringBuilder();
-  private int level;
-  private String indent = "";
   private boolean indentPending;
   private final boolean generics;
 
@@ -75,14 +75,14 @@ class ExpressionWriter {
    * Increases the indentation level.
    */
   public void begin() {
-    indent = INDENT.get(++level);
+    spacer.add(INDENT);
   }
 
   /**
    * Decreases the indentation level.
    */
   public void end() {
-    indent = INDENT.get(--level);
+    spacer.subtract(INDENT);
   }
 
   public ExpressionWriter newlineAndIndent() {
@@ -92,7 +92,7 @@ class ExpressionWriter {
   }
 
   public ExpressionWriter indent() {
-    buf.append(indent);
+    spacer.spaces(buf);
     return this;
   }
 
@@ -144,7 +144,7 @@ class ExpressionWriter {
 
   private void checkIndent() {
     if (indentPending) {
-      buf.append(indent);
+      spacer.spaces(buf);
       indentPending = false;
     }
   }
@@ -194,31 +194,6 @@ class ExpressionWriter {
       indentPending = false;
     }
   }
-
-  /** Helps generate strings of spaces, to indent text. */
-  private static class Indent extends ArrayList<String> {
-    Indent(int initialCapacity) {
-      super(initialCapacity);
-      ensureSize(initialCapacity);
-    }
-
-    public synchronized String of(int index) {
-      ensureSize(index + 1);
-      return get(index);
-    }
-
-    private void ensureSize(int targetSize) {
-      if (targetSize < size()) {
-        return;
-      }
-      char[] chars = new char[2 * targetSize];
-      Arrays.fill(chars, ' ');
-      clear();
-      for (int i = 0; i < targetSize; i++) {
-        add(String.valueOf(chars, 0, i * 2));
-      }
-    }
-  }
 }
 
 // End ExpressionWriter.java