From 211ffed77120f8c12e4d19190738e457715d66f6 Mon Sep 17 00:00:00 2001 From: Aditya Pareek <119134401+LOGANBLUE1@users.noreply.github.com> Date: Tue, 28 Apr 2026 10:16:44 +0530 Subject: [PATCH] added date field and tests --- .../zingg/common/core/hash/IdentityDate.java | 20 +++++++++++ .../zingg/common/core/hash/SameMonthYear.java | 19 +++++++++++ .../java/zingg/hash/TestIdentityDate.java | 28 +++++++++++++++ .../java/zingg/hash/TestSameMonthYear.java | 34 +++++++++++++++++++ .../spark/core/hash/SparkSameMonthYear.java | 15 ++++++++ 5 files changed, 116 insertions(+) create mode 100644 common/core/src/main/java/zingg/common/core/hash/IdentityDate.java create mode 100644 common/core/src/main/java/zingg/common/core/hash/SameMonthYear.java create mode 100644 common/core/src/test/java/zingg/hash/TestIdentityDate.java create mode 100644 common/core/src/test/java/zingg/hash/TestSameMonthYear.java create mode 100644 spark/core/src/main/java/zingg/spark/core/hash/SparkSameMonthYear.java diff --git a/common/core/src/main/java/zingg/common/core/hash/IdentityDate.java b/common/core/src/main/java/zingg/common/core/hash/IdentityDate.java new file mode 100644 index 000000000..a0150a844 --- /dev/null +++ b/common/core/src/main/java/zingg/common/core/hash/IdentityDate.java @@ -0,0 +1,20 @@ +package zingg.common.core.hash; +import java.util.Date; +import java.time.LocalDate; +public class IdentityDate extends BaseHash { + + public IdentityDate() { + setName("identityDate"); + } + + @Override + public Integer call(Date date) { + if(date == null) { + return null; + } + int year = date.getYear() + 1900; + int month = date.getMonth() + 1; + int day = date.getDate(); + return year * 10000 + month * 100 + day; + } +} diff --git a/common/core/src/main/java/zingg/common/core/hash/SameMonthYear.java b/common/core/src/main/java/zingg/common/core/hash/SameMonthYear.java new file mode 100644 index 000000000..3fa2e211f --- /dev/null +++ b/common/core/src/main/java/zingg/common/core/hash/SameMonthYear.java @@ -0,0 +1,19 @@ +package zingg.common.core.hash; + +import java.util.Date; + +public class SameMonthYear extends BaseHash{ + public SameMonthYear() { + setName("sameMonthYear"); + } + + @Override + public Integer call(Date date) { + if(date == null) { + return null; + } + int year = date.getYear() + 1900; + int month = date.getMonth() + 1; + return year * 100 + month; + } +} diff --git a/common/core/src/test/java/zingg/hash/TestIdentityDate.java b/common/core/src/test/java/zingg/hash/TestIdentityDate.java new file mode 100644 index 000000000..a30c2d31e --- /dev/null +++ b/common/core/src/test/java/zingg/hash/TestIdentityDate.java @@ -0,0 +1,28 @@ +package zingg.hash; + +import org.junit.jupiter.api.Test; +import zingg.common.core.hash.IdentityDate; +import zingg.common.core.hash.IdentityInteger; + +import java.util.Date; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestIdentityDate { + + @Test + public void testIdentityInteger() { + IdentityDate value = getInstance(); + assertEquals(20020624, value.call(new Date("2002/06/24"))); + } + + @Test + public void testNullValue() { + IdentityDate value = getInstance(); + assertEquals(null, value.call(null)); + } + + private IdentityDate getInstance() { + return new IdentityDate(); + } +} diff --git a/common/core/src/test/java/zingg/hash/TestSameMonthYear.java b/common/core/src/test/java/zingg/hash/TestSameMonthYear.java new file mode 100644 index 000000000..db60ff4f2 --- /dev/null +++ b/common/core/src/test/java/zingg/hash/TestSameMonthYear.java @@ -0,0 +1,34 @@ +package zingg.hash; + +import java.util.Date; +import org.junit.jupiter.api.Test; +import zingg.common.core.hash.SameMonthYear; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestSameMonthYear { + @Test + public void testSameMonthYear() { + SameMonthYear value = getInstance(); + assertEquals(200206, value.call(new Date("2002/06/24"))); + } + + @Test + public void testSameMonthYear2() { + SameMonthYear value = getInstance(); + assertEquals(value.call(new Date("2002/06/11")), + value.call(new Date("2002/06/24"))); + } + + @Test + public void testNullValue() { + SameMonthYear value = getInstance(); + assertEquals(null, value.call(null)); + } + + private SameMonthYear getInstance() { + return new SameMonthYear(); + } + + +} diff --git a/spark/core/src/main/java/zingg/spark/core/hash/SparkSameMonthYear.java b/spark/core/src/main/java/zingg/spark/core/hash/SparkSameMonthYear.java new file mode 100644 index 000000000..936831bf1 --- /dev/null +++ b/spark/core/src/main/java/zingg/spark/core/hash/SparkSameMonthYear.java @@ -0,0 +1,15 @@ +package zingg.spark.core.hash; + +import org.apache.spark.sql.types.DataTypes; +import zingg.common.core.hash.SameMonthYear; + +import java.util.Date; + +public class SparkSameMonthYear extends SparkHashFunction { + public SparkSameMonthYear() { + setBaseHash(new SameMonthYear()); + setDataType(DataTypes.DateType); + setReturnType(DataTypes.IntegerType); + } +} +