-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_handler.c
More file actions
315 lines (283 loc) · 7.5 KB
/
query_handler.c
File metadata and controls
315 lines (283 loc) · 7.5 KB
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
/*
* Legal Notice
*
* This document and associated source code (the "Work") is a part of a
* benchmark specification maintained by the TPC.
*
* The TPC reserves all right, title, and interest to the Work as provided
* under U.S. and international laws, including without limitation all patent
* and trademark rights therein.
*
* No Warranty
*
* 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
* CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
* AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
* WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
* INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
* DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
* PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
* WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
* ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
* QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
* WITH REGARD TO THE WORK.
* 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
* ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
* COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
* OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
* INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
* OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
* RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
* ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
*
* Contributors:
* Gradient Systems
*/
#include "config.h"
#include "porting.h"
#include <stdio.h>
#ifndef USE_STDLIB_H
#include <malloc.h>
#endif
#include "StringBuffer.h"
#include "eval.h"
#include "substitution.h"
#include "error_msg.h"
#include "qgen_params.h"
#include "genrand.h"
#include "r_params.h"
extern list_t *TemplateList;
extern StringBuffer_t *g_sbTemplateName;
extern int g_nQueryNumber, g_nStreamNumber;
extern option_t *Options;
/*
* Routine:
* Purpose:
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By:
* Calls:
* Assumptions:
* Side Effects:
* TODO: None
*/
substitution_t *
defineSubstitution(template_t *pQuery, char *szSubstitutionName, expr_t *pDefinition)
{
substitution_t *pSub;
pSub = (substitution_t *)malloc(sizeof(struct SUBSTITUTION_T));
MALLOC_CHECK(pSub);
if (pSub == NULL)
return(NULL);
memset(pSub, 0, sizeof(struct SUBSTITUTION_T));
pSub->name = szSubstitutionName;
pSub->pAssignment = pDefinition;
pSub->nSubParts = pDefinition->nValueCount;
addList(pQuery->SubstitutionList, (void *)pSub);
return(pSub);
}
/*
* Routine:
* Purpose:
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By:
* Calls:
* Assumptions:
* Side Effects:
* TODO: None
*/
int
AddQuerySegment(template_t *pQuery, char *szText)
{
segment_t *pSegment;
pSegment = (segment_t *)malloc(sizeof(struct SEGMENT_T));
MALLOC_CHECK(pSegment);
if (pSegment == NULL)
return(-1);
memset(pSegment, 0, sizeof(struct SEGMENT_T));
pSegment->text = szText;
addList(pQuery->SegmentList, (void *)pSegment);
return(0);
}
/*
* Routine:
* Purpose:
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By:
* Calls:
* Assumptions:
* Side Effects:
* TODO: None
*/
Expr_Val_t *
findValue(segment_t *pSegment)
{
Expr_Val_t *pReturnValue;
substitution_t *pSub;
pSub = pSegment->pSubstitution;
pReturnValue = pSub->arValues;
pReturnValue += pSub->nSubParts * pSegment->nSubCount;
pReturnValue += pSegment->nSubUse;
return(pReturnValue);
}
/*
* Routine:
* Purpose:
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By:
* Calls:
* Assumptions:
* Side Effects:
* TODO: None
*/
void PrintTemplate(template_t *t)
{
substitution_t *pSubstitution;
segment_t *pSegment;
for (pSubstitution = (substitution_t *)getHead(t->SubstitutionList);
pSubstitution;
pSubstitution = (substitution_t *)getNext(t->SubstitutionList))
{
printf("DEFINE %s = ", pSubstitution->name);
PrintExpr(pSubstitution->pAssignment);
printf(";\n");
}
printf("\n\n");
for (pSegment = (segment_t *)getHead(t->SegmentList); pSegment; pSegment = (segment_t *)getNext(t->SegmentList))
{
printf("%s", pSegment->text);
if (pSegment->pSubstitution)
{
printf("[%s]", pSegment->pSubstitution->name);
}
printf(" ");
}
printf(";\n");
return;
}
/*
* Routine:
* Purpose:
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By:
* Calls:
* Assumptions:
* Side Effects:
* TODO: None
*/
void GenerateQuery(FILE *pOutFile, FILE *pLogFile, int nQuery)
{
int i,
nBufferCount;
substitution_t *pSub;
segment_t *pSegment;
Expr_Val_t *pValue;
static int nQueryCount = 1;
if (pOutFile == NULL)
pOutFile = stdout;
/* get the template */
pCurrentQuery = getItem(TemplateList, nQuery);
if (!pCurrentQuery)
ReportError(QERR_QUERY_RANGE, NULL, 1);
if (g_sbTemplateName == NULL)
{
g_sbTemplateName = InitBuffer(20, 10);
}
ResetBuffer(g_sbTemplateName);
AddBuffer(g_sbTemplateName, pCurrentQuery->name);
if (pLogFile)
fprintf(pLogFile, "Template: %s\n", pCurrentQuery->name);
if (is_set("DEBUG"))
printf("STATUS: Generating Template: %s\n", pCurrentQuery->name);
/* initialize the template if required */
if (!(pCurrentQuery->flags & QT_INIT))
{
for (pSub = (substitution_t *)getHead(pCurrentQuery->SubstitutionList);
pSub;
pSub = (substitution_t *)getNext(pCurrentQuery->SubstitutionList))
{
nBufferCount = ((pSub->nUse)?pSub->nUse:1) * ((pSub->nSubParts)?pSub->nSubParts:1);
pSub->arValues = (Expr_Val_t *)malloc(nBufferCount * sizeof(struct EXPR_VAL_T));
MALLOC_CHECK(pSub->arValues);
for (i=0; i < nBufferCount; i++)
{
memset(&pSub->arValues[i], 0, sizeof(struct EXPR_VAL_T));
#ifdef MEM_TEST
fprintf(stderr, "pSub arValues %d: %x\n", i, &pSub->arValues[i]);
#endif
pSub->arValues[i].pBuf = InitBuffer(15, 15);
}
}
pCurrentQuery->flags |= QT_INIT;
}
/* select the values for this query */
for (pSub = (substitution_t *)getHead(pCurrentQuery->SubstitutionList);
pSub;
pSub = (substitution_t *)getNext(pCurrentQuery->SubstitutionList))
{
nBufferCount = ((pSub->nUse)?pSub->nUse:1) * ((pSub->nSubParts)?pSub->nSubParts:1);
for (i=0; i < nBufferCount; i++)
{
ResetBuffer(pSub->arValues[i].pBuf);
}
#ifdef MEM_TEST
if (pSub->pAssignment->Value.pBuf == NULL) fprintf(stderr, "NULL pBuf %x @ %d\n", pSub->pAssignment, __LINE__);
#endif
pSub->nDataType = EvalExpr(pSub->pAssignment, pSub->arValues, 0, nQueryCount);
if (pLogFile)
{
for (i=0; i < nBufferCount; i++)
{
fprintf(pLogFile, "\t%s.%02d = ", pSub->name, i+1);
if (!pSub->arValues[i].bUseInt)
fprintf(pLogFile, "%s\n", GetBuffer(pSub->arValues[i].pBuf));
else
{
fprintf(pLogFile, HUGE_FORMAT, pSub->arValues[i].nValue);
fprintf(pLogFile, "\n");
}
}
}
}
/* output the query */
for (pSegment = (segment_t *)getHead(pCurrentQuery->SegmentList);
pSegment;
pSegment = (segment_t *)getNext(pCurrentQuery->SegmentList))
{
if (pSegment->text)
fprintf(pOutFile, "%s", pSegment->text);
if (pSegment->pSubstitution)
{
pValue = findValue(pSegment);
if (!pValue->bUseInt)
fprintf(pOutFile, "%s", GetBuffer(pValue->pBuf));
else
{
fprintf(pOutFile, HUGE_FORMAT, pValue->nValue);
}
}
if (pSegment->flags & QS_EOS)
fprintf(pOutFile, ";\n");
}
nQueryCount += 1;
return;
}