Skip to content
Open
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 @@ -1265,7 +1265,7 @@ void testAlterMaterializedTableAsQueryInContinuousMode(@TempDir Path temporaryPa
assertThat(newTable.getExpandedQuery())
.hasToString(
String.format(
"SELECT COALESCE(`tmp`.`user_id`, CAST(0 AS BIGINT)) AS `user_id`, `tmp`.`shop_id`, COALESCE(`tmp`.`ds`, '') AS `ds`, SUM(`tmp`.`payment_amount_cents`) AS `payed_buy_fee_sum`, SUM(1) AS `pv`\n"
"SELECT COALESCE(`tmp`.`user_id`, 0) AS `user_id`, `tmp`.`shop_id`, COALESCE(`tmp`.`ds`, '') AS `ds`, SUM(`tmp`.`payment_amount_cents`) AS `payed_buy_fee_sum`, SUM(1) AS `pv`\n"
+ "FROM (SELECT `datagenSource`.`user_id`, `datagenSource`.`shop_id`, DATE_FORMAT(`datagenSource`.`order_created_at`, 'yyyy-MM-dd') AS `ds`, `datagenSource`.`payment_amount_cents`\n"
+ "FROM `%s`.`%s`.`datagenSource` AS `datagenSource`) AS `tmp`\n"
+ "GROUP BY ROW(`tmp`.`user_id`, `tmp`.`shop_id`, `tmp`.`ds`)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
.kind(SCALAR)
.inputTypeStrategy(varyingSequence(COMMON_ARG_NULLABLE, COMMON_ARG_NULLABLE))
.outputTypeStrategy(nullableIfAllArgs(COMMON))
.runtimeClass(
"org.apache.flink.table.runtime.functions.scalar.CoalesceFunction")
.runtimeDeferred()
.build();

public static final BuiltInFunctionDefinition ARRAY_APPEND =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.sql.fun;

import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.OperandTypes;
import org.apache.calcite.sql.type.ReturnTypes;
import org.apache.calcite.sql.type.SqlReturnTypeInference;
import org.apache.calcite.sql.type.SqlTypeTransforms;
import org.apache.calcite.sql.validate.SqlValidator;

import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.requireNonNull;

/** The class copied from Calcite in order to turn off COALESCE rewrite with CASE ... WHEN ... */
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.

Do we want to add a note that we are removing the NULLs?

public class SqlCoalesceFunction extends SqlFunction {
// ~ Constructors -----------------------------------------------------------

public SqlCoalesceFunction() {
// NOTE jvs 26-July-2006: We fill in the type strategies here,
// but normally they are not used because the validator invokes
// rewriteCall to convert COALESCE into CASE early. However,
// validator rewrite can optionally be disabled, in which case these
// strategies are used.
super(
"COALESCE",
SqlKind.COALESCE,
ReturnTypes.LEAST_RESTRICTIVE.andThen(SqlTypeTransforms.LEAST_NULLABLE),
null,
OperandTypes.SAME_VARIADIC,
SqlFunctionCategory.SYSTEM);
}

// ~ Methods ----------------------------------------------------------------

// ----- FLINK MODIFICATION BEGIN -----
Copy link
Copy Markdown
Contributor Author

@snuyanzin snuyanzin Apr 29, 2026

Choose a reason for hiding this comment

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

In calcite's properties there is a flag to turn it off
the problem with that flag is that it turns off for all functions, not only for coalesce

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.

Can you say more about what this modification does?

Copy link
Copy Markdown
Contributor Author

@snuyanzin snuyanzin Apr 29, 2026

Choose a reason for hiding this comment

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

it's a standard way of marking places in classes copied from Calcite what exactly area is changed in this classes

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.

Sorry, I do understand that we are overriding / changing something from Calcite.

I am trying to understand what we are changing about the Calcite class.

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.

// override SqlOperator
@Override
public SqlNode rewriteCall(SqlValidator validator, SqlCall call) {
validateQuantifier(validator, call); // check DISTINCT/ALL

List<SqlNode> operands = call.getOperandList();

if (operands.size() == 1) {
return operands.get(0);
}

SqlParserPos pos = call.getParserPosition();
List<SqlNode> nodes = new ArrayList<>();
for (SqlNode operand : operands) {
if (!SqlUtil.isNullLiteral(operand, false)) {
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.

Is it intentional that we are not checking for casts?

Could something interesting happen if someone trying to cast a wider type to null?

nodes.add(operand);
}
}

if (nodes.isEmpty()) {
return SqlLiteral.createNull(pos);
}
if (nodes.size() == 1) {
return nodes.get(0);
}

return new SqlBasicCall(this, nodes, pos);
}

// ----- FLINK MODIFICATION END -----

@Override
public SqlReturnTypeInference getReturnTypeInference() {
return requireNonNull(super.getReturnTypeInference(), "returnTypeInference");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ void initNonDynamicFunctions() {
definitionSqlOperatorHashMap.put(
BuiltInFunctionDefinitions.ARRAY_ELEMENT, FlinkSqlOperatorTable.ELEMENT);

definitionSqlOperatorHashMap.put(
BuiltInFunctionDefinitions.COALESCE, FlinkSqlOperatorTable.COALESCE);

// crypto hash
definitionSqlOperatorHashMap.put(BuiltInFunctionDefinitions.MD5, FlinkSqlOperatorTable.MD5);
definitionSqlOperatorHashMap.put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ public List<SqlGroupedWindowFunction> getAuxiliaryFunctions() {
public static final SqlFunction ABS = SqlStdOperatorTable.ABS;
public static final SqlFunction EXP = SqlStdOperatorTable.EXP;
public static final SqlFunction NULLIF = SqlStdOperatorTable.NULLIF;
public static final SqlFunction COALESCE = SqlStdOperatorTable.COALESCE;
public static final SqlFunction FLOOR = SqlStdOperatorTable.FLOOR;
public static final SqlFunction CEIL = SqlStdOperatorTable.CEIL;
public static final SqlFunction CAST = SqlStdOperatorTable.CAST;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,9 @@ class ExprCodeGenerator(ctx: CodeGeneratorContext, nullableInput: Boolean)
case CASE =>
generateIfElse(ctx, operands, resultType)

case COALESCE =>
generateCoalesce(ctx, operands, resultType)

case IS_TRUE =>
val operand = operands.head
requireBoolean(operand)
Expand Down Expand Up @@ -846,6 +849,9 @@ class ExprCodeGenerator(ctx: CodeGeneratorContext, nullableInput: Boolean)
case BuiltInFunctionDefinitions.JSON =>
new JsonCallGen().generate(ctx, operands, FlinkTypeFactory.toLogicalType(call.getType))

case BuiltInFunctionDefinitions.COALESCE =>
generateCoalesce(ctx, operands, resultType)

case _ =>
new BridgingSqlFunctionCallGen(call).generate(ctx, operands, resultType)
}
Expand Down
Loading