Fix bug that user can set null default value to non-nullable column in create table stmt (#1453)

In create table stmt, column definition `k1 INT NOT NULL DEFAULT NULL`
should not be allowed
This commit is contained in:
Mingyu Chen
2019-07-10 23:48:29 +08:00
committed by ZHAO Chun
parent 98bd4b4565
commit 9c96a688c3
5 changed files with 69 additions and 30 deletions

View File

@ -25,6 +25,7 @@ import org.apache.doris.analysis.AddColumnClause;
import org.apache.doris.analysis.AlterClause;
import org.apache.doris.analysis.Analyzer;
import org.apache.doris.analysis.ColumnDef;
import org.apache.doris.analysis.ColumnDef.DefaultValue;
import org.apache.doris.analysis.ColumnPosition;
import org.apache.doris.analysis.TypeDef;
import org.apache.doris.catalog.AggregateType;
@ -84,7 +85,7 @@ public class SchemaChangeJobTest {
private String transactionSource = "localfe";
private static Analyzer analyzer;
private static ColumnDef newCol = new ColumnDef("add_v", new TypeDef(ScalarType.createType(PrimitiveType.INT)), false, AggregateType.MAX,
false, "1", "");
false, new DefaultValue(true, "1"), "");
private static AddColumnClause addColumnClause = new AddColumnClause(newCol, new ColumnPosition("v"), null, null);
@Before

View File

@ -17,9 +17,9 @@
package org.apache.doris.analysis;
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.analysis.ColumnDef.DefaultValue;
import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.AnalysisException;
import com.google.common.collect.Lists;
@ -42,9 +42,10 @@ public class AddColumnsClauseTest {
public void testNormal() throws AnalysisException {
List<ColumnDef> columns = Lists.newArrayList();
ColumnDef definition = new ColumnDef("col1", new TypeDef(ScalarType.createType(PrimitiveType.INT)),
true, null, false,"0", "");
true, null, false, new DefaultValue(true, "0"), "");
columns.add(definition);
definition = new ColumnDef("col2", new TypeDef(ScalarType.createType(PrimitiveType.INT)), true, null, false, "0", "");
definition = new ColumnDef("col2", new TypeDef(ScalarType.createType(PrimitiveType.INT)), true, null, false,
new DefaultValue(true, "0"), "");
columns.add(definition);
AddColumnsClause clause = new AddColumnsClause(columns, null, null);
clause.analyze(analyzer);

View File

@ -17,15 +17,16 @@
package org.apache.doris.analysis;
import org.apache.doris.analysis.ColumnDef.DefaultValue;
import org.apache.doris.catalog.AggregateType;
import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.common.AnalysisException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.apache.doris.catalog.AggregateType;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.PrimitiveType;
import org.apache.doris.common.AnalysisException;
public class ColumnDefTest {
private TypeDef intCol;
private TypeDef stringCol;
@ -50,14 +51,14 @@ public class ColumnDefTest {
Assert.assertNull(column.getDefaultValue());
// default
column = new ColumnDef("col", intCol, true, null, false, "10", "");
column = new ColumnDef("col", intCol, true, null, false, new DefaultValue(true, "10"), "");
column.analyze(true);
Assert.assertNull(column.getAggregateType());
Assert.assertEquals("10", column.getDefaultValue());
Assert.assertEquals("`col` int(11) NOT NULL DEFAULT \"10\" COMMENT \"\"", column.toSql());
// agg
column = new ColumnDef("col", floatCol, false, AggregateType.SUM, false, "10", "");
column = new ColumnDef("col", floatCol, false, AggregateType.SUM, false, new DefaultValue(true, "10"), "");
column.analyze(true);
Assert.assertEquals("10", column.getDefaultValue());
Assert.assertEquals(AggregateType.SUM, column.getAggregateType());
@ -73,7 +74,7 @@ public class ColumnDefTest {
@Test(expected = AnalysisException.class)
public void testStrSum() throws AnalysisException {
ColumnDef column = new ColumnDef("col", stringCol, false, AggregateType.SUM, true, null, "");
ColumnDef column = new ColumnDef("col", stringCol, false, AggregateType.SUM, true, DefaultValue.NOT_SET, "");
column.analyze(true);
}