Skip to content

Commit

Permalink
Removed need for random temp tables + added join tests
Browse files Browse the repository at this point in the history
  • Loading branch information
harelba committed Jun 7, 2014
1 parent 9bfca3f commit 362037a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bin/q
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ q_version = "1.4.0" # Not released yet

import os
import sys
import random
import sqlite3
import gzip
import glob
Expand Down Expand Up @@ -167,6 +166,7 @@ class Sqlite3DB(object):
def __init__(self, show_sql=SHOW_SQL):
self.show_sql = show_sql
self.conn = sqlite3.connect(':memory:')
self.last_temp_table_id = 10000
self.cursor = self.conn.cursor()
self.type_names = {
str: 'TEXT', int: 'INT', long : 'INT' , float: 'FLOAT', None: 'TEXT'}
Expand Down Expand Up @@ -243,7 +243,8 @@ class Sqlite3DB(object):
return 'CREATE TABLE %s (%s)' % (table_name, column_defs)

def generate_temp_table_name(self):
return "temp_table_%s" % random.randint(0, 1000000000)
self.last_temp_table_id += 1
return "temp_table_%s" % self.last_temp_table_id

def generate_drop_table(self, table_name):
return "DROP TABLE %s" % table_name
Expand Down
48 changes: 48 additions & 0 deletions test/test-suite
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,54 @@ class SqlTests(AbstractQTestCase):
self.assertEquals(o[1], 'ppp [email protected]')
self.assertEquals(o[2], 'ppp [email protected]')

def test_self_join1(self):
tmpfile = self.create_file_with_data("\n".join(["%s 9000" % i for i in range(0,10)]))
cmd = '../bin/q "select * from %s a1 join %s a2 on (a1.c1 = a2.c1)"' % (tmpfile.name,tmpfile.name)
retcode, o, e = run_command(cmd)

self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 10)

self.cleanup(tmpfile)

def test_self_join_reuses_table(self):
tmpfile = self.create_file_with_data("\n".join(["%s 9000" % i for i in range(0,10)]))
cmd = '../bin/q "select * from %s a1 join %s a2 on (a1.c1 = a2.c1)" -A' % (tmpfile.name,tmpfile.name)
retcode, o, e = run_command(cmd)

self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 3)

self.assertEquals(o[0],'Table for file: %s' % tmpfile.name)
self.assertEquals(o[1],' `c1` - int')
self.assertEquals(o[2],' `c2` - int')

self.cleanup(tmpfile)

def test_self_join2(self):
tmpfile1 = self.create_file_with_data("\n".join(["%s 9000" % i for i in range(0,10)]))
cmd = '../bin/q "select * from %s a1 join %s a2 on (a1.c2 = a2.c2)"' % (tmpfile1.name,tmpfile1.name)
retcode, o, e = run_command(cmd)

self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 10*10)

self.cleanup(tmpfile1)

tmpfile2 = self.create_file_with_data("\n".join(["%s 9000" % i for i in range(0,10)]))
cmd = '../bin/q "select * from %s a1 join %s a2 on (a1.c2 = a2.c2) join %s a3 on (a1.c2 = a3.c2)"' % (tmpfile2.name,tmpfile2.name,tmpfile2.name)
retcode, o, e = run_command(cmd)

self.assertEquals(retcode, 0)
self.assertEquals(len(e), 0)
self.assertEquals(len(o), 10*10*10)

self.cleanup(tmpfile2)


def suite():
tl = unittest.TestLoader()
basic_stuff = tl.loadTestsFromTestCase(BasicTests)
Expand Down

0 comments on commit 362037a

Please sign in to comment.