Skip to content

Commit

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

cherry-pick: #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

## Proposed changes

Issue Number: close #xxx

<!--Describe your changes.-->
  • Loading branch information
LiBinfeng-01 authored Aug 22, 2024
1 parent 8f580b5 commit ca9e50e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -920,11 +920,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 @@ -498,6 +498,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 @@ -344,7 +344,7 @@ private static Map<String, NamedExpression> getColumnToOutput(
} else if (column.getDefaultValue() == null) {
// throw exception if explicitly use Default value but no default value present
// insert into table t values(DEFAULT)
if (columnToChildOutput.get(column) instanceof DefaultValueSlot) {
if (columnToChildOutput.get(column) instanceof DefaultValueSlot && !column.isAllowNull()) {
throw new AnalysisException("Column has no default value,"
+ " column=" + column.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ public static TableIf getTargetTable(Plan plan, ConnectContext ctx) {
private static NamedExpression generateDefaultExpression(Column column) {
try {
if (column.getDefaultValue() == null) {
throw new AnalysisException("Column has no default value, column=" + column.getName());
if (!column.isAllowNull()) {
throw new AnalysisException("Column has no default value, column=" + column.getName());
}
}
if (column.getDefaultValueExpr() != null) {
Expression defualtValueExpression = new NereidsParser().parseExpression(
Expand Down

0 comments on commit ca9e50e

Please sign in to comment.