Skip to content

Commit 9abfbaf

Browse files
authored
chore: adding scalar factory (#68)
fixes: #67
1 parent 400c406 commit 9abfbaf

File tree

5 files changed

+162
-3
lines changed

5 files changed

+162
-3
lines changed

lib/src/main/java/io/cloudquery/scalar/Scalar.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.cloudquery.scalar;
22

3+
import io.cloudquery.types.UUIDType;
34
import org.apache.arrow.vector.types.pojo.ArrowType;
45

56
import java.util.Objects;
@@ -73,4 +74,88 @@ public final int hashCode() {
7374
}
7475

7576
public static final java.lang.String NULL_VALUE_STRING = "(null)";
77+
78+
public static Scalar<?> fromArrowType(ArrowType arrowType) {
79+
switch (arrowType.getTypeID()) {
80+
case Timestamp -> {
81+
return new Timestamp();
82+
}
83+
case Binary -> {
84+
return new Binary();
85+
}
86+
case LargeBinary -> {
87+
return new Binary.LargeBinary();
88+
}
89+
case Bool -> {
90+
return new Bool();
91+
}
92+
case Utf8, LargeUtf8 -> {
93+
return new String();
94+
}
95+
case Int -> {
96+
return fromIntArrowType((ArrowType.Int) arrowType);
97+
}
98+
case FloatingPoint -> {
99+
return fromFloatArrowType((ArrowType.FloatingPoint) arrowType);
100+
}
101+
case Date -> {
102+
return fromDateArrowType((ArrowType.Date) arrowType);
103+
}
104+
case Duration -> {
105+
return new Duration();
106+
}
107+
}
108+
109+
if (arrowType instanceof ArrowType.ExtensionType extensionType) {
110+
//noinspection SwitchStatementWithTooFewBranches
111+
switch (extensionType.extensionName()) {
112+
case UUIDType.EXTENSION_NAME -> {
113+
return new UUID();
114+
}
115+
// TODO: Add support for these types when scalar available
116+
// case JSONType.EXTENSION_NAME -> {
117+
// return new JSON();
118+
// }
119+
// case INETType.EXTENSION_NAME -> {
120+
// return new INET();
121+
// }
122+
}
123+
}
124+
125+
throw new UnsupportedOperationException("Unsupported type: " + arrowType);
126+
}
127+
128+
private static Scalar<?> fromIntArrowType(ArrowType.Int intType) {
129+
Number<? extends java.lang.Number> numberType;
130+
switch (intType.getBitWidth()) {
131+
case 8 -> numberType = intType.getIsSigned() ? new Number.Int8() : new Number.UInt8();
132+
case 16 -> numberType = intType.getIsSigned() ? new Number.Int16() : new Number.UInt16();
133+
case 32 -> numberType = intType.getIsSigned() ? new Number.Int32() : new Number.UInt32();
134+
case 64 -> numberType = intType.getIsSigned() ? new Number.Int64() : new Number.UInt64();
135+
default -> throw new UnsupportedOperationException("Unsupported type: " + intType);
136+
}
137+
return numberType;
138+
}
139+
140+
private static Scalar<?> fromFloatArrowType(ArrowType.FloatingPoint floatType) {
141+
Number<? extends java.lang.Number> numberType;
142+
switch (floatType.getPrecision()) {
143+
case SINGLE -> numberType = new Number.Float32();
144+
case DOUBLE -> numberType = new Number.Float64();
145+
default -> throw new UnsupportedOperationException("Unsupported type: " + floatType);
146+
}
147+
return numberType;
148+
}
149+
150+
private static Scalar<?> fromDateArrowType(ArrowType.Date dateType) {
151+
switch (dateType.getUnit()) {
152+
case DAY -> {
153+
return new DateDay();
154+
}
155+
case MILLISECOND -> {
156+
return new DateMilli();
157+
}
158+
}
159+
throw new UnsupportedOperationException("Unsupported type: " + dateType);
160+
}
76161
}

lib/src/main/java/io/cloudquery/types/InetType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
public class InetType extends ArrowType.ExtensionType {
99
public static final InetType INSTANCE = new InetType();
10+
public static final String EXTENSION_NAME = "inet";
1011

1112
@Override
1213
public ArrowType storageType() {
@@ -15,7 +16,7 @@ public ArrowType storageType() {
1516

1617
@Override
1718
public String extensionName() {
18-
return "inet";
19+
return EXTENSION_NAME;
1920
}
2021

2122
@Override

lib/src/main/java/io/cloudquery/types/JSONType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public class JSONType extends ExtensionType {
1010
public static final JSONType INSTANCE = new JSONType();
11+
public static final String EXTENSION_NAME = "json";
1112

1213
@Override
1314
public ArrowType storageType() {
@@ -16,7 +17,7 @@ public ArrowType storageType() {
1617

1718
@Override
1819
public String extensionName() {
19-
return "json";
20+
return EXTENSION_NAME;
2021
}
2122

2223
@Override

lib/src/main/java/io/cloudquery/types/UUIDType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
public class UUIDType extends ExtensionType {
1717
public static final int BYTE_WIDTH = 16;
18+
public static final String EXTENSION_NAME = "uuid";
1819

1920
@Override
2021
public ArrowType storageType() {
@@ -23,7 +24,7 @@ public ArrowType storageType() {
2324

2425
@Override
2526
public String extensionName() {
26-
return "uuid";
27+
return EXTENSION_NAME;
2728
}
2829

2930
@Override
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.cloudquery.scalar;
2+
3+
import io.cloudquery.types.UUIDType;
4+
import org.apache.arrow.vector.types.DateUnit;
5+
import org.apache.arrow.vector.types.FloatingPointPrecision;
6+
import org.apache.arrow.vector.types.TimeUnit;
7+
import org.apache.arrow.vector.types.pojo.ArrowType;
8+
import org.apache.arrow.vector.types.pojo.ExtensionTypeRegistry;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.Arguments;
11+
import org.junit.jupiter.params.provider.MethodSource;
12+
13+
import java.time.ZoneOffset;
14+
import java.util.stream.Stream;
15+
16+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
17+
18+
public class ScalarTest {
19+
@ParameterizedTest
20+
@MethodSource("testDataSource")
21+
public void shouldCreateScalarFromArrowType(ArrowType arrowType, Class<? extends Scalar<?>> scalarClazz) {
22+
ExtensionTypeRegistry.register(new UUIDType());
23+
24+
assertInstanceOf(scalarClazz, Scalar.fromArrowType(arrowType));
25+
}
26+
27+
public static Stream<Arguments> testDataSource() {
28+
return Stream.of(
29+
// Timestamp
30+
Arguments.of(new ArrowType.Timestamp(TimeUnit.MILLISECOND, ZoneOffset.UTC.toString()), Timestamp.class),
31+
32+
// String
33+
Arguments.of(new ArrowType.Utf8(), String.class),
34+
Arguments.of(new ArrowType.LargeUtf8(), String.class),
35+
36+
// Binary
37+
Arguments.of(new ArrowType.Binary(), Binary.class),
38+
Arguments.of(new ArrowType.LargeBinary(), Binary.LargeBinary.class),
39+
40+
// Boolean
41+
Arguments.of(new ArrowType.Bool(), Bool.class),
42+
43+
// Signed Integers
44+
Arguments.of(new ArrowType.Int(8, true), Number.Int8.class),
45+
Arguments.of(new ArrowType.Int(16, true), Number.Int16.class),
46+
Arguments.of(new ArrowType.Int(32, true), Number.Int32.class),
47+
Arguments.of(new ArrowType.Int(64, true), Number.Int64.class),
48+
49+
// Unsigned Integers
50+
Arguments.of(new ArrowType.Int(8, false), Number.UInt8.class),
51+
Arguments.of(new ArrowType.Int(16, false), Number.UInt16.class),
52+
Arguments.of(new ArrowType.Int(32, false), Number.UInt32.class),
53+
Arguments.of(new ArrowType.Int(64, false), Number.UInt64.class),
54+
55+
// Float
56+
// Arguments.of( new ArrowType.FloatingPoint(FloatingPointPrecision.HALF), Number.Float16.class),
57+
Arguments.of(new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE), Number.Float32.class),
58+
Arguments.of(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE), Number.Float64.class),
59+
60+
// Extension
61+
Arguments.of(new UUIDType(), UUID.class),
62+
63+
// Date
64+
Arguments.of(new ArrowType.Date(DateUnit.DAY), DateDay.class),
65+
Arguments.of(new ArrowType.Date(DateUnit.MILLISECOND), DateMilli.class),
66+
67+
// Duration
68+
Arguments.of(new ArrowType.Duration(TimeUnit.MILLISECOND), Duration.class)
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)