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 //  $Id: virtualtable.cpp 6412 2010-03-29 04:58:09Z xlou $
19 
20 
21 #include <iostream>
22 //#define NDEBUG
23 #include <cassert>
24 using namespace std;
25 
26 #include <boost/scoped_ptr.hpp>
27 #include <boost/shared_ptr.hpp>
28 using namespace boost;
29 
30 #include "errorids.h"
31 #include "exceptclasses.h"
32 using namespace logging;
33 
34 #include "returnedcolumn.h"
35 #include "aggregatecolumn.h"
36 #include "windowfunctioncolumn.h"
37 #include "arithmeticcolumn.h"
38 #include "constantcolumn.h"
39 #include "functioncolumn.h"
40 #include "simplecolumn.h"
41 using namespace execplan;
42 
43 #include "jobstep.h"
44 #include "jlf_tuplejoblist.h"
45 #include "virtualtable.h"
46 
47 
48 namespace joblist
49 {
50 
VirtualTable()51 VirtualTable::VirtualTable() : fTableOid(CNX_VTABLE_ID), fVarBinOK(false)
52 {
53 }
54 
55 
initialize()56 void VirtualTable::initialize()
57 {
58 }
59 
60 
addColumn(const SRCP & column)61 void VirtualTable::addColumn(const SRCP& column)
62 {
63     // As of bug3695, make sure varbinary is not used in subquery.
64     if (column->resultType().colDataType == CalpontSystemCatalog::VARBINARY && !fVarBinOK)
65         throw runtime_error ("VARBINARY in subquery is not supported.");
66 
67     AggregateColumn*      agc = NULL;
68     ArithmeticColumn*     arc = NULL;
69     ConstantColumn*       cc  = NULL;
70     FunctionColumn*       fc  = NULL;
71     SimpleColumn*         sc  = NULL;
72     WindowFunctionColumn* wc  = NULL;
73 
74     string columnName;
75     ostringstream oss;
76     UniqId colId;
77 
78     if ((sc = dynamic_cast<SimpleColumn*>(column.get())) != NULL)
79     {
80         if (sc->schemaName().empty())
81             sc->oid(fTableOid + sc->colPosition() + 1);
82 
83         columnName = sc->columnName();
84         colId = UniqId(sc);
85     }
86     else if ((agc = dynamic_cast<AggregateColumn*>(column.get())) != NULL)
87     {
88 //		oss << agc->functionName() << "_" << agc->expressionId();
89 //		oss << "Aggregate_" << agc->expressionId();
90         columnName = agc->data();
91         colId = UniqId(agc->expressionId(), "", "", "");
92     }
93     else if ((wc = dynamic_cast<WindowFunctionColumn*>(column.get())) != NULL)
94     {
95 //		oss << wc->functionName() << "_" << wc->expressionId();
96 //		oss << "Window_" << wc->expressionId();
97         columnName = wc->data();
98         colId = UniqId(wc->expressionId(), "", "", "");
99     }
100     else if ((arc = dynamic_cast<ArithmeticColumn*>(column.get())) != NULL)
101     {
102 //		oss << "Arithmetic_" << arc->expressionId();
103         columnName = arc->data();
104         colId = UniqId(arc->expressionId(), "", "", "");
105     }
106     else if ((fc = dynamic_cast<FunctionColumn*>(column.get())) != NULL)
107     {
108 //		oss << fc->functionName() << "_" << fc->expressionId();
109         columnName = fc->data();
110         colId = UniqId(fc->expressionId(), "", "", "");
111     }
112     else if ((cc = dynamic_cast<ConstantColumn*>(column.get())) != NULL)
113     {
114 //		oss << "Constant_" << cc->expressionId();
115         columnName = cc->data();
116         colId = UniqId(cc->expressionId(), cc->alias(), "", fView);
117     }
118     else // new column type has added, but this code is not updated.
119     {
120         oss << "not supported column type: " << typeid(*(column.get())).name();
121         throw runtime_error(oss.str());
122     }
123 
124     if (columnName.empty())
125         columnName = column->alias();
126 
127     SimpleColumn* vc = new SimpleColumn();
128     vc->tableName(fName);
129     vc->tableAlias(fAlias);
130     vc->columnName(columnName);
131     vc->alias(column->alias());
132     vc->viewName(fView);
133 
134     uint32_t index = fColumns.size();
135     vc->colPosition(index);
136     vc->oid(fTableOid + index + 1);
137     vc->resultType(column->resultType());
138 
139     SSC ssc(vc);
140     fColumns.push_back(ssc);
141     fColumnTypes.push_back(column->resultType());
142     fColumnMap.insert(make_pair(colId, index));
143 }
144 
145 
columnOid(uint32_t i) const146 const CalpontSystemCatalog::OID& VirtualTable::columnOid(uint32_t i) const
147 {
148     idbassert(i < fColumns.size());
149     return fColumns[i]->oid();
150 }
151 
columnType(CalpontSystemCatalog::ColType & type,uint32_t i)152 void VirtualTable::columnType(CalpontSystemCatalog::ColType& type, uint32_t i)
153 {
154     idbassert(i < fColumnTypes.size());
155     fColumnTypes[i] = type;
156     fColumns[i]->resultType(type);
157 }
158 
159 
columnType(uint32_t i) const160 const CalpontSystemCatalog::ColType& VirtualTable::columnType(uint32_t i) const
161 {
162     idbassert(i < fColumnTypes.size());
163     return fColumnTypes[i];
164 }
165 
166 
167 }
168 // vim:ts=4 sw=4:
169 
170