diff --git a/pom.xml b/pom.xml index 07f0336..360ee5c 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.github.thunderz99 java-cosmos jar - 0.8.26.RC1 + 0.8.26.RC2 ${project.groupId}:${project.artifactId}$ A lightweight Azure CosmosDB client for Java https://github.com/thunderz99/java-cosmos diff --git a/src/main/java/io/github/thunderz99/cosmos/impl/postgres/util/PGAggregateUtil.java b/src/main/java/io/github/thunderz99/cosmos/impl/postgres/util/PGAggregateUtil.java index fd7abb1..bbda2dc 100644 --- a/src/main/java/io/github/thunderz99/cosmos/impl/postgres/util/PGAggregateUtil.java +++ b/src/main/java/io/github/thunderz99/cosmos/impl/postgres/util/PGAggregateUtil.java @@ -8,7 +8,6 @@ import io.github.thunderz99.cosmos.util.NumberUtil; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.math.NumberUtils; import org.postgresql.util.PGobject; /** @@ -45,13 +44,6 @@ public static List> convertAggregateResultsToInteger(List(); } - // check if the value is a String that can be parsed to Long or Integer - if(value instanceof String strValue && !StringUtils.contains(strValue, ".")){ - if(NumberUtils.isCreatable(strValue)){ - value = NumberUtils.createNumber(strValue); - } - } - // Check if the value is an instance of Long if (value instanceof Number) { var numberValue = (Number) value; diff --git a/src/test/java/io/github/thunderz99/cosmos/impl/postgres/PostgresDatabaseImplTest.java b/src/test/java/io/github/thunderz99/cosmos/impl/postgres/PostgresDatabaseImplTest.java index 05de4e3..4f913d1 100644 --- a/src/test/java/io/github/thunderz99/cosmos/impl/postgres/PostgresDatabaseImplTest.java +++ b/src/test/java/io/github/thunderz99/cosmos/impl/postgres/PostgresDatabaseImplTest.java @@ -2076,6 +2076,40 @@ void aggregate_should_work_grouping_by_array() throws Exception { } + @Test + void aggregate_should_preserve_numeric_string_group_keys() throws Exception { + + var partition = "Users2"; + var id1 = "group_numeric_string1_" + RandomStringUtils.randomAlphanumeric(3); + var id2 = "group_numeric_string2_" + RandomStringUtils.randomAlphanumeric(3); + var id3 = "group_numeric_string3_" + RandomStringUtils.randomAlphanumeric(3); + + try { + db.upsert(host, Map.of("id", id1, "employeeCode", "123"), partition); + db.upsert(host, Map.of("id", id2, "employeeCode", "123"), partition); + db.upsert(host, Map.of("id", id3, "employeeCode", "ABC"), partition); + + var aggregate = Aggregate.function("COUNT(1) AS facetCount").groupBy("employeeCode"); + var cond = Condition.filter("id STARTSWITH", "group_numeric_string"); + + var result = db.aggregate(host, aggregate, cond, partition).toMap(); + assertThat(result).hasSize(2); + + var numericStringGroup = result.stream() + .filter(row -> "123".equals(String.valueOf(row.get("employeeCode")))) + .findFirst() + .orElseThrow(); + + assertThat(numericStringGroup.get("employeeCode")).isInstanceOf(String.class).isEqualTo("123"); + assertThat(numericStringGroup.get("facetCount")).isInstanceOf(Integer.class).isEqualTo(2); + } finally { + db.delete(host, id1, partition); + db.delete(host, id2, partition); + db.delete(host, id3, partition); + } + + } + @Test void aggregate_should_work_without_group_by() throws Exception { diff --git a/src/test/java/io/github/thunderz99/cosmos/impl/postgres/util/PGAggregateUtilTest.java b/src/test/java/io/github/thunderz99/cosmos/impl/postgres/util/PGAggregateUtilTest.java index 3614929..afd5877 100644 --- a/src/test/java/io/github/thunderz99/cosmos/impl/postgres/util/PGAggregateUtilTest.java +++ b/src/test/java/io/github/thunderz99/cosmos/impl/postgres/util/PGAggregateUtilTest.java @@ -1,6 +1,5 @@ package io.github.thunderz99.cosmos.impl.postgres.util; -import io.github.thunderz99.cosmos.impl.postgres.PostgresDatabaseImpl; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -9,7 +8,6 @@ import java.util.Map; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.*; class PGAggregateUtilTest { @@ -56,34 +54,17 @@ void convertAggregateResultsToInteger_should_work() { } @Test - void convertJsonb() { - // Setup - - // Test data setup + void convertAggregateResultsToInteger_should_preserve_numeric_strings() { List> testMaps = new ArrayList<>(); - LinkedHashMap map1 = new LinkedHashMap<>(); - map1.put("itemsCount", 1L); - map1.put("name", "TestName1"); - LinkedHashMap map2 = new LinkedHashMap<>(); - map2.put("itemsCount", Long.MAX_VALUE); - map2.put("name", "TestName2"); - LinkedHashMap map3 = new LinkedHashMap<>(); - map3.put("itemsCount", 100L); - map3.put("itemsWithinRange", Integer.MAX_VALUE); - testMaps.add(map1); - testMaps.add(map2); - testMaps.add(map3); + LinkedHashMap map = new LinkedHashMap<>(); + map.put("employeeCode", "123"); + map.put("facetCount", 2L); + testMaps.add(map); - // Call the method under test var resultMaps = PGAggregateUtil.convertAggregateResultsToInteger(testMaps); - // Assertions - assertThat(resultMaps).isNotNull(); - assertThat(resultMaps.size()).isEqualTo(3); - - assertThat(resultMaps.get(0).get("itemsCount")).isInstanceOf(Integer.class).isEqualTo(1); - assertThat(resultMaps.get(1).get("itemsCount")).isInstanceOf(Long.class).isEqualTo(Long.MAX_VALUE); // Should remain Long because it's out of Integer range - assertThat(resultMaps.get(2).get("itemsCount")).isInstanceOf(Integer.class).isEqualTo(100); - assertThat(resultMaps.get(2).get("itemsWithinRange")).isInstanceOf(Integer.class).isEqualTo(Integer.MAX_VALUE); // Should remain Integer + assertThat(resultMaps).hasSize(1); + assertThat(resultMaps.get(0).get("employeeCode")).isInstanceOf(String.class).isEqualTo("123"); + assertThat(resultMaps.get(0).get("facetCount")).isInstanceOf(Integer.class).isEqualTo(2); } -} \ No newline at end of file +}