|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.sql.planner; |
| 7 | + |
| 8 | +import org.elasticsearch.test.AbstractBuilderTestCase; |
| 9 | +import org.elasticsearch.xpack.sql.analysis.analyzer.Analyzer; |
| 10 | +import org.elasticsearch.xpack.sql.analysis.index.EsIndex; |
| 11 | +import org.elasticsearch.xpack.sql.analysis.index.IndexResolution; |
| 12 | +import org.elasticsearch.xpack.sql.expression.function.FunctionRegistry; |
| 13 | +import org.elasticsearch.xpack.sql.optimizer.Optimizer; |
| 14 | +import org.elasticsearch.xpack.sql.parser.SqlParser; |
| 15 | +import org.elasticsearch.xpack.sql.plan.physical.LocalExec; |
| 16 | +import org.elasticsearch.xpack.sql.plan.physical.PhysicalPlan; |
| 17 | +import org.elasticsearch.xpack.sql.session.EmptyExecutable; |
| 18 | +import org.elasticsearch.xpack.sql.type.EsField; |
| 19 | +import org.elasticsearch.xpack.sql.type.TypesTests; |
| 20 | +import org.junit.AfterClass; |
| 21 | +import org.junit.BeforeClass; |
| 22 | + |
| 23 | +import java.util.Map; |
| 24 | +import java.util.TimeZone; |
| 25 | + |
| 26 | +import static org.hamcrest.Matchers.startsWith; |
| 27 | + |
| 28 | +public class QueryFolderTests extends AbstractBuilderTestCase { |
| 29 | + |
| 30 | + private static SqlParser parser; |
| 31 | + private static Analyzer analyzer; |
| 32 | + private static Optimizer optimizer; |
| 33 | + private static Planner planner; |
| 34 | + |
| 35 | + @BeforeClass |
| 36 | + public static void init() { |
| 37 | + parser = new SqlParser(); |
| 38 | + |
| 39 | + Map<String, EsField> mapping = TypesTests.loadMapping("mapping-multi-field-variation.json"); |
| 40 | + EsIndex test = new EsIndex("test", mapping); |
| 41 | + IndexResolution getIndexResult = IndexResolution.valid(test); |
| 42 | + analyzer = new Analyzer(new FunctionRegistry(), getIndexResult, TimeZone.getTimeZone("UTC")); |
| 43 | + optimizer = new Optimizer(); |
| 44 | + planner = new Planner(); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterClass |
| 48 | + public static void destroy() { |
| 49 | + parser = null; |
| 50 | + analyzer = null; |
| 51 | + } |
| 52 | + |
| 53 | + private PhysicalPlan plan(String sql) { |
| 54 | + return planner.plan(optimizer.optimize(analyzer.analyze(parser.createStatement(sql), true)), true); |
| 55 | + } |
| 56 | + |
| 57 | + public void testFoldingToLocalExecWithProject() { |
| 58 | + PhysicalPlan p = plan("SELECT keyword FROM test WHERE 1 = 2"); |
| 59 | + assertEquals(LocalExec.class, p.getClass()); |
| 60 | + LocalExec le = (LocalExec) p; |
| 61 | + assertEquals(EmptyExecutable.class, le.executable().getClass()); |
| 62 | + EmptyExecutable ee = (EmptyExecutable) le.executable(); |
| 63 | + assertEquals(1, ee.output().size()); |
| 64 | + assertThat(ee.output().get(0).toString(), startsWith("keyword{f}#")); |
| 65 | + } |
| 66 | + |
| 67 | + public void testFoldingToLocalExecWithProject_WithOrderAndLimit() { |
| 68 | + PhysicalPlan p = plan("SELECT keyword FROM test WHERE 1 = 2 ORDER BY int LIMIT 10"); |
| 69 | + assertEquals(LocalExec.class, p.getClass()); |
| 70 | + LocalExec le = (LocalExec) p; |
| 71 | + assertEquals(EmptyExecutable.class, le.executable().getClass()); |
| 72 | + EmptyExecutable ee = (EmptyExecutable) le.executable(); |
| 73 | + assertEquals(1, ee.output().size()); |
| 74 | + assertThat(ee.output().get(0).toString(), startsWith("keyword{f}#")); |
| 75 | + } |
| 76 | + |
| 77 | + public void testFoldingToLocalExecWithProjectWithGroupBy_WithOrderAndLimit() { |
| 78 | + PhysicalPlan p = plan("SELECT keyword, max(int) FROM test WHERE 1 = 2 GROUP BY keyword ORDER BY 1 LIMIT 10"); |
| 79 | + assertEquals(LocalExec.class, p.getClass()); |
| 80 | + LocalExec le = (LocalExec) p; |
| 81 | + assertEquals(EmptyExecutable.class, le.executable().getClass()); |
| 82 | + EmptyExecutable ee = (EmptyExecutable) le.executable(); |
| 83 | + assertEquals(2, ee.output().size()); |
| 84 | + assertThat(ee.output().get(0).toString(), startsWith("keyword{f}#")); |
| 85 | + assertThat(ee.output().get(1).toString(), startsWith("MAX(int){a->")); |
| 86 | + } |
| 87 | + |
| 88 | + public void testFoldingToLocalExecWithProjectWithGroupBy_WithHaving_WithOrderAndLimit() { |
| 89 | + PhysicalPlan p = plan("SELECT keyword, max(int) FROM test GROUP BY keyword HAVING 1 = 2 ORDER BY 1 LIMIT 10"); |
| 90 | + assertEquals(LocalExec.class, p.getClass()); |
| 91 | + LocalExec le = (LocalExec) p; |
| 92 | + assertEquals(EmptyExecutable.class, le.executable().getClass()); |
| 93 | + EmptyExecutable ee = (EmptyExecutable) le.executable(); |
| 94 | + assertEquals(2, ee.output().size()); |
| 95 | + assertThat(ee.output().get(0).toString(), startsWith("keyword{f}#")); |
| 96 | + assertThat(ee.output().get(1).toString(), startsWith("MAX(int){a->")); |
| 97 | + } |
| 98 | +} |
0 commit comments