1 /* Copyright (C) 2014 InfiniDB, Inc.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 #include <string>
19 #include <stdexcept>
20 #include <typeinfo>
21 using namespace std;
22 
23 #include <cppunit/extensions/HelperMacros.h>
24 
25 #include<sstream>
26 #include<exception>
27 #include<iostream>
28 #include <unistd.h>
29 
30 #include "messagequeue.h"
31 #include "bytestream.h"
32 using namespace messageqcpp;
33 
34 #include "calpontselectexecutionplan.h"
35 #include "simplefilter.h"
36 #include "simplecolumn.h"
37 #include "expressionparser.h"
38 #include "constantcolumn.h"
39 #include "treenode.h"
40 #include "operator.h"
41 #include "arithmeticcolumn.h"
42 #include "aggregatecolumn.h"
43 #include "existsfilter.h"
44 
45 using namespace execplan;
46 
47 class TPCH_EXECPLAN : public CppUnit::TestFixture
48 {
49 
50     CPPUNIT_TEST_SUITE( TPCH_EXECPLAN );
51 
52     CPPUNIT_TEST( Q1 );
53 
54     CPPUNIT_TEST_SUITE_END();
55 
56 private:
57 public:
58 
setUp()59     void setUp()
60     {
61     }
62 
tearDown()63     void tearDown()
64     {
65     }
66 
Q1()67     void Q1()
68     {
69         string sql = "\
70         select\
71 	        n_name,\
72 	        sum(l_extendedprice * (1 - l_discount)) as revenue\
73         from\
74         	customer,\
75         	orders,\
76         	lineitem,\
77         	supplier,\
78         	nation,\
79         	region\
80         where\
81         	c_custkey = o_custkey\
82         	and l_orderkey = o_orderkey\
83         	and l_suppkey = s_suppkey\
84         	and c_nationkey = s_nationkey\
85         	and s_nationkey = n_nationkey\
86         	and n_regionkey = r_regionkey\
87         	and r_name = ':1'\
88         	and o_orderdate >= date ':2'\
89         	and o_orderdate < date ':2' + interval '1' year\
90         group by\
91         	n_name\
92         order by\
93         	revenue desc;";
94 
95 
96         CalpontSelectExecutionPlan csep;
97 
98         // Returned columns
99         CalpontSelectExecutionPlan::ReturnedColumnList returnedColumnList;
100 
101         SimpleColumn* c1 = new SimpleColumn("tpch.nation.n_name");
102         returnedColumnList.push_back(c1);
103 
104         ArithmeticColumn* c2 = new ArithmeticColumn("sum(tpch.lineitem.l_extendedprice * (1 - tpch.lineitem.l_discount))");
105         c2->alias("revenue");
106         returnedColumnList.push_back(c2);
107 
108         csep.returnedCols(returnedColumnList);
109 
110         // Filters
111         CalpontSelectExecutionPlan::FilterTokenList filterTokenList;
112         SimpleFilter* f1 = new SimpleFilter (new Operator("="),
113                                              new SimpleColumn("tpch.customer.c_custkey"),
114                                              new SimpleColumn("tpch.orders.o_custkey"));
115         filterTokenList.push_back(f1);
116         filterTokenList.push_back( new Operator ("and"));
117 
118         SimpleFilter* f2 = new SimpleFilter (new Operator("="),
119                                              new SimpleColumn("tpch.lineitem.l_orderkey"),
120                                              new SimpleColumn("tpch.orders.o_orderkey"));
121         filterTokenList.push_back(f2);
122         filterTokenList.push_back( new Operator ("and"));
123 
124         SimpleFilter* f3 = new SimpleFilter (new Operator("="),
125                                              new SimpleColumn("tpch.lineitem.l_suppkey"),
126                                              new SimpleColumn("tpch.supplier.s_suppkey"));
127         filterTokenList.push_back(f3);
128         filterTokenList.push_back( new Operator ("and"));
129 
130         SimpleFilter* f4 = new SimpleFilter (new Operator("="),
131                                              new SimpleColumn("tpch.customer.c_nationkey"),
132                                              new SimpleColumn("tpch.supplier.s_nationkey"));
133         filterTokenList.push_back(f4);
134         filterTokenList.push_back( new Operator ("and"));
135 
136         SimpleFilter* f5 = new SimpleFilter (new Operator("="),
137                                              new SimpleColumn("tpch.supplier.s_nstionkey"),
138                                              new SimpleColumn("tpch.nation.n_nationkey"));
139         filterTokenList.push_back(f5);
140         filterTokenList.push_back( new Operator ("and"));
141 
142         SimpleFilter* f6 = new SimpleFilter (new Operator("="),
143                                              new SimpleColumn("tpch.region.r_name"),
144                                              new ConstantColumn(":1"));
145         filterTokenList.push_back(f6);
146         filterTokenList.push_back( new Operator ("and"));
147         SimpleFilter* f7 = new SimpleFilter (new Operator(">="),
148                                              new SimpleColumn("tpch.orders.o_orderdate"),
149                                              new ArithmeticColumn("date(':2')"));
150         filterTokenList.push_back(f7);
151         filterTokenList.push_back( new Operator ("and"));
152 
153         SimpleFilter* f8 = new SimpleFilter (new Operator("<"),
154                                              new SimpleColumn("tpch.orders.o_orderdate"),
155                                              new ArithmeticColumn("date(':2') + interval ('1', year)"));
156         filterTokenList.push_back(f8);
157 
158         csep.filterTokenList(filterTokenList);
159 
160         ParseTree* pt = const_cast<ParseTree*>(csep.filters());
161         pt->drawTree ("q5.dot");
162 
163         // Group by
164         CalpontSelectExecutionPlan::GroupByColumnList groupByList;
165         groupByList.push_back(c1->clone());
166 
167         csep.groupByCols (groupByList);
168 
169         // Order by
170         CalpontSelectExecutionPlan::OrderByColumnList orderByList;
171         ArithmeticColumn* o1 = new ArithmeticColumn(*c2);
172         o1->asc(false);
173         orderByList.push_back(o1);
174         csep.orderByCols(orderByList);
175 
176         cout << csep;
177     }
178 
179 
180 };
181 
182 CPPUNIT_TEST_SUITE_REGISTRATION( TPCH_EXECPLAN );
183 
184 #include <cppunit/extensions/TestFactoryRegistry.h>
185 #include <cppunit/ui/text/TestRunner.h>
186 
main(int argc,char ** argv)187 int main( int argc, char** argv)
188 {
189     CppUnit::TextUi::TestRunner runner;
190     CppUnit::TestFactoryRegistry& registry = CppUnit::TestFactoryRegistry::getRegistry();
191     runner.addTest( registry.makeTest() );
192     bool wasSuccessful = runner.run( "", false );
193     return (wasSuccessful ? 0 : 1);
194 }
195 
196 
197