17
17
18
18
package org .apache .seatunnel .connectors .seatunnel .fake .utils ;
19
19
20
+ import org .apache .seatunnel .api .table .catalog .Column ;
21
+ import org .apache .seatunnel .api .table .type .DecimalType ;
20
22
import org .apache .seatunnel .common .utils .BufferUtils ;
21
23
import org .apache .seatunnel .connectors .seatunnel .fake .config .FakeConfig ;
22
24
25
27
import org .apache .commons .lang3 .RandomUtils ;
26
28
27
29
import java .math .BigDecimal ;
30
+ import java .math .RoundingMode ;
28
31
import java .nio .ByteBuffer ;
29
32
import java .time .LocalDate ;
30
33
import java .time .LocalDateTime ;
@@ -45,38 +48,42 @@ private static <T> T randomFromList(List<T> list) {
45
48
return list .get (index );
46
49
}
47
50
48
- public Boolean randomBoolean () {
51
+ public Boolean randomBoolean (Column column ) {
49
52
return RandomUtils .nextInt (0 , 2 ) == 1 ;
50
53
}
51
54
52
- public BigDecimal randomBigDecimal (int precision , int scale ) {
55
+ public BigDecimal randomBigDecimal (Column column ) {
56
+ DecimalType dataType = (DecimalType ) column .getDataType ();
53
57
return new BigDecimal (
54
- RandomStringUtils .randomNumeric (precision - scale )
58
+ RandomStringUtils .randomNumeric (dataType . getPrecision () - dataType . getScale () )
55
59
+ "."
56
- + RandomStringUtils .randomNumeric (scale ));
60
+ + RandomStringUtils .randomNumeric (dataType . getScale () ));
57
61
}
58
62
59
- public byte [] randomBytes () {
63
+ public byte [] randomBytes (Column column ) {
60
64
return RandomStringUtils .randomAlphabetic (fakeConfig .getBytesLength ()).getBytes ();
61
65
}
62
66
63
- public String randomString () {
67
+ public String randomString (Column column ) {
64
68
List <String > stringTemplate = fakeConfig .getStringTemplate ();
65
69
if (!CollectionUtils .isEmpty (stringTemplate )) {
66
70
return randomFromList (stringTemplate );
67
71
}
68
- return RandomStringUtils .randomAlphabetic (fakeConfig .getStringLength ());
72
+ return RandomStringUtils .randomAlphabetic (
73
+ column .getColumnLength () != null
74
+ ? column .getColumnLength ().intValue ()
75
+ : fakeConfig .getStringLength ());
69
76
}
70
77
71
- public Byte randomTinyint () {
78
+ public Byte randomTinyint (Column column ) {
72
79
List <Integer > tinyintTemplate = fakeConfig .getTinyintTemplate ();
73
80
if (!CollectionUtils .isEmpty (tinyintTemplate )) {
74
81
return randomFromList (tinyintTemplate ).byteValue ();
75
82
}
76
83
return (byte ) RandomUtils .nextInt (fakeConfig .getTinyintMin (), fakeConfig .getTinyintMax ());
77
84
}
78
85
79
- public Short randomSmallint () {
86
+ public Short randomSmallint (Column column ) {
80
87
List <Integer > smallintTemplate = fakeConfig .getSmallintTemplate ();
81
88
if (!CollectionUtils .isEmpty (smallintTemplate )) {
82
89
return randomFromList (smallintTemplate ).shortValue ();
@@ -85,48 +92,55 @@ public Short randomSmallint() {
85
92
RandomUtils .nextInt (fakeConfig .getSmallintMin (), fakeConfig .getSmallintMax ());
86
93
}
87
94
88
- public Integer randomInt () {
95
+ public Integer randomInt (Column column ) {
89
96
List <Integer > intTemplate = fakeConfig .getIntTemplate ();
90
97
if (!CollectionUtils .isEmpty (intTemplate )) {
91
98
return randomFromList (intTemplate );
92
99
}
93
100
return RandomUtils .nextInt (fakeConfig .getIntMin (), fakeConfig .getIntMax ());
94
101
}
95
102
96
- public Long randomBigint () {
103
+ public Long randomBigint (Column column ) {
97
104
List <Long > bigTemplate = fakeConfig .getBigTemplate ();
98
105
if (!CollectionUtils .isEmpty (bigTemplate )) {
99
106
return randomFromList (bigTemplate );
100
107
}
101
108
return RandomUtils .nextLong (fakeConfig .getBigintMin (), fakeConfig .getBigintMax ());
102
109
}
103
110
104
- public Float randomFloat () {
111
+ public Float randomFloat (Column column ) {
105
112
List <Double > floatTemplate = fakeConfig .getFloatTemplate ();
106
113
if (!CollectionUtils .isEmpty (floatTemplate )) {
107
114
return randomFromList (floatTemplate ).floatValue ();
108
115
}
109
- return RandomUtils .nextFloat (
110
- (float ) fakeConfig .getFloatMin (), (float ) fakeConfig .getFloatMax ());
116
+ float v =
117
+ RandomUtils .nextFloat (
118
+ (float ) fakeConfig .getFloatMin (), (float ) fakeConfig .getFloatMax ());
119
+ return column .getScale () == null
120
+ ? v
121
+ : new BigDecimal (v ).setScale (column .getScale (), RoundingMode .HALF_UP ).floatValue ();
111
122
}
112
123
113
- public Double randomDouble () {
124
+ public Double randomDouble (Column column ) {
114
125
List <Double > doubleTemplate = fakeConfig .getDoubleTemplate ();
115
126
if (!CollectionUtils .isEmpty (doubleTemplate )) {
116
127
return randomFromList (doubleTemplate );
117
128
}
118
- return RandomUtils .nextDouble (fakeConfig .getDoubleMin (), fakeConfig .getDoubleMax ());
129
+ double v = RandomUtils .nextDouble (fakeConfig .getDoubleMin (), fakeConfig .getDoubleMax ());
130
+ return column .getScale () == null
131
+ ? v
132
+ : new BigDecimal (v ).setScale (column .getScale (), RoundingMode .HALF_UP ).floatValue ();
119
133
}
120
134
121
- public LocalDate randomLocalDate () {
122
- return randomLocalDateTime ().toLocalDate ();
135
+ public LocalDate randomLocalDate (Column column ) {
136
+ return randomLocalDateTime (column ).toLocalDate ();
123
137
}
124
138
125
- public LocalTime randomLocalTime () {
126
- return randomLocalDateTime ().toLocalTime ();
139
+ public LocalTime randomLocalTime (Column column ) {
140
+ return randomLocalDateTime (column ).toLocalTime ();
127
141
}
128
142
129
- public LocalDateTime randomLocalDateTime () {
143
+ public LocalDateTime randomLocalDateTime (Column column ) {
130
144
int year ;
131
145
int month ;
132
146
int day ;
@@ -172,25 +186,32 @@ public LocalDateTime randomLocalDateTime() {
172
186
return LocalDateTime .of (year , month , day , hour , minute , second );
173
187
}
174
188
175
- public ByteBuffer randomBinaryVector () {
176
- int byteCount = fakeConfig .getBinaryVectorDimension () / 8 ;
189
+ public ByteBuffer randomBinaryVector (Column column ) {
190
+ int byteCount =
191
+ (column .getScale () != null )
192
+ ? column .getScale () / 8
193
+ : fakeConfig .getBinaryVectorDimension () / 8 ;
177
194
// binary vector doesn't care endian since each byte is independent
178
195
return ByteBuffer .wrap (RandomUtils .nextBytes (byteCount ));
179
196
}
180
197
181
- public ByteBuffer randomFloatVector () {
182
- Float [] floatVector = new Float [fakeConfig .getVectorDimension ()];
183
- for (int i = 0 ; i < fakeConfig .getVectorDimension (); i ++) {
198
+ public ByteBuffer randomFloatVector (Column column ) {
199
+ int count =
200
+ (column .getScale () != null ) ? column .getScale () : fakeConfig .getVectorDimension ();
201
+ Float [] floatVector = new Float [count ];
202
+ for (int i = 0 ; i < count ; i ++) {
184
203
floatVector [i ] =
185
204
RandomUtils .nextFloat (
186
205
fakeConfig .getVectorFloatMin (), fakeConfig .getVectorFloatMax ());
187
206
}
188
207
return BufferUtils .toByteBuffer (floatVector );
189
208
}
190
209
191
- public ByteBuffer randomFloat16Vector () {
192
- Short [] float16Vector = new Short [fakeConfig .getVectorDimension ()];
193
- for (int i = 0 ; i < fakeConfig .getVectorDimension (); i ++) {
210
+ public ByteBuffer randomFloat16Vector (Column column ) {
211
+ int count =
212
+ (column .getScale () != null ) ? column .getScale () : fakeConfig .getVectorDimension ();
213
+ Short [] float16Vector = new Short [count ];
214
+ for (int i = 0 ; i < count ; i ++) {
194
215
float value =
195
216
RandomUtils .nextFloat (
196
217
fakeConfig .getVectorFloatMin (), fakeConfig .getVectorFloatMax ());
@@ -199,9 +220,11 @@ public ByteBuffer randomFloat16Vector() {
199
220
return BufferUtils .toByteBuffer (float16Vector );
200
221
}
201
222
202
- public ByteBuffer randomBFloat16Vector () {
203
- Short [] bfloat16Vector = new Short [fakeConfig .getVectorDimension ()];
204
- for (int i = 0 ; i < fakeConfig .getVectorDimension (); i ++) {
223
+ public ByteBuffer randomBFloat16Vector (Column column ) {
224
+ int count =
225
+ (column .getScale () != null ) ? column .getScale () : fakeConfig .getVectorDimension ();
226
+ Short [] bfloat16Vector = new Short [count ];
227
+ for (int i = 0 ; i < count ; i ++) {
205
228
float value =
206
229
RandomUtils .nextFloat (
207
230
fakeConfig .getVectorFloatMin (), fakeConfig .getVectorFloatMax ());
@@ -210,10 +233,10 @@ public ByteBuffer randomBFloat16Vector() {
210
233
return BufferUtils .toByteBuffer (bfloat16Vector );
211
234
}
212
235
213
- public Map <Integer , Float > randomSparseFloatVector () {
236
+ public Map <Integer , Float > randomSparseFloatVector (Column column ) {
214
237
Map <Integer , Float > sparseVector = new HashMap <>();
215
-
216
- Integer nonZeroElements = fakeConfig .getVectorDimension ();
238
+ int nonZeroElements =
239
+ ( column . getScale () != null ) ? column . getScale () : fakeConfig .getVectorDimension ();
217
240
while (nonZeroElements > 0 ) {
218
241
Integer index = RandomUtils .nextInt ();
219
242
Float value =
0 commit comments