forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-14490][table-api] Introduce UnresolvedIdentifier
- Loading branch information
Showing
8 changed files
with
180 additions
and
63 deletions.
There are no files selected for viewing
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
127 changes: 127 additions & 0 deletions
127
...ink-table-api-java/src/main/java/org/apache/flink/table/catalog/UnresolvedIdentifier.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,127 @@ | ||
/* | ||
* 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.catalog; | ||
|
||
import org.apache.flink.annotation.Internal; | ||
import org.apache.flink.table.api.ValidationException; | ||
import org.apache.flink.table.utils.EncodingUtils; | ||
import org.apache.flink.util.Preconditions; | ||
import org.apache.flink.util.StringUtils; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Identifier of an object, such as table, view, function or type in a catalog. This identifier | ||
* cannot be used directly to access an object in a {@link CatalogManager}, but has to be first | ||
* fully resolved into {@link ObjectIdentifier}. | ||
*/ | ||
@Internal | ||
public class UnresolvedIdentifier { | ||
|
||
private final String catalogName; | ||
|
||
private final String databaseName; | ||
|
||
private final String objectName; | ||
|
||
/** | ||
* Constructs an {@link UnresolvedIdentifier} from an array of identifier segments. | ||
* The length of the path must be between 1 (only object name) and 3 (fully qualified | ||
* identifier with catalog, database and object name). | ||
* | ||
* @param path array of identifier segments | ||
* @return an identifier that must be resolved before accessing an object from a {@link CatalogManager} | ||
*/ | ||
public static UnresolvedIdentifier of(String... path) { | ||
if (path == null) { | ||
throw new ValidationException("Object identifier can not be null!"); | ||
} | ||
if (path.length < 1 || path.length > 3) { | ||
throw new ValidationException("Object identifier must consist of 1 to 3 parts."); | ||
} | ||
if (Arrays.stream(path).anyMatch(StringUtils::isNullOrWhitespaceOnly)) { | ||
throw new ValidationException("Parts of the object identifier are null or whitespace-only."); | ||
} | ||
|
||
if (path.length == 3) { | ||
return new UnresolvedIdentifier(path[0], path[1], path[2]); | ||
} else if (path.length == 2) { | ||
return new UnresolvedIdentifier(null, path[0], path[1]); | ||
} else { | ||
return new UnresolvedIdentifier(null, null, path[0]); | ||
} | ||
} | ||
|
||
private UnresolvedIdentifier( | ||
@Nullable String catalogName, | ||
@Nullable String databaseName, | ||
String objectName) { | ||
this.catalogName = catalogName; | ||
this.databaseName = databaseName; | ||
this.objectName = Preconditions.checkNotNull(objectName, "Object name must not be null."); | ||
} | ||
|
||
public Optional<String> getCatalogName() { | ||
return Optional.ofNullable(catalogName); | ||
} | ||
|
||
public Optional<String> getDatabaseName() { | ||
return Optional.ofNullable(databaseName); | ||
} | ||
|
||
public String getObjectName() { | ||
return objectName; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
UnresolvedIdentifier that = (UnresolvedIdentifier) o; | ||
return catalogName.equals(that.catalogName) && | ||
databaseName.equals(that.databaseName) && | ||
objectName.equals(that.objectName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(catalogName, databaseName, objectName); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Stream.of( | ||
catalogName, | ||
databaseName, | ||
objectName | ||
).filter(Objects::nonNull) | ||
.map(EncodingUtils::escapeIdentifier) | ||
.collect(Collectors.joining(".")); | ||
} | ||
} |
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
Oops, something went wrong.