All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.trino.sql.analyzer.CanonicalizationAware Maven / Gradle / Ivy

There is a newer version: 465
Show newest version
/*
 * 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.sql.analyzer;

import io.trino.sql.tree.Identifier;
import io.trino.sql.tree.Node;

import java.util.OptionalInt;

import static io.trino.sql.util.AstUtils.treeEqual;
import static io.trino.sql.util.AstUtils.treeHash;
import static java.util.Objects.requireNonNull;

public class CanonicalizationAware
{
    private final T node;

    // Updates to this field are thread-safe despite benign data race due to:
    // 1. idempotent hash computation
    // 2. atomic updates to int fields per JMM
    private int hashCode;

    private CanonicalizationAware(T node)
    {
        this.node = requireNonNull(node, "node is null");
    }

    public static  CanonicalizationAware canonicalizationAwareKey(T node)
    {
        return new CanonicalizationAware(node);
    }

    public T getNode()
    {
        return node;
    }

    @Override
    public int hashCode()
    {
        int hash = hashCode;
        if (hash == 0) {
            hash = treeHash(node, CanonicalizationAware::canonicalizationAwareHash);
            if (hash == 0) {
                hash = 1;
            }

            hashCode = hash;
        }

        return hash;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        CanonicalizationAware other = (CanonicalizationAware) o;
        return treeEqual(node, other.node, CanonicalizationAware::canonicalizationAwareComparison);
    }

    @Override
    public String toString()
    {
        return "CanonicalizationAware(" + node + ")";
    }

    public static Boolean canonicalizationAwareComparison(Node left, Node right)
    {
        if (left instanceof Identifier leftIdentifier && right instanceof Identifier rightIdentifier) {
            return leftIdentifier.getCanonicalValue().equals(rightIdentifier.getCanonicalValue());
        }

        return null;
    }

    public static OptionalInt canonicalizationAwareHash(Node node)
    {
        if (node instanceof Identifier identifier) {
            return OptionalInt.of(identifier.getCanonicalValue().hashCode());
        }
        if (node.getChildren().isEmpty()) {
            return OptionalInt.of(node.hashCode());
        }
        return OptionalInt.empty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy