summaryrefslogtreecommitdiff
path: root/ndb/src/old_files/client/odbc/codegen/Code_pred_op.cpp
blob: 29736e45818ed1a0d5e82c33483103b324fe4c9c (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
/* 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_pred.hpp"
#include "Code_pred_op.hpp"
#include "Code_root.hpp"

// Pred_op

const char*
Pred_op::name() const
{
    switch (m_opcode) {
    case And:
	return "and";
    case Or:
	return "or";
    case Not:
	return "not";
    }
    ctx_assert(false);
    return "";
}

unsigned
Pred_op::arity() const
{
    switch (m_opcode) {
    case And:
    case Or:
	return 2;
    case Not:
	return 1;
    }
    ctx_assert(false);
    return 0;
}

// Plan_pred_op

Plan_pred_op::~Plan_pred_op()
{
}

Plan_base*
Plan_pred_op::analyze(Ctx& ctx, Ctl& ctl)
{
    m_exec = 0;
    unsigned arity = m_op.arity();
    // check if we remain in top-level AND-clause
    const bool topand = ctl.m_topand;
    if (m_op.m_opcode != Pred_op::And)
	ctl.m_topand = false;
    // analyze sub-predicates
    for (unsigned i = 1; i <= arity; i++) {
	ctx_assert(m_pred[i] != 0);
	m_pred[i]->analyze(ctx, ctl);
	if (! ctx.ok())
	    return 0;
    }
    // save top level predicate on list
    if (topand && ! ctl.m_topand) {
	ctl.m_topcomp.push_back(this);
    }
    ctl.m_topand = topand;
    // table dependencies are union from operands
    m_tableSet.clear();
    for (unsigned i = 1; i <= arity; i++) {
	const TableSet& ts = m_pred[i]->tableSet();
	m_tableSet.insert(ts.begin(), ts.end());
    }
    // set of tables for which interpreter cannot be used
    m_noInterp.clear();
    for (unsigned i = 1; i <= arity; i++) {
	const TableSet& ts = m_pred[i]->noInterp();
	m_noInterp.insert(ts.begin(), ts.end());
    }
    return this;
}

Exec_base*
Plan_pred_op::codegen(Ctx& ctx, Ctl& ctl)
{
    if (m_exec != 0)
	return m_exec;
    unsigned arity = m_op.arity();
    Exec_pred_op* exec = new Exec_pred_op(ctl.m_execRoot);
    ctl.m_execRoot->saveNode(exec);
    // create code for operands
    for (unsigned i = 1; i <= arity; i++) {
	ctx_assert(m_pred[i] != 0);
	Exec_pred* execPred = static_cast<Exec_pred*>(m_pred[i]->codegen(ctx, ctl));
	if (! ctx.ok())
	    return 0;
	ctx_assert(execPred != 0);
	exec->setPred(i, execPred);
    }
    // create the code
    Exec_pred_op::Code& code = *new Exec_pred_op::Code(m_op);
    exec->setCode(code);
    m_exec = exec;
    return exec;
}

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

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

// Code_pred_op

Exec_pred_op::Code::~Code()
{
}

Exec_pred_op::Data::~Data()
{
}

Exec_pred_op::~Exec_pred_op()
{
}

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

void
Exec_pred_op::close(Ctx& ctx)
{
    const Code& code = getCode();
    unsigned arity = code.m_op.arity();
    for (unsigned i = 1; i <= arity; i++) {
	ctx_assert(m_pred[i] != 0);
	m_pred[i]->close(ctx);
    }
}

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