Skip to content

Commit b9b73d1

Browse files
authored
feat: io.cloudquery.scalar.Binary implementation (#20)
1 parent 22b2350 commit b9b73d1

File tree

4 files changed

+165
-6
lines changed

4 files changed

+165
-6
lines changed

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

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package io.cloudquery.scalar;
2+
3+
import org.apache.arrow.vector.types.pojo.ArrowType;
4+
import org.apache.commons.codec.binary.Base64;
5+
6+
import java.util.Arrays;
7+
8+
public class Binary implements Scalar {
9+
protected byte[] value;
10+
protected boolean valid;
11+
12+
public Binary() {
13+
}
14+
15+
public Binary(Object value) throws ValidationException {
16+
this.set(value);
17+
}
18+
19+
@Override
20+
public String toString() {
21+
if (this.valid) {
22+
return Base64.encodeBase64String(this.value);
23+
}
24+
return NULL_VALUE_STRING;
25+
}
26+
27+
@Override
28+
public boolean isValid() {
29+
return this.valid;
30+
}
31+
32+
@Override
33+
public ArrowType dataType() {
34+
return ArrowType.Binary.INSTANCE;
35+
}
36+
37+
@Override
38+
public void set(Object value) throws ValidationException {
39+
if (value == null) {
40+
this.valid = false;
41+
this.value = null;
42+
return;
43+
}
44+
45+
if (value instanceof Scalar scalar) {
46+
if (!scalar.isValid()) {
47+
this.valid = false;
48+
this.value = null;
49+
return;
50+
}
51+
52+
this.set(scalar.get());
53+
return;
54+
}
55+
56+
if (value instanceof byte[] bytes) {
57+
this.valid = true;
58+
this.value = bytes;
59+
return;
60+
}
61+
62+
if (value instanceof String string) {
63+
this.valid = true;
64+
this.value = Base64.decodeBase64(string);
65+
return;
66+
}
67+
68+
if (value instanceof char[] chars) {
69+
this.valid = true;
70+
this.value = Base64.decodeBase64(new String(chars));
71+
return;
72+
}
73+
74+
throw new ValidationException(ValidationException.NO_CONVERSION_AVAILABLE, this.dataType(), value);
75+
}
76+
77+
@Override
78+
public Object get() {
79+
if (this.valid) {
80+
return this.value;
81+
}
82+
return null;
83+
}
84+
85+
@Override
86+
public boolean equals(Object other) {
87+
if (other == null) {
88+
return false;
89+
}
90+
91+
if (!(other instanceof Binary o)) {
92+
return false;
93+
}
94+
95+
return (this.valid && o.valid) && Arrays.equals(this.value, o.value);
96+
}
97+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.cloudquery.scalar;
2+
3+
import org.apache.arrow.vector.types.pojo.ArrowType;
4+
5+
public class LargeBinary extends Binary {
6+
7+
public LargeBinary() {
8+
}
9+
10+
public LargeBinary(Object value) throws ValidationException {
11+
this.set(value);
12+
}
13+
14+
@Override
15+
public ArrowType dataType() {
16+
return ArrowType.LargeBinary.INSTANCE;
17+
}
18+
}

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
import org.apache.arrow.vector.types.pojo.ArrowType;
44

55
public interface Scalar {
6-
String String();
6+
String toString();
77

8-
Boolean IsValid();
8+
boolean isValid();
99

10-
ArrowType DataType();
10+
ArrowType dataType();
1111

12-
void Set(Object obj);
12+
void set(Object value) throws ValidationException;
1313

14-
Object Get();
14+
Object get();
1515

16-
Boolean Equal(Scalar other);
16+
boolean equals(Object other);
17+
18+
String NULL_VALUE_STRING = "(null)";
1719
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.cloudquery.scalar;
2+
3+
import org.apache.arrow.vector.types.pojo.ArrowType;
4+
5+
public class ValidationException extends Exception {
6+
public Throwable cause;
7+
public String message;
8+
public ArrowType type;
9+
private final Object value;
10+
11+
static final String NO_CONVERSION_AVAILABLE = "no conversion available";
12+
13+
14+
ValidationException(Throwable cause, String message, ArrowType type, Object value) {
15+
super(message, cause);
16+
this.cause = cause;
17+
this.message = message;
18+
this.type = type;
19+
this.value = value;
20+
}
21+
22+
ValidationException(String message, ArrowType type, Object value) {
23+
super(message);
24+
this.message = message;
25+
this.type = type;
26+
this.value = value;
27+
}
28+
29+
public String Error() {
30+
if (this.cause == null) {
31+
return String.format("cannot set `%s` with value `%s`: %s", this.type, this.value, this.message);
32+
}
33+
return String.format("cannot set `%s` with value `%s`: %s (%s)", this.type, this.value, this.message, this.cause);
34+
}
35+
36+
public String Masked() {
37+
if (this.cause == null) {
38+
return String.format("cannot set `%s`: %s", this.type.toString(), this.message);
39+
}
40+
return String.format("cannot set `%s`: %s (%s)", this.type.toString(), this.message, this.cause);
41+
}
42+
}

0 commit comments

Comments
 (0)