summaryrefslogtreecommitdiff
path: root/storage/ndb/src/old_files/client/odbc/codegen/Code_query_range.cpp
blob: 5d29c5af315aeca5f28a5e37c93e75e20fd7af90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <common/StmtArea.hpp>
#include <dictionary/DictTable.hpp>
#include <dictionary/DictColumn.hpp>
#include "Code_query_range.hpp"
#include "Code_column.hpp"
#include "Code_expr.hpp"
#include "Code_root.hpp"

// Plan_query_range

Plan_query_range::~Plan_query_range()
{
}

Plan_base*
Plan_query_range::analyze(Ctx& ctx, Ctl& ctl)
{
    ctx_assert(m_table != 0);
    m_table->analyze(ctx, ctl);
    if (! ctx.ok())
	return 0;
    return this;
}

Exec_base*
Plan_query_range::codegen(Ctx& ctx, Ctl& ctl)
{
    // set up
    ctx_assert(m_table != 0 && m_index != 0);
    const BaseString& tableName = m_table->getName();
    ctx_assert(m_index->m_dictIndex != 0);
    const DictIndex& dictIndex = *m_index->m_dictIndex;
    const BaseString& indexName = dictIndex.getName();
    const unsigned keyCount = m_index->m_keyCountUsed;
    const ColumnVector& columns = m_table->exprColumns();
    ctx_assert(columns.size() > 0);
    const unsigned attrCount = columns.size() - 1;
    // create the code
    Exec_query_range::Code& code = *new Exec_query_range::Code(keyCount, attrCount);
    code.m_tableName = strcpy(new char[tableName.length() + 1], tableName.c_str());
    code.m_indexName = strcpy(new char[indexName.length() + 1], indexName.c_str());
    code.m_exclusive = m_exclusive;
    // key attributes
    code.m_keyId = new NdbAttrId[1 + keyCount];
    code.m_keyId[0] = (NdbAttrId)-1;
    for (unsigned k = 1; k <= keyCount; k++) {
	const DictColumn* keyColumn = dictIndex.getColumn(k);
	const SqlType& sqlType = keyColumn->sqlType();
	SqlSpec sqlSpec(sqlType, SqlSpec::Physical);
	code.m_keySpecs.setEntry(k, sqlSpec);
	code.m_keyId[k] = k - 1;	// index column order
    }
    // matching expressions
    ctx_assert(m_index->m_keyFound);
    const ExprVector& keyEq = m_index->m_keyEq;
    // check size matches
    ctx_assert(keyEq.size() == 1 + keyCount);
    code.m_keyMatch = new Exec_expr* [1 + keyCount];
    code.m_keyMatch[0] = 0;
    for (unsigned k = 1; k <= keyCount; k++) {
	Plan_expr* expr = keyEq[k];
	Exec_expr* execExpr = static_cast<Exec_expr*>(expr->codegen(ctx, ctl));
	if (! ctx.ok())
	    return 0;
	ctx_assert(execExpr != 0);
	code.m_keyMatch[k] = execExpr;
    }
    // queried attributes
    code.m_attrId = new NdbAttrId[1 + attrCount];
    code.m_attrId[0] = (NdbAttrId)-1;
    for (unsigned i = 1; i <= attrCount; i++) {
	Plan_column* column = columns[i];
	ctx_assert(column != 0);
	const DictColumn& dictColumn = column->dictColumn();
	const SqlType& sqlType = dictColumn.sqlType();
	SqlSpec sqlSpec(sqlType, SqlSpec::Physical);
	code.m_sqlSpecs.setEntry(i, sqlSpec);
	code.m_attrId[i] = dictColumn.getAttrId();
    }
    // create the exec
    Exec_query_range* exec = new Exec_query_range(ctl.m_execRoot);
    ctl.m_execRoot->saveNode(exec);
    exec->setCode(code);
    // interpreter
    ctl.m_execQuery = exec;
    Exec_pred* execInterp = 0;
    ctl.m_topTable = m_table;
    if (m_interp != 0) {
	execInterp = static_cast<Exec_pred*>(m_interp->codegen(ctx, ctl));
	if (! ctx.ok())
	    return 0;
	ctx_assert(execInterp != 0);
    }
    ctl.m_topTable = 0;
    if (m_interp != 0)
	exec->setInterp(execInterp);
    return exec;
}

void
Plan_query_range::print(Ctx& ctx)
{
    ctx.print(" [query_range");
    Plan_base* a[] = { m_table };
    printList(ctx, a, 1);
    ctx.print("]");
}

// Exec_query_range

Exec_query_range::Code::~Code()
{
    delete[] m_tableName;
    delete[] m_keyId;
    delete[] m_keyMatch;
    delete[] m_attrId;
}

Exec_query_range::Data::~Data()
{
    delete[] m_recAttr;
}

Exec_query_range::~Exec_query_range()
{
}

void
Exec_query_range::alloc(Ctx& ctx, Ctl& ctl)
{
    const Code& code = getCode();
    // create data
    Data& data = *new Data(this, code.sqlSpecs());
    setData(data);
    // allocate matching expressions
    for (unsigned k = 1; k <= code.m_keyCount; k++) {
	Exec_expr* expr = code.m_keyMatch[k];
	ctx_assert(expr != 0);
	expr->alloc(ctx, ctl);
	if (! ctx.ok())
	    return;
    }
    // needed for isNULL
    data.m_recAttr = new NdbRecAttr* [1 + code.m_attrCount];
    for (unsigned i = 0; i <= code.m_attrCount; i++) {
	data.m_recAttr[i] = 0;
    }
    // parallel
    data.m_parallel = code.m_exclusive ? 1 : 240;	// best supported
    // interpreter
    if (m_interp != 0) {
	//m_interp->alloc(ctx, ctl); XXX
	if (! ctx.ok())
	    return;
    }
}

void
Exec_query_range::close(Ctx& ctx)
{
    Data& data = getData();
    if (data.m_con != 0) {
	Ndb* const ndb = ndbObject();
	ndb->closeTransaction(data.m_con);
	data.m_con = 0;
	data.m_op = 0;
	data.m_done = true;
	ctx_log2(("lookup closed at statement close"));
    }
    //    if (m_interp != 0)
    //	m_interp->close(ctx);
}

void
Exec_query_range::print(Ctx& ctx)
{
    ctx.print(" [query_range");
    if (m_code != 0) {
	const Code& code = getCode();
	ctx.print(" keyId=");
	for (unsigned i = 1; i <= code.m_keyCount; i++) {
	    if (i > 1)
		ctx.print(",");
	    ctx.print("%u", (unsigned)code.m_keyId[i]);
	}
	ctx.print(" attrId=");
	for (unsigned i = 1; i <= code.m_attrCount; i++) {
	    if (i > 1)
		ctx.print(",");
	    ctx.print("%u", (unsigned)code.m_attrId[i]);
	}
	ctx.print(" table=%s", code.m_tableName);
    }
    ctx.print("]");
}