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_datetbl.h"
41#include "columns.h"
42#include "build_support.h"
43#include "tables.h"
44#include "nulls.h"
45#include "tdefs.h"
46
47#include "append_info.h"
48
49struct W_DATE_TBL g_w_date;
50/* extern tdef w_tdefs[]; */
51
52/*
53 * Routine: mk_datetbl
54 * Purpose: populate the date dimension
55 * Algorithm:
56 * Data Structures:
57 *
58 * Params:
59 * Returns:
60 * Called By:
61 * Calls:
62 * Assumptions:
63 * Side Effects:
64 * TODO:
65 */
66int mk_w_date(void *info_arr, ds_key_t index) {
67 int res = 0;
68
69 /* begin locals declarations */
70 static date_t base_date;
71 int day_index, nTemp;
72 date_t temp_date, dTemp2;
73 struct W_DATE_TBL *r;
74 static int bInit = 0;
75 tdef *pT = getSimpleTdefsByNumber(DATET);
76
77 r = &g_w_date;
78
79 if (!bInit) {
80 r->d_month_seq = 0;
81 r->d_week_seq = 1;
82 r->d_quarter_seq = 1;
83 r->d_current_month = 0;
84 r->d_current_quarter = 0;
85 r->d_current_week = 0;
86 strtodt(&base_date, "1900-01-01");
87 /* Make exceptions to the 1-rng-call-per-row rule */
88 bInit = 1;
89 }
90
91 nullSet(&pT->kNullBitMap, D_NULLS);
92 nTemp = (long)index + base_date.julian;
93 r->d_date_sk = nTemp;
94 mk_bkey(&r->d_date_id[0], nTemp, D_DATE_ID);
95 jtodt(&temp_date, nTemp);
96 r->d_year = temp_date.year;
97 r->d_dow = set_dow(&temp_date);
98 r->d_moy = temp_date.month;
99 r->d_dom = temp_date.day;
100 /* set the sequence counts; assumes that the date table starts on a year
101 * boundary */
102 r->d_week_seq = ((int)index + 6) / 7;
103 r->d_month_seq = (r->d_year - 1900) * 12 + r->d_moy - 1;
104 r->d_quarter_seq = (r->d_year - 1900) * 4 + r->d_moy / 3 + 1;
105 day_index = day_number(&temp_date);
106 dist_member(&r->d_qoy, "calendar", day_index, 6);
107 /* fiscal year is identical to calendar year */
108 r->d_fy_year = r->d_year;
109 r->d_fy_quarter_seq = r->d_quarter_seq;
110 r->d_fy_week_seq = r->d_week_seq;
111 r->d_day_name = weekday_names[r->d_dow + 1];
112 dist_member(&r->d_holiday, "calendar", day_index, 8);
113 if ((r->d_dow == 5) || (r->d_dow == 6))
114 r->d_weekend = 1;
115 else
116 r->d_weekend = 0;
117 if (day_index == 1)
118 dist_member(&r->d_following_holiday, "calendar", 365 + is_leap(r->d_year - 1), 8);
119 else
120 dist_member(&r->d_following_holiday, "calendar", day_index - 1, 8);
121 date_t_op(&dTemp2, OP_FIRST_DOM, &temp_date, 0);
122 r->d_first_dom = dTemp2.julian;
123 date_t_op(&dTemp2, OP_LAST_DOM, &temp_date, 0);
124 r->d_last_dom = dTemp2.julian;
125 date_t_op(&dTemp2, OP_SAME_LY, &temp_date, 0);
126 r->d_same_day_ly = dTemp2.julian;
127 date_t_op(&dTemp2, OP_SAME_LQ, &temp_date, 0);
128 r->d_same_day_lq = dTemp2.julian;
129 r->d_current_day = (r->d_date_sk == CURRENT_DAY) ? 1 : 0;
130 r->d_current_year = (r->d_year == CURRENT_YEAR) ? 1 : 0;
131 if (r->d_current_year) {
132 r->d_current_month = (r->d_moy == CURRENT_MONTH) ? 1 : 0;
133 r->d_current_quarter = (r->d_qoy == CURRENT_QUARTER) ? 1 : 0;
134 r->d_current_week = (r->d_week_seq == CURRENT_WEEK) ? 1 : 0;
135 }
136
137 char sQuarterName[7];
138
139 void *info = append_info_get(info_arr, DATET);
140 append_row_start(info);
141
142 append_key(info, r->d_date_sk);
143 append_varchar(info, r->d_date_id);
144 append_date(info, r->d_date_sk);
145 append_integer(info, r->d_month_seq);
146 append_integer(info, r->d_week_seq);
147 append_integer(info, r->d_quarter_seq);
148 append_integer(info, r->d_year);
149 append_integer(info, r->d_dow);
150 append_integer(info, r->d_moy);
151 append_integer(info, r->d_dom);
152 append_integer(info, r->d_qoy);
153 append_integer(info, r->d_fy_year);
154 append_integer(info, r->d_fy_quarter_seq);
155 append_integer(info, r->d_fy_week_seq);
156 append_varchar(info, r->d_day_name);
157 sprintf(sQuarterName, "%4dQ%d", r->d_year, r->d_qoy);
158 append_varchar(info, sQuarterName);
159 append_boolean(info, r->d_holiday);
160 append_boolean(info, r->d_weekend);
161 append_boolean(info, r->d_following_holiday);
162 append_integer(info, r->d_first_dom);
163 append_integer(info, r->d_last_dom);
164 append_integer(info, r->d_same_day_ly);
165 append_integer(info, r->d_same_day_lq);
166 append_boolean(info, r->d_current_day);
167 append_boolean(info, r->d_current_week);
168 append_boolean(info, r->d_current_month);
169 append_boolean(info, r->d_current_quarter);
170 append_boolean(info, r->d_current_year);
171
172 append_row_end(info);
173
174 return (res);
175}
176