1/*
2 * Legal Notice
3 *
4 * This document and associated source code (the "Work") is a part of a
5 * benchmark specification maintained by the TPC.
6 *
7 * The TPC reserves all right, title, and interest to the Work as provided
8 * under U.S. and international laws, including without limitation all patent
9 * and trademark rights therein.
10 *
11 * No Warranty
12 *
13 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
14 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
15 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
16 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
17 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
18 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
19 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
20 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
21 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
22 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
23 * WITH REGARD TO THE WORK.
24 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
25 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
26 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
27 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
28 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
29 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
30 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
31 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * Contributors:
34 * Gradient Systems
35 */
36#include "config.h"
37#include "porting.h"
38#include <stdio.h>
39#include "constants.h"
40#include "w_store.h"
41#include "date.h"
42#include "decimal.h"
43#include "genrand.h"
44#include "build_support.h"
45#include "misc.h"
46#include "tables.h"
47#include "scaling.h"
48#include "nulls.h"
49#include "tdefs.h"
50#include "scd.h"
51
52#include "append_info.h"
53
54struct W_STORE_TBL g_w_store;
55static struct W_STORE_TBL g_OldValues;
56
57/*
58 * mk_store
59 */
60int mk_w_store(void *info_arr, ds_key_t index) {
61 int32_t nFieldChangeFlags, bFirstRecord = 0;
62
63 /* begin locals declarations */
64 static decimal_t dRevMin, dRevMax;
65 char *sName1, *sName2, *szTemp;
66 int32_t nHierarchyTotal, nStoreType, nPercentage, nDaysOpen, nMin, nMax;
67 static date_t tDate;
68 static decimal_t min_rev_growth, max_rev_growth, dMinTaxPercentage, dMaxTaxPercentage;
69 static int32_t bInit = 0;
70 struct W_STORE_TBL *r, *rOldValues = &g_OldValues;
71 tdef *pT = getSimpleTdefsByNumber(STORE);
72
73 r = &g_w_store;
74
75 if (!bInit) {
76 nHierarchyTotal = (int)get_rowcount(DIVISIONS);
77 nHierarchyTotal *= (int)get_rowcount(COMPANY);
78 strtodt(&tDate, DATE_MINIMUM);
79 strtodec(&min_rev_growth, STORE_MIN_REV_GROWTH);
80 strtodec(&max_rev_growth, STORE_MAX_REV_GROWTH);
81 strtodec(&dRevMin, "1.00");
82 strtodec(&dRevMax, "1000000.00");
83 strtodec(&dMinTaxPercentage, STORE_MIN_TAX_PERCENTAGE);
84 strtodec(&dMaxTaxPercentage, STORE_MAX_TAX_PERCENTAGE);
85
86 /* columns that should be dynamic */
87 r->rec_end_date_id = -1;
88 }
89
90 nullSet(&pT->kNullBitMap, W_STORE_NULLS);
91 r->store_sk = index;
92
93 /* if we have generated the required history for this business key and
94 * generate a new one then reset associate fields (e.g., rec_start_date
95 * minimums)
96 */
97 if (setSCDKeys(S_STORE_ID, index, r->store_id, &r->rec_start_date_id, &r->rec_end_date_id)) {
98 bFirstRecord = 1;
99 }
100
101 /*
102 * this is where we select the random number that controls if a field
103 * changes from one record to the next.
104 */
105 nFieldChangeFlags = next_random(W_STORE_SCD);
106
107 /* the rest of the record in a history-keeping dimension can either be a new
108 * data value or not; use a random number and its bit pattern to determine
109 * which fields to replace and which to retain
110 */
111 nPercentage = genrand_integer(NULL, DIST_UNIFORM, 1, 100, 0, W_STORE_CLOSED_DATE_ID);
112 nDaysOpen =
113 genrand_integer(NULL, DIST_UNIFORM, STORE_MIN_DAYS_OPEN, STORE_MAX_DAYS_OPEN, 0, W_STORE_CLOSED_DATE_ID);
114 if (nPercentage < STORE_CLOSED_PCT)
115 r->closed_date_id = tDate.julian + nDaysOpen;
116 else
117 r->closed_date_id = -1;
118 changeSCD(SCD_KEY, &r->closed_date_id, &rOldValues->closed_date_id, &nFieldChangeFlags, bFirstRecord);
119 if (!r->closed_date_id)
120 r->closed_date_id = -1; /* dates use a special NULL indicator */
121
122 mk_word(r->store_name, "syllables", (long)index, 5, W_STORE_NAME);
123 changeSCD(SCD_CHAR, &r->store_name, &rOldValues->store_name, &nFieldChangeFlags, bFirstRecord);
124
125 /*
126 * use the store type to set the parameters for the rest of the attributes
127 */
128 nStoreType = pick_distribution(&szTemp, "store_type", 1, 1, W_STORE_TYPE);
129 dist_member(&nMin, "store_type", nStoreType, 2);
130 dist_member(&nMax, "store_type", nStoreType, 3);
131 genrand_integer(&r->employees, DIST_UNIFORM, nMin, nMax, 0, W_STORE_EMPLOYEES);
132 changeSCD(SCD_INT, &r->employees, &rOldValues->employees, &nFieldChangeFlags, bFirstRecord);
133
134 dist_member(&nMin, "store_type", nStoreType, 4);
135 dist_member(&nMax, "store_type", nStoreType, 5),
136 genrand_integer(&r->floor_space, DIST_UNIFORM, nMin, nMax, 0, W_STORE_FLOOR_SPACE);
137 changeSCD(SCD_INT, &r->floor_space, &rOldValues->floor_space, &nFieldChangeFlags, bFirstRecord);
138
139 pick_distribution(&r->hours, "call_center_hours", 1, 1, W_STORE_HOURS);
140 changeSCD(SCD_PTR, &r->hours, &rOldValues->hours, &nFieldChangeFlags, bFirstRecord);
141
142 pick_distribution(&sName1, "first_names", 1, 1, W_STORE_MANAGER);
143 pick_distribution(&sName2, "last_names", 1, 1, W_STORE_MANAGER);
144 sprintf(r->store_manager, "%s %s", sName1, sName2);
145 changeSCD(SCD_CHAR, &r->store_manager, &rOldValues->store_manager, &nFieldChangeFlags, bFirstRecord);
146
147 r->market_id = genrand_integer(NULL, DIST_UNIFORM, 1, 10, 0, W_STORE_MARKET_ID);
148 changeSCD(SCD_INT, &r->market_id, &rOldValues->market_id, &nFieldChangeFlags, bFirstRecord);
149
150 genrand_decimal(&r->dTaxPercentage, DIST_UNIFORM, &dMinTaxPercentage, &dMaxTaxPercentage, NULL,
151 W_STORE_TAX_PERCENTAGE);
152 changeSCD(SCD_DEC, &r->dTaxPercentage, &rOldValues->dTaxPercentage, &nFieldChangeFlags, bFirstRecord);
153
154 pick_distribution(&r->geography_class, "geography_class", 1, 1, W_STORE_GEOGRAPHY_CLASS);
155 changeSCD(SCD_PTR, &r->geography_class, &rOldValues->geography_class, &nFieldChangeFlags, bFirstRecord);
156
157 gen_text(&r->market_desc[0], STORE_DESC_MIN, RS_S_MARKET_DESC, W_STORE_MARKET_DESC);
158 changeSCD(SCD_CHAR, &r->market_desc, &rOldValues->market_desc, &nFieldChangeFlags, bFirstRecord);
159
160 pick_distribution(&sName1, "first_names", 1, 1, W_STORE_MARKET_MANAGER);
161 pick_distribution(&sName2, "last_names", 1, 1, W_STORE_MARKET_MANAGER);
162 sprintf(r->market_manager, "%s %s", sName1, sName2);
163 changeSCD(SCD_CHAR, &r->market_manager, &rOldValues->market_manager, &nFieldChangeFlags, bFirstRecord);
164
165 r->division_id = pick_distribution(&r->division_name, "divisions", 1, 1, W_STORE_DIVISION_NAME);
166 changeSCD(SCD_KEY, &r->division_id, &rOldValues->division_id, &nFieldChangeFlags, bFirstRecord);
167 changeSCD(SCD_PTR, &r->division_name, &rOldValues->division_name, &nFieldChangeFlags, bFirstRecord);
168
169 r->company_id = pick_distribution(&r->company_name, "stores", 1, 1, W_STORE_COMPANY_NAME);
170 changeSCD(SCD_KEY, &r->company_id, &rOldValues->company_id, &nFieldChangeFlags, bFirstRecord);
171 changeSCD(SCD_PTR, &r->company_name, &rOldValues->company_name, &nFieldChangeFlags, bFirstRecord);
172
173 mk_address(&r->address, W_STORE_ADDRESS);
174 changeSCD(SCD_PTR, &r->address.city, &rOldValues->address.city, &nFieldChangeFlags, bFirstRecord);
175 changeSCD(SCD_PTR, &r->address.county, &rOldValues->address.county, &nFieldChangeFlags, bFirstRecord);
176 changeSCD(SCD_INT, &r->address.gmt_offset, &rOldValues->address.gmt_offset, &nFieldChangeFlags, bFirstRecord);
177 changeSCD(SCD_PTR, &r->address.state, &rOldValues->address.state, &nFieldChangeFlags, bFirstRecord);
178 changeSCD(SCD_PTR, &r->address.street_type, &rOldValues->address.street_type, &nFieldChangeFlags, bFirstRecord);
179 changeSCD(SCD_PTR, &r->address.street_name1, &rOldValues->address.street_name1, &nFieldChangeFlags, bFirstRecord);
180 changeSCD(SCD_PTR, &r->address.street_name2, &rOldValues->address.street_name2, &nFieldChangeFlags, bFirstRecord);
181 changeSCD(SCD_INT, &r->address.street_num, &rOldValues->address.street_num, &nFieldChangeFlags, bFirstRecord);
182 changeSCD(SCD_INT, &r->address.zip, &rOldValues->address.zip, &nFieldChangeFlags, bFirstRecord);
183
184 char szTemp2[128];
185
186 void *info = append_info_get(info_arr, STORE);
187 append_row_start(info);
188
189 append_key(info, r->store_sk);
190 append_varchar(info, r->store_id);
191 append_date(info, r->rec_start_date_id);
192 append_date(info, r->rec_end_date_id);
193 append_key(info, r->closed_date_id);
194 append_varchar(info, r->store_name);
195 append_integer(info, r->employees);
196 append_integer(info, r->floor_space);
197 append_varchar(info, r->hours);
198 append_varchar(info, &r->store_manager[0]);
199 append_integer(info, r->market_id);
200 append_varchar(info, r->geography_class);
201 append_varchar(info, &r->market_desc[0]);
202 append_varchar(info, &r->market_manager[0]);
203 append_key(info, r->division_id);
204 append_varchar(info, r->division_name);
205 append_key(info, r->company_id);
206 append_varchar(info, r->company_name);
207 append_integer(info, r->address.street_num);
208 if (r->address.street_name2) {
209 sprintf(szTemp2, "%s %s", r->address.street_name1, r->address.street_name2);
210 append_varchar(info, szTemp2);
211 } else
212 append_varchar(info, r->address.street_name1);
213 append_varchar(info, r->address.street_type);
214 append_varchar(info, r->address.suite_num);
215 append_varchar(info, r->address.city);
216 append_varchar(info, r->address.county);
217 append_varchar(info, r->address.state);
218 sprintf(szTemp2, "%05d", r->address.zip);
219 append_varchar(info, szTemp2);
220 append_varchar(info, r->address.country);
221 append_integer(info, r->address.gmt_offset);
222 append_decimal(info, &r->dTaxPercentage);
223
224 append_row_end(info);
225
226 return 0;
227}
228