/
schema.ts
214 lines (193 loc) · 5.09 KB
/
schema.ts
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
/**
* A column of a table.
*/
export interface Column {
/**
* Name of the column.
*/
readonly name: string;
/**
* Type of the column.
*/
readonly type: Type;
/**
* Coment describing the column.
*
* @default none
*/
readonly comment?: string;
}
/**
* Represents a type of a column in a table schema.
*/
export interface Type {
/**
* Indicates whether this type is a primitive data type.
*/
readonly isPrimitive: boolean;
/**
* Glue InputString for this type.
*/
readonly inputString: string;
}
/**
* @see https://docs.aws.amazon.com/athena/latest/ug/data-types.html
*/
export class Schema {
public static readonly BOOLEAN: Type = {
isPrimitive: true,
inputString: 'boolean',
};
public static readonly BINARY: Type = {
isPrimitive: true,
inputString: 'binary',
};
/**
* A 64-bit signed INTEGER in two’s complement format, with a minimum value of -2^63 and a maximum value of 2^63-1.
*/
public static readonly BIG_INT: Type = {
isPrimitive: true,
inputString: 'bigint',
};
public static readonly DOUBLE: Type = {
isPrimitive: true,
inputString: 'double',
};
public static readonly FLOAT: Type = {
isPrimitive: true,
inputString: 'float',
};
/**
* A 32-bit signed INTEGER in two’s complement format, with a minimum value of -2^31 and a maximum value of 2^31-1.
*/
public static readonly INTEGER: Type = {
isPrimitive: true,
inputString: 'int',
};
/**
* A 16-bit signed INTEGER in two’s complement format, with a minimum value of -2^15 and a maximum value of 2^15-1.
*/
public static readonly SMALL_INT: Type = {
isPrimitive: true,
inputString: 'smallint',
};
/**
* A 8-bit signed INTEGER in two’s complement format, with a minimum value of -2^7 and a maximum value of 2^7-1
*/
public static readonly TINY_INT: Type = {
isPrimitive: true,
inputString: 'tinyint',
};
/**
* Date type.
*/
public static readonly DATE: Type = {
isPrimitive: true,
inputString: 'date',
};
/**
* Timestamp type (date and time).
*/
public static readonly TIMESTAMP: Type = {
isPrimitive: true,
inputString: 'timestamp',
};
/**
* Arbitrary-length string type.
*/
public static readonly STRING: Type = {
isPrimitive: true,
inputString: 'string',
};
/**
* Creates a decimal type.
*
* TODO: Bounds
*
* @param precision the total number of digits
* @param scale the number of digits in fractional part, the default is 0
*/
public static decimal(precision: number, scale?: number): Type {
return {
isPrimitive: true,
inputString: scale !== undefined ? `decimal(${precision},${scale})` : `decimal(${precision})`,
};
}
/**
* Fixed length character data, with a specified length between 1 and 255.
*
* @param length length between 1 and 255
*/
public static char(length: number): Type {
if (length <= 0 || length > 255) {
throw new Error(`char length must be (inclusively) between 1 and 255, but was ${length}`);
}
if (length % 1 !== 0) {
throw new Error(`char length must be a positive integer, was ${length}`);
}
return {
isPrimitive: true,
inputString: `char(${length})`,
};
}
/**
* Variable length character data, with a specified length between 1 and 65535.
*
* @param length length between 1 and 65535.
*/
public static varchar(length: number): Type {
if (length <= 0 || length > 65535) {
throw new Error(`varchar length must be (inclusively) between 1 and 65535, but was ${length}`);
}
if (length % 1 !== 0) {
throw new Error(`varchar length must be a positive integer, was ${length}`);
}
return {
isPrimitive: true,
inputString: `varchar(${length})`,
};
}
/**
* Creates an array of some other type.
*
* @param itemType type contained by the array.
*/
public static array(itemType: Type): Type {
return {
isPrimitive: false,
inputString: `array<${itemType.inputString}>`,
};
}
/**
* Creates a map of some primitive key type to some value type.
*
* @param keyType type of key, must be a primitive.
* @param valueType type fo the value indexed by the key.
*/
public static map(keyType: Type, valueType: Type): Type {
if (!keyType.isPrimitive) {
throw new Error(`the key type of a 'map' must be a primitive, but was ${keyType.inputString}`);
}
return {
isPrimitive: false,
inputString: `map<${keyType.inputString},${valueType.inputString}>`,
};
}
/**
* Creates a nested structure containing individually named and typed columns.
*
* @param columns the columns of the structure.
*/
public static struct(columns: Column[]): Type {
return {
isPrimitive: false,
inputString: `struct<${columns.map(column => {
if (column.comment === undefined) {
return `${column.name}:${column.type.inputString}`;
} else {
return `${column.name}:${column.type.inputString} COMMENT '${column.comment}'`;
}
}).join(',')}>`,
};
}
}