
%*********************************************************************************************************************************
% this relation gives the shape of the input query plan.

parent(planRoot, node0).
parent(node0, node1). 
parent(node1, node2).
parent(node1, node3).
parent(node2, node6).
parent(node2, node4).
parent(node4, node7).
parent(node4, node8).
parent(node3, node5).
parent(node3, node11).
parent(node5, node9).
parent(node5, node10).

%*********************************************************************************************************************************
% the three relations join, selection, and tableScan describe the various nodes in the query plan.  The first att
% in each relation is the name of the node in the query plan.  The second att in the join and selection nodes lists
% all of the boolean predicates that are ANDed together in the node.  The second att in the tableScan relation
% lists all of the attributes that will be pulled upwards from the table scan. 

selection(node0, [pred1, pred2, pred3, pred4, pred5, pred6, pred7, pred8]).
%selection(node0, [pred8]).
join(node1, []). 
join(node2, []).
join(node3, []).
join(node4, []).
join(node5, []).
tableScan(node6, [region(regionkey), region(name)]).
tableScan(node7, [lineitem(orderkey), lineitem(suppkey)]).
tableScan(node8, [supplier(suppkey), supplier(nationkey)]).
tableScan(node9, [customer(nationkey), customer(custkey)]).
tableScan(node10, [orders(custkey), orders(orderdate), orders(orderkey)]).
tableScan(node11, [nation(regionkey), nation(nationkey)]).


%*********************************************************************************************************************************
% the stats relation gives, for each node, the number of distinct attribute values coming out of the node (the second attribute),
% and the number of tuples coming out of the node (the third attribute).  It is assumed that tuples in this relation are defined
% for each of the table scans

stats(node6, [numVals(region(regionkey), 5), numVals(region(name), 5)], 5).
stats(node7, [numVals(lineitem(orderkey), 1500000), numVals(lineitem(suppkey), 10000)], 6000000).
stats(node8, [numVals(supplier(suppkey), 10000), numVals(supplier(nationkey), 25)], 10000).
stats(node9, [numVals(customer(nationkey), 25), numVals(customer(custkey), 150000)], 150000).
stats(node10, [numVals(orders(custkey), 150000), numVals(orders(orderdate), 3650), numVals(orders(orderkey), 1500000)], 1500000).
stats(node11, [numVals(nation(nationkey), 25), numVals(nation(regionkey), 5)], 25).

%*********************************************************************************************************************************
% this lists all of the predicates that appear in anywhere in the underlying query.  The assumption is that the query 
% has a WHERE clause of the form pred1 AND pred2 AND so on. The boolPred relation has two attributes.  The first att 
% is an ID for the boolPred, the second att is a list of database attributes that appear in the boolPred. 

boolPred(pred1, [customer(custkey), orders(custkey)]).
boolPred(pred2, [lineitem(orderkey), orders(orderkey)]).
boolPred(pred3, [lineitem(suppkey), supplier(suppkey)]).
boolPred(pred4, [customer(nationkey), supplier(nationkey)]).
boolPred(pred5, [supplier(nationkey), nation(nationkey)]).
boolPred(pred6, [nation(regionkey), region(regionkey)]).
boolPred(pred7, [region(name)]).
boolPred(pred8, [orders(orderdate)]).

%*********************************************************************************************************************************
% there are also three other relations that store the actual expression trees for the predicates: equals, booland, boolor.  

equals(pred1, customer(custkey), orders(custkey)).
equals(pred2, lineitem(orderkey), orders(orderkey)).
equals(pred3, lineitem(suppkey), supplier(suppkey)).
equals(pred4, customer(nationkey), supplier(nationkey)).
equals(pred5, supplier(nationkey), nation(nationkey)).
equals(pred6, nation(regionkey), region(regionkey)).
equals(pred7, region(name), literal).
equals(pred8_1, orders(orderdate), literal).
equals(pred8_3, orders(orderdate), literal).
equals(pred8_4, orders(orderdate), literal).
boolor(pred8, pred8_1, pred8_2).
boolor(pred8_2, pred8_3, pred8_4).


