Skip to content
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

feat: Date scalars #36

Merged
merged 2 commits into from
Aug 14, 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
95 changes: 95 additions & 0 deletions lib/src/main/java/io/cloudquery/scalar/DateDay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package io.cloudquery.scalar;

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

public class DateDay implements Scalar {
protected int value;
protected boolean valid;

public DateDay() {
}

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

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

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

@Override
public ArrowType dataType() {
return new ArrowType.Date(DateUnit.DAY);
}

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

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

if (scalar instanceof DateDay date) {
this.valid = date.valid;
this.value = date.value;
return;
}

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

if (value instanceof Integer b) {
this.valid = true;
this.value = b;
return;
}

if (value instanceof String string) {
this.valid = true;
this.value = Integer.parseInt(string);
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 DateDay o)) {
return false;
}

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

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

public class DateMilli implements Scalar {
protected long value;
protected boolean valid;

public DateMilli() {
}

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

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

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

@Override
public ArrowType dataType() {
return new ArrowType.Date(DateUnit.MILLISECOND);
}

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

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

if (scalar instanceof DateMilli date) {
this.valid = date.valid;
this.value = date.value;
return;
}

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

if (value instanceof Long b) {
this.valid = true;
this.value = b;
return;
}

if (value instanceof Integer b) {
this.valid = true;
this.value = b;
return;
}

if (value instanceof String string) {
this.valid = true;
this.value = Long.parseLong(string);
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 DateMilli o)) {
return false;
}

return (this.valid == o.valid) && (this.value == o.value);
}
}
129 changes: 129 additions & 0 deletions lib/src/test/java/io/cloudquery/scalar/DateDayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package io.cloudquery.scalar;

import org.apache.arrow.vector.types.DateUnit;
import org.apache.arrow.vector.types.pojo.ArrowType;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;


public class DateDayTest {
@Test
public void testNew() {
assertDoesNotThrow(() -> {
new DateDay();
});
}

@Test
public void testNewWithValidParam() {
assertDoesNotThrow(() -> {
new DateDay(1);
new DateDay("1");

Scalar s = new DateDay(2);
new DateDay(s);
});
}

@Test
public void testNewWithInvalidParam() {
assertThrows(ValidationException.class, () -> {
new DateDay(new char[]{'q'});
});
}

@Test
public void testToString() {
DateDay dateDay = new DateDay();
assertEquals(Scalar.NULL_VALUE_STRING, dateDay.toString());

assertDoesNotThrow(() -> {
dateDay.set("1");
});
assertEquals("1", dateDay.toString());

assertDoesNotThrow(() -> {
dateDay.set(2);
});
assertEquals("2", dateDay.toString());
}

@Test
public void testDataType() {
DateDay dateDay = new DateDay();
assertEquals(new ArrowType.Date(DateUnit.DAY), dateDay.dataType());
}

@Test
public void testIsValid() {
DateDay dateDay = new DateDay();
assertFalse(dateDay.isValid());

assertDoesNotThrow(() -> {
dateDay.set("1");
});
assertTrue(dateDay.isValid());
}

@Test
public void testSet() {
DateDay dateDay = new DateDay();
assertDoesNotThrow(() -> {
new DateDay(1);
new DateDay("2");

Scalar s = new DateDay(1);
dateDay.set(s);
});
}

@Test
public void testSetWithInvalidParam() {
DateDay dateDay = new DateDay();
assertThrows(ValidationException.class, () -> {
dateDay.set(new char[]{});
});
}

@Test
public void testGet() {
DateDay dateDay = new DateDay();
assertFalse(dateDay.isValid());
assertNull(dateDay.get());

assertDoesNotThrow(() -> {
dateDay.set(1);
});
assertTrue(dateDay.isValid());
assertEquals(1, dateDay.get());

assertDoesNotThrow(() -> {
dateDay.set("-1");
});
assertTrue(dateDay.isValid());
assertEquals(-1, dateDay.get());
}

@Test
public void testEquals() {
DateDay a = new DateDay();
DateDay b = new DateDay();
assertEquals(a, b);
assertNotEquals(a, null);
assertNotEquals(a, new Binary()); // we can't cast Binary to DateDay
assertNotEquals(null, a);

assertDoesNotThrow(() -> {
a.set(1);
});
assertNotEquals(a, b);

assertDoesNotThrow(() -> {
for (Object obj : new Object[]{null, 1, -1, "2"}) {
a.set(obj);
assertEquals(a, new DateDay(obj));
}
});
}
}
Loading