Skip to content
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.thunderz99</groupId>
<artifactId>java-cosmos</artifactId>
<packaging>jar</packaging>
<version>0.8.26.RC1</version>
<version>0.8.26.RC2</version>
<name>${project.groupId}:${project.artifactId}$</name>
<description>A lightweight Azure CosmosDB client for Java</description>
<url>https://github.com/thunderz99/java-cosmos</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -45,13 +44,6 @@ public static List<Map<String, Object>> convertAggregateResultsToInteger(List<Ma
return new LinkedHashMap<>();
}

// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -56,34 +54,17 @@ void convertAggregateResultsToInteger_should_work() {
}

@Test
void convertJsonb() {
// Setup

// Test data setup
void convertAggregateResultsToInteger_should_preserve_numeric_strings() {
List<Map<String, Object>> testMaps = new ArrayList<>();
LinkedHashMap<String, Object> map1 = new LinkedHashMap<>();
map1.put("itemsCount", 1L);
map1.put("name", "TestName1");
LinkedHashMap<String, Object> map2 = new LinkedHashMap<>();
map2.put("itemsCount", Long.MAX_VALUE);
map2.put("name", "TestName2");
LinkedHashMap<String, Object> map3 = new LinkedHashMap<>();
map3.put("itemsCount", 100L);
map3.put("itemsWithinRange", Integer.MAX_VALUE);
testMaps.add(map1);
testMaps.add(map2);
testMaps.add(map3);
LinkedHashMap<String, Object> 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);
}
}
}
Loading