Skip to content

Commit

Permalink
[fix](Nereids) fix insert into table with null literal default value (a…
Browse files Browse the repository at this point in the history
…pache#39122)

Problem:
when use insert with default value null, it can not be insert
successfully
Solved:
when column is allow to be null, it can be null in create table with
null default value
  • Loading branch information
LiBinfeng-01 committed Aug 23, 2024
1 parent f894a6b commit 160d83b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -853,11 +853,15 @@ private void analyzeRow(Analyzer analyzer, List<Column> targetColumns, List<Arra
Column col = targetColumns.get(i);

if (expr instanceof DefaultValueExpr) {
if (targetColumns.get(i).getDefaultValue() == null) {
if (targetColumns.get(i).getDefaultValue() == null && !targetColumns.get(i).isAllowNull()) {
throw new AnalysisException("Column has no default value, column="
+ targetColumns.get(i).getName());
}
expr = new StringLiteral(targetColumns.get(i).getDefaultValue());
if (targetColumns.get(i).getDefaultValue() == null) {
expr = new NullLiteral();
} else {
expr = new StringLiteral(targetColumns.get(i).getDefaultValue());
}
}
if (expr instanceof Subquery) {
throw new AnalysisException("Insert values can not be query");
Expand Down
3 changes: 3 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/catalog/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ public String getDefaultValue() {
}

public Expr getDefaultValueExpr() throws AnalysisException {
if (defaultValue == null) {
return null;
}
StringLiteral defaultValueLiteral = new StringLiteral(defaultValue);
if (getDataType() == PrimitiveType.VARCHAR) {
return defaultValueLiteral;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ public List<Rule> buildRules() {
// should not contain these unmentioned columns, so we just skip them.
continue;
} else if (column.getDefaultValue() == null) {
// throw exception if explicitly use Default value null when not nullable
// insert into table t values(DEFAULT)
if (!column.isAllowNull()) {
throw new AnalysisException("Column has no default value,"
+ " column=" + column.getName());
}
// Otherwise, the unmentioned columns should be filled with default values
// or null values
columnToOutput.put(column.getName(), new Alias(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public void testScalarSubQuery() {

@Test
public void testInsertInto() {
String sql = "INSERT INTO supplier(s_suppkey) SELECT lo_orderkey FROM lineorder";
String sql = "INSERT INTO supplier(s_suppkey, s_name, s_address, s_city, s_nation, s_region, s_phone) "
+ "SELECT lo_orderkey, '', '', '', '', '', '' FROM lineorder";
StatementContext statementContext = MemoTestUtils.createStatementContext(connectContext, sql);
boolean originalDML = connectContext.getSessionVariable().enableNereidsDML;
connectContext.getSessionVariable().enableNereidsDML = true;
Expand Down

0 comments on commit 160d83b

Please sign in to comment.