Skip to content

feat: io.cloudquery.scalar.Binary implementation #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions lib/src/main/java/io/cloudquery/scalar/Binary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package io.cloudquery.scalar;

import org.apache.arrow.vector.types.pojo.ArrowType;
import org.apache.commons.codec.binary.Base64;

import java.util.Arrays;

public class Binary implements Scalar {
protected byte[] value;
protected boolean valid;

public Binary() {
}

public Binary(Object value) throws ValidationException {
this.set(value);
}

@Override
public String toString() {
if (this.valid) {
return Base64.encodeBase64String(this.value);
}
return NULL_VALUE_STRING;
}

@Override
public boolean isValid() {
return this.valid;
}

@Override
public ArrowType dataType() {
return ArrowType.Binary.INSTANCE;
}

@Override
public void set(Object value) throws ValidationException {
if (value == null) {
this.valid = false;
this.value = null;
return;
}

if (value instanceof Scalar scalar) {
if (!scalar.isValid()) {
this.valid = false;
this.value = null;
return;
}

this.set(scalar.get());
return;
}

if (value instanceof byte[] bytes) {
this.valid = true;
this.value = bytes;
return;
}

if (value instanceof String string) {
this.valid = true;
this.value = Base64.decodeBase64(string);
return;
}

if (value instanceof char[] chars) {
this.valid = true;
this.value = Base64.decodeBase64(new String(chars));
return;
}

throw new ValidationException(ValidationException.NO_CONVERSION_AVAILABLE, this.dataType(), value);
}

@Override
public Object get() {
if (this.valid) {
return this.value;
}
return null;
}

@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}

if (!(other instanceof Binary o)) {
return false;
}

return (this.valid && o.valid) && Arrays.equals(this.value, o.value);
}
}
18 changes: 18 additions & 0 deletions lib/src/main/java/io/cloudquery/scalar/LargeBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.cloudquery.scalar;

import org.apache.arrow.vector.types.pojo.ArrowType;

public class LargeBinary extends Binary {

public LargeBinary() {
}

public LargeBinary(Object value) throws ValidationException {
this.set(value);
}

@Override
public ArrowType dataType() {
return ArrowType.LargeBinary.INSTANCE;
}
}
14 changes: 8 additions & 6 deletions lib/src/main/java/io/cloudquery/scalar/Scalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import org.apache.arrow.vector.types.pojo.ArrowType;

public interface Scalar {
String String();
String toString();

Boolean IsValid();
boolean isValid();

ArrowType DataType();
ArrowType dataType();

void Set(Object obj);
void set(Object value) throws ValidationException;

Object Get();
Object get();

Boolean Equal(Scalar other);
boolean equals(Object other);

String NULL_VALUE_STRING = "(null)";
}
42 changes: 42 additions & 0 deletions lib/src/main/java/io/cloudquery/scalar/ValidationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.cloudquery.scalar;

import org.apache.arrow.vector.types.pojo.ArrowType;

public class ValidationException extends Exception {
public Throwable cause;
public String message;
public ArrowType type;
private final Object value;

static final String NO_CONVERSION_AVAILABLE = "no conversion available";


ValidationException(Throwable cause, String message, ArrowType type, Object value) {
super(message, cause);
this.cause = cause;
this.message = message;
this.type = type;
this.value = value;
}

ValidationException(String message, ArrowType type, Object value) {
super(message);
this.message = message;
this.type = type;
this.value = value;
}

public String Error() {
if (this.cause == null) {
return String.format("cannot set `%s` with value `%s`: %s", this.type, this.value, this.message);
}
return String.format("cannot set `%s` with value `%s`: %s (%s)", this.type, this.value, this.message, this.cause);
}

public String Masked() {
if (this.cause == null) {
return String.format("cannot set `%s`: %s", this.type.toString(), this.message);
}
return String.format("cannot set `%s`: %s (%s)", this.type.toString(), this.message, this.cause);
}
}