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 @@ -265,6 +265,9 @@ private static Object defaultValueOf(
if (fullType instanceof RowType && !fullType.equals(type)) {
InternalRow fullDefault =
(InternalRow) convertDefaultValue(fullType, defaultValueStr);
if (fullDefault == null) {
return null;
}
NestedProjectedRow projected =
NestedProjectedRow.create((RowType) fullType, (RowType) type);
return new InternalRowSerializer((RowType) type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ private InternalArray parseArray(
BinaryString value, CastExecutor<BinaryString, Object> elementCastExecutor) {
try {
String str = value.toString().trim();
if ("NULL".equalsIgnoreCase(str)) {
return null;
}
if ("[]".equals(str) || "ARRAY()".equalsIgnoreCase(str)) {
return new GenericArray(new Object[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ private InternalRow parseRow(
int fieldCount) {
try {
String str = value.toString().trim();
if ("NULL".equalsIgnoreCase(str)) {
return null;
}
if ("{}".equals(str) || "STRUCT()".equalsIgnoreCase(str)) {
return createNullRow(fieldCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import org.apache.paimon.utils.DecimalUtils;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.time.LocalDateTime;
import java.util.Arrays;
Expand Down Expand Up @@ -641,6 +643,74 @@ public void testStringToArray() {
assertThat(result.getInt(2)).isEqualTo(3);
}

@ParameterizedTest
@ValueSource(strings = {"null", "NULL", "NuLl"})
public void testStringToArrayNullLiterals(String literal) {
CastExecutor<?, ?> cast =
CastExecutors.resolve(VarCharType.STRING_TYPE, DataTypes.ARRAY(DataTypes.INT()));
compareCastResult(cast, BinaryString.fromString(" " + literal + " "), null);
}

@ParameterizedTest
@ValueSource(strings = {"null", "NULL", "NuLl"})
public void testStringToMapNullLiterals(String literal) {
CastExecutor<?, ?> cast =
CastExecutors.resolve(
VarCharType.STRING_TYPE, DataTypes.MAP(DataTypes.INT(), DataTypes.INT()));
compareCastResult(cast, BinaryString.fromString(" " + literal + " "), null);
}

@ParameterizedTest
@ValueSource(strings = {"null", "NULL", "NuLl"})
public void testStringToRowNullLiterals(String literal) {
CastExecutor<?, ?> cast =
CastExecutors.resolve(
VarCharType.STRING_TYPE, RowType.of(DataTypes.INT(), DataTypes.INT()));
compareCastResult(cast, BinaryString.fromString(" " + literal + " "), null);
}

@Test
public void testOnlyUnquotedLowercaseNullIsNullToken() {
// Persisted defaults such as [NULL] have always meant the four-character string.
for (String literal : Arrays.asList("NULL", "NuLl")) {
BinaryString value = BinaryString.fromString(literal);
String fields = "\"" + literal + "\", \\" + literal + ", " + literal + ", null";
compareCastResult(
CastExecutors.resolve(
VarCharType.STRING_TYPE, DataTypes.ARRAY(DataTypes.STRING())),
BinaryString.fromString("ARRAY(" + fields + ")"),
new GenericArray(new Object[] {value, value, value, null}));
compareCastResult(
CastExecutors.resolve(
VarCharType.STRING_TYPE,
RowType.of(
DataTypes.STRING(),
DataTypes.STRING(),
DataTypes.STRING(),
DataTypes.STRING())),
BinaryString.fromString("STRUCT(" + fields + ")"),
GenericRow.of(value, value, value, null));
Map<Object, Object> expected = new HashMap<>();
expected.put(1, value);
expected.put(2, value);
expected.put(3, value);
expected.put(4, null);
compareCastResult(
CastExecutors.resolve(
VarCharType.STRING_TYPE,
DataTypes.MAP(DataTypes.INT(), DataTypes.STRING())),
BinaryString.fromString(
"MAP(1, \""
+ literal
+ "\", 2, \\"
+ literal
+ ", 3, "
+ literal
+ ", 4, null)"),
new GenericMap(expected));
}
}

@Test
public void testStringToMap() {
// Test string to map<string, int>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ public void testDefaultValueRowProjectsRowDefaultToWriteType() {
assertThat(nest.getString(0).toString()).isEqualTo("z");
}

@Test
public void testNullRowDefaultProjectsToWriteType() {
RowType fullType =
RowType.of(
new DataField(
0,
"nest",
RowType.of(
new DataField(1, "a", DataTypes.INT()),
new DataField(2, "b", DataTypes.STRING())),
null,
"NULL"));
RowType writeType = fullType.projectByPaths(Collections.singletonList("nest.b"));

DefaultValueRow defaultValueRow = DefaultValueRow.create(writeType, fullType);
InternalRow row = defaultValueRow.replaceRow(GenericRow.of((Object) null));
assertThat(row.isNullAt(0)).isTrue();
assertThat(row.getRow(0, 1)).isNull();
}

@Test
public void testDefaultValueRowWithRowType() {
// Test with Row default value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
import static org.apache.paimon.spark.utils.CatalogUtils.isUpdateColumnDefaultValue;
import static org.apache.paimon.spark.utils.CatalogUtils.removeCatalogName;
import static org.apache.paimon.spark.utils.CatalogUtils.toIdentifier;
import static org.apache.paimon.spark.utils.CatalogUtils.toPaimonDefaultValue;
import static org.apache.paimon.spark.utils.CatalogUtils.toUpdateColumnDefaultValue;
import static org.apache.paimon.utils.Preconditions.checkArgument;

Expand Down Expand Up @@ -783,7 +784,8 @@ private Schema toInitialSchema(
type,
comment,
field.metadata().contains(CURRENT_DEFAULT_COLUMN_METADATA_KEY)
? field.metadata().getString(CURRENT_DEFAULT_COLUMN_METADATA_KEY)
? toPaimonDefaultValue(
field.metadata().getString(CURRENT_DEFAULT_COLUMN_METADATA_KEY))
: null,
generationExpression);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.apache.spark.sql.types.LongType;
import org.apache.spark.sql.types.ShortType;

import javax.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -279,6 +281,53 @@ public static boolean isUpdateColumnDefaultValue(TableChange tableChange) {
public static SchemaChange toUpdateColumnDefaultValue(TableChange tableChange) {
TableChange.UpdateColumnDefaultValue update =
(TableChange.UpdateColumnDefaultValue) tableChange;
return SchemaChange.updateColumnDefaultValue(update.fieldNames(), update.newDefaultValue());
return SchemaChange.updateColumnDefaultValue(
update.fieldNames(), toPaimonDefaultValue(update.newDefaultValue()));
}

/**
* Converts a SQL default value to Paimon's default value literal. Paimon reads only an unquoted
* lowercase {@code null} inside an array, map or row literal as a null element, key, value or
* field, so the case-insensitive SQL keyword is lowercased there. Elsewhere the text is kept.
*/
@Nullable
public static String toPaimonDefaultValue(@Nullable String sql) {
if (sql == null) {
return null;
}
StringBuilder result = new StringBuilder(sql.length());
int depth = 0;
char quote = 0;
for (int i = 0; i < sql.length(); i++) {
char c = sql.charAt(i);
if (quote != 0) {
result.append(c);
if (c == '\\' && quote != '`' && i + 1 < sql.length()) {
result.append(sql.charAt(++i));
} else if (c == quote) {
quote = 0;
}
} else if (c == '\'' || c == '"' || c == '`') {
quote = c;
result.append(c);
} else if (Character.isLetterOrDigit(c) || c == '_') {
int end = i + 1;
while (end < sql.length()
&& (Character.isLetterOrDigit(sql.charAt(end)) || sql.charAt(end) == '_')) {
end++;
}
String word = sql.substring(i, end);
result.append(depth > 0 && "null".equalsIgnoreCase(word) ? "null" : word);
i = end - 1;
} else {
if (c == '(' || c == '[' || c == '{') {
depth++;
} else if (c == ')' || c == ']' || c == '}') {
depth--;
}
result.append(c);
}
}
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,47 @@ import org.apache.spark.sql.Row

class DefaultValueTest extends PaimonSparkTestBase {

test("Default Value: NULL elements in an array") {
withTable("t") {
sql("CREATE TABLE t (id INT, a ARRAY<INT> DEFAULT ARRAY(1, NULL))")
sql("INSERT INTO t (id) VALUES (1)")
checkAnswer(sql("SELECT a FROM t"), Seq(Row(Seq(1, null))))
}
}

test("Default Value: NULL map value") {
withTable("t") {
sql("CREATE TABLE t (id INT, m MAP<INT, INT> DEFAULT MAP(1, NULL))")
sql("INSERT INTO t (id) VALUES (1)")
checkAnswer(sql("SELECT m FROM t"), Seq(Row(Map(1 -> null))))
}
}

test("Default Value: NULL field in a struct") {
withTable("t") {
sql("CREATE TABLE t (id INT, r STRUCT<a: INT, b: INT> DEFAULT STRUCT(1, NULL))")
sql("INSERT INTO t (id) VALUES (1)")
checkAnswer(sql("SELECT r FROM t"), Seq(Row(Row(1, null))))
}
}

test("Default Value: NULL element set by ALTER COLUMN") {
withTable("t") {
sql("CREATE TABLE t (id INT, a ARRAY<STRING>)")
sql("ALTER TABLE t ALTER COLUMN a SET DEFAULT ARRAY(\"x\", NULL)")
sql("INSERT INTO t (id) VALUES (1)")
checkAnswer(sql("SELECT a FROM t"), Seq(Row(Seq("x", null))))
}
}

test("Default Value: NULL array and struct") {
withTable("t") {
sql("CREATE TABLE t (id INT, a ARRAY<INT> DEFAULT NULL, r STRUCT<b: INT> DEFAULT NULL)")
sql("INSERT INTO t (id) VALUES (1)")
checkAnswer(sql("SELECT a, r FROM t"), Seq(Row(null, null)))
}
}

test("Default Value: MAP TYPE") {
withTable("t") {
spark.sql("""CREATE TABLE IF NOT EXISTS t (
Expand Down
Loading