Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -423,27 +423,12 @@ public void validate(boolean isOlap, Set<String> keysSet, Set<String> clusterKey
+ DefaultValue.BITMAP_EMPTY_DEFAULT_VALUE);
}
defaultValue = Optional.of(DefaultValue.BITMAP_EMPTY_DEFAULT_VALUE);
} else if (type.isArrayType() && defaultValue.isPresent() && isOlap
&& defaultValue.get() != DefaultValue.NULL_DEFAULT_VALUE && !defaultValue.get()
.getValue().equals(DefaultValue.ARRAY_EMPTY_DEFAULT_VALUE.getValue())) {
throw new AnalysisException("Array type column default value only support null or "
+ DefaultValue.ARRAY_EMPTY_DEFAULT_VALUE);
} else if (type.isMapType()) {
if (defaultValue.isPresent() && defaultValue.get() != DefaultValue.NULL_DEFAULT_VALUE) {
throw new AnalysisException("Map type column default value just support null");
}
} else if (type.isStructType()) {
if (defaultValue.isPresent() && defaultValue.get() != DefaultValue.NULL_DEFAULT_VALUE) {
throw new AnalysisException("Struct type column default value just support null");
}
} else if (type.isJsonType() || type.isVariantType()) {
if (defaultValue.isPresent() && defaultValue.get() != DefaultValue.NULL_DEFAULT_VALUE) {
throw new AnalysisException("Json or Variant type column default value just support null");
}
} else if ((type.isComplexType() || type.isJsonType() || type.isVariantType())
&& hasNonNullDefaultValue()) {
throw new AnalysisException("Complex type column " + name + " only supports DEFAULT NULL");
}

if (!isNullable && defaultValue.isPresent()
&& defaultValue.get() == DefaultValue.NULL_DEFAULT_VALUE) {
if (!isNullable && hasNullDefaultValue()) {
throw new AnalysisException(
"Can not set null default value to non nullable column: " + name);
}
Expand Down Expand Up @@ -521,6 +506,14 @@ public void validate(boolean isOlap, Set<String> keysSet, Set<String> clusterKey
validateGeneratedColumnInfo();
}

private boolean hasNonNullDefaultValue() {
return defaultValue.isPresent() && defaultValue.get().getValue() != null;
}

private boolean hasNullDefaultValue() {
return defaultValue.isPresent() && defaultValue.get().getValue() == null;
}

/**
* translate to catalog create table stmt
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public class DefaultValue {
public static DefaultValue HLL_EMPTY_DEFAULT_VALUE = new DefaultValue(ZERO, HLL_EMPTY);
// default "value", "0" means empty bitmap
public static DefaultValue BITMAP_EMPTY_DEFAULT_VALUE = new DefaultValue(ZERO, BITMAP_EMPTY);
// default "value", "[]" means empty array
public static DefaultValue ARRAY_EMPTY_DEFAULT_VALUE = new DefaultValue("[]");
// Lets use the const value from math pacakge.
public static DefaultValue PI_DEFAULT_VALUE = new DefaultValue(Double.toString(Math.PI), PI);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,24 @@

package org.apache.doris.nereids.trees.plans.commands.info;

import org.apache.doris.catalog.KeysType;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.types.ArrayType;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.JsonType;
import org.apache.doris.nereids.types.MapType;
import org.apache.doris.nereids.types.StringType;
import org.apache.doris.nereids.types.StructField;
import org.apache.doris.nereids.types.StructType;
import org.apache.doris.nereids.types.VariantType;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Optional;

public class ColumnDefinitionTest {

@Test
Expand All @@ -33,4 +48,32 @@ public void testNameEquals() {
boolean expected2 = false;
Assertions.assertEquals(expected2, columnDefinition.nameEquals(otherColName2, false));
}

@Test
public void testComplexTypeOnlySupportsNullDefaultValue() {
assertRejectsNonNullDefaultValue(ArrayType.of(IntegerType.INSTANCE), "[]");
assertRejectsNonNullDefaultValue(MapType.of(StringType.INSTANCE, IntegerType.INSTANCE), "{}");
assertRejectsNonNullDefaultValue(
new StructType(Collections.singletonList(new StructField("f", IntegerType.INSTANCE, true, ""))), "{}");
assertRejectsNonNullDefaultValue(JsonType.INSTANCE, "{}");
assertRejectsNonNullDefaultValue(VariantType.INSTANCE, "{}");

Assertions.assertDoesNotThrow(() -> newColumnDefinition(
ArrayType.of(IntegerType.INSTANCE), Optional.empty()).validate(
true, Collections.emptySet(), Collections.emptySet(), false, KeysType.DUP_KEYS));
Assertions.assertDoesNotThrow(() -> newColumnDefinition(
ArrayType.of(IntegerType.INSTANCE), Optional.of(DefaultValue.NULL_DEFAULT_VALUE)).validate(
true, Collections.emptySet(), Collections.emptySet(), false, KeysType.DUP_KEYS));
}

private void assertRejectsNonNullDefaultValue(DataType type, String defaultValue) {
ColumnDefinition columnDefinition = newColumnDefinition(type, Optional.of(new DefaultValue(defaultValue)));
AnalysisException exception = Assertions.assertThrows(AnalysisException.class, () -> columnDefinition.validate(
true, Collections.emptySet(), Collections.emptySet(), false, KeysType.DUP_KEYS));
Assertions.assertTrue(exception.getMessage().contains("only supports DEFAULT NULL"));
}

private ColumnDefinition newColumnDefinition(DataType type, Optional<DefaultValue> defaultValue) {
return new ColumnDefinition("col1", type, false, null, true, defaultValue, "");
}
}
Loading
Loading