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

remove null assignments and check that the map is not empty check #479

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
*/
package com.teragrep.pth10.ast.commands.aggregate.UDAFs.BufferClasses;

import com.teragrep.pth10.ast.NullValue;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Map;

/**
Expand Down Expand Up @@ -93,18 +96,11 @@ public void mergeMap(Map<String, Long> another) {
* @return most frequent entry as a string
*/
public String mode() {
Map.Entry<String, Long> mostFrequentEntry = null;

for (Map.Entry<String, Long> entry : this.map.entrySet()) {
if (mostFrequentEntry == null) {
mostFrequentEntry = entry;
}
else if (mostFrequentEntry != null && entry.getValue() > mostFrequentEntry.getValue()) {
mostFrequentEntry = entry;
}
}

return mostFrequentEntry.getKey();
return map.entrySet()
.stream()
.max(Map.Entry.comparingByValue()) // find max value
.map(Map.Entry::getKey) // select key
.orElse(new NullValue(NullValue.Type.EMPTY_STRING).value()); // is an empty map return an empty string
}

}
21 changes: 21 additions & 0 deletions src/test/java/com/teragrep/pth10/statsTransformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*/
package com.teragrep.pth10;

import com.teragrep.pth10.ast.NullValue;
import org.apache.spark.sql.Row;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
Expand Down Expand Up @@ -358,6 +359,26 @@ void statsTransform_AggMode_Test() {
});
}

@Test
@DisabledIfSystemProperty(
named = "skipSparkTest",
matches = "true"
)
void statsTransformAggModeTestEmptyDataset() {
streamingTestUtil.performDPLTest("index=index_A earliest=2020-10-10T11:20:10.100+03:00 | stats mode(offset)", testFile, ds -> {
Assertions.assertEquals("[mode(offset)]", Arrays.toString(ds.columns()));

List<Object> destAsList = ds
.select("mode(offset)")
.collectAsList()
.stream()
.map(r -> r.getAs(0).toString())
.collect(Collectors.toList());

Assertions.assertEquals(Collections.singletonList(""), destAsList);
});
}

// Test min()
@Test
@DisabledIfSystemProperty(
Expand Down