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 2017/04/26 22:15:26 UTC

[2/4] calcite git commit: Cache the mock catalog used by several test suites

Cache the mock catalog used by several test suites


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/39c22f0c
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/39c22f0c
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/39c22f0c

Branch: refs/heads/master
Commit: 39c22f0c8b7b5b46a152f432e8708ce73ace1ef7
Parents: 7cec056
Author: Julian Hyde <jh...@apache.org>
Authored: Tue Apr 25 17:59:02 2017 -0700
Committer: Julian Hyde <jh...@apache.org>
Committed: Wed Apr 26 14:21:11 2017 -0700

----------------------------------------------------------------------
 .../calcite/sql/test/DefaultSqlTestFactory.java | 51 +++++++++++++++++---
 1 file changed, 44 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/39c22f0c/core/src/test/java/org/apache/calcite/sql/test/DefaultSqlTestFactory.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/sql/test/DefaultSqlTestFactory.java b/core/src/test/java/org/apache/calcite/sql/test/DefaultSqlTestFactory.java
index 63c1cdd..b9f9d66 100644
--- a/core/src/test/java/org/apache/calcite/sql/test/DefaultSqlTestFactory.java
+++ b/core/src/test/java/org/apache/calcite/sql/test/DefaultSqlTestFactory.java
@@ -34,8 +34,13 @@ import org.apache.calcite.test.CalciteAssert;
 import org.apache.calcite.test.MockCatalogReader;
 import org.apache.calcite.test.MockSqlOperatorTable;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
 import com.google.common.collect.ImmutableMap;
 
+import javax.annotation.Nonnull;
+
 /**
  * Default implementation of {@link SqlTestFactory}.
  *
@@ -61,6 +66,28 @@ public class DefaultSqlTestFactory implements SqlTestFactory {
                           CalciteAssert.SchemaSpec.HR)))
           .build();
 
+  /** Caches the mock catalog.
+   * Due to view parsing, initializing a mock catalog is quite expensive.
+   * Validator is not re-entrant, so we create a new one for each test.
+   * Caching improves SqlValidatorTest from 23s to 8s,
+   * and CalciteSqlOperatorTest from 65s to 43s. */
+  private final LoadingCache<SqlTestFactory, Xyz> cache =
+      CacheBuilder.newBuilder()
+          .build(
+              new CacheLoader<SqlTestFactory, Xyz>() {
+                public Xyz load(@Nonnull SqlTestFactory factory)
+                    throws Exception {
+                  final SqlOperatorTable operatorTable =
+                      factory.createOperatorTable(factory);
+                  final boolean caseSensitive =
+                      (Boolean) factory.get("caseSensitive");
+                  final JavaTypeFactory typeFactory =
+                      new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+                  return new Xyz(operatorTable, typeFactory,
+                      new MockCatalogReader(typeFactory, caseSensitive).init());
+                }
+              });
+
   public static final DefaultSqlTestFactory INSTANCE =
       new DefaultSqlTestFactory();
 
@@ -86,15 +113,11 @@ public class DefaultSqlTestFactory implements SqlTestFactory {
   }
 
   public SqlValidator getValidator(SqlTestFactory factory) {
-    final SqlOperatorTable operatorTable = factory.createOperatorTable(factory);
-    final boolean caseSensitive = (Boolean) factory.get("caseSensitive");
+    final Xyz xyz = cache.getUnchecked(factory);
     final SqlConformance conformance =
         (SqlConformance) factory.get("conformance");
-    final JavaTypeFactory typeFactory =
-        new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
-    return SqlValidatorUtil.newValidator(operatorTable,
-        new MockCatalogReader(typeFactory, caseSensitive).init(),
-        typeFactory, conformance);
+    return SqlValidatorUtil.newValidator(xyz.operatorTable,
+        xyz.catalogReader, xyz.typeFactory, conformance);
   }
 
   public SqlAdvisor createAdvisor(SqlValidatorWithHints validator) {
@@ -104,6 +127,20 @@ public class DefaultSqlTestFactory implements SqlTestFactory {
   public Object get(String name) {
     return DEFAULT_OPTIONS.get(name);
   }
+
+  /** State that can be cached and shared among tests. */
+  private static class Xyz {
+    private final SqlOperatorTable operatorTable;
+    private final JavaTypeFactory typeFactory;
+    private final MockCatalogReader catalogReader;
+
+    Xyz(SqlOperatorTable operatorTable, JavaTypeFactory typeFactory,
+        MockCatalogReader catalogReader) {
+      this.operatorTable = operatorTable;
+      this.typeFactory = typeFactory;
+      this.catalogReader = catalogReader;
+    }
+  }
 }
 
 // End DefaultSqlTestFactory.java