20 package org.apache.calcite.sql;
22 import org.apache.calcite.linq4j.Ord;
23 import org.apache.calcite.plan.Strong;
24 import org.apache.calcite.rel.type.RelDataType;
25 import org.apache.calcite.rel.type.RelDataTypeFactory;
26 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
27 import org.apache.calcite.sql.parser.SqlParserPos;
28 import org.apache.calcite.sql.type.SqlOperandTypeChecker;
29 import org.apache.calcite.sql.type.SqlOperandTypeInference;
30 import org.apache.calcite.sql.type.SqlReturnTypeInference;
31 import org.apache.calcite.sql.type.SqlTypeName;
32 import org.apache.calcite.sql.util.SqlBasicVisitor;
33 import org.apache.calcite.sql.util.SqlVisitor;
34 import org.apache.calcite.sql.validate.SqlMonotonicity;
35 import org.apache.calcite.sql.validate.SqlValidator;
37 import org.apache.calcite.sql.validate.SqlValidatorScope;
38 import org.apache.calcite.sql.validate.SqlValidatorUtil;
39 import org.apache.calcite.util.Litmus;
40 import org.apache.calcite.util.Util;
42 import com.google.common.collect.ImmutableList;
44 import java.util.Arrays;
45 import java.util.List;
46 import java.util.Objects;
47 import java.util.function.Supplier;
49 import static org.apache.calcite.util.Static.RESOURCE;
77 public static final String
NL = System.getProperty(
"line.separator");
136 this.leftPrec = leftPrecedence;
137 this.rightPrec = rightPrecedence;
166 protected static int leftPrec(
int prec,
boolean leftAssoc) {
167 assert (prec % 2) == 0;
174 protected static int rightPrec(
int prec,
boolean leftAssoc) {
175 assert (prec % 2) == 0;
196 return operandTypeChecker.getOperandCountRange();
201 throw Util.needToImplement(
this);
220 return new SqlIdentifier(
getName(), SqlParserPos.ZERO);
255 SqlLiteral functionQualifier,
257 SqlNode... operands) {
258 pos = pos.plusAll(Arrays.asList(operands));
259 return new SqlBasicCall(
this, operands, pos,
false, functionQualifier);
274 SqlNode... operands) {
288 SqlNodeList nodeList) {
291 nodeList.getParserPosition(),
303 List<? extends SqlNode> operandList) {
307 operandList.toArray(
new SqlNode[0]));
323 public SqlNode
rewriteCall(SqlValidator validator, SqlCall call) {
345 final SqlNodeList nodeList =
346 clause instanceof SqlNodeList
347 ? (SqlNodeList) clause
348 : SqlNodeList.of(clause);
349 writer.list(SqlWriter.FrameTypeEnum.SIMPLE, SqlWriter.COMMA, nodeList);
357 final SqlNodeList nodeList =
358 clause instanceof SqlNodeList
359 ? (SqlNodeList) clause
360 : SqlNodeList.of(clause);
361 final SqlBinaryOperator sepOp;
362 if (sepKind == null) {
363 sepOp = SqlWriter.COMMA;
367 sepOp = SqlStdOperatorTable.AND;
370 sepOp = SqlStdOperatorTable.OR;
373 throw new AssertionError();
376 writer.list(SqlWriter.FrameTypeEnum.SIMPLE, sepOp, nodeList);
384 if (!obj.getClass().equals(this.getClass())) {
391 public boolean isName(String testName,
boolean caseSensitive) {
392 return caseSensitive ? name.equals(testName) :
name.equalsIgnoreCase(testName);
425 SqlValidator validator,
426 SqlValidatorScope scope,
427 SqlValidatorScope operandScope) {
428 assert call.getOperator() ==
this;
429 for (SqlNode operand : call.getOperandList()) {
430 operand.validateExpr(validator, operandScope);
444 SqlValidator validator,
445 SqlValidatorScope scope,
453 SqlCallBinding opBinding =
new SqlCallBinding(validator, scope, call);
475 SqlValidator validator,
476 SqlValidatorScope scope,
491 SqlOperatorBinding opBinding) {
493 RelDataType returnType = returnTypeInference.inferReturnType(opBinding);
494 if (returnType == null) {
495 throw new IllegalArgumentException(
"Cannot infer return type for "
496 + opBinding.getOperator() +
"; operand types: "
497 + opBinding.collectOperandTypes());
504 throw Util.needToImplement(
this);
520 SqlValidator validator,
521 SqlValidatorScope scope,
523 for (SqlNode operand : call.getOperandList()) {
524 RelDataType nodeType = validator.deriveType(scope, operand);
525 assert nodeType != null;
536 SqlUtil.lookupRoutine(validator.getOperatorTable(),
getNameAsId(),
538 validator.getCatalogReader().nameMatcher(),
false);
540 ((SqlBasicCall) call).setOperator(sqlOperator);
546 SqlValidatorUtil.checkCharsetAndCollateConsistentIfCharType(
type);
552 final ImmutableList.Builder<String> nameBuilder = ImmutableList.builder();
553 for (SqlNode operand : call.getOperandList()) {
554 if (operand.getKind() == SqlKind.ARGUMENT_ASSIGNMENT) {
555 final List<SqlNode> operandList = ((SqlCall) operand).getOperandList();
556 nameBuilder.add(((SqlIdentifier) operandList.get(1)).getSimple());
559 ImmutableList<String> argNames = nameBuilder.build();
561 if (argNames.isEmpty()) {
569 SqlValidator validator,
571 List<String> argNames) {
572 if (argNames == null) {
573 return call.getOperandList();
575 if (argNames.size() < call.getOperandList().size()) {
576 throw validator.newValidationError(call,
577 RESOURCE.someButNotAllArgumentsAreNamed());
579 final int duplicate = Util.firstDuplicate(argNames);
580 if (duplicate >= 0) {
581 throw validator.newValidationError(call,
582 RESOURCE.duplicateArgumentName(argNames.get(duplicate)));
584 final ImmutableList.Builder<SqlNode> argBuilder = ImmutableList.builder();
585 for (SqlNode operand : call.getOperandList()) {
586 if (operand.getKind() == SqlKind.ARGUMENT_ASSIGNMENT) {
587 final List<SqlNode> operandList = ((SqlCall) operand).getOperandList();
588 argBuilder.add(operandList.get(0));
591 return argBuilder.build();
595 SqlValidator validator,
596 SqlValidatorScope scope,
599 boolean convertRowArgToColumnList) {
601 final SqlValidatorScope operandScope = scope.getOperandScope(call);
603 final ImmutableList.Builder<RelDataType> argTypeBuilder =
604 ImmutableList.builder();
605 for (SqlNode operand : args) {
606 RelDataType nodeType;
611 if (operand.getKind() == SqlKind.ROW && convertRowArgToColumnList) {
612 RelDataTypeFactory typeFactory = validator.getTypeFactory();
613 nodeType = typeFactory.createSqlType(SqlTypeName.COLUMN_LIST);
616 nodeType = validator.deriveType(operandScope, operand);
618 argTypeBuilder.add(nodeType);
621 return argTypeBuilder.build();
639 SqlValidator validator,
650 RelDataTypeFactory typeFactory,
651 List<RelDataType> operandTypes) {
653 new ExplicitOperatorBinding(
670 SqlCallBinding callBinding,
671 boolean throwOnFailure) {
676 throw Util.needToImplement(
this);
679 if (
kind != SqlKind.ARGUMENT_ASSIGNMENT) {
680 for (Ord<SqlNode> operand : Ord.zip(callBinding.operands())) {
681 if (operand.e != null
682 && operand.e.getKind() == SqlKind.DEFAULT
683 && !operandTypeChecker.isOptional(operand.i)) {
684 throw callBinding.newValidationError(RESOURCE.defaultForOptionalParameter());
689 return operandTypeChecker.checkOperandTypes(
695 SqlValidator validator,
696 SqlOperandTypeChecker argType,
699 if (od.isValidCount(call.operandCount())) {
702 if (od.getMin() == od.getMax()) {
703 throw validator.newValidationError(call,
704 RESOURCE.invalidArgCount(call.getOperator().
getName(), od.getMin()));
706 throw validator.newValidationError(call, RESOURCE.wrongNumOfArguments());
752 :
"If you see this, assign operandTypeChecker a value "
753 +
"or override this function";
754 return operandTypeChecker.getAllowedSignatures(
this, opNameToUse)
857 public <R> R
acceptCall(SqlVisitor<R> visitor, SqlCall call) {
858 for (SqlNode operand : call.getOperandList()) {
859 if (operand == null) {
862 operand.accept(visitor);
883 SqlVisitor<R> visitor,
885 boolean onlyExpressions,
886 SqlBasicVisitor.ArgHandler<R> argHandler) {
887 List<SqlNode> operands = call.getOperandList();
888 for (
int i = 0; i < operands.size(); i++) {
889 argHandler.visitChild(visitor, call, i, operands.get(i));
923 SqlValidatorScope scope) {
925 new SqlCallBinding(scope.getValidator(), scope, call));
937 return SqlMonotonicity.NOT_MONOTONIC;
956 return SqlKind.SYMMETRICAL.contains(
kind);
SqlCall createCall(SqlLiteral functionQualifier, SqlParserPos pos, SqlNode...operands)
Supplier< Strong.Policy > getStrongPolicyInference()
void unparseListClause(SqlWriter writer, SqlNode clause)
SqlReturnTypeInference getReturnTypeInference()
boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure)
boolean requiresCreate(List< SqlNode > operands)
SqlOperator(String name, SqlKind kind, int prec, boolean leftAssoc, SqlReturnTypeInference returnTypeInference, SqlOperandTypeInference operandTypeInference, SqlOperandTypeChecker operandTypeChecker)
RelDataType adjustType(SqlValidator validator, final SqlCall call, RelDataType type)
SqlIdentifier getNameAsId()
String getSignatureTemplate(final int operandsCount)
boolean isDeterministic()
final SqlOperandTypeChecker operandTypeChecker
SqlOperator(String name, SqlKind kind, int leftPrecedence, int rightPrecedence, SqlReturnTypeInference returnTypeInference, SqlOperandTypeInference operandTypeInference, SqlOperandTypeChecker operandTypeChecker)
boolean equals(Object obj)
final SqlCall createCall(SqlParserPos pos, SqlNode...operands)
static final int MDX_PRECEDENCE
public< R > void acceptCall(SqlVisitor< R > visitor, SqlCall call, boolean onlyExpressions, SqlBasicVisitor.ArgHandler< R > argHandler)
final RelDataType inferReturnType(RelDataTypeFactory typeFactory, List< RelDataType > operandTypes)
SqlOperandTypeInference getOperandTypeInference()
SqlMonotonicity getMonotonicity(SqlCall call, SqlValidatorScope scope)
static int rightPrec(int prec, boolean leftAssoc)
void preValidateCall(SqlValidator validator, SqlValidatorScope scope, SqlCall call)
void validateCall(SqlCall call, SqlValidator validator, SqlValidatorScope scope, SqlValidatorScope operandScope)
final SqlCall createCall(SqlParserPos pos, List<?extends SqlNode > operandList)
List< RelDataType > constructArgTypeList(SqlValidator validator, SqlValidatorScope scope, SqlCall call, List< SqlNode > args, boolean convertRowArgToColumnList)
SqlMonotonicity getMonotonicity(SqlOperatorBinding call)
boolean isName(String testName, boolean caseSensitive)
public< R > R acceptCall(SqlVisitor< R > visitor, SqlCall call)
abstract SqlSyntax getSyntax()
boolean isGroupAuxiliary()
final SqlCall createCall(SqlNodeList nodeList)
final String getAllowedSignatures()
void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec)
SqlNode rewriteCall(SqlValidator validator, SqlCall call)
static int leftPrec(int prec, boolean leftAssoc)
RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call)
boolean isDynamicFunction()
void checkOperandCount(SqlValidator validator, SqlOperandTypeChecker argType, SqlCall call)
String getAllowedSignatures(String opNameToUse)
RelDataType inferReturnType(SqlOperatorBinding opBinding)
final SqlReturnTypeInference returnTypeInference
boolean requiresDecimalExpansion()
List< SqlNode > constructOperandList(SqlValidator validator, SqlCall call, List< String > argNames)
boolean argumentMustBeScalar(int ordinal)
List< String > constructArgNameList(SqlCall call)
void unparseListClause(SqlWriter writer, SqlNode clause, SqlKind sepKind)
final SqlOperandTypeInference operandTypeInference
SqlOperandTypeChecker getOperandTypeChecker()
SqlOperandCountRange getOperandCountRange()
final RelDataType validateOperands(SqlValidator validator, SqlValidatorScope scope, SqlCall call)
boolean validRexOperands(int count, Litmus litmus)