Skip to content

Commit d76ace9

Browse files
committed
[#6136] improvement(CLI): Move check for enable and disable command in Gravitino CLI to command
fix some bugs.
1 parent 0dc630f commit d76ace9

File tree

6 files changed

+398
-38
lines changed

6 files changed

+398
-38
lines changed

clients/cli/src/main/java/org/apache/gravitino/cli/outputs/LineUtil.java

+70-6
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
import org.apache.gravitino.rel.expressions.Expression;
2626
import org.apache.gravitino.rel.expressions.FunctionExpression;
2727
import org.apache.gravitino.rel.expressions.literals.Literal;
28+
import org.apache.gravitino.rel.types.Type;
2829

2930
public class LineUtil {
3031
// This expression is primarily used to match characters that have a display width of
3132
// 2, such as characters from Korean, Chinese
3233
private static final Pattern FULL_WIDTH_PATTERN =
3334
Pattern.compile(
3435
"[\u1100-\u115F\u2E80-\uA4CF\uAC00-\uD7A3\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE6F\uFF00-\uFF60\uFFE0-\uFFE6]");
36+
public static final String EMPTY_DEFAULT_VALUE = "";
37+
public static final String EMPTY_STRING_TYPE_DEFAULT_VALUE = "''";
3538

3639
/**
3740
* Get the display width of a string.
@@ -106,25 +109,86 @@ public static String capitalize(String str) {
106109
}
107110

108111
/**
109-
* Get the default value of a column.
112+
* Get the default value of a column. if the column has a default value, return it as a string. if
113+
* the column does not set a default value, return {@link #EMPTY_DEFAULT_VALUE}. if the column is
114+
* of string type and the default value is an empty string, return {@link
115+
* #EMPTY_STRING_TYPE_DEFAULT_VALUE}
110116
*
111-
* @param defaultValue the default value expression.
117+
* @param column the column to get.
112118
* @return the default value as a string.
113119
*/
114-
public static String getDefaultValue(Expression defaultValue) {
120+
public static String getDefaultValue(org.apache.gravitino.rel.Column column) {
121+
Expression defaultValue = column.defaultValue();
115122
if (defaultValue == null
116123
|| defaultValue == org.apache.gravitino.rel.Column.DEFAULT_VALUE_NOT_SET) {
117-
return "N/A";
124+
return EMPTY_DEFAULT_VALUE;
118125
}
119126

120127
if (defaultValue instanceof Literal && ((Literal<?>) defaultValue).value() != null) {
121-
return ((Literal<?>) defaultValue).value().toString();
128+
String defaultValueStr = ((Literal<?>) defaultValue).value().toString().trim();
129+
if ("".equalsIgnoreCase(defaultValueStr) && isStringType(column)) {
130+
return EMPTY_STRING_TYPE_DEFAULT_VALUE;
131+
}
132+
return defaultValueStr;
122133
} else if (defaultValue instanceof FunctionExpression) {
123134
return defaultValue.toString();
124135
} else if (defaultValue.references().length == 0) {
125-
return "N/A";
136+
return EMPTY_DEFAULT_VALUE;
126137
}
127138

128139
return Arrays.toString(defaultValue.references());
129140
}
141+
142+
/**
143+
* If the column is of integer type, return whether it is auto-incremented, otherwise return an
144+
* empty string.
145+
*
146+
* @param column the column to check.
147+
* @return if the column is of integer type and auto-incremented, return "true", otherwise return
148+
* an empty string.
149+
*/
150+
public static String getAutoIncrement(org.apache.gravitino.rel.Column column) {
151+
if (isIntegerType(column)) {
152+
return column.autoIncrement() ? "true" : "false";
153+
}
154+
155+
return "";
156+
}
157+
158+
/**
159+
* Check if a column is of integer type.
160+
*
161+
* @param column the column to check.
162+
* @return true if the column is of integer type, false otherwise.
163+
*/
164+
public static boolean isIntegerType(org.apache.gravitino.rel.Column column) {
165+
Type.Name columnTypeName = column.dataType().name();
166+
return columnTypeName == Type.Name.LONG
167+
|| columnTypeName == Type.Name.INTEGER
168+
|| columnTypeName == Type.Name.SHORT
169+
|| columnTypeName == Type.Name.BYTE;
170+
}
171+
172+
/**
173+
* Check if a column is of string type.
174+
*
175+
* @param column the column to check.
176+
* @return true if the column is of string type, false otherwise.
177+
*/
178+
public static boolean isStringType(org.apache.gravitino.rel.Column column) {
179+
Type.Name columnTypeName = column.dataType().name();
180+
return columnTypeName == Type.Name.STRING
181+
|| columnTypeName == Type.Name.VARCHAR
182+
|| columnTypeName == Type.Name.FIXEDCHAR;
183+
}
184+
185+
/**
186+
* Get the comment of a column. If the column does not have a comment, return "N/A".
187+
*
188+
* @param column the column to get.
189+
* @return the comment of the column.
190+
*/
191+
public static String getComment(org.apache.gravitino.rel.Column column) {
192+
return column.comment() == null ? "N/A" : column.comment();
193+
}
130194
}

clients/cli/src/main/java/org/apache/gravitino/cli/outputs/PlainFormat.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ public String getOutput(Column[] columns) {
261261
for (int i = 0; i < columns.length; i++) {
262262
String name = columns[i].name();
263263
String dataType = columns[i].dataType().simpleString();
264-
String defaultValue = LineUtil.getDefaultValue(columns[i].defaultValue());
265-
String comment = columns[i].comment();
264+
String defaultValue = LineUtil.getDefaultValue(columns[i]);
265+
String comment = LineUtil.getComment(columns[i]);
266266
String nullable = columns[i].nullable() ? "true" : "false";
267-
String autoIncrement = columns[i].autoIncrement() ? "true" : "false";
267+
String autoIncrement = LineUtil.getAutoIncrement(columns[i]);
268268

269269
data.append(
270270
COMMA_JOINER.join(name, dataType, defaultValue, comment, nullable, autoIncrement));

clients/cli/src/main/java/org/apache/gravitino/cli/outputs/TableFormat.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,8 @@ public String getOutput(Table table) {
620620
for (org.apache.gravitino.rel.Column column : columns) {
621621
columnName.addCell(column.name());
622622
columnType.addCell(column.dataType().simpleString());
623-
columnDefaultValue.addCell(LineUtil.getDefaultValue(column.defaultValue()));
624-
columnAutoIncrement.addCell(column.autoIncrement());
623+
columnDefaultValue.addCell(LineUtil.getDefaultValue(column));
624+
columnAutoIncrement.addCell(LineUtil.getAutoIncrement(column));
625625
columnNullable.addCell(column.nullable());
626626
columnComment.addCell(
627627
column.comment() == null || column.comment().isEmpty() ? "N/A" : column.comment());
@@ -712,11 +712,10 @@ public String getOutput(org.apache.gravitino.rel.Column[] columns) {
712712
for (org.apache.gravitino.rel.Column column : columns) {
713713
columnName.addCell(column.name());
714714
columnType.addCell(column.dataType().simpleString());
715-
columnDefaultVal.addCell(LineUtil.getDefaultValue(column.defaultValue()));
716-
columnAutoIncrement.addCell(column.autoIncrement());
715+
columnDefaultVal.addCell(LineUtil.getDefaultValue(column));
716+
columnAutoIncrement.addCell(LineUtil.getAutoIncrement(column));
717717
columnNullable.addCell(column.nullable());
718-
columnComment.addCell(
719-
column.comment() == null || column.comment().isEmpty() ? "N/A" : column.comment());
718+
columnComment.addCell(LineUtil.getComment(column));
720719
}
721720

722721
return getTableFormat(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.gravitino.cli.output;
21+
22+
import static org.apache.gravitino.rel.expressions.NamedReference.field;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.when;
25+
26+
import org.apache.gravitino.cli.outputs.LineUtil;
27+
import org.apache.gravitino.rel.Column;
28+
import org.apache.gravitino.rel.expressions.FunctionExpression;
29+
import org.apache.gravitino.rel.expressions.literals.Literal;
30+
import org.apache.gravitino.rel.types.Type;
31+
import org.apache.gravitino.rel.types.Types;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.Test;
34+
35+
public class TestLineUtil {
36+
@Test
37+
void testGetDisplayWidthWithAscii() {
38+
Assertions.assertEquals(13, LineUtil.getDisplayWidth("Hello, world!"));
39+
Assertions.assertEquals(5, LineUtil.getDisplayWidth("Hello"));
40+
Assertions.assertEquals(1, LineUtil.getDisplayWidth("H"));
41+
}
42+
43+
@SuppressWarnings("DefaultCharset")
44+
@Test
45+
void testGetDisplayWidthWithNonAscii() {
46+
Assertions.assertEquals(8, LineUtil.getDisplayWidth("、世界!"));
47+
Assertions.assertEquals(10, LineUtil.getDisplayWidth("こんにちは"));
48+
Assertions.assertEquals(2, LineUtil.getDisplayWidth("こ"));
49+
}
50+
51+
@Test
52+
void testGetSpaces() {
53+
Assertions.assertEquals(" ", LineUtil.getSpaces(1));
54+
Assertions.assertEquals(" ", LineUtil.getSpaces(2));
55+
Assertions.assertEquals(" ", LineUtil.getSpaces(3));
56+
}
57+
58+
@Test
59+
void testGetAutoIncrementWithLong() {
60+
Column mockColumn1 = mock(Column.class);
61+
when(mockColumn1.dataType()).thenReturn(Types.LongType.get());
62+
when(mockColumn1.autoIncrement()).thenReturn(true);
63+
64+
Assertions.assertEquals("true", LineUtil.getAutoIncrement(mockColumn1));
65+
66+
Column mockColumn2 = mock(Column.class);
67+
when(mockColumn2.dataType()).thenReturn(Types.LongType.get());
68+
when(mockColumn2.autoIncrement()).thenReturn(false);
69+
70+
Assertions.assertEquals("false", LineUtil.getAutoIncrement(mockColumn2));
71+
}
72+
73+
@Test
74+
void testGetAutoIncrementWithInteger() {
75+
Column mockColumn1 = mock(Column.class);
76+
when(mockColumn1.dataType()).thenReturn(Types.IntegerType.get());
77+
when(mockColumn1.autoIncrement()).thenReturn(true);
78+
79+
Assertions.assertEquals("true", LineUtil.getAutoIncrement(mockColumn1));
80+
81+
Column mockColumn2 = mock(Column.class);
82+
when(mockColumn2.dataType()).thenReturn(Types.IntegerType.get());
83+
when(mockColumn2.autoIncrement()).thenReturn(false);
84+
85+
Assertions.assertEquals("false", LineUtil.getAutoIncrement(mockColumn2));
86+
}
87+
88+
@Test
89+
void testGetAutoIncrementWithShort() {
90+
Column mockColumn1 = mock(Column.class);
91+
when(mockColumn1.dataType()).thenReturn(Types.LongType.get());
92+
when(mockColumn1.autoIncrement()).thenReturn(true);
93+
94+
Assertions.assertEquals("true", LineUtil.getAutoIncrement(mockColumn1));
95+
96+
Column mockColumn2 = mock(Column.class);
97+
when(mockColumn2.dataType()).thenReturn(Types.LongType.get());
98+
when(mockColumn2.autoIncrement()).thenReturn(false);
99+
100+
Assertions.assertEquals("false", LineUtil.getAutoIncrement(mockColumn2));
101+
}
102+
103+
@Test
104+
void testGetAutoIncrementWithByte() {
105+
Column mockColumn1 = mock(Column.class);
106+
when(mockColumn1.dataType()).thenReturn(Types.ByteType.get());
107+
when(mockColumn1.autoIncrement()).thenReturn(true);
108+
109+
Assertions.assertEquals("true", LineUtil.getAutoIncrement(mockColumn1));
110+
111+
Column mockColumn2 = mock(Column.class);
112+
when(mockColumn2.dataType()).thenReturn(Types.ByteType.get());
113+
when(mockColumn2.autoIncrement()).thenReturn(false);
114+
115+
Assertions.assertEquals("false", LineUtil.getAutoIncrement(mockColumn2));
116+
}
117+
118+
@Test
119+
void testGetAutoIncrementWithNonInteger() {
120+
Column mockColumn1 = mock(Column.class);
121+
when(mockColumn1.dataType()).thenReturn(Types.StringType.get());
122+
when(mockColumn1.autoIncrement()).thenReturn(true);
123+
124+
Assertions.assertEquals("", LineUtil.getAutoIncrement(mockColumn1));
125+
126+
Column mockColumn2 = mock(Column.class);
127+
when(mockColumn2.dataType()).thenReturn(Types.BooleanType.get());
128+
when(mockColumn2.autoIncrement()).thenReturn(false);
129+
130+
Assertions.assertEquals("", LineUtil.getAutoIncrement(mockColumn2));
131+
132+
Column mockColumn3 = mock(Column.class);
133+
when(mockColumn3.dataType()).thenReturn(Types.TimeType.get());
134+
when(mockColumn3.autoIncrement()).thenReturn(false);
135+
136+
Assertions.assertEquals("", LineUtil.getAutoIncrement(mockColumn3));
137+
}
138+
139+
@Test
140+
void testGetComment() {
141+
Column mockColumn1 = mock(Column.class);
142+
when(mockColumn1.comment()).thenReturn("This is a comment");
143+
144+
Assertions.assertEquals("This is a comment", LineUtil.getComment(mockColumn1));
145+
146+
Column mockColumn2 = mock(Column.class);
147+
when(mockColumn2.comment()).thenReturn(null);
148+
149+
Assertions.assertEquals("N/A", LineUtil.getComment(mockColumn2));
150+
}
151+
152+
@Test
153+
void testGetDefaultValueWithInteger() {
154+
Column mockColumn1 = mock(Column.class);
155+
when(mockColumn1.dataType()).thenReturn(Types.IntegerType.get());
156+
when(mockColumn1.defaultValue())
157+
.thenReturn(
158+
new Literal<Integer>() {
159+
@Override
160+
public Integer value() {
161+
return 1;
162+
}
163+
164+
@Override
165+
public Type dataType() {
166+
return null;
167+
}
168+
});
169+
170+
Assertions.assertEquals("1", LineUtil.getDefaultValue(mockColumn1));
171+
}
172+
173+
@Test
174+
void testGetDefaultValueWithNull() {
175+
Column mockColumn1 = mock(Column.class);
176+
when(mockColumn1.dataType()).thenReturn(Types.IntegerType.get());
177+
when(mockColumn1.defaultValue()).thenReturn(Column.DEFAULT_VALUE_NOT_SET);
178+
179+
Assertions.assertEquals(LineUtil.EMPTY_DEFAULT_VALUE, LineUtil.getDefaultValue(mockColumn1));
180+
181+
Column mockColumn2 = mock(Column.class);
182+
when(mockColumn2.dataType()).thenReturn(Types.StringType.get());
183+
when(mockColumn2.defaultValue()).thenReturn(null);
184+
185+
Assertions.assertEquals(LineUtil.EMPTY_DEFAULT_VALUE, LineUtil.getDefaultValue(mockColumn2));
186+
}
187+
188+
@Test
189+
void testGetDefaultValueWithString() {
190+
Column mockColumn1 = mock(Column.class);
191+
when(mockColumn1.dataType()).thenReturn(Types.StringType.get());
192+
when(mockColumn1.defaultValue())
193+
.thenReturn(
194+
new Literal<String>() {
195+
@Override
196+
public String value() {
197+
return "";
198+
}
199+
200+
@Override
201+
public Type dataType() {
202+
return null;
203+
}
204+
});
205+
206+
Assertions.assertEquals("''", LineUtil.getDefaultValue(mockColumn1));
207+
208+
Column mockColumn2 = mock(Column.class);
209+
when(mockColumn2.dataType()).thenReturn(Types.StringType.get());
210+
when(mockColumn2.defaultValue())
211+
.thenReturn(
212+
new Literal<String>() {
213+
@Override
214+
public String value() {
215+
return "Hello, world!";
216+
}
217+
218+
@Override
219+
public Type dataType() {
220+
return null;
221+
}
222+
});
223+
224+
Assertions.assertEquals("Hello, world!", LineUtil.getDefaultValue(mockColumn2));
225+
}
226+
227+
@Test
228+
void testGetDefaultValueWithFunctionAndEmptyArgs() {
229+
Column mockColumn1 = mock(Column.class);
230+
when(mockColumn1.dataType()).thenReturn(Types.StringType.get());
231+
when(mockColumn1.defaultValue()).thenReturn(FunctionExpression.of("current_timestamp"));
232+
233+
Assertions.assertEquals("current_timestamp()", LineUtil.getDefaultValue(mockColumn1));
234+
}
235+
236+
@Test
237+
void testGetDefaultValueWithFunctionAndArgs() {
238+
Column mockColumn1 = mock(Column.class);
239+
when(mockColumn1.dataType()).thenReturn(Types.StringType.get());
240+
when(mockColumn1.defaultValue()).thenReturn(FunctionExpression.of("date", field("b")));
241+
242+
Assertions.assertEquals("date([b])", LineUtil.getDefaultValue(mockColumn1));
243+
}
244+
}

0 commit comments

Comments
 (0)