Repository: kayak/pypika (python)

Issue:
Descriptive error when .from_() call is omitted
I just spent some time figuring out what happened when I transcribed a slightly comples query from raw SQL to pypika (this is a great library, BTW!). The error I was getting was quite cryptic:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/butla/.virtualenvs/bla-Y9MmHOr2/lib/python3.6/site-packages/pypika/queries.py", line 161, in select
    return cls._builder().select(*terms)
  File "/home/butla/.virtualenvs/bla-Y9MmHOr2/lib/python3.6/site-packages/pypika/utils.py", line 46, in _copy
    result = func(self_copy, *args, **kwargs)
  File "/home/butla/.virtualenvs/bla-Y9MmHOr2/lib/python3.6/site-packages/pypika/queries.py", line 379, in select
    self._select_field_str(term)
  File "/home/butla/.virtualenvs/bla-Y9MmHOr2/lib/python3.6/site-packages/pypika/queries.py", line 565, in _select_field_str
    self._select_field(Field(term, table=self._from[0]))
IndexError: list index out of range
list index out of range
```

Turns out I forgot to add `from_()` call.

**Steps to reproduce:**
run `Query.select('asdf')`

Pull request:
Fixed Query.select('field') with no from table.
Fixes #140 Added a check when selecting from a field using a string argument for the field name when there are no tables currently being selected from in the query

Patch:
diff --git a/pypika/__init__.py b/pypika/__init__.py
index 74cb897..f58677f 100644
--- a/pypika/__init__.py
+++ b/pypika/__init__.py
@@ -68,6 +68,7 @@ from .utils import (
     CaseException,
     GroupingException,
     JoinException,
+    QueryException,
     RollupException,
     UnionException,
 )
diff --git a/pypika/queries.py b/pypika/queries.py
index 0eaf5cd..dcc97b1 100644
--- a/pypika/queries.py
+++ b/pypika/queries.py
@@ -1,19 +1,10 @@
 # coding: utf-8
 from functools import reduce
 
-from pypika.enums import (
+from .enums import (
     JoinType,
     UnionType,
 )
-from pypika.utils import (
-    JoinException,
-    RollupException,
-    UnionException,
-    alias_sql,
-    builder,
-    format_quotes,
-    ignore_copy,
-)
 from .terms import (
     ArithmeticExpression,
     Field,
@@ -24,6 +15,16 @@ from .terms import (
     Tuple,
     ValueWrapper,
 )
+from .utils import (
+    JoinException,
+    QueryException,
+    RollupException,
+    UnionException,
+    alias_sql,
+    builder,
+    format_quotes,
+    ignore_copy,
+)
 
 __author__ = "Timothy Heys"
 __email__ = "theys@kayak.com"
@@ -636,6 +637,10 @@ class QueryBuilder(Selectable, Term):
                 for field in field_set]
 
     def _select_field_str(self, term):
+        if 0 == len(self._from):
+            raise QueryException('Cannot select {term}, no FROM table specified.'
+                                 .format(term=term))
+
         if term == '*':
             self._select_star = True
             self._selects = [Star()]


Test patch:
diff --git a/pypika/tests/test_selects.py b/pypika/tests/test_selects.py
index bd395ba..02b7bb9 100644
--- a/pypika/tests/test_selects.py
+++ b/pypika/tests/test_selects.py
@@ -11,6 +11,7 @@ from pypika import (
     Order,
     PostgreSQLQuery,
     Query,
+    QueryException,
     RedshiftQuery,
     Table,
     Tables,
@@ -30,6 +31,15 @@ class SelectTests(unittest.TestCase):
 
         self.assertEqual('', str(q))
 
+    def test_select_no_from(self):
+        q = Query.select(1)
+
+        self.assertEqual('SELECT 1', str(q))
+
+    def test_select_no_from_with_field_raises_exception(self):
+        with self.assertRaises(QueryException):
+            Query.select('asdf')
+
     def test_select__star(self):
         q = Query.from_('abc').select('*')