[refactor](planner): make Table abstract. (#11642)

This commit is contained in:
jakevin
2022-08-11 12:07:25 +08:00
committed by GitHub
parent 2068bf2dea
commit d831322806
7 changed files with 67 additions and 67 deletions

View File

@ -49,7 +49,7 @@ import java.util.stream.Collectors;
/**
* Internal representation of table-related metadata. A table contains several partitions.
*/
public class Table extends MetaObject implements Writable, TableIf {
public abstract class Table extends MetaObject implements Writable, TableIf {
private static final Logger LOG = LogManager.getLogger(Table.class);
// empirical value.

View File

@ -42,7 +42,6 @@ public class FakeEnv extends MockUp<Env> {
@Mock
public static Env getCurrentEnv() {
System.out.println("fake get current env is called");
return env;
}

View File

@ -25,6 +25,7 @@ import org.apache.doris.common.MetaNotFoundException;
import org.apache.doris.common.jmockit.Deencapsulation;
import org.apache.doris.thrift.TStorageType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.junit.Assert;
import org.junit.Before;
@ -40,19 +41,38 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
public class TableTest {
public static OlapTable newOlapTable(long tableId, String tableName, int hashColumn) {
List<Column> columns = ImmutableList.of(
new Column("id", Type.INT, true, AggregateType.NONE, "0", ""));
return newOlapTable(tableId, tableName, hashColumn, columns);
}
public static OlapTable newOlapTable(long tableId, String tableName, int hashColumn, List<Column> columns) {
HashDistributionInfo hashDistributionInfo = new HashDistributionInfo(3,
ImmutableList.of(columns.get(hashColumn)));
OlapTable table = new OlapTable(tableId, tableName, columns,
KeysType.PRIMARY_KEYS, null, hashDistributionInfo);
table.setIndexMeta(-1,
"base",
table.getFullSchema(),
0, 0, (short) 0,
TStorageType.COLUMN,
KeysType.PRIMARY_KEYS);
return table;
}
private FakeEnv fakeEnv;
private Env env;
private Table table;
private long tableId = 10000;
@Before
public void setUp() {
table = newOlapTable(10000, "test", 0);
fakeEnv = new FakeEnv();
env = Deencapsulation.newInstance(Env.class);
table = new Table(Table.TableType.OLAP);
table.setName("test");
Env env = Deencapsulation.newInstance(Env.class);
FakeEnv.setEnv(env);
FakeEnv.setMetaVersion(FeConstants.meta_version);
}
@ -118,22 +138,22 @@ public class TableTest {
ScalarType.createType(PrimitiveType.TINYINT), false, AggregateType.MIN, "", "");
columns.add(column2);
columns.add(new Column("column3",
ScalarType.createType(PrimitiveType.SMALLINT), false, AggregateType.SUM, "", ""));
ScalarType.createType(PrimitiveType.SMALLINT), false, AggregateType.SUM, "", ""));
columns.add(new Column("column4",
ScalarType.createType(PrimitiveType.INT), false, AggregateType.REPLACE, "", ""));
ScalarType.createType(PrimitiveType.INT), false, AggregateType.REPLACE, "", ""));
columns.add(new Column("column5",
ScalarType.createType(PrimitiveType.BIGINT), false, AggregateType.REPLACE, "", ""));
ScalarType.createType(PrimitiveType.BIGINT), false, AggregateType.REPLACE, "", ""));
columns.add(new Column("column6",
ScalarType.createType(PrimitiveType.FLOAT), false, AggregateType.REPLACE, "", ""));
ScalarType.createType(PrimitiveType.FLOAT), false, AggregateType.REPLACE, "", ""));
columns.add(new Column("column7",
ScalarType.createType(PrimitiveType.DOUBLE), false, AggregateType.REPLACE, "", ""));
ScalarType.createType(PrimitiveType.DOUBLE), false, AggregateType.REPLACE, "", ""));
columns.add(new Column("column8", ScalarType.createChar(10), true, null, "", ""));
columns.add(new Column("column9", ScalarType.createVarchar(10), true, null, "", ""));
columns.add(new Column("column10", ScalarType.createType(PrimitiveType.DATE), true, null, "", ""));
columns.add(new Column("column11", ScalarType.createType(PrimitiveType.DATETIME), true, null, "", ""));
OlapTable table1 = new OlapTable(1000L, "group1", columns, KeysType.AGG_KEYS,
new SinglePartitionInfo(), new RandomDistributionInfo(10));
new SinglePartitionInfo(), new RandomDistributionInfo(10));
short shortKeyColumnCount = 1;
table1.setIndexMeta(1000, "group1", columns, 1, 1,
shortKeyColumnCount, TStorageType.COLUMN, KeysType.AGG_KEYS);

View File

@ -19,8 +19,10 @@ package org.apache.doris.common.util;
import org.apache.doris.catalog.Database;
import org.apache.doris.catalog.Table;
import org.apache.doris.catalog.TableTest;
import org.apache.doris.common.MetaNotFoundException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.junit.Assert;
import org.junit.Rule;
@ -32,6 +34,9 @@ import java.util.concurrent.TimeUnit;
public class MetaLockUtilsTest {
List<Table> tableList = ImmutableList.of(TableTest.newOlapTable(0, "test1", 0),
TableTest.newOlapTable(1, "test2", 0));
@Rule
public ExpectedException expectedException = ExpectedException.none();
@ -50,7 +55,6 @@ public class MetaLockUtilsTest {
@Test
public void testReadLockTables() {
List<Table> tableList = Lists.newArrayList(new Table(Table.TableType.OLAP), new Table(Table.TableType.OLAP));
MetaLockUtils.readLockTables(tableList);
Assert.assertFalse(tableList.get(0).tryWriteLock(1, TimeUnit.MILLISECONDS));
Assert.assertFalse(tableList.get(1).tryWriteLock(1, TimeUnit.MILLISECONDS));
@ -63,7 +67,6 @@ public class MetaLockUtilsTest {
@Test
public void testWriteLockTables() throws MetaNotFoundException {
List<Table> tableList = Lists.newArrayList(new Table(Table.TableType.OLAP), new Table(Table.TableType.OLAP));
MetaLockUtils.writeLockTables(tableList);
Assert.assertTrue(tableList.get(0).isWriteLockHeldByCurrentThread());
Assert.assertTrue(tableList.get(1).isWriteLockHeldByCurrentThread());
@ -83,51 +86,39 @@ public class MetaLockUtilsTest {
@Test
public void testWriteLockTablesWithMetaNotFoundException() throws MetaNotFoundException {
List<Table> tableList = Lists.newArrayList();
Table table1 = new Table(Table.TableType.OLAP);
Table table2 = new Table(Table.TableType.OLAP);
table2.setName("test2");
tableList.add(table1);
tableList.add(table2);
MetaLockUtils.writeLockTablesOrMetaException(tableList);
Assert.assertTrue(table1.isWriteLockHeldByCurrentThread());
Assert.assertTrue(table2.isWriteLockHeldByCurrentThread());
Assert.assertTrue(tableList.get(0).isWriteLockHeldByCurrentThread());
Assert.assertTrue(tableList.get(1).isWriteLockHeldByCurrentThread());
MetaLockUtils.writeUnlockTables(tableList);
Assert.assertFalse(table1.isWriteLockHeldByCurrentThread());
Assert.assertFalse(table2.isWriteLockHeldByCurrentThread());
table2.markDropped();
Assert.assertFalse(tableList.get(0).isWriteLockHeldByCurrentThread());
Assert.assertFalse(tableList.get(1).isWriteLockHeldByCurrentThread());
tableList.get(1).markDropped();
expectedException.expect(MetaNotFoundException.class);
expectedException.expectMessage("errCode = 7, detailMessage = unknown table, tableName=test2");
try {
MetaLockUtils.writeLockTablesOrMetaException(tableList);
} finally {
Assert.assertFalse(table1.isWriteLockHeldByCurrentThread());
Assert.assertFalse(table2.isWriteLockHeldByCurrentThread());
Assert.assertFalse(tableList.get(0).isWriteLockHeldByCurrentThread());
Assert.assertFalse(tableList.get(1).isWriteLockHeldByCurrentThread());
}
}
@Test
public void testTryWriteLockTablesWithMetaNotFoundException() throws MetaNotFoundException {
List<Table> tableList = Lists.newArrayList();
Table table1 = new Table(Table.TableType.OLAP);
Table table2 = new Table(Table.TableType.OLAP);
table2.setName("test2");
tableList.add(table1);
tableList.add(table2);
MetaLockUtils.tryWriteLockTablesOrMetaException(tableList, 1000, TimeUnit.MILLISECONDS);
Assert.assertTrue(table1.isWriteLockHeldByCurrentThread());
Assert.assertTrue(table2.isWriteLockHeldByCurrentThread());
Assert.assertTrue(tableList.get(0).isWriteLockHeldByCurrentThread());
Assert.assertTrue(tableList.get(1).isWriteLockHeldByCurrentThread());
MetaLockUtils.writeUnlockTables(tableList);
Assert.assertFalse(table1.isWriteLockHeldByCurrentThread());
Assert.assertFalse(table2.isWriteLockHeldByCurrentThread());
table2.markDropped();
Assert.assertFalse(tableList.get(0).isWriteLockHeldByCurrentThread());
Assert.assertFalse(tableList.get(1).isWriteLockHeldByCurrentThread());
tableList.get(1).markDropped();
expectedException.expect(MetaNotFoundException.class);
expectedException.expectMessage("errCode = 7, detailMessage = unknown table, tableName=test2");
try {
MetaLockUtils.tryWriteLockTablesOrMetaException(tableList, 1000, TimeUnit.MILLISECONDS);
} finally {
Assert.assertFalse(table1.isWriteLockHeldByCurrentThread());
Assert.assertFalse(table2.isWriteLockHeldByCurrentThread());
Assert.assertFalse(tableList.get(0).isWriteLockHeldByCurrentThread());
Assert.assertFalse(tableList.get(1).isWriteLockHeldByCurrentThread());
}
}
}

View File

@ -51,7 +51,7 @@ public class RewriteTopDownJobTest {
@Override
public Rule build() {
return unboundRelation().then(unboundRelation ->
new LogicalBoundRelation(PlanConstructor.newTable(0L, "test"), Lists.newArrayList("test"))
new LogicalBoundRelation(PlanConstructor.newOlapTable(0L, "test", 0), Lists.newArrayList("test"))
).toRule(RuleType.BINDING_RELATION);
}
}
@ -59,7 +59,7 @@ public class RewriteTopDownJobTest {
@Test
public void testSimplestScene() {
Plan leaf = new UnboundRelation(Lists.newArrayList("test"));
LogicalProject project = new LogicalProject(ImmutableList.of(
LogicalProject<Plan> project = new LogicalProject<>(ImmutableList.of(
new SlotReference("name", StringType.INSTANCE, true, ImmutableList.of("test"))),
leaf
);

View File

@ -22,7 +22,6 @@ import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.HashDistributionInfo;
import org.apache.doris.catalog.KeysType;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.Table;
import org.apache.doris.catalog.Type;
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
import org.apache.doris.thrift.TStorageType;
@ -59,18 +58,18 @@ public class PlanConstructor {
0, 0, (short) 0,
TStorageType.COLUMN,
KeysType.PRIMARY_KEYS);
course.setIndexMeta(-1,
"base",
course.getFullSchema(),
0, 0, (short) 0,
TStorageType.COLUMN,
KeysType.PRIMARY_KEYS);
score.setIndexMeta(-1,
"base",
score.getFullSchema(),
0, 0, (short) 0,
TStorageType.COLUMN,
KeysType.PRIMARY_KEYS);
course.setIndexMeta(-1,
"base",
course.getFullSchema(),
0, 0, (short) 0,
TStorageType.COLUMN,
KeysType.PRIMARY_KEYS);
}
public static OlapTable newOlapTable(long tableId, String tableName, int hashColumn) {
@ -92,14 +91,6 @@ public class PlanConstructor {
return table;
}
public static Table newTable(long tableId, String tableName) {
return new Table(tableId, tableName, Table.TableType.OLAP,
ImmutableList.<Column>of(
new Column("id", Type.INT, true, AggregateType.NONE, "0", ""),
new Column("name", Type.STRING, true, AggregateType.NONE, "", "")
));
}
// With OlapTable.
// Warning: equals() of Table depends on tableId.
public static LogicalOlapScan newLogicalOlapScan(long tableId, String tableName, int hashColumn) {

View File

@ -31,15 +31,16 @@ import org.apache.doris.analysis.TupleDescriptor;
import org.apache.doris.analysis.TupleId;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.Env;
import org.apache.doris.catalog.OlapTable;
import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.catalog.Table;
import org.apache.doris.catalog.TableTest;
import org.apache.doris.common.UserException;
import org.apache.doris.common.jmockit.Deencapsulation;
import org.apache.doris.datasource.InternalDataSource;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.thrift.TPartitionType;
import com.google.common.collect.Lists;
import com.google.common.collect.ImmutableList;
import mockit.Expectations;
import mockit.Mocked;
import org.junit.Assert;
@ -76,12 +77,11 @@ public class RuntimeFilterGeneratorTest {
TableName lhsTableName = new TableName(InternalDataSource.INTERNAL_DS_NAME, "default_cluster:test_db", "test_lhs_tbl");
SlotRef lhsExpr = new SlotRef(lhsTableName, "test_lhs_col");
SlotDescriptor lhsSlotDescriptor = new SlotDescriptor(new SlotId(0), lhsTupleDescriptor);
Column k1 = new Column("test_lhs_col", PrimitiveType.BIGINT);
Column k1 = new Column("test_lhs_col", PrimitiveType.BIGINT, false);
k1.setIsKey(true);
k1.setIsAllowNull(false);
lhsSlotDescriptor.setColumn(k1);
lhsExpr.setDesc(lhsSlotDescriptor);
Table lhsTable = new Table(0, "test_lhs_tbl", Table.TableType.OLAP, Lists.newArrayList(k1));
OlapTable lhsTable = TableTest.newOlapTable(0, "test_lhs_tbl", 0, ImmutableList.of(k1));
BaseTableRef lhsTableRef = new BaseTableRef(tableRef, lhsTable, lhsTableName);
lhsTableRef.analyze(analyzer);
@ -90,12 +90,11 @@ public class RuntimeFilterGeneratorTest {
TableName rhsTableName = new TableName(InternalDataSource.INTERNAL_DS_NAME, "default_cluster:test_db", "test_rhs_tbl");
SlotRef rhsExpr = new SlotRef(rhsTableName, "test_rhs_col");
SlotDescriptor rhsSlotDescriptor = new SlotDescriptor(new SlotId(1), rhsTupleDescriptor);
Column k2 = new Column("test_rhs_col", PrimitiveType.INT);
Column k2 = new Column("test_rhs_col", PrimitiveType.INT, false);
k2.setIsKey(true);
k2.setIsAllowNull(false);
rhsSlotDescriptor.setColumn(k2);
rhsExpr.setDesc(rhsSlotDescriptor);
Table rhsTable = new Table(0, "test_rhs_tbl", Table.TableType.OLAP, Lists.newArrayList(k2));
OlapTable rhsTable = TableTest.newOlapTable(0, "test_rhs_tbl", 0, ImmutableList.of(k2));
BaseTableRef rhsTableRef = new BaseTableRef(tableRef, rhsTable, rhsTableName);
rhsTableRef.analyze(analyzer);