Skip to content

Commit

Permalink
Fix non-sqlcompat validation in CalciteWindowQueryTest (#15086) (#15161)
Browse files Browse the repository at this point in the history
Backport of #15086 to 28.0.0.
  • Loading branch information
LakshSingla authored Oct 16, 2023
1 parent bdb968f commit da24e1a
Show file tree
Hide file tree
Showing 20 changed files with 3,694 additions and 3,477 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import org.apache.druid.sql.calcite.util.TestDataBuilder;
import org.apache.druid.timeline.DataSegment;
import org.apache.druid.timeline.partition.LinearShardSpec;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -130,6 +129,7 @@ public void testComputingSketchOnNumericValues()
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
new String[]{
"\"AAAAAT/wAAAAAAAAQBgAAAAAAABAaQAAAAAAAAAAAAY/8AAAAAAAAD/wAAAAAAAAP/AAAAAAAABAAAAAAAAAAD/wAAAAAAAAQAgAAAAAAAA/8AAAAAAAAEAQAAAAAAAAP/AAAAAAAABAFAAAAAAAAD/wAAAAAAAAQBgAAAAAAAA=\""
Expand Down Expand Up @@ -166,6 +166,7 @@ public void testComputingSketchOnCastedString()
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
NullHandling.replaceWithDefault()
? new String[]{
Expand Down Expand Up @@ -198,6 +199,7 @@ public void testDefaultCompressionForTDigestGenerateSketchAgg()
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
new Object[]{"\"AAAAAT/wAAAAAAAAQBgAAAAAAABAWQAAAAAAAAAAAAY/8AAAAAAAAD/wAAAAAAAAP/AAAAAAAABAAAAAAAAAAD/wAAAAAAAAQAgAAAAAAAA/8AAAAAAAAEAQAAAAAAAAP/AAAAAAAABAFAAAAAAAAD/wAAAAAAAAQBgAAAAAAAA=\""}
)
Expand Down Expand Up @@ -242,6 +244,7 @@ public void testComputingQuantileOnPreAggregatedSketch()
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}
Expand Down Expand Up @@ -297,6 +300,7 @@ public void testGeneratingSketchAndComputingQuantileOnFly()
.setContext(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}
Expand Down Expand Up @@ -334,6 +338,7 @@ public void testQuantileOnNumericValues()
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}
Expand Down Expand Up @@ -370,6 +375,7 @@ public void testCompressionParamForTDigestQuantileAgg()
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
new Object[]{1.0, 3.5, 6.0}
)
Expand Down Expand Up @@ -421,6 +427,7 @@ public void testQuantileOnCastedString()
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
NullHandling.replaceWithDefault()
? new Object[]{0.0, 0.5, 10.1}
Expand Down Expand Up @@ -455,6 +462,7 @@ public void testEmptyTimeseriesResults()
.context(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
new Object[]{"\"AAAAAX/wAAAAAAAA//AAAAAAAABAWQAAAAAAAAAAAAA=\"", Double.NaN}
)
Expand Down Expand Up @@ -499,32 +507,13 @@ public void testGroupByAggregatorDefaultValues()
.setContext(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
new Object[]{"a", "\"AAAAAX/wAAAAAAAA//AAAAAAAABAWQAAAAAAAAAAAAA=\"", Double.NaN}
)
);
}

@Override
public void assertResultsEquals(String sql, List<Object[]> expectedResults, List<Object[]> results)
{
Assert.assertEquals(expectedResults.size(), results.size());
for (int i = 0; i < expectedResults.size(); i++) {
Object[] expectedResult = expectedResults.get(i);
Object[] result = results.get(i);
Assert.assertEquals(expectedResult.length, result.length);
for (int j = 0; j < expectedResult.length; j++) {
if (expectedResult[j] instanceof Float) {
Assert.assertEquals((Float) expectedResult[j], (Float) result[j], 0.000001);
} else if (expectedResult[j] instanceof Double) {
Assert.assertEquals((Double) expectedResult[j], (Double) result[j], 0.000001);
} else {
Assert.assertEquals(expectedResult[j], result[j]);
}
}
}
}

private static PostAggregator makeFieldAccessPostAgg(String name)
{
return new FieldAccessPostAggregator(name, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import org.apache.druid.sql.calcite.util.TestDataBuilder;
import org.apache.druid.timeline.DataSegment;
import org.apache.druid.timeline.partition.LinearShardSpec;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -196,6 +195,7 @@ public void testVarPop()
.context(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}
Expand Down Expand Up @@ -243,6 +243,7 @@ public void testVarSamp()
.context(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}
Expand Down Expand Up @@ -298,6 +299,7 @@ public void testStdDevPop()
.context(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}
Expand Down Expand Up @@ -351,6 +353,7 @@ public void testStdDevSamp()
.context(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}
Expand Down Expand Up @@ -409,6 +412,7 @@ public void testStdDevWithVirtualColumns()
.context(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}
Expand Down Expand Up @@ -455,6 +459,7 @@ public void testVarianceOrderBy()
.setContext(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}
Expand Down Expand Up @@ -491,6 +496,7 @@ public void testVariancesOnCastedString()
.context(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
NullHandling.replaceWithDefault()
? new Object[]{3.61497656362466, 3.960008417499471, 3.960008417499471, 15.681666666666667}
Expand Down Expand Up @@ -541,6 +547,7 @@ public void testEmptyTimeseriesResults()
.context(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
NullHandling.replaceWithDefault()
? new Object[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
Expand Down Expand Up @@ -621,6 +628,7 @@ public void testGroupByAggregatorDefaultValues()
.setContext(QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
ImmutableList.of(
NullHandling.replaceWithDefault()
? new Object[]{"a", 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
Expand Down Expand Up @@ -674,27 +682,8 @@ public void testVarianceAggAsInput()
.context(BaseCalciteQueryTest.QUERY_CONTEXT_DEFAULT)
.build()
),
ResultMatchMode.EQUALS_EPS,
expectedResults
);
}

@Override
public void assertResultsEquals(String sql, List<Object[]> expectedResults, List<Object[]> results)
{
Assert.assertEquals(expectedResults.size(), results.size());
for (int i = 0; i < expectedResults.size(); i++) {
Object[] expectedResult = expectedResults.get(i);
Object[] result = results.get(i);
Assert.assertEquals(expectedResult.length, result.length);
for (int j = 0; j < expectedResult.length; j++) {
if (expectedResult[j] instanceof Float) {
Assert.assertEquals((Float) expectedResult[j], (Float) result[j], 1e-5);
} else if (expectedResult[j] instanceof Double) {
Assert.assertEquals((Double) expectedResult[j], (Double) result[j], 1e-5);
} else {
Assert.assertEquals(expectedResult[j], result[j]);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.base.Strings;
import com.google.inject.Inject;
import org.apache.druid.math.expr.ExpressionProcessing;
import org.apache.druid.segment.column.ValueType;
import org.apache.druid.segment.data.Indexed;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -175,6 +176,28 @@ public static <T> T defaultValueForClass(final Class<T> clazz)
}
}

/**
* Returns the default value for the given {@link ValueType}.
*
* May be null or non-null based on the current SQL-compatible null handling mode.
*/
@Nullable
@SuppressWarnings("unchecked")
public static Object defaultValueForType(ValueType type)
{
if (type == ValueType.FLOAT) {
return defaultFloatValue();
} else if (type == ValueType.DOUBLE) {
return defaultDoubleValue();
} else if (type == ValueType.LONG) {
return defaultLongValue();
} else if (type == ValueType.STRING) {
return defaultStringValue();
} else {
return null;
}
}

public static boolean isNullOrEquivalent(@Nullable String value)
{
return replaceWithDefault() ? Strings.isNullOrEmpty(value) : value == null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
package org.apache.druid.common.config;

import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.segment.column.ValueType;
import org.apache.druid.segment.data.ListIndexed;
import org.apache.druid.testing.InitializedNullHandlingTest;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;

import static org.apache.druid.common.config.NullHandling.defaultValueForClass;
import static org.apache.druid.common.config.NullHandling.defaultValueForType;
import static org.apache.druid.common.config.NullHandling.replaceWithDefault;
import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -89,6 +92,17 @@ public void test_defaultValueForClass_object()
Assert.assertNull(NullHandling.defaultValueForClass(Object.class));
}

@Test
public void test_defaultValueForType()
{
assertEquals(defaultValueForClass(Float.class), defaultValueForType(ValueType.FLOAT));
assertEquals(defaultValueForClass(Double.class), defaultValueForType(ValueType.DOUBLE));
assertEquals(defaultValueForClass(Long.class), defaultValueForType(ValueType.LONG));
assertEquals(defaultValueForClass(String.class), defaultValueForType(ValueType.STRING));
assertEquals(defaultValueForClass(Object.class), defaultValueForType(ValueType.COMPLEX));
assertEquals(defaultValueForClass(Object.class), defaultValueForType(ValueType.ARRAY));
}

@Test
public void test_ignoreNullsStrings()
{
Expand Down
Loading

0 comments on commit da24e1a

Please sign in to comment.