diff --git a/lib/src/main/java/io/cloudquery/scalar/DateDay.java b/lib/src/main/java/io/cloudquery/scalar/DateDay.java new file mode 100644 index 0000000..843c0bb --- /dev/null +++ b/lib/src/main/java/io/cloudquery/scalar/DateDay.java @@ -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); + } +} diff --git a/lib/src/main/java/io/cloudquery/scalar/DateMilli.java b/lib/src/main/java/io/cloudquery/scalar/DateMilli.java new file mode 100644 index 0000000..0201eef --- /dev/null +++ b/lib/src/main/java/io/cloudquery/scalar/DateMilli.java @@ -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); + } +} diff --git a/lib/src/test/java/io/cloudquery/scalar/DateDayTest.java b/lib/src/test/java/io/cloudquery/scalar/DateDayTest.java new file mode 100644 index 0000000..e8b0189 --- /dev/null +++ b/lib/src/test/java/io/cloudquery/scalar/DateDayTest.java @@ -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)); + } + }); + } +} diff --git a/lib/src/test/java/io/cloudquery/scalar/DateMilliTest.java b/lib/src/test/java/io/cloudquery/scalar/DateMilliTest.java new file mode 100644 index 0000000..33913af --- /dev/null +++ b/lib/src/test/java/io/cloudquery/scalar/DateMilliTest.java @@ -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 DateMilliTest { + @Test + public void testNew() { + assertDoesNotThrow(() -> { + new DateMilli(); + }); + } + + @Test + public void testNewWithValidParam() { + assertDoesNotThrow(() -> { + new DateMilli(1); + new DateMilli("1"); + + Scalar s = new DateMilli(2); + new DateMilli(s); + }); + } + + @Test + public void testNewWithInvalidParam() { + assertThrows(ValidationException.class, () -> { + new DateMilli(new char[]{'q'}); + }); + } + + @Test + public void testToString() { + DateMilli dateMilli = new DateMilli(); + assertEquals(Scalar.NULL_VALUE_STRING, dateMilli.toString()); + + assertDoesNotThrow(() -> { + dateMilli.set("1"); + }); + assertEquals("1", dateMilli.toString()); + + assertDoesNotThrow(() -> { + dateMilli.set(2); + }); + assertEquals("2", dateMilli.toString()); + } + + @Test + public void testDataType() { + DateMilli dateMilli = new DateMilli(); + assertEquals(new ArrowType.Date(DateUnit.MILLISECOND), dateMilli.dataType()); + } + + @Test + public void testIsValid() { + DateMilli dateMilli = new DateMilli(); + assertFalse(dateMilli.isValid()); + + assertDoesNotThrow(() -> { + dateMilli.set("1"); + }); + assertTrue(dateMilli.isValid()); + } + + @Test + public void testSet() { + DateMilli dateMilli = new DateMilli(); + assertDoesNotThrow(() -> { + new DateMilli(1); + new DateMilli("2"); + + Scalar s = new DateMilli(1); + dateMilli.set(s); + }); + } + + @Test + public void testSetWithInvalidParam() { + DateMilli dateMilli = new DateMilli(); + assertThrows(ValidationException.class, () -> { + dateMilli.set(new char[]{}); + }); + } + + @Test + public void testGet() { + DateMilli dateMilli = new DateMilli(); + assertFalse(dateMilli.isValid()); + assertNull(dateMilli.get()); + + assertDoesNotThrow(() -> { + dateMilli.set(1); + }); + assertTrue(dateMilli.isValid()); + assertEquals(1L, dateMilli.get()); + + assertDoesNotThrow(() -> { + dateMilli.set("-1"); + }); + assertTrue(dateMilli.isValid()); + assertEquals(-1L, dateMilli.get()); + } + + @Test + public void testEquals() { + DateMilli a = new DateMilli(); + DateMilli b = new DateMilli(); + assertEquals(a, b); + assertNotEquals(a, null); + assertNotEquals(a, new Binary()); // we can't cast Binary to DateMilli + assertNotEquals(null, a); + + assertDoesNotThrow(() -> { + a.set(1); + }); + assertNotEquals(a, b); + + assertDoesNotThrow(() -> { + for (Object obj : new Object[]{null, 1, -1, 0, 1L, -2L, "2"}) { + a.set(obj); + assertEquals(a, new DateMilli(obj)); + } + }); + } +}