Repository: dbcli/athenacli

Issue:
Error when typing a query
I started writing this query:

```
SELECT d.* FROM doodads d JOIN trinkets t on d.
```

## `JOIN` doesn't appear

`JOIN` did not auto-complete as an option (I only saw `JSON`)

## Unhandled Exception

When I type the `.` at the end (I'm trying to join on `d.trinket_id = doodad.id`) I get the following output:

```
Unhandled exception in event loop:
  File "/home/admin/.local/lib/python3.7/site-packages/prompt_toolkit/eventloop/coroutine.py", line 92, in step_next
    new_f = coroutine.throw(exc)
  File "/home/admin/.local/lib/python3.7/site-packages/prompt_toolkit/buffer.py", line 1654, in new_coroutine
    yield From(coroutine(*a, **kw))
  File "/home/admin/.local/lib/python3.7/site-packages/prompt_toolkit/eventloop/coroutine.py", line 92, in step_next
    new_f = coroutine.throw(exc)
  File "/home/admin/.local/lib/python3.7/site-packages/prompt_toolkit/buffer.py", line 1506, in async_completer
    cancel=lambda: not proceed()))
  File "/home/admin/.local/lib/python3.7/site-packages/prompt_toolkit/eventloop/coroutine.py", line 88, in step_next
    new_f = coroutine.send(None)
  File "/home/admin/.local/lib/python3.7/site-packages/prompt_toolkit/eventloop/async_generator.py", line 117, in consume_async_generator
    item = iterator.send(send)
  File "/home/admin/.local/lib/python3.7/site-packages/prompt_toolkit/completion/base.py", line 176, in get_completions_async
    for item in self.get_completions(document, complete_event):
  File "/home/admin/.local/lib/python3.7/site-packages/athenacli/completer.py", line 210, in get_completions
    suggestions = suggest_type(document.text, document.text_before_cursor)
  File "/home/admin/.local/lib/python3.7/site-packages/athenacli/packages/completion_engine.py", line 121, in suggest_type
    full_text, identifier)
  File "/home/admin/.local/lib/python3.7/site-packages/athenacli/packages/completion_engine.py", line 293, in suggest_based_on_last_token
    tables = tuple(t for t in tables if identifies(parent, t))
  File "/home/admin/.local/lib/python3.7/site-packages/athenacli/packages/completion_engine.py", line 293, in <genexpr>
    tables = tuple(t for t in tables if identifies(parent, t))

Exception identifies() missing 2 required positional arguments: 'table' and 'alias'
```

I also tried this without the table aliases, e.g.:

```
SELECT doodads.* FROM doodads JOIN trinkets on doodads.
```

and got the same "unhandled exception" error when typing the `.` at the end.

Discussion:
getaaron: I think we also want to add `JOIN` (and maybe `ON`?) to [`athenacli/packages/literals/literals.json`](https://github.com/dbcli/athenacli/blob/master/athenacli/packages/literals/literals.json)
pgr0ss: > I think we also want to add `JOIN` (and maybe `ON`?) to [`athenacli/packages/literals/literals.json`](https://github.com/dbcli/athenacli/blob/master/athenacli/packages/literals/literals.json)

I think that's for completing the word `JOIN` itself? Which we can certainly add, but doesn't seem related to the bug this PR fixes? Unless I'm misunderstanding.
getaaron: 
> > I think we also want to add `JOIN` (and maybe `ON`?) to [`athenacli/packages/literals/literals.json`](https://github.com/dbcli/athenacli/blob/master/athenacli/packages/literals/literals.json)
> 
> I think that's for completing the word `JOIN` itself? Which we can certainly add, but doesn't seem related to the bug this PR fixes? Unless I'm misunderstanding.

It's the first part:

> JOIN did not auto-complete as an option (I only saw JSON)

but I can move that out to a separate issue :+1:
pgr0ss: Ah, sorry, I missed that part.

Patch:
diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md
new file mode 100644
index 0000000..4f20546
--- /dev/null
+++ b/.github/pull_request_template.md
@@ -0,0 +1,7 @@
+## Description
+<!--- Describe your changes in detail. -->
+
+## Checklist
+<!--- We appreciate your help and want to give you credit. Please take a moment to put an `x` in the boxes below as you complete them. -->
+- [ ] I've added this contribution to the `changelog.md`.
+- [ ] I've added my name to the `AUTHORS` file (or it's already there).
diff --git a/AUTHORS.rst b/AUTHORS.rst
index 93b5168..58ee8a4 100644
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@ -14,6 +14,7 @@ Contributors:
   * Joe Block
   * Jash Gala
   * Hourann
+  * Paul Gross
 
 Creator:
 --------
diff --git a/athenacli/packages/completion_engine.py b/athenacli/packages/completion_engine.py
index d4e7281..86b9abb 100644
--- a/athenacli/packages/completion_engine.py
+++ b/athenacli/packages/completion_engine.py
@@ -290,7 +290,7 @@ def suggest_based_on_last_token(token, text_before_cursor, full_text, identifier
         if parent:
             # "ON parent.<suggestion>"
             # parent can be either a schema name or table alias
-            tables = tuple(t for t in tables if identifies(parent, t))
+            tables = tuple(t for t in tables if identifies(parent, *t))
             return (
                 Column(tables=tables),
                 Table(schema=parent),
diff --git a/changelog.md b/changelog.md
index b0eb065..fe2b505 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,6 +1,10 @@
 (Unreleased; add upcoming change notes here)
 ==============================================
 
+Bugfix
+----------
+* Fix bug when completing `ON parent.` clauses. (Thanks @pgr0ss)
+
 1.1.2
 ========
 
diff --git a/docs/develop.rst b/docs/develop.rst
index 6c5d66d..084a312 100644
--- a/docs/develop.rst
+++ b/docs/develop.rst
@@ -20,12 +20,22 @@ The installation instructions in the README file are intended for users of athen
 
 It is highly recommended to use virtualenv for development. If you don't know what a virtualenv is, `this guide <https://docs.python-guide.org/dev/virtualenvs/#virtual-environments>`_ will help you get started.
 
-Create a virtualenv (let's call it athenacli-dev). Activate it:
+Create a virtualenv (let's call it athenacli-dev):
+
+.. code-block:: bash
+
+    $ virtualenv athenacli-dev
+
+Activate it:
+
+.. code-block:: bash
 
     $ source ./athenacli-dev/bin/activate
+
 Once the virtualenv is activated, cd into the local clone of athenacli folder and install athenacli using pip as follows:
 
 .. code-block:: bash
+
     $ pip install -e .
 
 This will install the necessary dependencies as well as install athenacli from the working folder into a virtualenv. Athenacli is installed in an editable way, so any changes made to the code is immediately available in the installed version of athenacli. This makes it easy to change something in the code, launch athenacli and check the effects of your change.
@@ -38,6 +48,7 @@ Currently we don't have enough tests for athenacli, because we haven't found an
 First, install the requirements for testing:
 
 .. code-block:: bash
+
     $ pip install -r requirements-dev.txt
 
 After that, tests can be run with:


Test patch:
diff --git a/test/test_completion_engine.py b/test/test_completion_engine.py
index 587fa6b..3a3c24a 100644
--- a/test/test_completion_engine.py
+++ b/test/test_completion_engine.py
@@ -2,7 +2,7 @@ import os
 import pytest
 
 from athenacli.packages.completion_engine import (
-    suggest_type, Column, Function, Alias, Keyword
+    suggest_type, Column, Function, Alias, Keyword, Table, View
 )
 
 
@@ -28,6 +28,15 @@ def test_select_suggests_cols_with_qualified_table_scope():
         Alias(aliases=['tabl']),
         Keyword(last_token='SELECT'))
 
+def test_join_suggests_cols_with_qualified_table_scope():
+    expression = 'SELECT * FROM tabl a JOIN tabl b on a.'
+    suggestions = suggest_type(expression, expression)
+
+    assert suggestions == (
+        Column(tables=((None, 'tabl', 'a'),), drop_unique=None),
+        Table(schema='a'),
+        View(schema='a'),
+        Function(schema='a', filter=None))
 
 @pytest.mark.parametrize('expression', [
     'SELECT * FROM tabl WHERE ',