-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
UNION ALLs in MSQ #14981
UNION ALLs in MSQ #14981
Changes from 12 commits
bc40808
a96dcb6
448b583
2f7b160
971589b
54a664a
75652e6
fd652a7
9b31be6
7e60702
a6ee632
9620c4e
0ee8d50
92f35c0
feb8875
368ae08
b09e300
7c4e048
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 |
---|---|---|
|
@@ -20,13 +20,16 @@ | |
package org.apache.druid.msq.exec; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.druid.error.DruidException; | ||
import org.apache.druid.error.DruidExceptionMatcher; | ||
import org.apache.druid.indexing.common.actions.SegmentAllocateAction; | ||
import org.apache.druid.java.util.common.Intervals; | ||
import org.apache.druid.java.util.common.StringUtils; | ||
import org.apache.druid.msq.indexing.error.InsertCannotAllocateSegmentFault; | ||
import org.apache.druid.msq.indexing.error.InsertCannotBeEmptyFault; | ||
import org.apache.druid.msq.indexing.error.InsertTimeNullFault; | ||
import org.apache.druid.msq.indexing.error.InsertTimeOutOfBoundsFault; | ||
import org.apache.druid.msq.indexing.error.QueryNotSupportedFault; | ||
import org.apache.druid.msq.indexing.error.TooManyClusteredByColumnsFault; | ||
import org.apache.druid.msq.indexing.error.TooManyColumnsFault; | ||
import org.apache.druid.msq.indexing.error.TooManyInputFilesFault; | ||
|
@@ -356,4 +359,49 @@ public void testTooManyInputFiles() throws IOException | |
.setExpectedMSQFault(new TooManyInputFilesFault(numFiles, Limits.MAX_INPUT_FILES_PER_WORKER, 2)) | ||
.verifyResults(); | ||
} | ||
|
||
@Test | ||
public void testUnionAllUsingUnionDataSourceDisallowed() | ||
{ | ||
final RowSignature rowSignature = | ||
RowSignature.builder().add("__time", ColumnType.LONG).build(); | ||
// This plans the query using DruidUnionDataSourceRule since the DruidUnionDataSourceRule#isCompatible | ||
// returns true (column names, types match, and it is a union on the table data sources). | ||
// It gets planned correctly, however MSQ engine cannot plan the query correctly | ||
testSelectQuery() | ||
.setSql("SELECT * FROM foo\n" | ||
+ "UNION ALL\n" | ||
+ "SELECT * FROM foo\n") | ||
.setExpectedRowSignature(rowSignature) | ||
.setExpectedMSQFault(QueryNotSupportedFault.instance()) | ||
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. Hmm… I am confused about why this yields a 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. It does plan using the 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. Added a comment on how it is getting planned. 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 guess this test can be removed. It should be planned using a UnionDataSource. |
||
.verifyResults(); | ||
} | ||
|
||
@Test | ||
public void testUnionAllUsingTopLevelUnionDisallowedWhilePlanning() | ||
{ | ||
// This results in a planning error however the planning error isn't an accurate representation of the actual error. | ||
// Calcite tries to plan the query using DruidUnionRule, which passes with native, however fails with MSQ (due to engine | ||
// feature ALLOW_TOP_LEVEL_UNION_ALL being absent) | ||
// Calcite then tries to write it using DruidUnionDataSourceRule. However, the isCompatible check fails because column | ||
// names mismatch. But it sets the planning error with this mismatch, which can be misleading since native queries can | ||
// plan fine using the DruidUnionRule (that's disabled in MSQ) | ||
// Once MSQ is able to support union datasources, we'd be able to execute this query fine in MSQ | ||
testIngestQuery() | ||
.setSql( | ||
"INSERT INTO druid.dst " | ||
+ "SELECT dim2, dim1, m1 FROM foo2 " | ||
+ "UNION ALL " | ||
+ "SELECT dim1, dim2, m1 FROM foo " | ||
+ "PARTITIONED BY ALL TIME") | ||
.setExpectedValidationErrorMatcher( | ||
new DruidExceptionMatcher( | ||
DruidException.Persona.ADMIN, | ||
DruidException.Category.INVALID_INPUT, | ||
"general" | ||
).expectMessageIs("Query planning failed for unknown reason, our best guess is this " | ||
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. Where is this error coming from? Looking at the code for the union rule, I would think it can't happen, because it's generated by 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. It tries to plan the query using the UnionDataSourceRule, goes into the 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'll rename the two test cases that I added 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. Added comments and renamed the test cases. Hope they clarify the confusion |
||
+ "[SQL requires union between two tables and column names queried for each table are different " | ||
+ "Left: [dim2, dim1, m1], Right: [dim1, dim2, m1].]")) | ||
.verifyPlanningErrors(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,4 +174,5 @@ public void testArrayAggQueryOnComplexDatatypes() | |
); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* 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.msq.test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.inject.Injector; | ||
import com.google.inject.Module; | ||
import org.apache.druid.common.config.NullHandling; | ||
import org.apache.druid.guice.DruidInjectorBuilder; | ||
import org.apache.druid.java.util.common.granularity.Granularities; | ||
import org.apache.druid.msq.exec.WorkerMemoryParameters; | ||
import org.apache.druid.msq.sql.MSQTaskSqlEngine; | ||
import org.apache.druid.query.QueryDataSource; | ||
import org.apache.druid.query.TableDataSource; | ||
import org.apache.druid.query.UnionDataSource; | ||
import org.apache.druid.query.aggregation.CountAggregatorFactory; | ||
import org.apache.druid.query.aggregation.LongSumAggregatorFactory; | ||
import org.apache.druid.query.dimension.DefaultDimensionSpec; | ||
import org.apache.druid.query.groupby.GroupByQuery; | ||
import org.apache.druid.query.groupby.TestGroupByBuffers; | ||
import org.apache.druid.server.QueryLifecycleFactory; | ||
import org.apache.druid.sql.calcite.BaseCalciteQueryTest; | ||
import org.apache.druid.sql.calcite.CalciteUnionQueryTest; | ||
import org.apache.druid.sql.calcite.QueryTestBuilder; | ||
import org.apache.druid.sql.calcite.filtration.Filtration; | ||
import org.apache.druid.sql.calcite.run.SqlEngine; | ||
import org.apache.druid.sql.calcite.util.CalciteTests; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Runs {@link CalciteUnionQueryTest} but with MSQ engine | ||
*/ | ||
public class CalciteUnionQueryMSQTest extends CalciteUnionQueryTest | ||
{ | ||
private TestGroupByBuffers groupByBuffers; | ||
|
||
@Before | ||
public void setup2() | ||
{ | ||
groupByBuffers = TestGroupByBuffers.createDefault(); | ||
} | ||
|
||
@After | ||
public void teardown2() | ||
{ | ||
groupByBuffers.close(); | ||
} | ||
|
||
@Override | ||
public void configureGuice(DruidInjectorBuilder builder) | ||
{ | ||
super.configureGuice(builder); | ||
builder.addModules(CalciteMSQTestsHelper.fetchModules(temporaryFolder, groupByBuffers).toArray(new Module[0])); | ||
} | ||
|
||
|
||
@Override | ||
public SqlEngine createEngine( | ||
QueryLifecycleFactory qlf, | ||
ObjectMapper queryJsonMapper, | ||
Injector injector | ||
) | ||
{ | ||
final WorkerMemoryParameters workerMemoryParameters = | ||
WorkerMemoryParameters.createInstance( | ||
WorkerMemoryParameters.PROCESSING_MINIMUM_BYTES * 50, | ||
2, | ||
10, | ||
2, | ||
0, | ||
0 | ||
); | ||
final MSQTestOverlordServiceClient indexingServiceClient = new MSQTestOverlordServiceClient( | ||
queryJsonMapper, | ||
injector, | ||
new MSQTestTaskActionClient(queryJsonMapper), | ||
workerMemoryParameters | ||
); | ||
return new MSQTaskSqlEngine(indexingServiceClient, queryJsonMapper); | ||
} | ||
|
||
@Override | ||
protected QueryTestBuilder testBuilder() | ||
{ | ||
return new QueryTestBuilder(new BaseCalciteQueryTest.CalciteTestConfig(true)) | ||
.addCustomRunner(new ExtractResultsFactory(() -> (MSQTestOverlordServiceClient) ((MSQTaskSqlEngine) queryFramework().engine()).overlordClient())) | ||
.skipVectorize(true) | ||
.verifyNativeQueries(new VerifyMSQSupportedNativeQueriesPredicate()) | ||
.msqCompatible(msqCompatible); | ||
} | ||
|
||
/** | ||
* Doesn't pass through Druid however the planning error is different as it rewrites to a union datasource. | ||
* This test is disabled because MSQ wants to support union datasources, and it makes little sense to add highly | ||
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. This comment seems outdated. |
||
* conditional planning error for the same. Planning errors are merely hints, and this is one of those times | ||
* when the hint is incorrect till MSQ starts supporting the union datasource. | ||
*/ | ||
@Test | ||
@Override | ||
public void testUnionIsUnplannable() | ||
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. Is this still required ? |
||
{ | ||
|
||
} | ||
|
||
@Test | ||
public void testUnionOnSubqueries() | ||
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 guess this test can be marked with ignore till we have the new calcite rule in place. |
||
{ | ||
testQuery( | ||
"SELECT\n" | ||
+ " SUM(cnt),\n" | ||
+ " COUNT(*)\n" | ||
+ "FROM (\n" | ||
+ " (SELECT dim2, SUM(cnt) AS cnt FROM druid.foo GROUP BY dim2)\n" | ||
+ " UNION ALL\n" | ||
+ " (SELECT dim2, SUM(cnt) AS cnt FROM druid.foo GROUP BY dim2)\n" | ||
+ ")", | ||
ImmutableList.of( | ||
GroupByQuery.builder() | ||
.setDataSource( | ||
new QueryDataSource( | ||
GroupByQuery.builder() | ||
.setDataSource( | ||
new UnionDataSource( | ||
ImmutableList.of( | ||
new TableDataSource(CalciteTests.DATASOURCE1), | ||
new TableDataSource(CalciteTests.DATASOURCE1) | ||
) | ||
) | ||
) | ||
.setInterval(querySegmentSpec(Filtration.eternity())) | ||
.setGranularity(Granularities.ALL) | ||
.setDimensions(dimensions(new DefaultDimensionSpec("dim2", "d0"))) | ||
.setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))) | ||
.setContext(QUERY_CONTEXT_DEFAULT) | ||
.build() | ||
) | ||
) | ||
.setInterval(querySegmentSpec(Filtration.eternity())) | ||
.setGranularity(Granularities.ALL) | ||
.setAggregatorSpecs(aggregators( | ||
new LongSumAggregatorFactory("_a0", "a0"), | ||
new CountAggregatorFactory("_a1") | ||
)) | ||
.setContext(QUERY_CONTEXT_DEFAULT) | ||
.build() | ||
), | ||
NullHandling.replaceWithDefault() ? | ||
ImmutableList.of( | ||
new Object[]{12L, 3L} | ||
) : | ||
ImmutableList.of( | ||
new Object[]{12L, 4L} | ||
) | ||
); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the MSQ known issues and the docs where ever we are calling union all as unsupported in MSQ.