org.apache.flink.table.planner.calcite.FlinkCalciteSqlValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flink-table-planner-blink_2.11 Show documentation
Show all versions of flink-table-planner-blink_2.11 Show documentation
This module bridges Table/SQL API and runtime. It contains
all resources that are required during pre-flight and runtime
phase. The content of this module is work-in-progress. It will
replace flink-table-planner once it is stable. See FLINK-11439
and FLIP-32 for more details.
/*
* 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.flink.table.planner.calcite;
import org.apache.flink.annotation.Internal;
import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.types.logical.DecimalType;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeFactory;
import org.apache.calcite.sql.JoinType;
import org.apache.calcite.sql.SqlBasicCall;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlJoin;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlLiteral;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperatorTable;
import org.apache.calcite.sql.SqlUtil;
import org.apache.calcite.sql.SqlWindowTableFunction;
import org.apache.calcite.sql.validate.SqlValidator;
import org.apache.calcite.sql.validate.SqlValidatorCatalogReader;
import org.apache.calcite.sql.validate.SqlValidatorImpl;
import org.apache.calcite.sql.validate.SqlValidatorScope;
import org.apache.calcite.util.Static;
import java.math.BigDecimal;
import java.util.List;
import static org.apache.calcite.sql.type.SqlTypeName.DECIMAL;
/** Extends Calcite's {@link SqlValidator} by Flink-specific behavior. */
@Internal
public final class FlinkCalciteSqlValidator extends SqlValidatorImpl {
public FlinkCalciteSqlValidator(
SqlOperatorTable opTab,
SqlValidatorCatalogReader catalogReader,
RelDataTypeFactory typeFactory,
SqlValidator.Config config) {
super(opTab, catalogReader, typeFactory, config);
}
@Override
public void validateLiteral(SqlLiteral literal) {
if (literal.getTypeName() == DECIMAL) {
final BigDecimal decimal = literal.getValueAs(BigDecimal.class);
if (decimal.precision() > DecimalType.MAX_PRECISION) {
throw newValidationError(
literal, Static.RESOURCE.numberLiteralOutOfRange(decimal.toString()));
}
}
super.validateLiteral(literal);
}
@Override
protected void validateJoin(SqlJoin join, SqlValidatorScope scope) {
// Due to the improper translation of lateral table left outer join in Calcite, we need to
// temporarily forbid the common predicates until the problem is fixed (see FLINK-7865).
if (join.getJoinType() == JoinType.LEFT
&& SqlUtil.stripAs(join.getRight()).getKind() == SqlKind.COLLECTION_TABLE) {
SqlNode right = SqlUtil.stripAs(join.getRight());
if (right instanceof SqlBasicCall) {
SqlBasicCall call = (SqlBasicCall) right;
SqlNode operand0 = call.operand(0);
if (operand0 instanceof SqlBasicCall
&& ((SqlBasicCall) operand0).getOperator()
instanceof SqlWindowTableFunction) {
return;
}
}
final SqlNode condition = join.getCondition();
if (condition != null
&& (!SqlUtil.isLiteral(condition)
|| ((SqlLiteral) condition).getValueAs(Boolean.class)
!= Boolean.TRUE)) {
throw new ValidationException(
String.format(
"Left outer joins with a table function do not accept a predicate such as %s. "
+ "Only literal TRUE is accepted.",
condition));
}
}
super.validateJoin(join, scope);
}
@Override
public void validateColumnListParams(
SqlFunction function, List argTypes, List operands) {
// we don't support column lists and translate them into the unknown type in the type
// factory,
// this makes it possible to ignore them in the validator and fall back to regular row types
// see also SqlFunction#deriveType
}
}