summaryrefslogtreecommitdiff
path: root/ndb/src/old_files/client/odbc/codegen/Code_expr_op.cpp
blob: 7e8314c17419a00df79ba9a2ea59496cbad5d384 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* 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 "Code_expr.hpp"
#include "Code_expr_op.hpp"
#include "Code_expr_conv.hpp"
#include "Code_root.hpp"

// Expr_op

const char*
Expr_op::name() const
{
    switch (m_opcode) {
    case Add:
	return "+";
    case Subtract:
	return "-";
    case Multiply:
	return "*";
    case Divide:
	return "/";
    case Plus:
	return "+";
    case Minus:
	return "-";
    }
    ctx_assert(false);
    return "";
}

unsigned
Expr_op::arity() const
{
    switch (m_opcode) {
    case Add:
    case Subtract:
    case Multiply:
    case Divide:
	return 2;
    case Plus:
    case Minus:
	return 1;
    }
    ctx_assert(false);
    return 0;
}

// Plan_expr_op

Plan_expr_op::~Plan_expr_op()
{
}

Plan_base*
Plan_expr_op::analyze(Ctx& ctx, Ctl& ctl)
{
    m_exec = 0;
    unsigned arity = m_op.arity();
    // analyze operands
    m_isAggr = false;
    m_isBound = true;
    for (unsigned i = 1; i <= arity; i++) {
	ctx_assert(m_expr[i] != 0);
	m_expr[i]->analyze(ctx, ctl);
	if (! ctx.ok())
	    return 0;
	if (m_expr[i]->m_isAggr)
	    m_isAggr = true;
	if (! m_expr[i]->m_isBound)
	    m_isBound = false;
    }
    // find result type and conversion types (currently same)
    SqlType res;
    SqlType con[1 + 2];
    if (arity == 1) {
	const SqlType& t1 = m_expr[1]->sqlType();
	switch (t1.type()) {
	case SqlType::Char:
	case SqlType::Varchar:
	    break;
	case SqlType::Smallint:
	case SqlType::Integer:
	case SqlType::Bigint:
	    res.setType(ctx, SqlType::Bigint);
	    con[1] = res;
	    break;
	case SqlType::Real:
	case SqlType::Double:
	    res.setType(ctx, SqlType::Double);
	    con[1] = res;
	    break;
	case SqlType::Null:
	    res.setType(ctx, SqlType::Null);
	    con[1] = res;
	    break;
	case SqlType::Unbound:
	    res.setType(ctx, SqlType::Unbound);
	    con[1] = res;
	default:
	    break;
	}
	if (con[1].type() == SqlType::Undef) {
	    char b1[40];
	    t1.print(b1, sizeof(b1));
	    ctx.pushStatus(Error::Gen, "type mismatch in operation: %s %s", m_op.name(), b1);
	    return 0;
	}
    } else if (arity == 2) {
	const SqlType& t1 = m_expr[1]->sqlType();
	const SqlType& t2 = m_expr[2]->sqlType();
	switch (t1.type()) {
	case SqlType::Char:		// handle char types as in oracle
	    switch (t2.type()) {
	    case SqlType::Char:
		res.setType(ctx, SqlType::Char, t1.length() + t2.length());
		con[1] = t1;
		con[2] = t2;
		break;
	    case SqlType::Varchar:
		res.setType(ctx, SqlType::Varchar, t1.length() + t2.length());
		con[1] = t1;
		con[2] = t2;
		break;
	    case SqlType::Null:
		res.setType(ctx, SqlType::Varchar, t1.length());
		con[1] = t1;
		con[2] = t2;
		break;
	    case SqlType::Unbound:
		res.setType(ctx, SqlType::Unbound);
		con[1] = con[2] = res;
		break;
	    default:
		break;
	    }
	    break;
	case SqlType::Varchar:
	    switch (t2.type()) {
	    case SqlType::Char:
		res.setType(ctx, SqlType::Varchar, t1.length() + t2.length());
		con[1] = t1;
		con[2] = t2;
		break;
	    case SqlType::Varchar:
		res.setType(ctx, SqlType::Varchar, t1.length() + t2.length());
		con[1] = t1;
		con[2] = t2;
		break;
	    case SqlType::Null:
		res.setType(ctx, SqlType::Varchar, t1.length());
		con[1] = t1;
		con[2] = t2;
		break;
	    case SqlType::Unbound:
		res.setType(ctx, SqlType::Unbound);
		con[1] = con[2] = res;
		break;
	    default:
		break;
	    }
	    break;
	case SqlType::Smallint:
	case SqlType::Integer:
	case SqlType::Bigint:
	    switch (t2.type()) {
	    case SqlType::Smallint:
	    case SqlType::Integer:
	    case SqlType::Bigint:
		res.setType(ctx, SqlType::Bigint);
		con[1] = con[2] = res;
		if (t1.unSigned() || t2.unSigned()) {
		    con[1].unSigned(true);
		    con[2].unSigned(true);
		}
		break;
	    case SqlType::Real:
	    case SqlType::Double:
		res.setType(ctx, SqlType::Double);
		con[1] = con[2] = res;
		break;
	    case SqlType::Null:
		res.setType(ctx, SqlType::Null);
		con[1] = con[2] = res;
		break;
	    case SqlType::Unbound:
		res.setType(ctx, SqlType::Unbound);
		con[1] = con[2] = res;
		break;
	    default:
		break;
	    }
	    break;
	case SqlType::Real:
	case SqlType::Double:
	    switch (t2.type()) {
	    case SqlType::Smallint:
	    case SqlType::Integer:
	    case SqlType::Bigint:
	    case SqlType::Real:
	    case SqlType::Double:
		res.setType(ctx, SqlType::Double);
		con[1] = con[2] = res;
		break;
	    case SqlType::Null:
		res.setType(ctx, SqlType::Null);
		con[1] = con[2] = res;
		break;
	    case SqlType::Unbound:
		res.setType(ctx, SqlType::Unbound);
		con[1] = con[2] = res;
		break;
	    default:
		break;
	    }
	    break;
	case SqlType::Null:
	    switch (t2.type()) {
	    case SqlType::Char:
	    case SqlType::Varchar:
		res.setType(ctx, SqlType::Varchar, t2.length());
		con[1] = con[2] = res;
		break;
	    case SqlType::Unbound:
		res.setType(ctx, SqlType::Unbound);
		con[1] = con[2] = res;
		break;
	    default:
		res.setType(ctx, SqlType::Null);
		con[1] = con[2] = res;
		break;
	    }
	    break;
	case SqlType::Unbound:
	    res.setType(ctx, SqlType::Unbound);
	    con[1] = con[2] = res;
	    break;
	default:
	    break;
	}
	if (con[1].type() == SqlType::Undef || con[2].type() == SqlType::Undef) {
	    char b1[40], b2[40];
	    t1.print(b1, sizeof(b1));
	    t2.print(b2, sizeof(b2));
	    ctx.pushStatus(Error::Gen, "type mismatch in operation: %s %s %s", b1, m_op.name(), b2);
	    return 0;
	}
    } else {
	ctx_assert(false);
	return 0;
    }
    if (! ctx.ok())
	return 0;
    // insert required conversions
    for (unsigned i = 1; i <= arity; i++) {
	if (con[i].type() == SqlType::Undef) {
	    ctx.pushStatus(Error::Gen, "mismatched types in operation");
	    return 0;
	}
	if (con[i].type() == SqlType::Unbound) {
	    // parameter type not yet bound
	    continue;
	}
	Plan_expr_conv* exprConv = new Plan_expr_conv(m_root, con[i]);
	m_root->saveNode(exprConv);
	exprConv->setExpr(m_expr[i]);
	m_expr[i] = static_cast<Plan_expr*>(exprConv->analyze(ctx, ctl));
	if (! ctx.ok())
	    return 0;
	ctx_assert(m_expr[i] != 0);
    }
    // set result type
    m_sqlType = res;
    // table dependencies are union from operands
    for (unsigned i = 1; i <= arity; i++) {
	const TableSet& ts = m_expr[i]->tableSet();
	m_tableSet.insert(ts.begin(), ts.end());
    }
    // set alias name  XXX misses operator precedence
    if (arity == 1) {
	m_alias.assign(m_op.name());
	m_alias.append(m_expr[1]->m_alias);
    } else if (arity == 2) {
	m_alias.assign(m_expr[1]->m_alias);
	m_alias.append(m_op.name());
	m_alias.append(m_expr[2]->m_alias);
    }
    return this;
}

Exec_base*
Plan_expr_op::codegen(Ctx& ctx, Ctl& ctl)
{
    if (m_exec != 0)
	return m_exec;
    unsigned arity = m_op.arity();
    Exec_expr_op* exec = new Exec_expr_op(ctl.m_execRoot);
    ctl.m_execRoot->saveNode(exec);
    // create code for operands
    for (unsigned i = 1; i <= arity; i++) {
	ctx_assert(m_expr[i] != 0);
	Exec_expr* execExpr = static_cast<Exec_expr*>(m_expr[i]->codegen(ctx, ctl));
	if (! ctx.ok())
	    return 0;
	ctx_assert(execExpr != 0);
	exec->setExpr(i, execExpr);
    }
    // create the code
    SqlSpec sqlSpec(sqlType(), SqlSpec::Physical);
    Exec_expr_op::Code& code = *new Exec_expr_op::Code(m_op, sqlSpec);
    exec->setCode(code);
    m_exec = exec;
    return exec;
}

void
Plan_expr_op::print(Ctx& ctx)
{
    ctx.print(" [%s", m_op.name());
    Plan_base* a[] = { m_expr[1], m_expr[2] };
    printList(ctx, a, m_op.arity());
    ctx.print("]");
}

bool
Plan_expr_op::isEqual(const Plan_expr* expr) const
{
    ctx_assert(expr != 0);
    if (expr->type() != Plan_expr::TypeOp)
	return false;
    const Plan_expr_op* expr2 = static_cast<const Plan_expr_op*>(expr);
    if (m_op.m_opcode != expr2->m_op.m_opcode)
	return false;
    const unsigned arity = m_op.arity();
    for (unsigned i = 1; i <= arity; i++) {
	ctx_assert(m_expr[i] != 0);
	if (! m_expr[i]->isEqual(expr2->m_expr[i]))
	    return false;
    }
    return true;
}

bool
Plan_expr_op::isGroupBy(const Plan_expr_row* row) const
{
    if (isAnyEqual(row))
	return true;
    const unsigned arity = m_op.arity();
    for (unsigned i = 1; i <= arity; i++) {
	ctx_assert(m_expr[i] != 0);
	if (! m_expr[i]->isGroupBy(row))
	    return false;
    }
    return true;
}

// Code_expr_op

Exec_expr_op::Code::~Code()
{
}

Exec_expr_op::Data::~Data()
{
}

Exec_expr_op::~Exec_expr_op()
{
}

void
Exec_expr_op::alloc(Ctx& ctx, Ctl& ctl)
{
    if (m_data != 0)
	return;
    const Code& code = getCode();
    // allocate subexpressions
    unsigned arity = code.m_op.arity();
    for (unsigned i = 1; i <= arity; i++) {
	ctx_assert(m_expr[i] != 0);
	m_expr[i]->alloc(ctx, ctl);
	if (! ctx.ok())
	    return;
    }
    SqlField sqlField(code.m_sqlSpec);
    Data& data = *new Data(sqlField);
    setData(data);
}

void
Exec_expr_op::close(Ctx& ctx)
{
    const Code& code = getCode();
    unsigned arity = code.m_op.arity();
    for (unsigned i = 1; i <= arity; i++) {
	ctx_assert(m_expr[i] != 0);
	m_expr[i]->close(ctx);
    }
    Data& data = getData();
    data.m_groupField.clear();
}

void
Exec_expr_op::print(Ctx& ctx)
{
    const Code& code = getCode();
    ctx.print(" [%s", code.m_op.name());
    Exec_base* a[] = { m_expr[1], m_expr[2] };
    printList(ctx, a, code.m_op.arity());
    ctx.print("]");
}