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 "decimal.h"
39#include "w_store_sales.h"
40#include "w_store_returns.h"
41#include "genrand.h"
42#include "columns.h"
43#include "build_support.h"
44#include "tables.h"
45#include "constants.h"
46#include "nulls.h"
47#include "tdefs.h"
48#include "scaling.h"
49#include "permute.h"
50#include "scd.h"
51#include "parallel.h"
52
53#include "append_info.h"
54
55#ifdef JMS
56extern rng_t Streams[];
57#endif
58
59struct W_STORE_SALES_TBL g_w_store_sales;
60ds_key_t skipDays(int nTable, ds_key_t *pRemainder);
61static int *pItemPermutation, nItemCount, nItemIndex;
62static ds_key_t jDate, kNewDateIndex;
63
64/*
65 * mk_store_sales
66 */
67static void mk_master(void *info_arr, ds_key_t index) {
68 struct W_STORE_SALES_TBL *r;
69 static decimal_t dMin, dMax;
70 static int bInit = 0, nMaxItemCount;
71 static ds_key_t kNewDateIndex = 0;
72
73 r = &g_w_store_sales;
74
75 if (!bInit) {
76 strtodec(&dMin, "1.00");
77 strtodec(&dMax, "100000.00");
78 nMaxItemCount = 20;
79 jDate = skipDays(STORE_SALES, &kNewDateIndex);
80 pItemPermutation = makePermutation(NULL, nItemCount = (int)getIDCount(ITEM), SS_PERMUTATION);
81
82 bInit = 1;
83 }
84
85 while (index > kNewDateIndex) /* need to move to a new date */
86 {
87 jDate += 1;
88 kNewDateIndex += dateScaling(STORE_SALES, jDate);
89 }
90 r->ss_sold_store_sk = mk_join(SS_SOLD_STORE_SK, STORE, 1);
91 r->ss_sold_time_sk = mk_join(SS_SOLD_TIME_SK, TIME, 1);
92 r->ss_sold_date_sk = mk_join(SS_SOLD_DATE_SK, DATET, 1);
93 r->ss_sold_customer_sk = mk_join(SS_SOLD_CUSTOMER_SK, CUSTOMER, 1);
94 r->ss_sold_cdemo_sk = mk_join(SS_SOLD_CDEMO_SK, CUSTOMER_DEMOGRAPHICS, 1);
95 r->ss_sold_hdemo_sk = mk_join(SS_SOLD_HDEMO_SK, HOUSEHOLD_DEMOGRAPHICS, 1);
96 r->ss_sold_addr_sk = mk_join(SS_SOLD_ADDR_SK, CUSTOMER_ADDRESS, 1);
97 r->ss_ticket_number = index;
98 genrand_integer(&nItemIndex, DIST_UNIFORM, 1, nItemCount, 0, SS_SOLD_ITEM_SK);
99
100 return;
101}
102
103static void mk_detail(void *info_arr, int bPrint) {
104 int nTemp;
105 struct W_STORE_SALES_TBL *r;
106 tdef *pT = getSimpleTdefsByNumber(STORE_SALES);
107
108 r = &g_w_store_sales;
109
110 nullSet(&pT->kNullBitMap, SS_NULLS);
111 /*
112 * items need to be unique within an order
113 * use a sequence within the permutation
114 */
115 if (++nItemIndex > nItemCount)
116 nItemIndex = 1;
117 r->ss_sold_item_sk = matchSCDSK(getPermutationEntry(pItemPermutation, nItemIndex), r->ss_sold_date_sk, ITEM);
118 r->ss_sold_promo_sk = mk_join(SS_SOLD_PROMO_SK, PROMOTION, 1);
119 set_pricing(SS_PRICING, &r->ss_pricing);
120
121 /**
122 * having gone to the trouble to make the sale, now let's see if it gets
123 * returned
124 */
125 genrand_integer(&nTemp, DIST_UNIFORM, 0, 99, 0, SR_IS_RETURNED);
126 if (nTemp < SR_RETURN_PCT) {
127 struct W_STORE_RETURNS_TBL w_web_returns;
128 struct W_STORE_RETURNS_TBL *rr = &w_web_returns;
129 mk_w_store_returns(rr, 1);
130
131 void *info = append_info_get(info_arr, STORE_RETURNS);
132 append_row_start(info);
133
134 append_key(info, rr->sr_returned_date_sk);
135 append_key(info, rr->sr_returned_time_sk);
136 append_key(info, rr->sr_item_sk);
137 append_key(info, rr->sr_customer_sk);
138 append_key(info, rr->sr_cdemo_sk);
139 append_key(info, rr->sr_hdemo_sk);
140 append_key(info, rr->sr_addr_sk);
141 append_key(info, rr->sr_store_sk);
142 append_key(info, rr->sr_reason_sk);
143 append_key(info, rr->sr_ticket_number);
144 append_integer(info, rr->sr_pricing.quantity);
145 append_decimal(info, &rr->sr_pricing.net_paid);
146 append_decimal(info, &rr->sr_pricing.ext_tax);
147 append_decimal(info, &rr->sr_pricing.net_paid_inc_tax);
148 append_decimal(info, &rr->sr_pricing.fee);
149 append_decimal(info, &rr->sr_pricing.ext_ship_cost);
150 append_decimal(info, &rr->sr_pricing.refunded_cash);
151 append_decimal(info, &rr->sr_pricing.reversed_charge);
152 append_decimal(info, &rr->sr_pricing.store_credit);
153 append_decimal(info, &rr->sr_pricing.net_loss);
154 append_row_end(info);
155 }
156
157 void *info = append_info_get(info_arr, STORE_SALES);
158 append_row_start(info);
159
160 append_key(info, r->ss_sold_date_sk);
161 append_key(info, r->ss_sold_time_sk);
162 append_key(info, r->ss_sold_item_sk);
163 append_key(info, r->ss_sold_customer_sk);
164 append_key(info, r->ss_sold_cdemo_sk);
165 append_key(info, r->ss_sold_hdemo_sk);
166 append_key(info, r->ss_sold_addr_sk);
167 append_key(info, r->ss_sold_store_sk);
168 append_key(info, r->ss_sold_promo_sk);
169 append_key(info, r->ss_ticket_number);
170 append_integer(info, r->ss_pricing.quantity);
171 append_decimal(info, &r->ss_pricing.wholesale_cost);
172 append_decimal(info, &r->ss_pricing.list_price);
173 append_decimal(info, &r->ss_pricing.sales_price);
174 append_decimal(info, &r->ss_pricing.coupon_amt);
175 append_decimal(info, &r->ss_pricing.ext_sales_price);
176 append_decimal(info, &r->ss_pricing.ext_wholesale_cost);
177 append_decimal(info, &r->ss_pricing.ext_list_price);
178 append_decimal(info, &r->ss_pricing.ext_tax);
179 append_decimal(info, &r->ss_pricing.coupon_amt);
180 append_decimal(info, &r->ss_pricing.net_paid);
181 append_decimal(info, &r->ss_pricing.net_paid_inc_tax);
182 append_decimal(info, &r->ss_pricing.net_profit);
183
184 append_row_end(info);
185
186 return;
187}
188
189/*
190 * mk_store_sales
191 */
192int mk_w_store_sales(void *info_arr, ds_key_t index) {
193 int nLineitems, i;
194
195 /* build the static portion of an order */
196 mk_master(info_arr, index);
197
198 /* set the number of lineitems and build them */
199 genrand_integer(&nLineitems, DIST_UNIFORM, 8, 16, 0, SS_TICKET_NUMBER);
200 for (i = 1; i <= nLineitems; i++) {
201 mk_detail(info_arr, 1);
202 }
203
204 /**
205 * and finally return 1 since we have already printed the rows
206 */
207 return 0;
208}
209
210/*
211 * Routine:
212 * Purpose:
213 * Algorithm:
214 * Data Structures:
215 *
216 * Params:
217 * Returns:
218 * Called By:
219 * Calls:
220 * Assumptions:
221 * Side Effects:
222 * TODO: None
223 */
224int vld_w_store_sales(int nTable, ds_key_t kRow, int *Permutation) {
225 int nLineitem, nMaxLineitem, i;
226
227 row_skip(nTable, kRow - 1);
228 row_skip(STORE_RETURNS, kRow - 1);
229 jDate = skipDays(STORE_SALES, &kNewDateIndex);
230 mk_master(NULL, kRow);
231 genrand_integer(&nMaxLineitem, DIST_UNIFORM, 8, 16, 9, SS_TICKET_NUMBER);
232 genrand_integer(&nLineitem, DIST_UNIFORM, 1, nMaxLineitem, 0, SS_PRICING_QUANTITY);
233 for (i = 1; i < nLineitem; i++) {
234 mk_detail(NULL, 0);
235 }
236 mk_detail(NULL, 1);
237
238 return (0);
239}
240