Java Code Examples for org.apache.calcite.sql.SqlOperator#getSyntax()

The following examples show how to use org.apache.calcite.sql.SqlOperator#getSyntax() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListSqlOperatorTable.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void lookupOperatorOverloads(SqlIdentifier opName,
    SqlFunctionCategory category,
    SqlSyntax syntax,
    List<SqlOperator> operatorList) {
  for (SqlOperator operator : this.operatorList) {
    if (operator.getSyntax() != syntax) {
      continue;
    }
    if (!opName.isSimple()
        || !operator.isName(opName.getSimple())) {
      continue;
    }
    if (category != null
        && category != category(operator)
        && !category.isUserDefinedNotSpecificFunction()) {
      continue;
    }
    operatorList.add(operator);
  }
}
 
Example 2
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
@Nullable public SqlCall makeNullaryCall(SqlIdentifier id) {
	if (id.names.size() == 1 && !id.isComponentQuoted(0)) {
		final List<SqlOperator> list = new ArrayList<>();
		opTab.lookupOperatorOverloads(id, null, SqlSyntax.FUNCTION, list,
				catalogReader.nameMatcher());
		for (SqlOperator operator : list) {
			if (operator.getSyntax() == SqlSyntax.FUNCTION_ID) {
				// Even though this looks like an identifier, it is a
				// actually a call to a function. Construct a fake
				// call to this function, so we can use the regular
				// operator validation.
				return new SqlBasicCall(operator, SqlNode.EMPTY_ARRAY,
						id.getParserPosition(), true, null);
			}
		}
	}
	return null;
}
 
Example 3
Source File: ListSqlOperatorTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void lookupOperatorOverloads(SqlIdentifier opName,
    SqlFunctionCategory category,
    SqlSyntax syntax,
    List<SqlOperator> operatorList,
    SqlNameMatcher nameMatcher) {
  for (SqlOperator operator : this.operatorList) {
    if (operator.getSyntax() != syntax) {
      continue;
    }
    if (!opName.isSimple()
        || !nameMatcher.matches(operator.getName(), opName.getSimple())) {
      continue;
    }
    if (category != null
        && category != category(operator)
        && !category.isUserDefinedNotSpecificFunction()) {
      continue;
    }
    operatorList.add(operator);
  }
}
 
Example 4
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public void validateCall(
	SqlCall call,
	SqlValidatorScope scope) {
	final SqlOperator operator = call.getOperator();
	if ((call.operandCount() == 0)
		&& (operator.getSyntax() == SqlSyntax.FUNCTION_ID)
		&& !call.isExpanded()
		&& !conformance.allowNiladicParentheses()) {
		// For example, "LOCALTIME()" is illegal. (It should be
		// "LOCALTIME", which would have been handled as a
		// SqlIdentifier.)
		throw handleUnresolvedFunction(call, (SqlFunction) operator,
			ImmutableList.of(), null);
	}

	SqlValidatorScope operandScope = scope.getOperandScope(call);

	if (operator instanceof SqlFunction
		&& ((SqlFunction) operator).getFunctionType()
		== SqlFunctionCategory.MATCH_RECOGNIZE
		&& !(operandScope instanceof MatchRecognizeScope)) {
		throw newValidationError(call,
			Static.RESOURCE.functionMatchRecognizeOnly(call.toString()));
	}
	// Delegate validation to the operator.
	operator.validateCall(call, this, scope, operandScope);
}
 
Example 5
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
public void validateCall(
	SqlCall call,
	SqlValidatorScope scope) {
	final SqlOperator operator = call.getOperator();
	if ((call.operandCount() == 0)
		&& (operator.getSyntax() == SqlSyntax.FUNCTION_ID)
		&& !call.isExpanded()
		&& !conformance.allowNiladicParentheses()) {
		// For example, "LOCALTIME()" is illegal. (It should be
		// "LOCALTIME", which would have been handled as a
		// SqlIdentifier.)
		throw handleUnresolvedFunction(call, (SqlFunction) operator,
			ImmutableList.of(), null);
	}

	SqlValidatorScope operandScope = scope.getOperandScope(call);

	if (operator instanceof SqlFunction
		&& ((SqlFunction) operator).getFunctionType()
		== SqlFunctionCategory.MATCH_RECOGNIZE
		&& !(operandScope instanceof MatchRecognizeScope)) {
		throw newValidationError(call,
			Static.RESOURCE.functionMatchRecognizeOnly(call.toString()));
	}
	// Delegate validation to the operator.
	operator.validateCall(call, this, scope, operandScope);
}
 
Example 6
Source File: ReflectiveSqlOperatorTable.java    From Bats with Apache License 2.0 4 votes vote down vote up
public void lookupOperatorOverloads(SqlIdentifier opName,
    SqlFunctionCategory category,
    SqlSyntax syntax,
    List<SqlOperator> operatorList) {
  // NOTE jvs 3-Mar-2005:  ignore category until someone cares

  String simpleName;
  if (opName.names.size() > 1) {
    if (opName.names.get(opName.names.size() - 2).equals(IS_NAME)) {
      // per SQL99 Part 2 Section 10.4 Syntax Rule 7.b.ii.1
      simpleName = Util.last(opName.names);
    } else {
      return;
    }
  } else {
    simpleName = opName.getSimple();
  }

  // Always look up built-in operators case-insensitively. Even in sessions
  // with unquotedCasing=UNCHANGED and caseSensitive=true.
  final Collection<SqlOperator> list =
      operators.get(new Key(simpleName, syntax));
  if (list.isEmpty()) {
    return;
  }
  for (SqlOperator op : list) {
    if (op.getSyntax() == syntax) {
      operatorList.add(op);
    } else if (syntax == SqlSyntax.FUNCTION
        && op instanceof SqlFunction) {
      // this special case is needed for operators like CAST,
      // which are treated as functions but have special syntax
      operatorList.add(op);
    }
  }

  // REVIEW jvs 1-Jan-2005:  why is this extra lookup required?
  // Shouldn't it be covered by search above?
  switch (syntax) {
  case BINARY:
  case PREFIX:
  case POSTFIX:
    for (SqlOperator extra : operators.get(new Key(simpleName, syntax))) {
      // REVIEW: should only search operators added during this method?
      if (extra != null && !operatorList.contains(extra)) {
        operatorList.add(extra);
      }
    }
    break;
  }
}
 
Example 7
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static void findAllValidFunctionNames(
	List<String> names,
	SqlValidator validator,
	Collection<SqlMoniker> result,
	SqlParserPos pos) {
	// a function name can only be 1 part
	if (names.size() > 1) {
		return;
	}
	for (SqlOperator op : validator.getOperatorTable().getOperatorList()) {
		SqlIdentifier curOpId =
			new SqlIdentifier(
				op.getName(),
				pos);

		final SqlCall call =
			SqlUtil.makeCall(
				validator.getOperatorTable(),
				curOpId);
		if (call != null) {
			result.add(
				new SqlMonikerImpl(
					op.getName(),
					SqlMonikerType.FUNCTION));
		} else {
			if ((op.getSyntax() == SqlSyntax.FUNCTION)
				|| (op.getSyntax() == SqlSyntax.PREFIX)) {
				if (op.getOperandTypeChecker() != null) {
					String sig = op.getAllowedSignatures();
					sig = sig.replaceAll("'", "");
					result.add(
						new SqlMonikerImpl(
							sig,
							SqlMonikerType.FUNCTION));
					continue;
				}
				result.add(
					new SqlMonikerImpl(
						op.getName(),
						SqlMonikerType.FUNCTION));
			}
		}
	}
}
 
Example 8
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
private static void findAllValidFunctionNames(
	List<String> names,
	SqlValidator validator,
	Collection<SqlMoniker> result,
	SqlParserPos pos) {
	// a function name can only be 1 part
	if (names.size() > 1) {
		return;
	}
	for (SqlOperator op : validator.getOperatorTable().getOperatorList()) {
		SqlIdentifier curOpId =
			new SqlIdentifier(
				op.getName(),
				pos);

		final SqlCall call = validator.makeNullaryCall(curOpId);
		if (call != null) {
			result.add(
				new SqlMonikerImpl(
					op.getName(),
					SqlMonikerType.FUNCTION));
		} else {
			if ((op.getSyntax() == SqlSyntax.FUNCTION)
				|| (op.getSyntax() == SqlSyntax.PREFIX)) {
				if (op.getOperandTypeChecker() != null) {
					String sig = op.getAllowedSignatures();
					sig = sig.replaceAll("'", "");
					result.add(
						new SqlMonikerImpl(
							sig,
							SqlMonikerType.FUNCTION));
					continue;
				}
				result.add(
					new SqlMonikerImpl(
						op.getName(),
						SqlMonikerType.FUNCTION));
			}
		}
	}
}
 
Example 9
Source File: ReflectiveSqlOperatorTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void lookupOperatorOverloads(SqlIdentifier opName,
    SqlFunctionCategory category, SqlSyntax syntax,
    List<SqlOperator> operatorList, SqlNameMatcher nameMatcher) {
  // NOTE jvs 3-Mar-2005:  ignore category until someone cares

  String simpleName;
  if (opName.names.size() > 1) {
    if (opName.names.get(opName.names.size() - 2).equals(IS_NAME)) {
      // per SQL99 Part 2 Section 10.4 Syntax Rule 7.b.ii.1
      simpleName = Util.last(opName.names);
    } else {
      return;
    }
  } else {
    simpleName = opName.getSimple();
  }

  final Collection<SqlOperator> list =
      lookUpOperators(simpleName, syntax, nameMatcher);
  if (list.isEmpty()) {
    return;
  }
  for (SqlOperator op : list) {
    if (op.getSyntax() == syntax) {
      operatorList.add(op);
    } else if (syntax == SqlSyntax.FUNCTION
        && op instanceof SqlFunction) {
      // this special case is needed for operators like CAST,
      // which are treated as functions but have special syntax
      operatorList.add(op);
    }
  }

  // REVIEW jvs 1-Jan-2005:  why is this extra lookup required?
  // Shouldn't it be covered by search above?
  switch (syntax) {
  case BINARY:
  case PREFIX:
  case POSTFIX:
    for (SqlOperator extra
        : lookUpOperators(simpleName, syntax, nameMatcher)) {
      // REVIEW: should only search operators added during this method?
      if (extra != null && !operatorList.contains(extra)) {
        operatorList.add(extra);
      }
    }
    break;
  }
}