Java Code Examples for org.apache.calcite.sql.SqlFunctionCategory

The following examples show how to use org.apache.calcite.sql.SqlFunctionCategory. These examples are extracted from open source projects. 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 Project: Bats   Source File: StandardConvertletTable.java    License: Apache License 2.0 6 votes vote down vote up
public RexNode convertFunction(
    SqlRexContext cx,
    SqlFunction fun,
    SqlCall call) {
  final List<SqlNode> operands = call.getOperandList();
  final List<RexNode> exprs = convertExpressionList(cx, operands,
      SqlOperandTypeChecker.Consistency.NONE);
  if (fun.getFunctionType() == SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR) {
    return makeConstructorCall(cx, fun, exprs);
  }
  RelDataType returnType =
      cx.getValidator().getValidatedNodeTypeIfKnown(call);
  if (returnType == null) {
    returnType = cx.getRexBuilder().deriveReturnType(fun, exprs);
  }
  return cx.getRexBuilder().makeCall(returnType, fun, exprs);
}
 
Example 2
Source Project: dremio-oss   Source File: SqlDatePartOperator.java    License: Apache License 2.0 6 votes vote down vote up
public SqlDatePartOperator() {
  super(
      "DATE_PART",
      SqlKind.OTHER_FUNCTION,
      ReturnTypes.BIGINT_NULLABLE,
      null,
      OperandTypes.sequence(
          "<PERIOD LITERAL>, <DATE or TIMESTAMP or INTERVAL>",
          new EnumeratedListChecker(VALID_PERIODS.keySet()),
          OperandTypes.or(
              OperandTypes.family(SqlTypeFamily.DATE),
              OperandTypes.family(SqlTypeFamily.TIMESTAMP),
              OperandTypes.family(SqlTypeFamily.DATETIME),
              OperandTypes.family(SqlTypeFamily.DATETIME_INTERVAL),
              OperandTypes.family(SqlTypeFamily.INTERVAL_DAY_TIME),
              OperandTypes.family(SqlTypeFamily.INTERVAL_YEAR_MONTH))
          ),
          SqlFunctionCategory.SYSTEM);
}
 
Example 3
Source Project: Bats   Source File: SqlMinMaxAggFunction.java    License: Apache License 2.0 6 votes vote down vote up
/** Creates a SqlMinMaxAggFunction. */
public SqlMinMaxAggFunction(SqlKind kind) {
  super(kind.name(),
      null,
      kind,
      ReturnTypes.ARG0_NULLABLE_IF_EMPTY,
      null,
      OperandTypes.COMPARABLE_ORDERED,
      SqlFunctionCategory.SYSTEM,
      false,
      false,
      Optionality.FORBIDDEN);
  this.argTypes = ImmutableList.of();
  this.minMaxKind = MINMAX_COMPARABLE;
  Preconditions.checkArgument(kind == SqlKind.MIN
      || kind == SqlKind.MAX);
}
 
Example 4
Source Project: flink   Source File: FunctionCatalogOperatorTable.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void lookupOperatorOverloads(
		SqlIdentifier opName,
		SqlFunctionCategory category,
		SqlSyntax syntax,
		List<SqlOperator> operatorList,
		SqlNameMatcher nameMatcher) {
	if (!opName.isSimple()) {
		return;
	}

	// We lookup only user functions via CatalogOperatorTable. Built in functions should
	// go through BasicOperatorTable
	if (isNotUserFunction(category)) {
		return;
	}

	String name = opName.getSimple();
	Optional<FunctionLookup.Result> candidateFunction = functionCatalog.lookupFunction(
		UnresolvedIdentifier.of(name));

	candidateFunction.flatMap(lookupResult ->
		convertToSqlFunction(category, name, lookupResult.getFunctionDefinition())
	).ifPresent(operatorList::add);
}
 
Example 5
Source Project: Bats   Source File: ListSqlOperatorTable.java    License: 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 6
Source Project: Bats   Source File: CalciteCatalogReader.java    License: Apache License 2.0 6 votes vote down vote up
public void lookupOperatorOverloads(final SqlIdentifier opName,
    SqlFunctionCategory category,
    SqlSyntax syntax,
    List<SqlOperator> operatorList) {
  if (syntax != SqlSyntax.FUNCTION) {
    return;
  }

  final Predicate<Function> predicate;
  if (category == null) {
    predicate = function -> true;
  } else if (category.isTableFunction()) {
    predicate = function ->
        function instanceof TableMacro
            || function instanceof TableFunction;
  } else {
    predicate = function ->
        !(function instanceof TableMacro
            || function instanceof TableFunction);
  }
  getFunctionsFrom(opName.names)
      .stream()
      .filter(predicate)
      .map(function -> toOp(opName, function))
      .forEachOrdered(operatorList::add);
}
 
Example 7
Source Project: calcite   Source File: SqlBitOpAggFunction.java    License: Apache License 2.0 6 votes vote down vote up
/** Creates a SqlBitOpAggFunction. */
public SqlBitOpAggFunction(SqlKind kind) {
  super(kind.name(),
      null,
      kind,
      ReturnTypes.ARG0_NULLABLE_IF_EMPTY,
      null,
      OperandTypes.INTEGER,
      SqlFunctionCategory.NUMERIC,
      false,
      false,
      Optionality.FORBIDDEN);
  Preconditions.checkArgument(kind == SqlKind.BIT_AND
      || kind == SqlKind.BIT_OR
      || kind == SqlKind.BIT_XOR);
}
 
Example 8
Source Project: calcite   Source File: StandardConvertletTable.java    License: Apache License 2.0 6 votes vote down vote up
public RexNode convertFunction(
    SqlRexContext cx,
    SqlFunction fun,
    SqlCall call) {
  final List<SqlNode> operands = call.getOperandList();
  final List<RexNode> exprs = convertExpressionList(cx, operands,
      SqlOperandTypeChecker.Consistency.NONE);
  if (fun.getFunctionType() == SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR) {
    return makeConstructorCall(cx, fun, exprs);
  }
  RelDataType returnType =
      cx.getValidator().getValidatedNodeTypeIfKnown(call);
  if (returnType == null) {
    returnType = cx.getRexBuilder().deriveReturnType(fun, exprs);
  }
  return cx.getRexBuilder().makeCall(returnType, fun, exprs);
}
 
Example 9
Source Project: flink   Source File: SqlListAggFunction.java    License: Apache License 2.0 6 votes vote down vote up
public SqlListAggFunction() {
	super("LISTAGG",
			null,
			SqlKind.LISTAGG,
			ReturnTypes.ARG0_NULLABLE,
			null,
			OperandTypes.or(
					OperandTypes.CHARACTER,
					OperandTypes.sequence(
							"'LISTAGG(<CHARACTER>, <CHARACTER_LITERAL>)'",
							OperandTypes.CHARACTER,
							OperandTypes.and(OperandTypes.CHARACTER, OperandTypes.LITERAL)
					)),
			SqlFunctionCategory.SYSTEM,
			false,
			false);
}
 
Example 10
Source Project: Bats   Source File: SqlUserDefinedAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a SqlUserDefinedAggFunction. */
public SqlUserDefinedAggFunction(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker, AggregateFunction function,
    boolean requiresOrder, boolean requiresOver,
    Optionality requiresGroupOrder, RelDataTypeFactory typeFactory) {
  super(Util.last(opName.names), opName, SqlKind.OTHER_FUNCTION,
      returnTypeInference, operandTypeInference, operandTypeChecker,
      SqlFunctionCategory.USER_DEFINED_FUNCTION, requiresOrder, requiresOver,
      requiresGroupOrder);
  this.function = function;
  this.typeFactory = typeFactory;
}
 
Example 11
Source Project: Bats   Source File: SqlUserDefinedTableMacro.java    License: Apache License 2.0 5 votes vote down vote up
public SqlUserDefinedTableMacro(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker, List<RelDataType> paramTypes,
    TableMacro tableMacro) {
  super(Util.last(opName.names), opName, SqlKind.OTHER_FUNCTION,
      returnTypeInference, operandTypeInference, operandTypeChecker,
      Objects.requireNonNull(paramTypes),
      SqlFunctionCategory.USER_DEFINED_TABLE_FUNCTION);
  this.tableMacro = tableMacro;
}
 
Example 12
Source Project: Bats   Source File: SqlUserDefinedFunction.java    License: Apache License 2.0 5 votes vote down vote up
/** Creates a {@link SqlUserDefinedFunction}. */
public SqlUserDefinedFunction(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    List<RelDataType> paramTypes,
    Function function) {
  this(opName, returnTypeInference, operandTypeInference, operandTypeChecker,
      paramTypes, function, SqlFunctionCategory.USER_DEFINED_FUNCTION);
}
 
Example 13
Source Project: Bats   Source File: SqlUserDefinedFunction.java    License: Apache License 2.0 5 votes vote down vote up
/** Constructor used internally and by derived classes. */
protected SqlUserDefinedFunction(SqlIdentifier opName,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    List<RelDataType> paramTypes,
    Function function,
    SqlFunctionCategory category) {
  super(Util.last(opName.names), opName, SqlKind.OTHER_FUNCTION,
      returnTypeInference, operandTypeInference, operandTypeChecker,
      paramTypes, category);
  this.function = function;
}
 
Example 14
Source Project: calcite   Source File: SqlSingleValueAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlSingleValueAggFunction(
    RelDataType type) {
  super(
      "SINGLE_VALUE",
      null,
      SqlKind.SINGLE_VALUE,
      ReturnTypes.ARG0,
      null,
      OperandTypes.ANY,
      SqlFunctionCategory.SYSTEM,
      false,
      false,
      Optionality.FORBIDDEN);
  this.type = type;
}
 
Example 15
Source Project: Bats   Source File: SqlCurrentDateFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlCurrentDateFunction() {
  super(
      "CURRENT_DATE",
      SqlKind.OTHER_FUNCTION,
      ReturnTypes.DATE,
      null,
      OperandTypes.NILADIC,
      SqlFunctionCategory.TIMEDATE);
}
 
Example 16
Source Project: Bats   Source File: SqlFloorFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlFloorFunction(SqlKind kind) {
  super(kind.name(), kind, ReturnTypes.ARG0_OR_EXACT_NO_SCALE, null,
      OperandTypes.or(OperandTypes.NUMERIC_OR_INTERVAL,
          OperandTypes.sequence(
              "'" + kind + "(<DATE> TO <TIME_UNIT>)'\n"
              + "'" + kind + "(<TIME> TO <TIME_UNIT>)'\n"
              + "'" + kind + "(<TIMESTAMP> TO <TIME_UNIT>)'",
              OperandTypes.DATETIME,
              OperandTypes.ANY)),
      SqlFunctionCategory.NUMERIC);
  Preconditions.checkArgument(kind == SqlKind.FLOOR || kind == SqlKind.CEIL);
}
 
Example 17
Source Project: marble   Source File: HiveSqlAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
protected HiveSqlAggFunction(String name, SqlIdentifier sqlIdentifier,
    SqlKind kind,
    SqlFunctionCategory funcType,
    boolean requiresOrder,
    boolean requiresOver,
    SqlReturnTypeInference sqlReturnTypeInference) {
  super(name, sqlIdentifier,
      kind, sqlReturnTypeInference, null,
      HiveSqlFunction.ArgChecker.INSTANCE,
      funcType, requiresOrder, requiresOver);
}
 
Example 18
Source Project: calcite   Source File: CalciteCatalogReader.java    License: Apache License 2.0 5 votes vote down vote up
public void lookupOperatorOverloads(final SqlIdentifier opName,
    SqlFunctionCategory category,
    SqlSyntax syntax,
    List<SqlOperator> operatorList,
    SqlNameMatcher nameMatcher) {
  if (syntax != SqlSyntax.FUNCTION) {
    return;
  }

  final Predicate<Function> predicate;
  if (category == null) {
    predicate = function -> true;
  } else if (category.isTableFunction()) {
    predicate = function ->
        function instanceof TableMacro
            || function instanceof TableFunction;
  } else {
    predicate = function ->
        !(function instanceof TableMacro
            || function instanceof TableFunction);
  }
  getFunctionsFrom(opName.names)
      .stream()
      .filter(predicate)
      .map(function -> toOp(opName, function))
      .forEachOrdered(operatorList::add);
}
 
Example 19
Source Project: calcite   Source File: SqlTranslate3Function.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates the SqlTranslate3Function.
 */
SqlTranslate3Function() {
  super("TRANSLATE3",
      SqlKind.OTHER_FUNCTION,
      ReturnTypes.ARG0_NULLABLE_VARYING,
      null,
      OperandTypes.STRING_STRING_STRING,
      SqlFunctionCategory.STRING);
}
 
Example 20
Source Project: calcite   Source File: SqlJsonObjectFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlJsonObjectFunction() {
  super("JSON_OBJECT", SqlKind.OTHER_FUNCTION, ReturnTypes.VARCHAR_2000,
      (callBinding, returnType, operandTypes) -> {
        RelDataTypeFactory typeFactory = callBinding.getTypeFactory();
        for (int i = 0; i < operandTypes.length; i++) {
          if (i % 2 == 0) {
            operandTypes[i] = typeFactory.createSqlType(SqlTypeName.VARCHAR);
            continue;
          }
          operandTypes[i] = typeFactory.createTypeWithNullability(
              typeFactory.createSqlType(SqlTypeName.ANY), true);
        }
      }, null, SqlFunctionCategory.SYSTEM);
}
 
Example 21
Source Project: calcite   Source File: SqlHistogramAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlHistogramAggFunction(RelDataType type) {
  super(
      "$HISTOGRAM",
      null,
      SqlKind.OTHER_FUNCTION,
      ReturnTypes.HISTOGRAM,
      null,
      OperandTypes.NUMERIC_OR_STRING,
      SqlFunctionCategory.NUMERIC,
      false,
      false,
      Optionality.FORBIDDEN);
  this.type = type;
}
 
Example 22
Source Project: Bats   Source File: SqlSubstringFunction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates the SqlSubstringFunction.
 */
SqlSubstringFunction() {
  super(
      "SUBSTRING",
      SqlKind.OTHER_FUNCTION,
      ReturnTypes.ARG0_NULLABLE_VARYING,
      null,
      null,
      SqlFunctionCategory.STRING);
}
 
Example 23
Source Project: Bats   Source File: SqlDatePartFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlDatePartFunction(String name, TimeUnit timeUnit) {
  super(name,
      SqlKind.OTHER,
      ReturnTypes.BIGINT_NULLABLE,
      InferTypes.FIRST_KNOWN,
      OperandTypes.DATETIME,
      SqlFunctionCategory.TIMEDATE);
  this.timeUnit = timeUnit;
}
 
Example 24
Source Project: Bats   Source File: SqlLeadLagAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlLeadLagAggFunction(SqlKind kind) {
  super(kind.name(),
      null,
      kind,
      RETURN_TYPE,
      null,
      OPERAND_TYPES,
      SqlFunctionCategory.NUMERIC,
      false,
      true,
      Optionality.FORBIDDEN);
  Preconditions.checkArgument(kind == SqlKind.LEAD
      || kind == SqlKind.LAG);
}
 
Example 25
Source Project: calcite   Source File: SqlNtileAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlNtileAggFunction() {
  super(
      "NTILE",
      null,
      SqlKind.NTILE,
      ReturnTypes.RANK,
      null,
      OperandTypes.POSITIVE_INTEGER_LITERAL,
      SqlFunctionCategory.NUMERIC,
      false,
      true,
      Optionality.FORBIDDEN);
}
 
Example 26
Source Project: Bats   Source File: SqlMonotonicUnaryFunction.java    License: Apache License 2.0 5 votes vote down vote up
protected SqlMonotonicUnaryFunction(
    String name,
    SqlKind kind,
    SqlReturnTypeInference returnTypeInference,
    SqlOperandTypeInference operandTypeInference,
    SqlOperandTypeChecker operandTypeChecker,
    SqlFunctionCategory funcType) {
  super(
      name,
      kind,
      returnTypeInference,
      operandTypeInference,
      operandTypeChecker,
      funcType);
}
 
Example 27
Source Project: calcite   Source File: SqlCoalesceFunction.java    License: Apache License 2.0 5 votes vote down vote up
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.cascade(ReturnTypes.LEAST_RESTRICTIVE,
          SqlTypeTransforms.LEAST_NULLABLE),
      null,
      OperandTypes.SAME_VARIADIC,
      SqlFunctionCategory.SYSTEM);
}
 
Example 28
Source Project: flink   Source File: FlinkSqlOperatorTable.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void lookupOperatorOverloads(
		SqlIdentifier opName,
		SqlFunctionCategory category,
		SqlSyntax syntax,
		List<SqlOperator> operatorList,
		SqlNameMatcher nameMatcher) {
	// set caseSensitive=false to make sure the behavior is same with before.
	super.lookupOperatorOverloads(opName, category, syntax, operatorList, SqlNameMatchers.withCaseSensitive(false));
}
 
Example 29
Source Project: calcite   Source File: MockSqlOperatorTable.java    License: Apache License 2.0 5 votes vote down vote up
public DedupFunction() {
  super("DEDUP",
      SqlKind.OTHER_FUNCTION,
      null,
      null,
      OperandTypes.VARIADIC,
      SqlFunctionCategory.USER_DEFINED_FUNCTION);
}
 
Example 30
Source Project: dremio-oss   Source File: SqlAggOperator.java    License: Apache License 2.0 5 votes vote down vote up
public SqlAggOperator(String name, int argCountMin, int argCountMax, SqlReturnTypeInference sqlReturnTypeInference) {
  super(name,
      new SqlIdentifier(name, SqlParserPos.ZERO),
      SqlKind.OTHER_FUNCTION,
      sqlReturnTypeInference,
      null,
      Checker.getChecker(argCountMin, argCountMax),
      SqlFunctionCategory.USER_DEFINED_FUNCTION);
}