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 "genrand.h"
40#include "w_item.h"
41#include "columns.h"
42#include "build_support.h"
43#include "tables.h"
44#include "misc.h"
45#include "nulls.h"
46#include "tdefs.h"
47#include "scd.h"
48
49#include "append_info.h"
50
51/* extern tdef w_tdefs[]; */
52
53struct W_ITEM_TBL g_w_item, g_OldValues;
54
55/*
56 * mk_item
57 */
58int mk_w_item(void *info_arr, ds_key_t index) {
59 /* begin locals declarations */
60 decimal_t dMinPrice, dMaxPrice, dMarkdown;
61 static decimal_t dMinMarkdown, dMaxMarkdown;
62 int32_t bUseSize, bFirstRecord = 0, nFieldChangeFlags, nMin, nMax, nIndex, nTemp;
63 char *cp;
64 struct W_ITEM_TBL *r;
65 static int32_t bInit = 0;
66 struct W_ITEM_TBL *rOldValues = &g_OldValues;
67 char *szMinPrice = NULL, *szMaxPrice = NULL;
68 tdef *pT = getSimpleTdefsByNumber(ITEM);
69
70 r = &g_w_item;
71
72 if (!bInit) {
73 /* some fields are static throughout the data set */
74 strtodec(&dMinMarkdown, MIN_ITEM_MARKDOWN_PCT);
75 strtodec(&dMaxMarkdown, MAX_ITEM_MARKDOWN_PCT);
76
77 bInit = 1;
78 }
79
80 memset(r, 0, sizeof(struct W_ITEM_TBL));
81
82 /* build the new value */
83 nullSet(&pT->kNullBitMap, I_NULLS);
84 r->i_item_sk = index;
85
86 nIndex = pick_distribution(&nMin, "i_manager_id", 2, 1, I_MANAGER_ID);
87 dist_member(&nMax, "i_manager_id", nIndex, 3);
88 genrand_key(&r->i_manager_id, DIST_UNIFORM, (ds_key_t)nMin, (ds_key_t)nMax, 0, I_MANAGER_ID);
89
90 /* if we have generated the required history for this business key and
91 * generate a new one then reset associated fields (e.g., rec_start_date
92 * minimums)
93 */
94 if (setSCDKeys(I_ITEM_ID, index, r->i_item_id, &r->i_rec_start_date_id, &r->i_rec_end_date_id)) {
95 /*
96 * some fields are not changed, even when a new version of the row is
97 * written
98 */
99 bFirstRecord = 1;
100 }
101
102 /*
103 * this is where we select the random number that controls if a field
104 * changes from one record to the next.
105 */
106 nFieldChangeFlags = next_random(I_SCD);
107
108 /* the rest of the record in a history-keeping dimension can either be a new
109 * data value or not; use a random number and its bit pattern to determine
110 * which fields to replace and which to retain
111 */
112 gen_text(r->i_item_desc, 1, RS_I_ITEM_DESC, I_ITEM_DESC);
113 changeSCD(SCD_CHAR, &r->i_item_desc, &rOldValues->i_item_desc, &nFieldChangeFlags, bFirstRecord);
114
115 nIndex = pick_distribution(&szMinPrice, "i_current_price", 2, 1, I_CURRENT_PRICE);
116 dist_member(&szMaxPrice, "i_current_price", nIndex, 3);
117 strtodec(&dMinPrice, szMinPrice);
118 strtodec(&dMaxPrice, szMaxPrice);
119 genrand_decimal(&r->i_current_price, DIST_UNIFORM, &dMinPrice, &dMaxPrice, NULL, I_CURRENT_PRICE);
120 changeSCD(SCD_INT, &r->i_current_price, &rOldValues->i_current_price, &nFieldChangeFlags, bFirstRecord);
121
122 genrand_decimal(&dMarkdown, DIST_UNIFORM, &dMinMarkdown, &dMaxMarkdown, NULL, I_WHOLESALE_COST);
123 decimal_t_op(&r->i_wholesale_cost, OP_MULT, &r->i_current_price, &dMarkdown);
124 changeSCD(SCD_DEC, &r->i_wholesale_cost, &rOldValues->i_wholesale_cost, &nFieldChangeFlags, bFirstRecord);
125
126 hierarchy_item(I_CATEGORY, &r->i_category_id, &r->i_category, index);
127 /*
128 * changeSCD(SCD_INT, &r->i_category_id, &rOldValues->i_category_id,
129 * &nFieldChangeFlags, bFirstRecord);
130 */
131
132 hierarchy_item(I_CLASS, &r->i_class_id, &r->i_class, index);
133 changeSCD(SCD_KEY, &r->i_class_id, &rOldValues->i_class_id, &nFieldChangeFlags, bFirstRecord);
134
135 cp = &r->i_brand[0];
136 hierarchy_item(I_BRAND, &r->i_brand_id, &cp, index);
137 changeSCD(SCD_KEY, &r->i_brand_id, &rOldValues->i_brand_id, &nFieldChangeFlags, bFirstRecord);
138
139 /* some categories have meaningful sizes, some don't */
140 if (r->i_category_id) {
141 dist_member(&bUseSize, "categories", (int)r->i_category_id, 3);
142 pick_distribution(&r->i_size, "sizes", 1, bUseSize + 2, I_SIZE);
143 changeSCD(SCD_PTR, &r->i_size, &rOldValues->i_size, &nFieldChangeFlags, bFirstRecord);
144 } else {
145 bUseSize = 0;
146 r->i_size = NULL;
147 }
148
149 nIndex = pick_distribution(&nMin, "i_manufact_id", 2, 1, I_MANUFACT_ID);
150 genrand_integer(&nTemp, DIST_UNIFORM, nMin, dist_member(NULL, "i_manufact_id", nIndex, 3), 0, I_MANUFACT_ID);
151 r->i_manufact_id = nTemp;
152 changeSCD(SCD_KEY, &r->i_manufact_id, &rOldValues->i_manufact_id, &nFieldChangeFlags, bFirstRecord);
153
154 mk_word(r->i_manufact, "syllables", (int)r->i_manufact_id, RS_I_MANUFACT, ITEM);
155 changeSCD(SCD_CHAR, &r->i_manufact, &rOldValues->i_manufact, &nFieldChangeFlags, bFirstRecord);
156
157 gen_charset(r->i_formulation, DIGITS, RS_I_FORMULATION, RS_I_FORMULATION, I_FORMULATION);
158 embed_string(r->i_formulation, "colors", 1, 2, I_FORMULATION);
159 changeSCD(SCD_CHAR, &r->i_formulation, &rOldValues->i_formulation, &nFieldChangeFlags, bFirstRecord);
160
161 pick_distribution(&r->i_color, "colors", 1, 2, I_COLOR);
162 changeSCD(SCD_PTR, &r->i_color, &rOldValues->i_color, &nFieldChangeFlags, bFirstRecord);
163
164 pick_distribution(&r->i_units, "units", 1, 1, I_UNITS);
165 changeSCD(SCD_PTR, &r->i_units, &rOldValues->i_units, &nFieldChangeFlags, bFirstRecord);
166
167 pick_distribution(&r->i_container, "container", 1, 1, ITEM);
168 changeSCD(SCD_PTR, &r->i_container, &rOldValues->i_container, &nFieldChangeFlags, bFirstRecord);
169
170 mk_word(r->i_product_name, "syllables", (int)index, RS_I_PRODUCT_NAME, ITEM);
171
172 r->i_promo_sk = mk_join(I_PROMO_SK, PROMOTION, 1);
173 genrand_integer(&nTemp, DIST_UNIFORM, 1, 100, 0, I_PROMO_SK);
174 if (nTemp > I_PROMO_PERCENTAGE)
175 r->i_promo_sk = -1;
176
177 /*
178 * if this is the first of a set of revisions, then baseline the old values
179 */
180 if (bFirstRecord)
181 memcpy(&g_OldValues, r, sizeof(struct W_ITEM_TBL));
182
183 if (index == 1)
184 memcpy(&g_OldValues, r, sizeof(struct W_ITEM_TBL));
185
186 void *info = append_info_get(info_arr, ITEM);
187 append_row_start(info);
188
189 append_key(info, r->i_item_sk);
190 append_varchar(info, r->i_item_id);
191 append_date(info, r->i_rec_start_date_id);
192 append_date(info, r->i_rec_end_date_id);
193 append_varchar(info, r->i_item_desc);
194 append_decimal(info, &r->i_current_price);
195 append_decimal(info, &r->i_wholesale_cost);
196 append_key(info, r->i_brand_id);
197 append_varchar(info, r->i_brand);
198 append_key(info, r->i_class_id);
199 append_varchar(info, r->i_class);
200 append_key(info, r->i_category_id);
201 append_varchar(info, r->i_category);
202 append_key(info, r->i_manufact_id);
203 append_varchar(info, r->i_manufact);
204 append_varchar(info, r->i_size);
205 append_varchar(info, r->i_formulation);
206 append_varchar(info, r->i_color);
207 append_varchar(info, r->i_units);
208 append_varchar(info, r->i_container);
209 append_key(info, r->i_manager_id);
210 append_varchar(info, r->i_product_name);
211
212 append_row_end(info);
213
214 return 0;
215}
216