Skip to content

Commit

Permalink
(improvement)[test] Combine multiple tests to use only one doris clus…
Browse files Browse the repository at this point in the history
…ter (apache#7934)

This PR mainly includes the following two changes:
1. Shorten FE single measurement time
In Doris's FE unit test, starting a Doris cluster is a time-consuming operation.
In this PR, the unit tests of some small functions are merged into @QueryPlanTest,
the same cluster is used centrally,
so as to avoid the problem that the overall unit test time of FE is too long.

2. Refine the logic of "PR 7851"
Although the function can be implemented correctly in PR apache#7851,
the logic is not brief enough.
This PR mainly succinct redundant code in terms of engineering implementation.
  • Loading branch information
EmmyMiao87 authored Jan 31, 2022
1 parent 8c179bb commit 58ad8b7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 92 deletions.
30 changes: 10 additions & 20 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@

package org.apache.doris.analysis;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.doris.catalog.Catalog;
import org.apache.doris.catalog.Function;
import org.apache.doris.catalog.FunctionSet;
Expand All @@ -46,6 +39,13 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


public class CastExpr extends Expr {
private static final Logger LOG = LogManager.getLogger(CastExpr.class);
Expand Down Expand Up @@ -178,22 +178,12 @@ public Expr clone() {

@Override
public String toSqlImpl() {
if (ConnectContext.get() != null &&
boolean isVerbose = ConnectContext.get() != null &&
ConnectContext.get().getExecutor() != null &&
ConnectContext.get().getExecutor().getParsedStmt() != null &&
ConnectContext.get().getExecutor().getParsedStmt().getExplainOptions() != null &&
ConnectContext.get().getExecutor().getParsedStmt().getExplainOptions().isVerbose()) {
if (isAnalyzed) {
if (type.isStringType()) {
return "CAST(" + getChild(0).toSql() + " AS " + "CHARACTER" + ")";
} else {
return "CAST(" + getChild(0).toSql() + " AS " + type.toString() + ")";
}
} else {
return "CAST(" + getChild(0).toSql() + " AS " + targetTypeDef.toSql() + ")";
}
}
if (isImplicit) {
ConnectContext.get().getExecutor().getParsedStmt().getExplainOptions().isVerbose();
if (isImplicit && !isVerbose) {
return getChild(0).toSql();
}
if (isAnalyzed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ private void handleInsertStmt() throws Exception {
}

if (insertStmt.getQueryStmt().isExplain()) {
insertStmt.setIsExplain(new ExplainOptions(true, false));
String explainString = planner.getExplainString(planner.getFragments(), new ExplainOptions(true, false));
handleExplainStmt(explainString);
return;
Expand Down
42 changes: 17 additions & 25 deletions fe/fe-core/src/test/java/org/apache/doris/analysis/ExplainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,23 @@
import org.apache.doris.catalog.Catalog;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.utframe.UtFrameUtils;
import org.junit.AfterClass;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.UUID;


public class ExplainTest {
private static String runningDir = "fe/mocked/TableFunctionPlanTest/" + UUID.randomUUID().toString() + "/";
private static ConnectContext ctx;


@BeforeClass
public static void beforeClass() throws Exception {
UtFrameUtils.createDorisCluster(runningDir);
ctx = UtFrameUtils.createDefaultCtx();
String createDbStmtStr = "create database test;";
public void before(ConnectContext ctx) throws Exception {
this.ctx = ctx;
String createDbStmtStr = "create database test_explain;";
CreateDbStmt createDbStmt = (CreateDbStmt) UtFrameUtils.parseAndAnalyzeStmt(createDbStmtStr, ctx);
Catalog.getCurrentCatalog().createDb(createDbStmt);

String t1 =("CREATE TABLE test.t1 (\n" +
String t1 = ("CREATE TABLE test_explain.explain_t1 (\n" +
" `dt` int(11) COMMENT \"\",\n" +
" `id` int(11) COMMENT \"\",\n" +
" `value` varchar(8) COMMENT \"\"\n" +
Expand All @@ -56,7 +51,7 @@ public static void beforeClass() throws Exception {
CreateTableStmt createTableStmt = (CreateTableStmt) UtFrameUtils.parseAndAnalyzeStmt(t1, ctx);
Catalog.getCurrentCatalog().createTable(createTableStmt);

String t2 =("CREATE TABLE test.t2 (\n" +
String t2 =("CREATE TABLE test_explain.explain_t2 (\n" +
" `dt` bigint(11) COMMENT \"\",\n" +
" `id` bigint(11) COMMENT \"\",\n" +
" `value` bigint(8) COMMENT \"\"\n" +
Expand All @@ -70,49 +65,46 @@ public static void beforeClass() throws Exception {
");");
createTableStmt = (CreateTableStmt) UtFrameUtils.parseAndAnalyzeStmt(t2, ctx);
Catalog.getCurrentCatalog().createTable(createTableStmt);

}

@AfterClass
public static void tearDown() {
UtFrameUtils.cleanDorisFeDir(runningDir);
public void after() throws Exception {
String dropSchemaSql = "drop schema if exists test_explain";
String dropDbSql = "drop database if exists test_explain";
DropDbStmt dropSchemaStmt = (DropDbStmt) UtFrameUtils.parseAndAnalyzeStmt(dropSchemaSql, ctx);
DropDbStmt dropDbStmt = (DropDbStmt) UtFrameUtils.parseAndAnalyzeStmt(dropDbSql, ctx);
Assert.assertEquals(dropDbStmt.toSql(), dropSchemaStmt.toSql());
}

@Test
public void testExplainInsertInto() throws Exception {
String sql = "explain insert into test.t1 select * from test.t2";
String sql = "explain insert into test_explain.explain_t1 select * from test_explain.explain_t2";
String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(ctx, sql, true);
System.out.println(explainString);
Assert.assertTrue(explainString.contains("CAST"));
}

@Test
public void testExplainSelect() throws Exception {
String sql = "explain select * from test.t1 where dt = '1001';";
String sql = "explain select * from test_explain.explain_t1 where dt = '1001';";
String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(ctx, sql, false);
System.out.println(explainString);
Assert.assertFalse(explainString.contains("CAST"));
}

@Test
public void testExplainVerboseSelect() throws Exception {
String queryStr = "explain verbose select * from test.t1 where dt = '1001';";
String queryStr = "explain verbose select * from test_explain.explain_t1 where dt = '1001';";
String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(ctx, queryStr, true);
System.out.println(explainString);
Assert.assertTrue(explainString.contains("CAST"));
}

@Test
public void testExplainConcatSelect() throws Exception {
String sql = "explain select concat(dt, id) from test.t1;";
String sql = "explain select concat(dt, id) from test_explain.explain_t1;";
String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(ctx, sql, false);
System.out.println(explainString);
Assert.assertFalse(explainString.contains("CAST"));
}

@Test
public void testExplainVerboseConcatSelect() throws Exception {
String sql = "explain verbose select concat(dt, id) from test.t1;";
String sql = "explain verbose select concat(dt, id) from test_explain.explain_t1;";
String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(ctx, sql, true);
System.out.println(explainString);
Assert.assertTrue(explainString.contains("CAST"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.doris.analysis.CreateTableStmt;
import org.apache.doris.analysis.CreateViewStmt;
import org.apache.doris.analysis.DropDbStmt;
import org.apache.doris.analysis.ExplainTest;
import org.apache.doris.analysis.Expr;
import org.apache.doris.analysis.InformationFunction;
import org.apache.doris.analysis.LoadStmt;
Expand All @@ -44,6 +45,7 @@
import org.apache.doris.load.EtlJobType;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.QueryState.MysqlStateType;
import org.apache.doris.rewrite.RewriteDateLiteralRuleTest;
import org.apache.doris.thrift.TRuntimeFilterType;
import org.apache.doris.utframe.UtFrameUtils;

Expand Down Expand Up @@ -1874,7 +1876,7 @@ public void testOutfile() throws Exception {
String explainStr = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext, "EXPLAIN " + sql);
Assert.assertTrue(explainStr.contains("PREDICATES: `date` >= '2021-10-07 00:00:00', `date` <= '2021-10-11 00:00:00'"));
}

// Fix: issue-#7929
@Test
public void testEmptyNodeWithOuterJoinAndAnalyticFunction() throws Exception {
Expand Down Expand Up @@ -1913,4 +1915,31 @@ public void testEmptyNodeWithOuterJoinAndAnalyticFunction() throws Exception {

}

// --begin-- implicit cast in explain verbose
@Test
public void testExplainInsertInto() throws Exception {
ExplainTest explainTest = new ExplainTest();
explainTest.before(connectContext);
explainTest.testExplainInsertInto();
explainTest.testExplainSelect();
explainTest.testExplainVerboseSelect();
explainTest.testExplainConcatSelect();
explainTest.testExplainVerboseConcatSelect();
explainTest.after();
}
// --end--

// --begin-- rewrite date literal rule
@Test
public void testRewriteDateLiteralRule() throws Exception {
RewriteDateLiteralRuleTest rewriteDateLiteralRuleTest = new RewriteDateLiteralRuleTest();
rewriteDateLiteralRuleTest.before(connectContext);
rewriteDateLiteralRuleTest.testWithDoubleFormatDate();
rewriteDateLiteralRuleTest.testWithIntFormatDate();
rewriteDateLiteralRuleTest.testWithInvalidFormatDate();
rewriteDateLiteralRuleTest.testWithStringFormatDate();
rewriteDateLiteralRuleTest.after();
}
// --end--

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,144 +20,130 @@

import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.FeConstants;

import org.apache.doris.qe.ConnectContext;
import org.apache.doris.utframe.DorisAssert;
import org.apache.doris.utframe.UtFrameUtils;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.UUID;
import org.junit.Assert;

public class RewriteDateLiteralRuleTest {
private static String baseDir = "fe";
private static String runningDir = baseDir + "/mocked/RewriteDateLiteralRuleTest/"
+ UUID.randomUUID() + "/";
private static DorisAssert dorisAssert;
private static final String DB_NAME = "db1";
private DorisAssert dorisAssert;
private static final String DB_NAME = "rewritedaterule";
private static final String TABLE_NAME_1 = "tb1";

@BeforeClass
public static void beforeClass() throws Exception {
public void before(ConnectContext ctx) throws Exception {
FeConstants.default_scheduler_interval_millisecond = 10;
FeConstants.runningUnitTest = true;
UtFrameUtils.createDorisCluster(runningDir);
dorisAssert = new DorisAssert();
dorisAssert = new DorisAssert(ctx);
dorisAssert.withDatabase(DB_NAME).useDatabase(DB_NAME);
String createTableSQL = "create table " + DB_NAME + "." + TABLE_NAME_1
+ " (k1 datetime, k2 int) "
+ "distributed by hash(k2) buckets 3 properties('replication_num' = '1');";
dorisAssert.withTable(createTableSQL);
}

@AfterClass
public static void afterClass() throws Exception {
UtFrameUtils.cleanDorisFeDir(baseDir);
public void after() throws Exception {
String dropDbSql = "drop database if exists " + DB_NAME;
dorisAssert.dropDB(DB_NAME);
}

@Test
public void testWithIntFormatDate() throws Exception {
String query = "select * from db1.tb1 where k1 > 20210301";
String query = "select * from " + DB_NAME + ".tb1 where k1 > 20210301";
String planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
query = "select k1 > 20210301 from db1.tb1";
query = "select k1 > 20210301 from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));
query = "select k1 > 20210301223344 from db1.tb1";
query = "select k1 > 20210301223344 from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 22:33:44'"));
}

@Test
public void testWithStringFormatDate() throws Exception {
String query = "select * from db1.tb1 where k1 > '2021030112334455'";
String query = "select * from " + DB_NAME + ".tb1 where k1 > '2021030112334455'";
String planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 12:33:44'"));

query = "select k1 > '20210301' from db1.tb1";
query = "select k1 > '20210301' from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));

query = "select k1 > '20210301233234.34' from db1.tb1";
query = "select k1 > '20210301233234.34' from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 23:32:34'"));

query = "select * from db1.tb1 where k1 > '2021-03-01'";
query = "select * from " + DB_NAME + ".tb1 where k1 > '2021-03-01'";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));

query = "select k1 > '2021-03-01 11:22:33' from db1.tb1";
query = "select k1 > '2021-03-01 11:22:33' from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 11:22:33'"));

query = "select k1 > '2021-03-01 16:22:33' from db1.tb1";
query = "select k1 > '2021-03-01 16:22:33' from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 16:22:33'"));

query = "select k1 > '2021-03-01 11:22' from db1.tb1";
query = "select k1 > '2021-03-01 11:22' from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 11:22:00'"));

query = "select k1 > '20210301T221133' from db1.tb1";
query = "select k1 > '20210301T221133' from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 22:11:33'"));

query = "select k1 > '2021-03-01dd 11:22' from db1.tb1";
query = "select k1 > '2021-03-01dd 11:22' from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2021-03-01 00:00:00'"));

query = "select k1 > '80-03-01 11:22' from db1.tb1";
query = "select k1 > '80-03-01 11:22' from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '1980-03-01 11:22:00'"));

query = "select k1 > '12-03-01 11:22' from db1.tb1";
query = "select k1 > '12-03-01 11:22' from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > '2012-03-01 11:22:00'"));
}

@Test
public void testWithDoubleFormatDate() throws Exception {
String query = "select * from db1.tb1 where k1 > 20210301.22";
String query = "select * from " + DB_NAME + ".tb1 where k1 > 20210301.22";
String planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > 2.021030122E7"));

query = "select k1 > 20210331.22 from db1.tb1";
query = "select k1 > 20210331.22 from " + DB_NAME + ".tb1";
planString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(planString.contains("`k1` > 2.021033122E7"));
}

@Test
public void testWithInvalidFormatDate() throws Exception {
String query = "select * from db1.tb1 where k1 > '2021030125334455'";
String query = "select * from " + DB_NAME + ".tb1 where k1 > '2021030125334455'";
try {
dorisAssert.query(query).explainQuery();
} catch (AnalysisException e) {
Assert.assertTrue(e.getMessage().contains(
"Incorrect datetime value: '2021030125334455' in expression: `k1` > '2021030125334455'"));
}

query = "select k1 > '2021030125334455' from db1.tb1";
query = "select k1 > '2021030125334455' from " + DB_NAME + ".tb1";
String plainString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(plainString.contains("NULL"));

query = "select * from db1.tb1 where k1 > '2021-03-32 23:33:55'";
query = "select * from " + DB_NAME + ".tb1 where k1 > '2021-03-32 23:33:55'";
try {
dorisAssert.query(query).explainQuery();
} catch (AnalysisException e) {
Assert.assertTrue(e.getMessage().contains(
"Incorrect datetime value: '2021-03-32 23:33:55' in expression: `k1` > '2021-03-32 23:33:55'"));
}

query = "select * from db1.tb1 where k1 > '2021-03- 03 23:33:55'";
query = "select * from " + DB_NAME + ".tb1 where k1 > '2021-03- 03 23:33:55'";
try {
dorisAssert.query(query).explainQuery();
} catch (AnalysisException e) {
Assert.assertTrue(e.getMessage().contains(
"Incorrect datetime value: '2021-03- 03 23:33:55' in expression: `k1` > '2021-03- 03 23:33:55'"));
}

query = "select k1 > '2021-03- 03 23:33:55' from db1.tb1";
query = "select k1 > '2021-03- 03 23:33:55' from " + DB_NAME + ".tb1";
plainString = dorisAssert.query(query).explainQuery();
Assert.assertTrue(plainString.contains("NULL"));
}
Expand Down
Loading

0 comments on commit 58ad8b7

Please sign in to comment.