-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Support maxSubqueryBytes for window functions #16800
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
Changes from all commits
22e01a7
f6a9611
a546b7f
ea1d57e
ef52b84
350661e
27ae254
211f493
6b58856
b34f3be
d209f4a
da9c533
0741dc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.druid.query; | ||
|
|
||
| /** | ||
| * Serialization medium of the query results on the broker. It is currently used to communicate the result's format between | ||
| * the main query processing walker and the individual toolchests while materializing subquery's rows | ||
| */ | ||
| public enum ResultSerializationMode | ||
| { | ||
| /** | ||
| * Materialize the inner results as rows | ||
| */ | ||
| ROWS, | ||
|
|
||
| /** | ||
| * Materialize the inner results as frames | ||
| */ | ||
| FRAMES; | ||
|
|
||
| public static final String CTX_SERIALIZATION_PARAMETER = "serialization"; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,24 +23,30 @@ | |
| import com.google.common.base.Function; | ||
| import com.google.common.base.Functions; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import org.apache.druid.error.DruidException; | ||
| import org.apache.druid.frame.allocation.MemoryAllocatorFactory; | ||
| import org.apache.druid.java.util.common.guava.Sequence; | ||
| import org.apache.druid.java.util.common.guava.Sequences; | ||
| import org.apache.druid.query.DefaultQueryMetrics; | ||
| import org.apache.druid.query.FrameSignaturePair; | ||
| import org.apache.druid.query.QueryMetrics; | ||
| import org.apache.druid.query.QueryPlus; | ||
| import org.apache.druid.query.QueryRunner; | ||
| import org.apache.druid.query.QueryToolChest; | ||
| import org.apache.druid.query.ResultSerializationMode; | ||
| import org.apache.druid.query.aggregation.MetricManipulationFn; | ||
| import org.apache.druid.query.context.ResponseContext; | ||
| import org.apache.druid.query.rowsandcols.RowsAndColumns; | ||
| import org.apache.druid.query.rowsandcols.column.Column; | ||
| import org.apache.druid.query.rowsandcols.column.ColumnAccessor; | ||
| import org.apache.druid.query.rowsandcols.column.NullColumn; | ||
| import org.apache.druid.query.rowsandcols.semantic.FrameMaker; | ||
| import org.apache.druid.segment.column.ColumnType; | ||
| import org.apache.druid.segment.column.RowSignature; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.function.Supplier; | ||
|
|
||
| public class WindowOperatorQueryQueryToolChest extends QueryToolChest<RowsAndColumns, WindowOperatorQuery> | ||
|
|
@@ -50,7 +56,7 @@ public class WindowOperatorQueryQueryToolChest extends QueryToolChest<RowsAndCol | |
| @SuppressWarnings("unchecked") | ||
| public QueryRunner<RowsAndColumns> mergeResults(QueryRunner<RowsAndColumns> runner) | ||
| { | ||
| return new RowsAndColumnsUnravelingQueryRunner( | ||
| return new RowsAndColumnsSerializingQueryRunner( | ||
| (queryPlus, responseContext) -> { | ||
| final WindowOperatorQuery query = (WindowOperatorQuery) queryPlus.getQuery(); | ||
| final List<OperatorFactory> opFactories = query.getOperators(); | ||
|
|
@@ -61,7 +67,7 @@ public QueryRunner<RowsAndColumns> mergeResults(QueryRunner<RowsAndColumns> runn | |
| Supplier<Operator> opSupplier = () -> { | ||
| Operator retVal = new SequenceOperator( | ||
| runner.run( | ||
| queryPlus.withQuery(query.withOperators(new ArrayList<OperatorFactory>())), | ||
| queryPlus.withQuery(query.withOperators(new ArrayList<>())), | ||
| responseContext | ||
| ) | ||
| ); | ||
|
|
@@ -112,16 +118,29 @@ public Sequence<Object[]> resultsAsArrays( | |
| Sequence<RowsAndColumns> resultSequence | ||
| ) | ||
| { | ||
| // Dark magic; see RowsAndColumnsUnravelingQueryRunner. | ||
| // Dark magic; see RowsAndColumnsSerializingQueryRunner. | ||
| return (Sequence) resultSequence; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings({"unchecked", "rawtypes"}) | ||
| public Optional<Sequence<FrameSignaturePair>> resultsAsFrames( | ||
| WindowOperatorQuery query, | ||
| Sequence<RowsAndColumns> resultSequence, | ||
| MemoryAllocatorFactory memoryAllocatorFactory, | ||
| boolean useNestedForUnknownTypes | ||
| ) | ||
| { | ||
| // see RowsAndColumnsSerializingQueryRunner | ||
| return Optional.of((Sequence) resultSequence); | ||
| } | ||
|
|
||
| /** | ||
| * This class exists to unravel the RowsAndColumns that are used in this query and make it the return Sequence | ||
| * actually be a Sequence of rows. This is relatively broken in a number of regards, the most obvious of which | ||
| * is that it is going to run counter to the stated class on the Generic of the QueryToolChest. That is, the | ||
| * code makes it look like you are getting a Sequence of RowsAndColumns, but, by using this, the query will | ||
| * actually ultimately produce a Sequence of Object[]. This works because of type Erasure in Java (it's all Object | ||
| * This class exists to serialize the RowsAndColumns that are used in this query and make it the return Sequence | ||
| * actually be a Sequence of rows or frames, as the query requires. | ||
| * This is relatively broken in a number of regards, the most obvious of which is that it is going to run counter to the stated class on the Generic of the QueryToolChest. | ||
| * That is, the code makes it look like you are getting a Sequence of RowsAndColumns, but, by using this, the query will | ||
| * actually ultimately produce a Sequence of Object[] or Frames. This works because of type Erasure in Java (it's all Object | ||
| * at the end of the day). | ||
| * <p> | ||
| * While it might seem like this will break all sorts of things, the Generic type is actually there more as a type | ||
|
|
@@ -132,12 +151,12 @@ public Sequence<Object[]> resultsAsArrays( | |
| * Not our proudest moment, but we use the tools available to us. | ||
| */ | ||
| @SuppressWarnings({"unchecked", "rawtypes"}) | ||
| private static class RowsAndColumnsUnravelingQueryRunner implements QueryRunner | ||
| private static class RowsAndColumnsSerializingQueryRunner implements QueryRunner | ||
| { | ||
|
|
||
| private final QueryRunner<RowsAndColumns> baseQueryRunner; | ||
|
|
||
| private RowsAndColumnsUnravelingQueryRunner( | ||
| private RowsAndColumnsSerializingQueryRunner( | ||
| QueryRunner<RowsAndColumns> baseQueryRunner | ||
| ) | ||
| { | ||
|
|
@@ -158,42 +177,77 @@ public Sequence run( | |
| queryPlus.withQuery(query.withOverriddenContext(ImmutableMap.of("unravel", false))), | ||
| responseContext | ||
| ); | ||
| final ResultSerializationMode serializationMode = query.context().getEnum( | ||
| ResultSerializationMode.CTX_SERIALIZATION_PARAMETER, | ||
| ResultSerializationMode.class, | ||
| ResultSerializationMode.ROWS | ||
| ); | ||
| switch (serializationMode) { | ||
| case ROWS: | ||
| return asRows(baseSequence, query); | ||
| case FRAMES: | ||
| return asFrames(baseSequence); | ||
| default: | ||
| throw DruidException.defensive("Serialization mode[%s] not supported", serializationMode); | ||
| } | ||
| } | ||
|
|
||
| final RowSignature rowSignature = query.getRowSignature(); | ||
| return baseSequence.flatMap( | ||
| rac -> { | ||
| List<Object[]> results = new ArrayList<>(rac.numRows()); | ||
|
|
||
| ColumnAccessor[] accessors = new ColumnAccessor[rowSignature.size()]; | ||
| int index = 0; | ||
| for (String columnName : rowSignature.getColumnNames()) { | ||
| final Column column = rac.findColumn(columnName); | ||
| if (column == null) { | ||
| final ColumnType columnType = rowSignature | ||
| .getColumnType(columnName) | ||
| .orElse(ColumnType.UNKNOWN_COMPLEX); | ||
|
|
||
| accessors[index] = new NullColumn.Accessor(columnType, rac.numRows()); | ||
| } else { | ||
| accessors[index] = column.toAccessor(); | ||
| } | ||
| ++index; | ||
| } | ||
| return baseQueryRunner.run(queryPlus, responseContext); | ||
| } | ||
|
|
||
| for (int i = 0; i < rac.numRows(); ++i) { | ||
| Object[] objArr = new Object[accessors.length]; | ||
| for (int j = 0; j < accessors.length; j++) { | ||
| objArr[j] = accessors[j].getObject(i); | ||
| } | ||
| results.add(objArr); | ||
| /** | ||
| * Translates Sequence of RACs to a Sequence of Object[] | ||
| */ | ||
| private static Sequence asRows(final Sequence<RowsAndColumns> baseSequence, final WindowOperatorQuery query) | ||
| { | ||
| final RowSignature rowSignature = query.getRowSignature(); | ||
| return baseSequence.flatMap( | ||
| rac -> { | ||
| List<Object[]> results = new ArrayList<>(rac.numRows()); | ||
|
|
||
| ColumnAccessor[] accessors = new ColumnAccessor[rowSignature.size()]; | ||
| int index = 0; | ||
| for (String columnName : rowSignature.getColumnNames()) { | ||
| final Column column = rac.findColumn(columnName); | ||
| if (column == null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this should not happen at all - could it be an exception instead?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Druid has a lot of places where the absence of a column is treated as all nulls, so leaving it as is is a less risky proposition. Can this happen if one of the segments doesn't contain a row, while the others do? That said, this portion has been refactored as-is from the pre-existing code. I'll leave it to you to decide if this is applicable or not.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. around here it seems like we already had that in the signature; but if the rac doesn't know about that column I think its already inconsistent for some reason; it might be due to some other issue...
if we could add an exception and all tests pass I think we are better of with that 🤞 |
||
| final ColumnType columnType = rowSignature | ||
| .getColumnType(columnName) | ||
| .orElse(ColumnType.UNKNOWN_COMPLEX); | ||
|
|
||
| accessors[index] = new NullColumn.Accessor(columnType, rac.numRows()); | ||
| } else { | ||
| accessors[index] = column.toAccessor(); | ||
| } | ||
| ++index; | ||
| } | ||
|
|
||
| return Sequences.simple(results); | ||
| for (int i = 0; i < rac.numRows(); ++i) { | ||
| Object[] objArr = new Object[accessors.length]; | ||
| for (int j = 0; j < accessors.length; j++) { | ||
| objArr[j] = accessors[j].getObject(i); | ||
| } | ||
| results.add(objArr); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| return baseQueryRunner.run(queryPlus, responseContext); | ||
| return Sequences.simple(results); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Translates a sequence of RACs to a Sequence of Frames | ||
| */ | ||
| private static Sequence asFrames(final Sequence<RowsAndColumns> baseSequence) | ||
| { | ||
| return baseSequence.map( | ||
| rac -> { | ||
| FrameMaker frameMaker = FrameMaker.fromRAC(rac); | ||
| return new FrameSignaturePair( | ||
| frameMaker.toColumnBasedFrame(), | ||
| frameMaker.computeSignature() | ||
| ); | ||
|
Comment on lines
+244
to
+248
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a note we could possibly improve on this later by adding an operator which could compact smaller racs into bigger ones or something.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we could have a recombining operator at the end that basically exists to recombine things based on whatever input boundaries would have existed. We should be able to make this "magically" happen, if it's not already then it's just a question of what the stacktrace looks like and which concrete RAC classes are involved. |
||
| } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.