AMashenkov commented on code in PR #4971: URL: https://github.com/apache/ignite-3/pull/4971#discussion_r1897756040
########## modules/api/src/main/java/org/apache/ignite/table/QualifiedName.java: ########## @@ -0,0 +1,306 @@ +/* + * 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.ignite.table; + +import java.util.Objects; +import org.apache.ignite.lang.util.IgniteNameUtils; +import org.jetbrains.annotations.Nullable; + +/** + * Class represents a catalog object name (table, index and etc.) and provides factory methods. + * + * <p>Qualified name is a pair of schema name and object name. Schema name is optional and can be null, which means that it will be + * resolved in lazy manner against default schema. Default schema depends on the context, where the name will be used. If no context is + * available (e.g. client or embedded KeyValue API), then "PUBLIC" will be used as default schema name. + * + * <p>Both schema name and object name can be case sensitive or insensitive respecting SQL-parser style quotation. + * Unquoted name will be cast to upper case. E.g. "tbl0" - is equivalent to "TBL0", "\"Tbl0\"" - "Tbl0", etc. + */ +public class QualifiedName { + /** + * Parses given simple or canonical name, and returns the object qualified name. + */ + public static QualifiedName parse(String simpleOrCanonicalName) { + verifyObjectName(simpleOrCanonicalName); + + Tokenizer tokenizer = new Tokenizer(simpleOrCanonicalName); + + String schemaName = null; + String objectName = tokenizer.nextToken(); + + if (tokenizer.hasNext()) { + // Canonical name + schemaName = objectName; + objectName = tokenizer.nextToken(); + } + + if (tokenizer.hasNext()) { + throw new IllegalArgumentException("Canonical name format mismatch: " + simpleOrCanonicalName); + } + + verifySchemaName(schemaName); + verifyObjectName(objectName); + + return new QualifiedName(schemaName, objectName); + } + + /** + * Resolves (maybe lazily) given simple name against default schema. + */ + public static QualifiedName fromSimple(String simpleName) { + return of(null, simpleName); + } + + /** + * Normalize schemaName and objectName, and returns the object qualified name. + */ + public static QualifiedName of(@Nullable String schemaName, String objectName) { + String normalizedSchemaName = schemaName == null ? null : parseIdentifier(schemaName); + String normalizedObjectName = parseIdentifier(objectName); + + verifySchemaName(normalizedSchemaName); + verifyObjectName(normalizedObjectName); + + return new QualifiedName(normalizedSchemaName, normalizedObjectName); + } + + private static String parseIdentifier(String name) { + if (name == null || name.isEmpty()) { + return name; + } + + var tokenizer = new Tokenizer(name); + + String parsedName = tokenizer.nextToken(); + + if (tokenizer.hasNext()) { + throw new IllegalArgumentException("Non-qualified identifier expected: " + name); + } + + return parsedName; + } + + private static void verifyObjectName(@Nullable String name) { + if (name == null || name.isEmpty()) { + throw new IllegalArgumentException("Object name can't be null or empty."); + } + } + + private static void verifySchemaName(@Nullable String schemaName) { + if (schemaName != null && schemaName.isEmpty()) { + throw new IllegalArgumentException("Schema name can't be empty."); + } + } + + private final @Nullable String schemaName; + private final String objectName; + + /** + * Constructs a qualified name. + * + * @param schemaName Normalized schema name. + * @param objectName Normalized object name. + */ + private QualifiedName(@Nullable String schemaName, String objectName) { + this.schemaName = schemaName; + this.objectName = objectName; + } + + /** Returns normalized schema name. */ + public @Nullable String schemaName() { + return schemaName; + } + + /** Returns normalized object name. */ + public String name() { + return objectName; + } + + /** {@inheritDoc} */ + @Override + public boolean equals(Object object) { + if (this == object) { + return true; + } + if (object == null || getClass() != object.getClass()) { + return false; + } + QualifiedName that = (QualifiedName) object; + return Objects.equals(schemaName, that.schemaName) && Objects.equals(objectName, that.objectName); + } + + /** {@inheritDoc} */ + @Override + public int hashCode() { + return Objects.hash(schemaName, objectName); + } + + /** {@inheritDoc} */ + @Override + public String toString() { + String simpleName = IgniteNameUtils.quoteIfNeeded(objectName); + + return schemaName == null ? simpleName : IgniteNameUtils.quoteIfNeeded(schemaName) + '.' + simpleName; + } + + /** + * Identifier chain tokenizer. + * + * <p>Splits provided identifier chain (complex identifier like PUBLIC.MY_TABLE) into its component parts. + * + * <p>This tokenizer is not SQL compliant, but it is ok since it used to retrieve an object only. The sole purpose of this tokenizer + * is to split the chain into parts by a dot considering the quotation. + */ + private static class Tokenizer { Review Comment: It has wrong implementation. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org