Repository: calcite
Updated Branches:
  refs/heads/master 96b28f7ba -> 7088dc726


SqlTestFactory: use lazy initialization of objects

This avoids creating catalogReader objects when several with(key, value)
methods are called in sequence


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

Branch: refs/heads/master
Commit: 7088dc7261d294b7c6d5c9f4463435e95f0b9c15
Parents: 96b28f7
Author: Vladimir Sitnikov <[email protected]>
Authored: Thu Aug 2 13:10:52 2018 +0300
Committer: Vladimir Sitnikov <[email protected]>
Committed: Thu Aug 2 13:11:32 2018 +0300

----------------------------------------------------------------------
 .../apache/calcite/sql/test/SqlTestFactory.java | 33 ++++++++++----------
 1 file changed, 17 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/7088dc72/core/src/test/java/org/apache/calcite/sql/test/SqlTestFactory.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/sql/test/SqlTestFactory.java 
b/core/src/test/java/org/apache/calcite/sql/test/SqlTestFactory.java
index 7269c8a..4abf867 100644
--- a/core/src/test/java/org/apache/calcite/sql/test/SqlTestFactory.java
+++ b/core/src/test/java/org/apache/calcite/sql/test/SqlTestFactory.java
@@ -37,6 +37,8 @@ import org.apache.calcite.test.MockSqlOperatorTable;
 import org.apache.calcite.test.catalog.MockCatalogReader;
 import org.apache.calcite.test.catalog.MockCatalogReaderSimple;
 
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSortedMap;
 
@@ -73,10 +75,10 @@ public class SqlTestFactory {
   private final MockCatalogReaderFactory catalogReaderFactory;
   private final ValidatorFactory validatorFactory;
 
-  private final RelDataTypeFactory typeFactory;
-  private final SqlOperatorTable operatorTable;
-  private final SqlValidatorCatalogReader catalogReader;
-  private final SqlParser.Config parserConfig;
+  private final Supplier<RelDataTypeFactory> typeFactory;
+  private final Supplier<SqlOperatorTable> operatorTable;
+  private final Supplier<SqlValidatorCatalogReader> catalogReader;
+  private final Supplier<SqlParser.Config> parserConfig;
 
   protected SqlTestFactory() {
     this(DEFAULT_OPTIONS, MockCatalogReaderSimple::new, 
SqlValidatorUtil::newValidator);
@@ -88,17 +90,15 @@ public class SqlTestFactory {
     this.options = options;
     this.catalogReaderFactory = catalogReaderFactory;
     this.validatorFactory = validatorFactory;
-    this.operatorTable = createOperatorTable((SqlOperatorTable) 
options.get("operatorTable"));
-    this.typeFactory = createTypeFactory((SqlConformance) 
options.get("conformance"));
+    this.operatorTable = Suppliers.memoize(
+        () -> createOperatorTable((SqlOperatorTable) 
options.get("operatorTable")));
+    this.typeFactory = Suppliers.memoize(
+        () -> createTypeFactory((SqlConformance) options.get("conformance")));
     Boolean caseSensitive = (Boolean) options.get("caseSensitive");
-    this.catalogReader = catalogReaderFactory.create(typeFactory, 
caseSensitive).init();
-    this.parserConfig = createParserConfig(options);
-  }
-
-  public static MockCatalogReader createCatalogReader(
-      RelDataTypeFactory typeFactory,
-      boolean caseSensitive) {
-    return new MockCatalogReaderSimple(typeFactory, caseSensitive).init();
+    this.catalogReader = Suppliers.memoize(
+        () -> catalogReaderFactory.create(typeFactory.get(), 
caseSensitive).init());
+    this.parserConfig = Suppliers.memoize(
+        () -> createParserConfig(options));
   }
 
   private static SqlOperatorTable createOperatorTable(SqlOperatorTable opTab0) 
{
@@ -108,7 +108,7 @@ public class SqlTestFactory {
   }
 
   public SqlParser createParser(String sql) {
-    return SqlParser.create(sql, parserConfig);
+    return SqlParser.create(sql, parserConfig.get());
   }
 
   public static SqlParser.Config createParserConfig(ImmutableMap<String, 
Object> options) {
@@ -123,7 +123,8 @@ public class SqlTestFactory {
   public SqlValidator getValidator() {
     final SqlConformance conformance =
         (SqlConformance) options.get("conformance");
-    return validatorFactory.create(operatorTable, catalogReader, typeFactory, 
conformance);
+    return validatorFactory.create(operatorTable.get(), catalogReader.get(), 
typeFactory.get(),
+        conformance);
   }
 
   public SqlAdvisor createAdvisor() {

Reply via email to