Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-2914

Improve how LatticeSuggester deduces foreign keys

    XMLWordPrintableJSON

Details

    Description

      Improve how LatticeSuggester deduces foreign keys.

      Currently LatticeSuggester uses SqlStatisticProvider and deduces that a join between two tables is "many to one" if the left table has more rows than the right table. Clearly this is wrong, because it does not take into account the keys of the join.

      I think we will change the method

      double tableCardinality(List<String> qualifiedTableName)

      to

      double tableCardinality(RelOptTable table)

      and add a method

      boolean isForeignKey(RelOptTable fromTable, List<String> fromColumns,
         RelOptTable toTable, List<String> toColumns)

      For example, isForeignKey(empTable, ["DEPTNO"], deptTable, ["DEPTNO"]) would return true.

      One valid implementation might look at the catalog for foreign key constraints; another might execute a query to see whether every row in emp is matched by exaclty one row in dept. Here is a query that might do it:

      select emp.deptno
      from (select distinct deptno from emp) as emp
      left join dept using (deptno)
      group by emp.deptno
      having count(dept.deptno) != 1

      If the query returns any rows it is not a foreign key.

      Attachments

        Issue Links

          Activity

            People

              julianhyde Julian Hyde
              julianhyde Julian Hyde
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 3.5h
                  3.5h