|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.calcite.sql.fun; |
| 19 | + |
| 20 | +import org.apache.calcite.sql.SqlFunction; |
| 21 | +import org.apache.calcite.sql.SqlFunctionCategory; |
| 22 | +import org.apache.calcite.sql.SqlKind; |
| 23 | +import org.apache.calcite.sql.type.OperandTypes; |
| 24 | +import org.apache.calcite.sql.type.ReturnTypes; |
| 25 | + |
| 26 | +/** |
| 27 | + * Compatibility shim for Calcite 1.37+ migration. |
| 28 | + * |
| 29 | + * In Calcite 1.36, RAND was implemented as a dedicated SqlRandFunction class extending SqlFunction. |
| 30 | + * In Calcite 1.37, RAND became a SqlBasicFunction in SqlStdOperatorTable. |
| 31 | + * |
| 32 | + * This class provides backward compatibility for deserializing view definitions |
| 33 | + * that were created with Calcite 1.36 and contain serialized SqlRandFunction references. |
| 34 | + * |
| 35 | + * When Java deserializes an old view definition and encounters |
| 36 | + * "org.apache.calcite.sql.fun.SqlRandFunction", it will load this shim class instead. |
| 37 | + * The readResolve() method ensures that the deserialized object is replaced with |
| 38 | + * the current Calcite 1.37 RAND implementation from SqlStdOperatorTable. |
| 39 | + */ |
| 40 | +public class SqlRandFunction extends SqlFunction { |
| 41 | + |
| 42 | + /** |
| 43 | + * Constructor matching the original SqlRandFunction signature from Calcite 1.36. |
| 44 | + * This is needed for deserialization to work properly. |
| 45 | + */ |
| 46 | + public SqlRandFunction() { |
| 47 | + super("RAND", |
| 48 | + SqlKind.OTHER_FUNCTION, |
| 49 | + ReturnTypes.DOUBLE, |
| 50 | + null, |
| 51 | + OperandTypes.or(OperandTypes.NILADIC, OperandTypes.NUMERIC), |
| 52 | + SqlFunctionCategory.NUMERIC); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Matches the original Calcite 1.36 behavior where RAND was marked as non-deterministic. |
| 57 | + */ |
| 58 | + @Override |
| 59 | + public boolean isDynamicFunction() { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Serialization replacement method. |
| 65 | + * When this object is deserialized, Java will call readResolve() and replace |
| 66 | + * this shim instance with the actual Calcite 1.37 RAND function. |
| 67 | + * |
| 68 | + * @return The current RAND implementation from SqlStdOperatorTable |
| 69 | + */ |
| 70 | + private Object readResolve() { |
| 71 | + return SqlStdOperatorTable.RAND; |
| 72 | + } |
| 73 | +} |
0 commit comments