Java Code Examples for org.apache.calcite.sql.type.ReturnTypes

The following examples show how to use org.apache.calcite.sql.type.ReturnTypes. 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: 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 2
Source Project: Bats   Source File: SqlFloorFunction.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Most dialects that natively support datetime floor will use this.
 * In those cases the call will look like TRUNC(datetime, 'year').
 *
 * @param writer SqlWriter
 * @param call SqlCall
 * @param funName Name of the sql function to call
 * @param datetimeFirst Specify the order of the datetime & timeUnit
 * arguments
 */
public static void unparseDatetimeFunction(SqlWriter writer, SqlCall call,
    String funName, Boolean datetimeFirst) {
  SqlFunction func = new SqlFunction(funName, SqlKind.OTHER_FUNCTION,
      ReturnTypes.ARG0_NULLABLE_VARYING, null, null,
      SqlFunctionCategory.STRING);

  SqlCall call1;
  if (datetimeFirst) {
    call1 = call;
  } else {
    // switch order of operands
    SqlNode op1 = call.operand(0);
    SqlNode op2 = call.operand(1);

    call1 = call.getOperator().createCall(call.getParserPosition(), op2, op1);
  }

  SqlUtil.unparseFunctionSyntax(func, writer, call1);
}
 
Example 3
Source Project: Bats   Source File: SqlLikeOperator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a SqlLikeOperator.
 *
 * @param name    Operator name
 * @param kind    Kind
 * @param negated Whether this is 'NOT LIKE'
 */
SqlLikeOperator(
    String name,
    SqlKind kind,
    boolean negated) {
  // LIKE is right-associative, because that makes it easier to capture
  // dangling ESCAPE clauses: "a like b like c escape d" becomes
  // "a like (b like c escape d)".
  super(
      name,
      kind,
      32,
      false,
      ReturnTypes.BOOLEAN_NULLABLE,
      InferTypes.FIRST_KNOWN,
      OperandTypes.STRING_SAME_SAME_SAME);
  this.negated = negated;
}
 
Example 4
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 5
Source Project: calcite   Source File: RexProgramTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test void testIsDeterministic() {
  SqlOperator ndc = new SqlSpecialOperator(
          "NDC",
          SqlKind.OTHER_FUNCTION,
          0,
          false,
          ReturnTypes.BOOLEAN,
          null, null) {
    @Override public boolean isDeterministic() {
      return false;
    }
  };
  RexNode n = rexBuilder.makeCall(ndc);
  assertFalse(RexUtil.isDeterministic(n));
  assertEquals(0,
          RexUtil.retainDeterministic(RelOptUtil.conjunctions(n)).size());
}
 
Example 6
Source Project: mat-calcite-plugin   Source File: RexBuilderContext.java    License: Apache License 2.0 6 votes vote down vote up
public RexNode getIObject() {
    if (object == null) {
        RelDataTypeFactory typeFactory = getCluster().getTypeFactory();
        RexBuilder b = getBuilder();
        final SqlFunction GET_IOBJECT =
                new SqlUserDefinedFunction(
                        new SqlIdentifier("GET_IOBJECT", SqlParserPos.ZERO),
                        ReturnTypes.explicit(typeFactory.createTypeWithNullability(typeFactory.createJavaType(IObject.class), false)),
                        null,
                        OperandTypes.ANY_ANY,
                        ImmutableList.of(typeFactory.createTypeWithNullability(typeFactory.createJavaType(ISnapshot.class), false),
                                typeFactory.createJavaType(int.class)),
                        ScalarFunctionImpl.create(ISnapshotMethods.class, "getIObject"));
        object = b.makeCall(GET_IOBJECT, getSnapshot(), getIObjectId());
    }
    return object;
}
 
Example 7
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 8
@Override
public RexNode getSnapshot() {
    if (snapshot == null) {
        RelDataTypeFactory typeFactory = getCluster().getTypeFactory();
        RexBuilder b = getBuilder();
        final SqlFunction UDF =
                new SqlUserDefinedFunction(
                        new SqlIdentifier("GET_SNAPSHOT", SqlParserPos.ZERO),
                        ReturnTypes.explicit(typeFactory.createTypeWithNullability(typeFactory.createJavaType(ISnapshot.class), false)),
                        null,
                        OperandTypes.NUMERIC,
                        ImmutableList.of(typeFactory.createJavaType(Integer.class)),
                        ScalarFunctionImpl.create(SnapshotHolder.class, "get"));
        snapshot = b.makeCall(UDF, b.makeLiteral(snapshotId, typeFactory.createSqlType(SqlTypeName.INTEGER), false));
    }
    return snapshot;
}
 
Example 9
Source Project: calcite   Source File: SqlFloorFunction.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Most dialects that natively support datetime floor will use this.
 * In those cases the call will look like TRUNC(datetime, 'year').
 *
 * @param writer SqlWriter
 * @param call SqlCall
 * @param funName Name of the sql function to call
 * @param datetimeFirst Specify the order of the datetime &amp; timeUnit
 * arguments
 */
public static void unparseDatetimeFunction(SqlWriter writer, SqlCall call,
    String funName, Boolean datetimeFirst) {
  SqlFunction func = new SqlFunction(funName, SqlKind.OTHER_FUNCTION,
      ReturnTypes.ARG0_NULLABLE_VARYING, null, null,
      SqlFunctionCategory.STRING);

  SqlCall call1;
  if (datetimeFirst) {
    call1 = call;
  } else {
    // switch order of operands
    SqlNode op1 = call.operand(0);
    SqlNode op2 = call.operand(1);

    call1 = call.getOperator().createCall(call.getParserPosition(), op2, op1);
  }

  SqlUtil.unparseFunctionSyntax(func, writer, call1);
}
 
Example 10
Source Project: mat-calcite-plugin   Source File: ClassRowTypeCache.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public RexNode apply(RexBuilderContext context) {
	RelOptCluster cluster = context.getCluster();
	RelDataTypeFactory typeFactory = cluster.getTypeFactory();
	final SqlFunction UDF =
			new SqlUserDefinedFunction(
					new SqlIdentifier("RESOLVE_REFERENCE", SqlParserPos.ZERO),
					ReturnTypes.explicit(typeFactory.createJavaType(HeapReference.class)),
					null,
					OperandTypes.ANY_ANY,
					ImmutableList.of(typeFactory.createTypeWithNullability(typeFactory.createJavaType(IObject.class), false),
							typeFactory.createJavaType(String.class)),
					ScalarFunctionImpl.create(IObjectMethods.class, "resolveReferenceValue"));
	RexBuilder b = context.getBuilder();
	return b.makeCall(UDF, context.getIObject(), b.makeLiteral(name));
}
 
Example 11
Source Project: calcite   Source File: SqlLikeOperator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a SqlLikeOperator.
 *
 * @param name    Operator name
 * @param kind    Kind
 * @param negated Whether this is 'NOT LIKE'
 */
SqlLikeOperator(
    String name,
    SqlKind kind,
    boolean negated) {
  // LIKE is right-associative, because that makes it easier to capture
  // dangling ESCAPE clauses: "a like b like c escape d" becomes
  // "a like (b like c escape d)".
  super(
      name,
      kind,
      32,
      false,
      ReturnTypes.BOOLEAN_NULLABLE,
      InferTypes.FIRST_KNOWN,
      OperandTypes.STRING_SAME_SAME_SAME);
  this.negated = negated;
}
 
Example 12
Source Project: Bats   Source File: SqlGroupedWindowFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public SqlGroupedWindowFunction(String name, SqlKind kind,
    SqlGroupedWindowFunction groupFunction,
    SqlOperandTypeChecker operandTypeChecker) {
  this(name, kind, groupFunction, ReturnTypes.ARG0, null, operandTypeChecker,
      SqlFunctionCategory.SYSTEM);
}
 
Example 13
Source Project: calcite   Source File: SqlCovarAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a SqlCovarAggFunction.
 */
public SqlCovarAggFunction(SqlKind kind) {
  super(kind.name(),
      null,
      kind,
      kind == SqlKind.REGR_COUNT ? ReturnTypes.BIGINT : ReturnTypes.COVAR_REGR_FUNCTION,
      null,
      OperandTypes.NUMERIC_NUMERIC,
      SqlFunctionCategory.NUMERIC,
      false,
      false,
      Optionality.FORBIDDEN);
  Preconditions.checkArgument(SqlKind.COVAR_AVG_AGG_FUNCTIONS.contains(kind),
      "unsupported sql kind: " + kind);
}
 
Example 14
Source Project: mat-calcite-plugin   Source File: ClassRowTypeCache.java    License: Apache License 2.0 5 votes vote down vote up
public RexNode apply(RexBuilderContext context) {
	RelOptCluster cluster = context.getCluster();
	RelDataTypeFactory typeFactory = cluster.getTypeFactory();
	final SqlFunction UDF =
			new SqlUserDefinedFunction(
					new SqlIdentifier("TO_REFERENCE", SqlParserPos.ZERO),
					ReturnTypes.explicit(typeFactory.createJavaType(HeapReference.class)),
					null,
					OperandTypes.ANY,
					ImmutableList.of(typeFactory.createTypeWithNullability(typeFactory.createJavaType(IObject.class), false)),
					ScalarFunctionImpl.create(ISnapshotMethods.class, "toReference")
			);
	return context.getBuilder().makeCall(UDF, context.getIObject());
}
 
Example 15
Source Project: calcite   Source File: SqlPositionFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlPositionFunction() {
  super(
      "POSITION",
      SqlKind.POSITION,
      ReturnTypes.INTEGER_NULLABLE,
      null,
      OTC_CUSTOM,
      SqlFunctionCategory.NUMERIC);
}
 
Example 16
Source Project: Bats   Source File: SqlSetOperator.java    License: Apache License 2.0 5 votes vote down vote up
public SqlSetOperator(
    String name,
    SqlKind kind,
    int prec,
    boolean all) {
  super(
      name,
      kind,
      prec,
      true,
      ReturnTypes.LEAST_RESTRICTIVE,
      null,
      OperandTypes.SET_OP);
  this.all = all;
}
 
Example 17
Source Project: calcite   Source File: CalciteCatalogReader.java    License: Apache License 2.0 5 votes vote down vote up
/** Converts a function to a {@link org.apache.calcite.sql.SqlOperator}.
 *
 * <p>The {@code typeFactory} argument is technical debt; see [CALCITE-2082]
 * Remove RelDataTypeFactory argument from SqlUserDefinedAggFunction
 * constructor. */
private static SqlOperator toOp(RelDataTypeFactory typeFactory,
    SqlIdentifier name, final Function function) {
  List<RelDataType> argTypes = new ArrayList<>();
  List<SqlTypeFamily> typeFamilies = new ArrayList<>();
  for (FunctionParameter o : function.getParameters()) {
    final RelDataType type = o.getType(typeFactory);
    argTypes.add(type);
    typeFamilies.add(
        Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
  }
  final FamilyOperandTypeChecker typeChecker =
      OperandTypes.family(typeFamilies, i ->
          function.getParameters().get(i).isOptional());
  final List<RelDataType> paramTypes = toSql(typeFactory, argTypes);
  if (function instanceof ScalarFunction) {
    return new SqlUserDefinedFunction(name, infer((ScalarFunction) function),
        InferTypes.explicit(argTypes), typeChecker, paramTypes, function);
  } else if (function instanceof AggregateFunction) {
    return new SqlUserDefinedAggFunction(name,
        infer((AggregateFunction) function), InferTypes.explicit(argTypes),
        typeChecker, (AggregateFunction) function, false, false,
        Optionality.FORBIDDEN, typeFactory);
  } else if (function instanceof TableMacro) {
    return new SqlUserDefinedTableMacro(name, ReturnTypes.CURSOR,
        InferTypes.explicit(argTypes), typeChecker, paramTypes,
        (TableMacro) function);
  } else if (function instanceof TableFunction) {
    return new SqlUserDefinedTableFunction(name, ReturnTypes.CURSOR,
        InferTypes.explicit(argTypes), typeChecker, paramTypes,
        (TableFunction) function);
  } else {
    throw new AssertionError("unknown function type " + function);
  }
}
 
Example 18
Source Project: Bats   Source File: SqlAsOperator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates an AS operator.
 */
public SqlAsOperator() {
  this(
      "AS",
      SqlKind.AS,
      20,
      true,
      ReturnTypes.ARG0,
      InferTypes.RETURN_TYPE,
      OperandTypes.ANY_ANY);
}
 
Example 19
Source Project: Bats   Source File: SqlMultisetMemberOfOperator.java    License: Apache License 2.0 5 votes vote down vote up
public SqlMultisetMemberOfOperator() {
  // TODO check if precedence is correct
  super(
      "MEMBER OF",
      SqlKind.OTHER,
      30,
      true,
      ReturnTypes.BOOLEAN_NULLABLE,
      null,
      null);
}
 
Example 20
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 21
Source Project: calcite   Source File: SqlNullifFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlNullifFunction() {
  // 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 NULLIF into CASE early.  However,
  // validator rewrite can optionally be disabled, in which case these
  // strategies are used.
  super("NULLIF",
      SqlKind.NULLIF,
      ReturnTypes.ARG0_FORCE_NULLABLE,
      null,
      OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED,
      SqlFunctionCategory.SYSTEM);
}
 
Example 22
Source Project: calcite   Source File: SqlAsOperator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates an AS operator.
 */
public SqlAsOperator() {
  this(
      "AS",
      SqlKind.AS,
      20,
      true,
      ReturnTypes.ARG0,
      InferTypes.RETURN_TYPE,
      OperandTypes.ANY_ANY);
}
 
Example 23
Source Project: Bats   Source File: SqlAvgAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
SqlAvgAggFunction(String name, SqlKind kind) {
  super(name,
      null,
      kind,
      ReturnTypes.AVG_AGG_FUNCTION,
      null,
      OperandTypes.NUMERIC,
      SqlFunctionCategory.NUMERIC,
      false,
      false,
      Optionality.FORBIDDEN);
  Preconditions.checkArgument(SqlKind.AVG_AGG_FUNCTIONS.contains(kind),
      "unsupported sql kind");
}
 
Example 24
Source Project: Bats   Source File: SqlJsonDepthFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlJsonDepthFunction() {
  super("JSON_DEPTH",
      SqlKind.OTHER_FUNCTION,
      ReturnTypes.INTEGER_NULLABLE,
      null,
      OperandTypes.ANY,
      SqlFunctionCategory.SYSTEM);
}
 
Example 25
Source Project: calcite   Source File: SqlJsonValueFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlJsonValueFunction(String name) {
  super(name, SqlKind.OTHER_FUNCTION,
      ReturnTypes.cascade(
          opBinding -> explicitTypeSpec(opBinding).orElse(getDefaultType(opBinding)),
          SqlTypeTransforms.FORCE_NULLABLE),
      null,
      OperandTypes.family(
          ImmutableList.of(SqlTypeFamily.ANY, SqlTypeFamily.CHARACTER),
          ordinal -> ordinal > 1),
      SqlFunctionCategory.SYSTEM);
}
 
Example 26
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 27
Source Project: calcite   Source File: SqlJsonStorageSizeFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlJsonStorageSizeFunction() {
  super("JSON_STORAGE_SIZE",
      SqlKind.OTHER_FUNCTION,
      ReturnTypes.cascade(ReturnTypes.INTEGER,
          SqlTypeTransforms.FORCE_NULLABLE),
      null,
      OperandTypes.ANY,
      SqlFunctionCategory.SYSTEM);
}
 
Example 28
Source Project: calcite   Source File: SqlJsonPrettyFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlJsonPrettyFunction() {
  super("JSON_PRETTY",
      SqlKind.OTHER_FUNCTION,
      ReturnTypes.cascade(ReturnTypes.VARCHAR_2000, SqlTypeTransforms.FORCE_NULLABLE),
      null,
      OperandTypes.ANY,
      SqlFunctionCategory.SYSTEM);
}
 
Example 29
Source Project: flink   Source File: SqlFirstLastValueAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlFirstLastValueAggFunction(SqlKind kind) {
	super(
			kind.name(),
			null,
			kind,
			ReturnTypes.ARG0_NULLABLE_IF_EMPTY,
			null,
			OperandTypes.ANY,
			SqlFunctionCategory.NUMERIC,
			false,
			false,
			Optionality.FORBIDDEN);
	Preconditions.checkArgument(kind == SqlKind.FIRST_VALUE
			|| kind == SqlKind.LAST_VALUE);
}
 
Example 30
Source Project: Bats   Source File: SqlMultisetQueryConstructor.java    License: Apache License 2.0 5 votes vote down vote up
protected SqlMultisetQueryConstructor(String name, SqlKind kind) {
  super(
      name,
      kind, MDX_PRECEDENCE,
      false,
      ReturnTypes.ARG0,
      null,
      OperandTypes.VARIADIC);
}