danny0405 commented on a change in pull request #1141: [CALCITE-2914] Improve 
how LatticeSuggester deduces foreign keys
URL: https://github.com/apache/calcite/pull/1141#discussion_r270725275
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/statistic/MapSqlStatisticProvider.java
 ##########
 @@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.statistic;
+
+import org.apache.calcite.adapter.jdbc.JdbcTable;
+import org.apache.calcite.materialize.SqlStatisticProvider;
+import org.apache.calcite.plan.RelOptTable;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Implementation of {@link SqlStatisticProvider} that looks up values in a
+ * table.
+ *
+ * <p>Only for testing.
+ */
+public enum MapSqlStatisticProvider implements SqlStatisticProvider {
+  INSTANCE;
+
+  private final Map<String, Double> cardinalityMap;
+
+  private final ImmutableMap<String, List<String>> keyMap;
+
+  MapSqlStatisticProvider() {
+    final Initializer initializer = new Initializer()
+        .put("foodmart", "agg_c_14_sales_fact_1997", 86_805d, "id")
+        .put("foodmart", "customer", 10_281d, "id")
+        .put("foodmart", "employee", 1_155d, "id")
+        .put("foodmart", "employee_closure", 7_179d, "id")
+        .put("foodmart", "department", 10_281d, "id")
+        .put("foodmart", "inventory_fact_1997", 4_070d, "id")
+        .put("foodmart", "position", 18d, "id")
+        .put("foodmart", "product", 1560d, "product_id")
+        .put("foodmart", "product_class", 110d, "id")
+        .put("foodmart", "promotion", 1_864d, "id")
+        // region really has 110 rows; made it smaller than store to trick FK
+        .put("foodmart", "region", 24d, "id")
+        .put("foodmart", "salary", 21_252d, "id")
+        .put("foodmart", "sales_fact_1997", 86_837d, "id")
+        .put("foodmart", "store", 25d, "id")
+        .put("foodmart", "store_ragged", 25d, "id")
+        .put("foodmart", "time_by_day", 730d, "id")
+        .put("foodmart", "warehouse", 24d, "id")
+        .put("foodmart", "warehouse_class", 6d, "id")
+        .put("scott", "EMP", 10d, "id")
+        .put("scott", "DEPT", 4d, "id")
+        .put("tpcds", "CALL_CENTER", 8d, "id")
+        .put("tpcds", "CATALOG_PAGE", 11_718d, "id")
+        .put("tpcds", "CATALOG_RETURNS", 144_067d, "id")
+        .put("tpcds", "CATALOG_SALES", 1_441_548d, "id")
+        .put("tpcds", "CUSTOMER", 100_000d, "id")
+        .put("tpcds", "CUSTOMER_ADDRESS", 50_000d, "id")
+        .put("tpcds", "CUSTOMER_DEMOGRAPHICS", 1_920_800d, "id")
+        .put("tpcds", "DATE_DIM", 73049d, "id")
+        .put("tpcds", "DBGEN_VERSION", 1d, "id")
+        .put("tpcds", "HOUSEHOLD_DEMOGRAPHICS", 7200d, "id")
+        .put("tpcds", "INCOME_BAND", 20d, "id")
+        .put("tpcds", "INVENTORY", 11_745_000d, "id")
+        .put("tpcds", "ITEM", 18_000d, "id")
+        .put("tpcds", "PROMOTION", 300d, "id")
+        .put("tpcds", "REASON", 35d, "id")
+        .put("tpcds", "SHIP_MODE", 20d, "id")
+        .put("tpcds", "STORE", 12d, "id")
+        .put("tpcds", "STORE_RETURNS", 287_514d, "id")
+        .put("tpcds", "STORE_SALES", 2_880_404d, "id")
+        .put("tpcds", "TIME_DIM", 86_400d, "id")
+        .put("tpcds", "WAREHOUSE", 5d, "id")
+        .put("tpcds", "WEB_PAGE", 60d, "id")
+        .put("tpcds", "WEB_RETURNS", 71_763d, "id")
+        .put("tpcds", "WEB_SALES", 719_384d, "id")
+        .put("tpcds", "WEB_SITE", 1d, "id");
+    cardinalityMap = initializer.cardinalityMapBuilder.build();
+    keyMap = initializer.keyMapBuilder.build();
+  }
+
+  public double tableCardinality(RelOptTable table) {
+    final JdbcTable jdbcTable = table.unwrap(JdbcTable.class);
+    final List<String> qualifiedName;
+    if (jdbcTable != null) {
+      qualifiedName = Arrays.asList(jdbcTable.jdbcSchemaName,
+          jdbcTable.jdbcTableName);
+    } else {
+      qualifiedName = table.getQualifiedName();
+    }
+    return cardinalityMap.get(qualifiedName.toString());
+  }
+
+  public boolean isForeignKey(RelOptTable fromTable, List<Integer> fromColumns,
+      RelOptTable toTable, List<Integer> toColumns) {
+    // Assume that anything that references a primary key is a foreign key.
+    // It's wrong but it's enough for our current test cases.
+    return isKey(toTable, toColumns);
+  }
+
+  public boolean isKey(RelOptTable table, List<Integer> columns) {
+    return columns.size() == 1
+        && keyMap.get(table.getQualifiedName().toString())
+            .equals(
+                ImmutableList.of(table.getRowType().getFieldList()
+                  .get(columns.get(0)).getName()));
 
 Review comment:
   We should pre-check for indexOutofBounds for 
ImmutableList.of(table.getRowType().getFieldList().get(columns.get(0))

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to