Skip to content

Commit

Permalink
Window Functions : Reject MVDs during window processing (apache#17002)
Browse files Browse the repository at this point in the history
This commit aims to reject MVDs in window processing as we do not support them.
Earlier to this commit, query running a window aggregate partitioned by an MVD column would fail with ClassCastException
  • Loading branch information
sreemanamala authored Sep 9, 2024
1 parent 67f5aa6 commit 51fe3c0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import it.unimi.dsi.fastutil.ints.IntList;
import org.apache.druid.common.semantic.SemanticCreator;
import org.apache.druid.common.semantic.SemanticUtils;
import org.apache.druid.error.InvalidInput;
import org.apache.druid.java.util.common.ISE;
import org.apache.druid.java.util.common.guava.Sequences;
import org.apache.druid.query.operator.ColumnWithDirection;
Expand Down Expand Up @@ -153,11 +154,28 @@ public Column findColumn(String name)
return new LimitedColumn(retVal, startOffset, endOffset);
}

final Function<RowType, Object> adapterForValue = rowAdapter.columnFunction(name);
final Optional<ColumnType> maybeColumnType = rowSignature.getColumnType(name);
final ColumnType columnType = maybeColumnType.orElse(ColumnType.UNKNOWN_COMPLEX);
final Comparator<Object> comparator = Comparator.nullsFirst(columnType.getStrategy());

final Function<RowType, Object> adapterForValue;
if (columnType.equals(ColumnType.STRING)) {
// special handling to reject MVDs
adapterForValue = f -> {
Object value = rowAdapter.columnFunction(name).apply(f);
if (value instanceof List) {
throw InvalidInput.exception(
"Encountered a multi value column [%s]. Window processing does not support MVDs. "
+ "Consider using UNNEST or MV_TO_ARRAY.",
name
);
}
return value;
};
} else {
adapterForValue = rowAdapter.columnFunction(name);
}

return new Column()
{
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.druid.error.DruidException;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.ISE;
import org.apache.druid.java.util.common.RE;
Expand Down Expand Up @@ -249,6 +250,28 @@ public void testWithArrayConcat()
.run();
}

@Test
public void testFailure_partitionByMVD()
{
final DruidException e = Assert.assertThrows(
DruidException.class,
() -> testBuilder()
.sql("select cityName, countryName, array_to_mv(array[1,length(cityName)]),\n"
+ "row_number() over (partition by array_to_mv(array[1,length(cityName)]) order by countryName, cityName)\n"
+ "from wikipedia\n"
+ "where countryName in ('Austria', 'Republic of Korea') and cityName is not null\n"
+ "order by 1, 2, 3")
.queryContext(DEFAULT_QUERY_CONTEXT)
.run()
);

assertEquals(
"Encountered a multi value column [v0]. Window processing does not support MVDs. "
+ "Consider using UNNEST or MV_TO_ARRAY.",
e.getMessage()
);
}

private WindowOperatorQuery getWindowOperatorQuery(List<Query<?>> queries)
{
assertEquals(1, queries.size());
Expand Down

0 comments on commit 51fe3c0

Please sign in to comment.