-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
### What changes were proposed in this pull request? Add column commands to the Gravitino CLI ### Why are the changes needed? Expand Gravitino CLI. Fix: #5383 ### Does this PR introduce _any_ user-facing change? No, but add extra commands. ### How was this patch tested? locally.
- Loading branch information
1 parent
0e4a0dc
commit 758cd2e
Showing
28 changed files
with
2,328 additions
and
6 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
clients/cli/src/main/java/org/apache/gravitino/cli/DefaultConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.gravitino.cli; | ||
|
||
import org.apache.gravitino.rel.Column; | ||
import org.apache.gravitino.rel.expressions.Expression; | ||
import org.apache.gravitino.rel.expressions.literals.Literals; | ||
import org.apache.gravitino.rel.types.Type; | ||
|
||
public class DefaultConverter { | ||
|
||
/** | ||
* Converts a default value string to the appropriate internal default value. | ||
* | ||
* @param defaultValue The string representing the default value i.e. "current_timestamp" or a | ||
* literal value. | ||
* @param dataType The string representing the default type. | ||
* @return An instance of the appropriate default value. | ||
*/ | ||
public static Expression convert(String defaultValue, String dataType) { | ||
Type convertedDatatype = ParseType.toType(dataType); | ||
|
||
if (defaultValue == null || defaultValue.isEmpty()) { | ||
return Column.DEFAULT_VALUE_NOT_SET; | ||
} else if (defaultValue.equalsIgnoreCase("current_timestamp")) { | ||
return Column.DEFAULT_VALUE_OF_CURRENT_TIMESTAMP; | ||
} else { | ||
return Literals.of(defaultValue, convertedDatatype); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
clients/cli/src/main/java/org/apache/gravitino/cli/ParseType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.gravitino.cli; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import org.apache.gravitino.rel.types.Type; | ||
|
||
public class ParseType { | ||
|
||
/** | ||
* Parses a data type string and returns a {@link org.apache.gravitino.cli.ParsedType} object | ||
* containing the type name and length or type name, precision, and scale if applicable. | ||
* | ||
* <p>This method supports SQL style types in the format of "typeName(length)" or | ||
* "typeName(precision, scale)". For example, "varchar(10)" and "decimal(10,5)" are valid inputs. | ||
* | ||
* @param datatype The data type string to parse e.g. "varchar(10)" or "decimal(10,5)". | ||
* @return a {@link org.apache.gravitino.cli.ParsedType} object representing the parsed type name. | ||
* @throws IllegalArgumentException if the data type format is unsupported or malformed | ||
*/ | ||
public static ParsedType parse(String datatype) { | ||
Pattern pattern = Pattern.compile("^(\\w+)\\((\\d+)(?:,(\\d+))?\\)$"); | ||
Matcher matcher = pattern.matcher(datatype); | ||
|
||
if (matcher.matches()) { | ||
String typeName = matcher.group(1); | ||
Integer lengthOrPrecision = Integer.parseInt(matcher.group(2)); | ||
Integer scale = matcher.group(3) != null ? Integer.parseInt(matcher.group(3)) : null; | ||
|
||
if (lengthOrPrecision != null && scale != null) { | ||
return new ParsedType(typeName, lengthOrPrecision, scale); | ||
} else if (lengthOrPrecision != null) { | ||
return new ParsedType(typeName, lengthOrPrecision); | ||
} else { | ||
throw new IllegalArgumentException("Unsupported/malformed data type: " + typeName); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static Type toType(String datatype) { | ||
ParsedType parsed = parse(datatype); | ||
|
||
if (parsed != null) { | ||
if (parsed.getPrecision() != null && parsed.getScale() != null) { | ||
return TypeConverter.convert(datatype, parsed.getPrecision(), parsed.getScale()); | ||
} else if (parsed.getLength() != null) { | ||
return TypeConverter.convert(datatype, parsed.getLength()); | ||
} | ||
} | ||
|
||
return TypeConverter.convert(datatype); | ||
} | ||
} |
Oops, something went wrong.