提交 9903ea97 编写于 作者: T Timo Walther

[FLINK-18090][core][table] Update Row#toString and provide legacy util

This updates the Row.toString method to provide a good summary string.

In particular it fixes the following issues:

Changeflag: According to FLIP-95, a row describes an entry in a
changelog. Therefore, it should visible whether a row is an insert,
delete, or update change. Now indicated with +I, -D, +U, -U.

Nested rows: In the old implementation it was not visible whether nested
rows exist or not due to missing start/end boundaries. Now indicated with
[...] or {...}.

Positioned rows vs. named rows: According to FLIP-136, it should be visible
whether a row operates in name-based or position-based field mode. Now
indicated with [...] or {...}.

Nested arrays in maps and lists: In the old implementation arrays in maps
or lists could not be represented.

Wrong formatting: Most programming languages use a space after a comma.

This is an incompatible change. If the legacy representation is still
required for tests, the old behavior can be restored via the flag
RowUtils.USE_LEGACY_TO_STRING for the local JVM. However, relying on
the row's string representation for tests is not a good idea in general as
field data types are not verified.
上级 9720e563
......@@ -74,6 +74,14 @@ public final class RowUtils {
// Internal utilities
// --------------------------------------------------------------------------------------------
/**
* Internal flag to enable the legacy {@link Row#toString()} implementation for tests. In
* general, tests should not depend on the string representation of rows but should fully
* compare instances (especially data types of fields). This flag will be dropped once all tests
* have been updated.
*/
public static boolean USE_LEGACY_TO_STRING = false;
/** Internal utility for creating a row in static named-position field mode. */
@Internal
public static Row createRowWithNamedPositions(
......@@ -149,10 +157,12 @@ public final class RowUtils {
@Nullable Map<String, Object> fieldByName) {
final StringBuilder sb = new StringBuilder();
if (fieldByPosition != null) {
// TODO enable this for FLINK-18090
// sb.append(kind.shortString());
// deepToStringArray(sb, fieldByPosition);
deepToStringArrayLegacy(sb, fieldByPosition);
if (USE_LEGACY_TO_STRING) {
deepToStringArrayLegacy(sb, fieldByPosition);
} else {
sb.append(kind.shortString());
deepToStringArray(sb, fieldByPosition);
}
} else {
assert fieldByName != null;
sb.append(kind.shortString());
......
......@@ -124,9 +124,7 @@ public class RowTest {
assertThat(row.getField(2), equalTo(null));
// test toString
// TODO enable this for FLINK-18090
// assertThat(row.toString(), equalTo("-D[42, true, null]"));
assertThat(row.toString(), equalTo("42,true,null"));
assertThat(row.toString(), equalTo("-D[42, true, null]"));
// test override
row.setField(0, 13);
......@@ -155,9 +153,7 @@ public class RowTest {
row.clear();
assertThat(row.getArity(), equalTo(3));
assertThat(row.getFieldNames(false), equalTo(null));
// TODO enable this for FLINK-18090
// assertThat(row.toString(), equalTo("-D[null, null, null]"));
assertThat(row.toString(), equalTo("null,null,null"));
assertThat(row.toString(), equalTo("-D[null, null, null]"));
// test invalid setter
try {
......@@ -198,9 +194,7 @@ public class RowTest {
assertThat(row.getField("c"), equalTo(null));
// test toString
// TODO enable this for FLINK-18090
// assertThat(row.toString(), equalTo("-D[42, true, null]"));
assertThat(row.toString(), equalTo("42,true,null"));
assertThat(row.toString(), equalTo("-D[42, true, null]"));
// test override
row.setField("a", 13);
......@@ -229,9 +223,7 @@ public class RowTest {
row.clear();
assertThat(row.getArity(), equalTo(3));
assertThat(row.getFieldNames(true), contains("a", "b", "c"));
// TODO enable this for FLINK-18090
// assertThat(row.toString(), equalTo("-D[null, null, null]"));
assertThat(row.toString(), equalTo("null,null,null"));
assertThat(row.toString(), equalTo("-D[null, null, null]"));
// test invalid setter
try {
......
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.flink.table.utils;
import org.apache.flink.types.Row;
import org.apache.flink.types.RowUtils;
import org.junit.rules.ExternalResource;
/**
* Enables the old behavior of {@link Row#toString()} for legacy tests.
*
* <p>It ignores the changes applied in FLINK-16998 and FLINK-19981.
*/
public class LegacyRowResource extends ExternalResource {
public static final LegacyRowResource INSTANCE = new LegacyRowResource();
private LegacyRowResource() {
// singleton
}
@Override
protected void before() {
RowUtils.USE_LEGACY_TO_STRING = true;
}
@Override
protected void after() {
RowUtils.USE_LEGACY_TO_STRING = false;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册