-
Notifications
You must be signed in to change notification settings - Fork 1.9k
IGNITE-28596 Add test to check TPCH plans #13068
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
Open
nizhikov
wants to merge
40
commits into
apache:master
Choose a base branch
from
nizhikov:IGNITE-28596
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
019b379
WIP. Fix plans for tpch
nizhikov 9aa088e
WIP. Fix plans for tpch
nizhikov d113bea
WIP. Fix plans for tpch
nizhikov 11cbb58
WIP. Fix plans for tpch
nizhikov 68f4b23
WIP. Fix plans for tpch
nizhikov 1fef5b1
WIP. Fix plans for tpch
nizhikov a955be1
WIP. Fix plans for tpch
nizhikov c741c69
WIP. Fix plans for tpch
nizhikov 15d0932
WIP. Fix plans for tpch
nizhikov 60ac5a0
WIP. Fix plans for tpch
nizhikov 27aee27
WIP. Fix plans for tpch
nizhikov 5f99d84
WIP. Fix plans for tpch
nizhikov 0526a2c
WIP. Fix plans for tpch
nizhikov e0e37b3
WIP. Fix plans for tpch
nizhikov 99b8e56
WIP. Fix plans for tpch
nizhikov 4f2b0ca
WIP. Fix plans for tpch
nizhikov 5a4577d
WIP. Fix plans for tpch
nizhikov c3bc7e1
WIP. Fix plans for tpch
nizhikov ef8f4f1
WIP. Fix plans for tpch
nizhikov 588bb39
WIP. Fix plans for tpch
nizhikov a3af4ad
WIP. Fix plans for tpch
nizhikov 6784fe0
WIP. Fix plans for tpch
nizhikov f17b799
WIP. Fix plans for tpch
nizhikov 95e5714
WIP. Fix plans for tpch
nizhikov 6e97d22
WIP. Fix plans for tpch
nizhikov 7cd83ca
WIP. Fix plans for tpch
nizhikov b49c93c
WIP. Fix plans for tpch
nizhikov 8472c03
WIP. Fix plans for tpch
nizhikov d81e611
WIP. Fix plans for tpch
nizhikov 59b0bdc
Merge branch 'master' into IGNITE-28596
nizhikov b9de799
WIP. Fix plans for tpch
nizhikov 34bb65b
WIP. Fix plans for tpch
nizhikov e706a74
WIP. Fix plans for tpch
nizhikov 179de98
WIP. Fix plans for tpch
nizhikov 6605cec
WIP. Fix plans for tpch
nizhikov 6f04b0d
WIP. Fix plans for tpch
nizhikov 6a4806d
WIP. Fix plans for tpch
nizhikov 16a4fe6
WIP. Fix plans for tpch
nizhikov 035eedb
WIP. Fix plans for tpch
nizhikov fdc78dc
WIP. Fix plans for tpch
nizhikov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
...che/ignite/internal/processors/query/calcite/planner/tpc/AbstractTpcQueryPlannerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| /* | ||
| * 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.ignite.internal.processors.query.calcite.planner.tpc; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.io.UncheckedIOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import com.google.common.io.CharStreams; | ||
| import org.apache.calcite.plan.RelOptUtil; | ||
| import org.apache.calcite.sql.SqlExplainLevel; | ||
| import org.apache.ignite.Ignite; | ||
| import org.apache.ignite.cache.query.FieldsQueryCursor; | ||
| import org.apache.ignite.cache.query.SqlFieldsQuery; | ||
| import org.apache.ignite.cache.query.annotations.QuerySqlFunction; | ||
| import org.apache.ignite.calcite.CalciteQueryEngineConfiguration; | ||
| import org.apache.ignite.configuration.CacheConfiguration; | ||
| import org.apache.ignite.configuration.IgniteConfiguration; | ||
| import org.apache.ignite.internal.IgniteEx; | ||
| import org.apache.ignite.internal.IgnitionEx; | ||
| import org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor; | ||
| import org.apache.ignite.internal.processors.query.calcite.integration.tpch.TpchHelper; | ||
| import org.apache.ignite.internal.processors.query.calcite.planner.AbstractPlannerTest; | ||
| import org.apache.ignite.internal.processors.query.calcite.planner.tpc.PlanChecker.AfterPlansTest; | ||
| import org.apache.ignite.internal.processors.query.calcite.planner.tpc.PlanChecker.BeforePlansTest; | ||
| import org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext; | ||
| import org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema; | ||
| import org.apache.ignite.testframework.GridTestUtils; | ||
| import org.junit.Test; | ||
| import org.junit.runners.Parameterized; | ||
|
|
||
| import static org.apache.ignite.internal.processors.query.calcite.planner.tpc.PlanChecker.RSRC_DIR; | ||
| import static org.apache.ignite.internal.processors.query.calcite.planner.tpc.PlanChecker.sqlTestName; | ||
|
|
||
| /** | ||
| * Abstract test class to ensure a planner generates expected plan for TPC queries. | ||
| */ | ||
| public class AbstractTpcQueryPlannerTest extends AbstractPlannerTest { | ||
| /** Set to {@code true} to write plan files, instead of checking. */ | ||
| private static final boolean UPDATE_PLAN = false; | ||
|
|
||
| /** Server node to run queries on. */ | ||
| private static IgniteEx srv; | ||
|
|
||
| /** Query id. Set by {@link PlanChecker}. */ | ||
| @Parameterized.Parameter | ||
| public String queryId; | ||
|
|
||
| /** Run once, before all queries check. */ | ||
| @BeforePlansTest | ||
| public static void startAll(Class<?> testClass) throws Exception { | ||
| AbstractTpcQueryPlannerTest mock = new AbstractTpcQueryPlannerTest(); | ||
|
|
||
| mock.beforeFirstTest(); | ||
|
|
||
| IgniteConfiguration cfg = mock.getConfiguration("server"); | ||
|
|
||
| cfg.getSqlConfiguration() | ||
| .setQueryEnginesConfiguration(new CalciteQueryEngineConfiguration().setDefault(true)); | ||
|
|
||
| srv = (IgniteEx)IgnitionEx.start(cfg); | ||
|
|
||
| srv.getOrCreateCache(new CacheConfiguration<>("mock") | ||
| .setSqlFunctionClasses(AbstractTpcQueryPlannerTest.TpchUDF.class) | ||
| .setSqlSchema("PUBLIC")); | ||
|
|
||
| TpchHelper.createTables(srv); | ||
|
|
||
| /* | ||
| TpchHelper.fillTables(srv, 0.01); | ||
|
Check warning on line 90 in modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/tpc/AbstractTpcQueryPlannerTest.java
|
||
|
|
||
| TpchHelper.collectSqlStatistics(srv); | ||
| */ | ||
| } | ||
|
|
||
| /** Run once, after all queries check. */ | ||
| @AfterPlansTest | ||
| public static void stopAll(Class<?> testClass) { | ||
| if (srv != null) { | ||
| srv.close(); | ||
|
|
||
| srv = null; | ||
| } | ||
| } | ||
|
|
||
| /** Test single query. */ | ||
| @Test | ||
| public void testQuery() throws Exception { | ||
| String actualPlan = queryPlan(); | ||
|
|
||
| if (UPDATE_PLAN) { | ||
| updatePlan(actualPlan); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| PlanTemplate pt = new PlanTemplate(loadFromResource(String.format("%s/%s.plan", sqlTestName(getClass()), queryId))); | ||
|
|
||
| if (!pt.match(actualPlan)) { | ||
| // This assertion will print nice diff in IDE that will help to investigate. | ||
| // Test will fail anyway. | ||
| assertEquals(pt.template, actualPlan); | ||
|
|
||
| assert false : "Should not happen"; | ||
| } | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override protected boolean isSafeTopology() { | ||
| return false; | ||
| } | ||
|
|
||
| /** */ | ||
| private String queryPlan() throws Exception { | ||
| Map<String, IgniteSchema> schemas = GridTestUtils.getFieldValue( | ||
| ((CalciteQueryProcessor)srv.context().query().defaultQueryEngine()).schemaHolder(), | ||
| "igniteSchemas" | ||
| ); | ||
|
|
||
| PlanningContext ctx = plannerCtx(loadFromResource(sqlTestName(getClass()) + "/" + queryId + ".sql"), schemas.values(), null); | ||
|
|
||
| return new PlanTemplate(RelOptUtil.toString(physicalPlan(ctx), SqlExplainLevel.ALL_ATTRIBUTES)).template; | ||
| } | ||
|
|
||
| /** */ | ||
| private void updatePlan(String newPlan) { | ||
| Path targetDir = Path.of(RSRC_DIR + sqlTestName(getClass())); | ||
|
|
||
| // A targetDirectory must be specified by hand when expected plans are generated. | ||
| if (targetDir == null) { | ||
| throw new RuntimeException("Please provide target directory to where save generated plans." | ||
| + " Usually plans are kept in resource folder of tests within the same module."); | ||
| } | ||
|
|
||
| try { | ||
| Files.createDirectories(targetDir); | ||
|
|
||
| Files.writeString(targetDir.resolve(String.format("%s.plan", queryId)), newPlan); | ||
| } | ||
| catch (Exception e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Execute SQL query. | ||
| * | ||
| * @param ignite Ignite. | ||
| * @param sql SQL query. | ||
| * @param params Query parameters. | ||
| */ | ||
| public static List<List<?>> sql(Ignite ignite, String sql, Object... params) { | ||
| SqlFieldsQuery qry = new SqlFieldsQuery(sql).setArgs(params); | ||
|
|
||
| try (FieldsQueryCursor<List<?>> cur = ((IgniteEx)ignite).context().query().querySqlFields(qry, false)) { | ||
| return cur.getAll(); | ||
| } | ||
| } | ||
|
|
||
| /** */ | ||
| public static class TpchUDF { | ||
| /** */ | ||
| @QuerySqlFunction(alias = "SUBSTR") | ||
| public static String substr(String str, int from, int cnt) { | ||
| return str.substring(from, from + cnt); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Loads resource with given name as string. | ||
| * | ||
| * @param resource Name of the resource to load. | ||
| * @return Resource as string. | ||
| */ | ||
| private static String loadFromResource(String resource) { | ||
| if (resource.startsWith(RSRC_DIR)) | ||
| resource = resource.substring(RSRC_DIR.length() + 1); | ||
|
|
||
| try (InputStream is = AbstractTpcQueryPlannerTest.class.getClassLoader().getResourceAsStream(resource)) { | ||
| if (is == null) | ||
| throw new IllegalArgumentException("Resource does not exist: " + resource); | ||
|
|
||
| try (InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) { | ||
| return CharStreams.toString(reader); | ||
| } | ||
| } | ||
| catch (IOException e) { | ||
| throw new UncheckedIOException("I/O operation failed: " + resource, e); | ||
| } | ||
| } | ||
| } | ||
140 changes: 140 additions & 0 deletions
140
...est/java/org/apache/ignite/internal/processors/query/calcite/planner/tpc/PlanChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| * 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.ignite.internal.processors.query.calcite.planner.tpc; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.annotation.Annotation; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import org.junit.runner.Runner; | ||
| import org.junit.runner.notification.RunNotifier; | ||
| import org.junit.runners.Suite; | ||
| import org.junit.runners.model.InitializationError; | ||
| import org.junit.runners.model.TestClass; | ||
| import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters; | ||
| import org.junit.runners.parameterized.TestWithParameters; | ||
|
|
||
| /** | ||
| * Suite to check queries plans. | ||
| * Makes pretty test names to simplify CI tracking. | ||
| */ | ||
| public class PlanChecker extends Suite { | ||
| /** */ | ||
| public static final String RSRC_DIR = "./src/test/resources/"; | ||
|
|
||
| /** | ||
| * Only called reflectively. Do not use programmatically. | ||
| */ | ||
| public PlanChecker(Class<?> klass) throws Throwable { | ||
| super(klass, createRunnersForParameters(new TestClass(klass)).collect(Collectors.toList())); | ||
| } | ||
|
|
||
| /** */ | ||
| private static Stream<Runner> createRunnersForParameters(TestClass testClass) throws IOException { | ||
| return Files.list(Path.of(RSRC_DIR, sqlTestName(testClass.getJavaClass()))) | ||
| .filter(p -> p.toString().endsWith(".sql") && !p.toString().endsWith("ddl.sql")) | ||
| .sorted() | ||
| .map(p -> { | ||
| String qryId = p.getFileName().toString().replace(".sql", ""); | ||
|
|
||
| try { | ||
| return new BlockJUnit4ClassRunnerWithParameters( | ||
| new TestWithParameters("[queryId=" + qryId + "]", testClass, Collections.singletonList(qryId)) | ||
| ); | ||
| } | ||
| catch (InitializationError e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override public void run(RunNotifier notifier) { | ||
| runAnnotated(BeforePlansTest.class); | ||
|
|
||
| try { | ||
| super.run(notifier); | ||
| } | ||
| finally { | ||
| runAnnotated(AfterPlansTest.class); | ||
| } | ||
| } | ||
|
|
||
| /** Runs static void method of test class annotated with the specific annotation. */ | ||
| private void runAnnotated(Class<? extends Annotation> annotation) { | ||
| getTestClass().getAnnotatedMethods(annotation).forEach(m -> { | ||
| List<Throwable> errors = new ArrayList<>(); | ||
|
|
||
| m.validatePublicVoid(true, errors); | ||
|
|
||
| try { | ||
| if (!errors.isEmpty()) | ||
| throw errors.get(0); | ||
|
|
||
| m.invokeExplosively(getTestClass().getJavaClass(), getTestClass().getJavaClass()); | ||
| } | ||
| catch (Throwable e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** @return SQL test name. */ | ||
| public static String sqlTestName(Class<?> klass) { | ||
| PlanChecker.PlansTest desc = klass.getAnnotation(PlanChecker.PlansTest.class); | ||
|
|
||
| if (desc == null) | ||
| throw new IllegalStateException("Test class must be annotated with @" + PlanChecker.PlansTest.class.getSimpleName()); | ||
|
|
||
| if (desc.name().isEmpty()) | ||
| throw new IllegalStateException("Please, set test name with the @PlanTest(name=\"XXX\")"); | ||
|
|
||
| return desc.name(); | ||
| } | ||
|
|
||
| /** */ | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.TYPE) | ||
| public @interface PlansTest { | ||
| /** @return Test name. */ | ||
| String name() default ""; | ||
| } | ||
|
|
||
| /** */ | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.METHOD) | ||
| public @interface BeforePlansTest { | ||
| // No-op. | ||
| } | ||
|
|
||
| /** */ | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.METHOD) | ||
| public @interface AfterPlansTest { | ||
| // No-op. | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
seems it need to be fixed somehow ?