Skip to content

Commit adc6ba2

Browse files
authored
feat: Date scalars (#36)
Closes #34
1 parent 2f2e107 commit adc6ba2

File tree

4 files changed

+454
-0
lines changed

4 files changed

+454
-0
lines changed

Diff for: lib/src/main/java/io/cloudquery/scalar/DateDay.java

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package io.cloudquery.scalar;
2+
3+
import org.apache.arrow.vector.types.DateUnit;
4+
import org.apache.arrow.vector.types.pojo.ArrowType;
5+
6+
public class DateDay implements Scalar {
7+
protected int value;
8+
protected boolean valid;
9+
10+
public DateDay() {
11+
}
12+
13+
public DateDay(Object value) throws ValidationException {
14+
this.set(value);
15+
}
16+
17+
@Override
18+
public String toString() {
19+
if (this.valid) {
20+
return Integer.toString(this.value);
21+
}
22+
return NULL_VALUE_STRING;
23+
}
24+
25+
@Override
26+
public boolean isValid() {
27+
return this.valid;
28+
}
29+
30+
@Override
31+
public ArrowType dataType() {
32+
return new ArrowType.Date(DateUnit.DAY);
33+
}
34+
35+
@Override
36+
public void set(Object value) throws ValidationException {
37+
if (value == null) {
38+
this.valid = false;
39+
this.value = 0;
40+
return;
41+
}
42+
43+
if (value instanceof Scalar scalar) {
44+
if (!scalar.isValid()) {
45+
this.valid = false;
46+
this.value = 0;
47+
return;
48+
}
49+
50+
if (scalar instanceof DateDay date) {
51+
this.valid = date.valid;
52+
this.value = date.value;
53+
return;
54+
}
55+
56+
this.set(scalar.get());
57+
return;
58+
}
59+
60+
if (value instanceof Integer b) {
61+
this.valid = true;
62+
this.value = b;
63+
return;
64+
}
65+
66+
if (value instanceof String string) {
67+
this.valid = true;
68+
this.value = Integer.parseInt(string);
69+
return;
70+
}
71+
72+
throw new ValidationException(ValidationException.NO_CONVERSION_AVAILABLE, this.dataType(), value);
73+
}
74+
75+
@Override
76+
public Object get() {
77+
if (this.valid) {
78+
return this.value;
79+
}
80+
return null;
81+
}
82+
83+
@Override
84+
public boolean equals(Object other) {
85+
if (other == null) {
86+
return false;
87+
}
88+
89+
if (!(other instanceof DateDay o)) {
90+
return false;
91+
}
92+
93+
return (this.valid == o.valid) && (this.value == o.value);
94+
}
95+
}
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.cloudquery.scalar;
2+
3+
import org.apache.arrow.vector.types.DateUnit;
4+
import org.apache.arrow.vector.types.pojo.ArrowType;
5+
6+
public class DateMilli implements Scalar {
7+
protected long value;
8+
protected boolean valid;
9+
10+
public DateMilli() {
11+
}
12+
13+
public DateMilli(Object value) throws ValidationException {
14+
this.set(value);
15+
}
16+
17+
@Override
18+
public String toString() {
19+
if (this.valid) {
20+
return Long.toString(this.value);
21+
}
22+
return NULL_VALUE_STRING;
23+
}
24+
25+
@Override
26+
public boolean isValid() {
27+
return this.valid;
28+
}
29+
30+
@Override
31+
public ArrowType dataType() {
32+
return new ArrowType.Date(DateUnit.MILLISECOND);
33+
}
34+
35+
@Override
36+
public void set(Object value) throws ValidationException {
37+
if (value == null) {
38+
this.valid = false;
39+
this.value = 0;
40+
return;
41+
}
42+
43+
if (value instanceof Scalar scalar) {
44+
if (!scalar.isValid()) {
45+
this.valid = false;
46+
this.value = 0;
47+
return;
48+
}
49+
50+
if (scalar instanceof DateMilli date) {
51+
this.valid = date.valid;
52+
this.value = date.value;
53+
return;
54+
}
55+
56+
this.set(scalar.get());
57+
return;
58+
}
59+
60+
if (value instanceof Long b) {
61+
this.valid = true;
62+
this.value = b;
63+
return;
64+
}
65+
66+
if (value instanceof Integer b) {
67+
this.valid = true;
68+
this.value = b;
69+
return;
70+
}
71+
72+
if (value instanceof String string) {
73+
this.valid = true;
74+
this.value = Long.parseLong(string);
75+
return;
76+
}
77+
78+
throw new ValidationException(ValidationException.NO_CONVERSION_AVAILABLE, this.dataType(), value);
79+
}
80+
81+
@Override
82+
public Object get() {
83+
if (this.valid) {
84+
return this.value;
85+
}
86+
return null;
87+
}
88+
89+
@Override
90+
public boolean equals(Object other) {
91+
if (other == null) {
92+
return false;
93+
}
94+
95+
if (!(other instanceof DateMilli o)) {
96+
return false;
97+
}
98+
99+
return (this.valid == o.valid) && (this.value == o.value);
100+
}
101+
}
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package io.cloudquery.scalar;
2+
3+
import org.apache.arrow.vector.types.DateUnit;
4+
import org.apache.arrow.vector.types.pojo.ArrowType;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
10+
public class DateDayTest {
11+
@Test
12+
public void testNew() {
13+
assertDoesNotThrow(() -> {
14+
new DateDay();
15+
});
16+
}
17+
18+
@Test
19+
public void testNewWithValidParam() {
20+
assertDoesNotThrow(() -> {
21+
new DateDay(1);
22+
new DateDay("1");
23+
24+
Scalar s = new DateDay(2);
25+
new DateDay(s);
26+
});
27+
}
28+
29+
@Test
30+
public void testNewWithInvalidParam() {
31+
assertThrows(ValidationException.class, () -> {
32+
new DateDay(new char[]{'q'});
33+
});
34+
}
35+
36+
@Test
37+
public void testToString() {
38+
DateDay dateDay = new DateDay();
39+
assertEquals(Scalar.NULL_VALUE_STRING, dateDay.toString());
40+
41+
assertDoesNotThrow(() -> {
42+
dateDay.set("1");
43+
});
44+
assertEquals("1", dateDay.toString());
45+
46+
assertDoesNotThrow(() -> {
47+
dateDay.set(2);
48+
});
49+
assertEquals("2", dateDay.toString());
50+
}
51+
52+
@Test
53+
public void testDataType() {
54+
DateDay dateDay = new DateDay();
55+
assertEquals(new ArrowType.Date(DateUnit.DAY), dateDay.dataType());
56+
}
57+
58+
@Test
59+
public void testIsValid() {
60+
DateDay dateDay = new DateDay();
61+
assertFalse(dateDay.isValid());
62+
63+
assertDoesNotThrow(() -> {
64+
dateDay.set("1");
65+
});
66+
assertTrue(dateDay.isValid());
67+
}
68+
69+
@Test
70+
public void testSet() {
71+
DateDay dateDay = new DateDay();
72+
assertDoesNotThrow(() -> {
73+
new DateDay(1);
74+
new DateDay("2");
75+
76+
Scalar s = new DateDay(1);
77+
dateDay.set(s);
78+
});
79+
}
80+
81+
@Test
82+
public void testSetWithInvalidParam() {
83+
DateDay dateDay = new DateDay();
84+
assertThrows(ValidationException.class, () -> {
85+
dateDay.set(new char[]{});
86+
});
87+
}
88+
89+
@Test
90+
public void testGet() {
91+
DateDay dateDay = new DateDay();
92+
assertFalse(dateDay.isValid());
93+
assertNull(dateDay.get());
94+
95+
assertDoesNotThrow(() -> {
96+
dateDay.set(1);
97+
});
98+
assertTrue(dateDay.isValid());
99+
assertEquals(1, dateDay.get());
100+
101+
assertDoesNotThrow(() -> {
102+
dateDay.set("-1");
103+
});
104+
assertTrue(dateDay.isValid());
105+
assertEquals(-1, dateDay.get());
106+
}
107+
108+
@Test
109+
public void testEquals() {
110+
DateDay a = new DateDay();
111+
DateDay b = new DateDay();
112+
assertEquals(a, b);
113+
assertNotEquals(a, null);
114+
assertNotEquals(a, new Binary()); // we can't cast Binary to DateDay
115+
assertNotEquals(null, a);
116+
117+
assertDoesNotThrow(() -> {
118+
a.set(1);
119+
});
120+
assertNotEquals(a, b);
121+
122+
assertDoesNotThrow(() -> {
123+
for (Object obj : new Object[]{null, 1, -1, "2"}) {
124+
a.set(obj);
125+
assertEquals(a, new DateDay(obj));
126+
}
127+
});
128+
}
129+
}

0 commit comments

Comments
 (0)