/*
 * Licensed 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 io.trino.spi;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public final class QueryId
{
    @JsonCreator
    public static QueryId valueOf(String queryId)
    {
        List<String> ids = parseDottedId(queryId, 1, "queryId");
        return new QueryId(ids.get(0));
    }

    private final String id;

    public QueryId(String id)
    {
        this.id = validateId(id);
    }

    public String getId()
    {
        return id;
    }

    @Override
    @JsonValue
    public String toString()
    {
        return id;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(id);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        QueryId other = (QueryId) obj;
        return Objects.equals(this.id, other.id);
    }

    //
    // Id helper methods
    //

    private static final Pattern ID_PATTERN = Pattern.compile("[_a-z0-9]+");

    public static String validateId(String id)
    {
        requireNonNull(id, "id is null");
        checkArgument(!id.isEmpty(), "id is empty");
        checkArgument(ID_PATTERN.matcher(id).matches(), "Invalid id %s", id);
        return id;
    }

    public static List<String> parseDottedId(String id, int expectedParts, String name)
    {
        requireNonNull(id, "id is null");
        checkArgument(expectedParts > 0, "expectedParts must be at least 1");
        requireNonNull(name, "name is null");

        List<String> ids = List.of(id.split("\\."));
        checkArgument(ids.size() == expectedParts, "Invalid %s %s", name, id);

        for (String part : ids) {
            checkArgument(!part.isEmpty(), "Invalid id %s", id);
            checkArgument(ID_PATTERN.matcher(part).matches(), "Invalid id %s", id);
        }
        return ids;
    }

    private static void checkArgument(boolean condition, String message, Object... messageArgs)
    {
        if (!condition) {
            throw new IllegalArgumentException(format(message, messageArgs));
        }
    }
}

@@ -735,40 +735,8 @@
 cts;
-%0Aimport java.util.regex.Pattern;
 %0A%0Aim
@@ -935,64 +935,44 @@
     
-List%3CString%3E ids = parseDottedId(queryId, 1, %22queryId%22);
+// ID is verified in the constructor
 %0A   
@@ -999,18 +999,15 @@
 yId(
-ids.get(0)
+queryId
 );%0A 
@@ -1712,80 +1712,457 @@
     
-private static final Pattern ID_PATTERN = Pattern.compile(%22%5B_a-z0-9%5D+%22);
+// Check if the string matches %5B_a-z0-9%5D+ , but without the overhead of regex%0A    private static final boolean isValidId(String id)%0A    %7B%0A        if (id.length() == 0) %7B%0A            return false;%0A        %7D%0A%0A        for (int i = 0; i %3C id.length(); i++) %7B%0A            char c = id.charAt(i);%0A            if (!(c == '_' %7C%7C c %3E= 'a' && c %3C= 'z' %7C%7C c %3E= '0' && c %3C= '9')) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;%0A    %7D
 %0A%0A  
@@ -2333,39 +2333,20 @@
 ent(
-ID_PATTERN.matcher(id).matches(
+isValidId(id
 ), %22
@@ -2915,41 +2915,22 @@
 ent(
-ID_PATTERN.matcher(part).matches(
+isValidId(part
 ), %22

Improve performance when constructing QueryId instance