Table Priority Calculation

Clarification on higher/lower terminology:

  • Lower Priority → Greater number value

  • Higher Priority → Lesser number value

Think about it like a race. 1 is a smaller number but generally means "the winner".

When a new table join is made, the logic ensures that the table on the right has lower priority than the table on the left. This is always the case regardless of the type of join (Inner, Left or Right joins), so that the SQL gets generated correctly.

If the table on the right has a higher priority, then the priority of the table on the right will be rewritten to be the same as the table on the left but with an increment of 0.5.

The list of tables is then re-ordered based on the priority numbers, with each priority number being resolved to an integer.

If a new table is inserted into the middle of the existing list of tables, all tables that come after the newly inserted table will have their priority numbers increased by 1 regardless of there being a gap in the numbers.

For example:

You have Table A, Table C, Table D and Table E. You’ve just added Table B. You want to make a join between Table A and Table B:

  • Table A - Priority 999

  • Table B - Priority 999.5 (the newly inserted table, where a join was made with Table A which was to the left of the newly inserted table)

  • Table C - Priority 1000

  • Table D - Priority 1009

  • Table E - Priority 1010

To ensure that all tables are displayed in the correct order and all priority numbers are integers the tables would be re-ordered and displayed as follows:

  • Table A - Priority 999

  • Table B - Priority 1000

  • Table C - Priority 1001

  • Table D - Priority 1010

  • Table E - Priority 1011