Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ public final class CalciteSystemProperty<T> {
public static final CalciteSystemProperty<Boolean> TOPDOWN_OPT =
booleanProperty("calcite.planner.topdown.opt", false);


/** Whether to disable generate rel data type digest string.
*
* <p> Disable generate rel data type digest string for every type can
* reduce composite type's digest memory and digest relative operation's latency. */
public static final CalciteSystemProperty<Boolean> DISABLE_GENERATE_REL_DATA_TYPE_DIGEST_STRING =
booleanProperty("calcite.disable.generate.rel.data.type.digest.string", false);

/**
* Whether to run integration tests.
*/
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/org/apache/calcite/jdbc/JavaRecordType.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,16 @@ public JavaRecordType(List<RelDataTypeField> fields, Class clazz) {
@Override public int hashCode() {
return Objects.hash(fieldList, clazz);
}

@Override public boolean deepEquals(@Nullable Object obj) {
return this == obj
|| obj instanceof JavaRecordType
&& Objects.equals(fieldList, ((JavaRecordType) obj).fieldList)
&& clazz == ((JavaRecordType) obj).clazz
&& this.isNullable() == ((JavaRecordType) obj).isNullable();
}

@Override public int deepHashCode() {
return Objects.hash(fieldList, this.isNullable(), clazz);
}
}
24 changes: 24 additions & 0 deletions core/src/main/java/org/apache/calcite/rel/HasDigestString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.calcite.rel;

/**
* Interface for objects that have a digest string.
*/
public interface HasDigestString {
String getDigestString();
}
22 changes: 22 additions & 0 deletions core/src/main/java/org/apache/calcite/rel/type/RelDataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,26 @@ default boolean equalsSansFieldNamesAndNullability(@Nullable RelDataType that) {
default boolean isMeasure() {
return getSqlTypeName() == SqlTypeName.MEASURE;
}

/**
* Returns the digest of this type.
*
* @return digest of this type
*/
RelDataTypeDigest getDigest();

/**
* Deep equality check for RelDataType digest.
*
* @return Whether the 2 RelDataTypes are equivalent or have the same digest.
* @see #deepHashCode()
*/
boolean deepEquals(@Nullable Object obj);

/**
* Compute deep hash code for RelDataType digest.
*
* @see #deepEquals(Object)
*/
int deepHashCode();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.calcite.rel.type;

import org.apache.calcite.rel.HasDigestString;

/**
* Digest of a RelDataType.
*/
public interface RelDataTypeDigest extends HasDigestString {
RelDataType getType();
}
138 changes: 120 additions & 18 deletions core/src/main/java/org/apache/calcite/rel/type/RelDataTypeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.calcite.rel.type;

import org.apache.calcite.config.CalciteSystemProperty;
import org.apache.calcite.sql.SqlCollation;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlIntervalQualifier;
Expand Down Expand Up @@ -46,8 +47,8 @@
* RelDataTypeImpl is an abstract base for implementations of
* {@link RelDataType}.
*
* <p>Identity is based upon the {@link #digest} field, which each derived class
* should set during construction.
* <p>Identity is based upon the {@link #digest} or {@link #innerDigest} field,
* which each derived class should set {@link #digest} or {@link #innerDigest} during construction.
*/
public abstract class RelDataTypeImpl
implements RelDataType, RelDataTypeFamily {
Expand All @@ -60,7 +61,14 @@ public abstract class RelDataTypeImpl
//~ Instance fields --------------------------------------------------------

protected final @Nullable List<RelDataTypeField> fieldList;
protected @Nullable String digest;

/**
* Use {@link #innerDigest} instead.
*
* @deprecated See {@link CalciteSystemProperty#DISABLE_GENERATE_REL_DATA_TYPE_DIGEST_STRING}.
*/
protected @Deprecated @Nullable String digest;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add also a @deprecated JavaDoc comment explaining what users are supposed to do instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we suggest using RelDataTypeDigest.getDigest().getDigestString() instead of digest? I also have a question: if a user extends and uses this field, will the current modification block the upgrade? Is this considered a blocking point?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.Yes. It has better performace and ondemand construction behavior.
2.Not a blocking point, if user set the String digest not null, all hashCode/equals/getXXXString logic will fallback to behaviors based on String digest.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is no. If you use this field and mark it with @Deprecated, the compiler will definitely throw an error. You are returning a string using RelDataTypeDigest.getDigest().getDigestString(), instead of directly using digest.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first meaning is to add a description like "Use RelDataTypeDigest.getDigest().getDigestString() instead of digest" to the Javadoc.

Copy link
Copy Markdown
Contributor Author

@zhuwenzhuang zhuwenzhuang Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, @depredate mark may cause user's compiler thorw an error. And I think use RelDataTypeDigest to handle digest is better than String digest. I don't know should we add the @depredate mark to indicate this suggestion.

For the java doc.
"Use RelDataTypeDigest.getDigest().getDigestString() instead of digest" is one usage of digest. If users managed the String digest by not standard way(maybe UDT)

digest = "MyType";

, they need implement this in

void generateTypeString(StringBuilder sb,boolean withDetail);

I'm not sure whether we need to add this to the Javadoc, either.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe a more detailed description would be better. While I think this breaks compatibility, I think it might be acceptable if no one objects in the next few days.

protected @Nullable RelDataTypeDigest innerDigest;

//~ Constructors -----------------------------------------------------------

Expand Down Expand Up @@ -232,18 +240,50 @@ private static void getFieldRecurse(List<Slot> slots, RelDataType type,
return fieldList != null;
}

/**
* Gets the {@link RelDataTypeDigest} of this type.
* If a user has set the legacy string {@code digest} and {@code innerDigest} has not
* been initialized yet, this method computes and initializes it.
*/
@Override public RelDataTypeDigest getDigest() {
if (digest != null && innerDigest == null) {
innerDigest = new InnerRelDataTypeDigest();
}
return requireNonNull(innerDigest, "innerDigest");
}

@Override public boolean equals(@Nullable Object obj) {
return this == obj
|| obj instanceof RelDataTypeImpl
&& Objects.equals(this.digest, ((RelDataTypeImpl) obj).digest);
if (obj == this) {
return true;
}
if (obj instanceof RelDataTypeImpl) {
final RelDataTypeImpl that = (RelDataTypeImpl) obj;
return this.getDigest().equals(that.getDigest());
}
return false;
}

@Override public int hashCode() {
return Objects.hashCode(digest);
return getDigest().hashCode();
}

@Override public boolean deepEquals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
return Objects.equals(this.getDigest().getDigestString(),
((RelDataTypeImpl) obj).getDigest().getDigestString());
}

@Override public int deepHashCode() {
return Objects.hashCode(this.getDigest().getDigestString());
}

@Override public String getFullTypeString() {
return requireNonNull(digest, "digest");
return requireNonNull(this.getDigest().getDigestString(), "digest");
}

@Override public boolean isNullable() {
Expand Down Expand Up @@ -309,23 +349,85 @@ protected abstract void generateTypeString(
boolean withDetail);

/**
* Computes the digest field. This should be called in every non-abstract
* subclass constructor once the type is fully defined.
* Init the lazy digest computing field {@link #innerDigest}.
* This should be called in every non-abstract subclass
* constructor once the type is fully defined.
*/
@SuppressWarnings("method.invocation.invalid")
protected void computeDigest(@UnknownInitialization RelDataTypeImpl this) {
StringBuilder sb = new StringBuilder();
generateTypeString(sb, true);
if (!isNullable()) {
sb.append(NON_NULLABLE_SUFFIX);
digest = null;
innerDigest = new InnerRelDataTypeDigest();
if (!CalciteSystemProperty.DISABLE_GENERATE_REL_DATA_TYPE_DIGEST_STRING.value()) {
digest = this.getDigest().getDigestString();
}
digest = sb.toString();
}

@Override public String toString() {
StringBuilder sb = new StringBuilder();
generateTypeString(sb, false);
return sb.toString();
return getDigest().toString();
}

/** Implementation of {@link RelDataTypeDigest}. */
private class InnerRelDataTypeDigest implements RelDataTypeDigest {
/** Cached hash code. */
private int hash = 0;
/** Cached type string. */
private @Nullable String digestWithDetail = null; // NOTE: shorter detail will be better
private @Nullable String digestWithoutDetail = null;

@Override public RelDataType getType() {
return RelDataTypeImpl.this;
}

@Override public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final RelDataTypeImpl.InnerRelDataTypeDigest otherDigest =
(RelDataTypeImpl.InnerRelDataTypeDigest) o;
if (digest != null) {
return digest.equals(otherDigest.getDigestString());
}
return deepEquals(otherDigest.getType());
}

@Override public int hashCode() {
if (digest != null) {
return Objects.hashCode(digest);
}
if (hash == 0) {
hash = deepHashCode();
}
return hash;
}

@Override public String getDigestString() {
// return user defined digest by set legacy digest string field.
if (digest != null) {
return digest;
}

if (digestWithDetail == null) {
StringBuilder sb = new StringBuilder();
generateTypeString(sb, true);
if (!isNullable()) {
sb.append(NON_NULLABLE_SUFFIX);
}
digestWithDetail = sb.toString();
}
return digestWithDetail;
}

@Override public String toString() {
if (digestWithoutDetail == null || digest != null) {
StringBuilder sb = new StringBuilder();
RelDataTypeImpl.this.generateTypeString(sb, false);
digestWithoutDetail = sb.toString();
}
return digestWithoutDetail;
}
}

@Override public RelDataTypePrecedenceList getPrecedenceList() {
Expand Down
34 changes: 34 additions & 0 deletions core/src/main/java/org/apache/calcite/rel/type/RelRecordType.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -146,6 +147,39 @@ public RelRecordType(List<RelDataTypeField> fields) {
sb.append(")");
}

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

RelRecordType that = (RelRecordType) obj;
if (kind != that.kind || nullable != that.nullable) {
return false;
}

if (fieldList == null || that.fieldList == null) {
return fieldList == null && that.fieldList == null;
}

if (fieldList.size() != that.fieldList.size()) {
return false;
}

for (int i = 0; i < fieldList.size(); i++) {
if (!fieldList.get(i).equals(that.fieldList.get(i))) {
return false;
}
}
return true;
}

@Override public int deepHashCode() {
return Objects.hash(kind.ordinal(), nullable, fieldList);
}

/**
* Per {@link Serializable} API, provides a replacement object to be written
* during serialization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,16 @@ public SingleColumnAliasRelDataType(RelDataType original, RelDataType alias) {
@Override public boolean isDynamicStruct() {
return original.isDynamicStruct();
}

@Override public RelDataTypeDigest getDigest() {
return original.getDigest();
}

@Override public boolean deepEquals(@Nullable Object obj) {
return original.deepEquals(obj);
}

@Override public int deepHashCode() {
return original.deepHashCode();
}
}
19 changes: 19 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/type/ArraySqlType.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import org.apache.calcite.rel.type.RelDataTypeFamily;
import org.apache.calcite.rel.type.RelDataTypePrecedenceList;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Objects;

import static org.apache.calcite.sql.type.NonNullableAccessors.getComponentTypeOrThrow;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -56,6 +60,21 @@ public ArraySqlType(RelDataType elementType, boolean isNullable) {
sb.append(" ARRAY");
}

@Override public boolean deepEquals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
ArraySqlType that = (ArraySqlType) obj;
return this.isNullable() == that.isNullable() && elementType.equals(that.elementType);
}

@Override public int deepHashCode() {
return Objects.hash(SqlTypeName.ARRAY.ordinal(), isNullable, elementType.hashCode());
}

// implement RelDataType
@Override public RelDataType getComponentType() {
return elementType;
Expand Down
Loading
Loading