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

The following examples show how to use org.apache.calcite.sql.type.InferTypes. 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: 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 2
Source Project: alchemy   Source File: MysqlSideFunction.java    License: Apache License 2.0 6 votes vote down vote up
private List<SqlBasicCall> createConditionNodes(List<String> conditions, Alias alias) {
    SqlBinaryOperator equal = new SqlBinaryOperator("=", SqlKind.EQUALS, 30, true, ReturnTypes.BOOLEAN_NULLABLE,
        InferTypes.FIRST_KNOWN, OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED);
    List<SqlBasicCall> nodes = new ArrayList<>(conditions.size());
    int num = 0;
    for (String condition : conditions) {
        List<String> fields = new ArrayList<>(2);
        fields.add(alias.getAlias());
        fields.add(condition);
        SqlIdentifier leftIdentifier = new SqlIdentifier(fields, new SqlParserPos(0, 0));
        SqlDynamicParam sqlDynamicParam = new SqlDynamicParam(num++, new SqlParserPos(0, 0));
        SqlNode[] sqlNodes = new SqlNode[2];
        sqlNodes[0] = leftIdentifier;
        sqlNodes[1] = sqlDynamicParam;
        SqlBasicCall andEqual = new SqlBasicCall(equal, sqlNodes, new SqlParserPos(0, 0));
        nodes.add(andEqual);
    }
    return nodes;
}
 
Example 3
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 4
Source Project: calcite   Source File: SqlPosixRegexOperator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a SqlPosixRegexOperator.
 *
 * @param name    Operator name
 * @param kind    Kind
 * @param negated Whether this is '!~' or '!~*'
 */
SqlPosixRegexOperator(
    String name,
    SqlKind kind,
    boolean caseSensitive,
    boolean negated) {
  super(
      name,
      kind,
      32,
      true,
      ReturnTypes.BOOLEAN_NULLABLE,
      InferTypes.FIRST_KNOWN,
      OperandTypes.STRING_SAME_SAME_SAME);
  this.caseSensitive = caseSensitive;
  this.negated = negated;
}
 
Example 5
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 6
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 7
Source Project: Bats   Source File: SqlLiteralChainOperator.java    License: Apache License 2.0 5 votes vote down vote up
SqlLiteralChainOperator() {
  super(
      "$LiteralChain",
      SqlKind.LITERAL_CHAIN,
      80,
      true,

      // precedence tighter than the * and || operators
      ReturnTypes.ARG0,
      InferTypes.FIRST_KNOWN,
      OperandTypes.VARIADIC);
}
 
Example 8
Source Project: Bats   Source File: SqlInOperator.java    License: Apache License 2.0 5 votes vote down vote up
protected SqlInOperator(String name, SqlKind kind) {
  super(name, kind,
      32,
      true,
      ReturnTypes.BOOLEAN_NULLABLE,
      InferTypes.FIRST_KNOWN,
      null);
}
 
Example 9
Source Project: Bats   Source File: SqlMultisetValueConstructor.java    License: Apache License 2.0 5 votes vote down vote up
protected SqlMultisetValueConstructor(String name, SqlKind kind) {
  super(
      name,
      kind, MDX_PRECEDENCE,
      false,
      ReturnTypes.ARG0,
      InferTypes.FIRST_KNOWN,
      OperandTypes.VARIADIC);
}
 
Example 10
Source Project: Bats   Source File: SqlCastFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlCastFunction() {
  super(
      "CAST",
      SqlKind.CAST,
      null,
      InferTypes.FIRST_KNOWN,
      null,
      SqlFunctionCategory.SYSTEM);
}
 
Example 11
Source Project: Bats   Source File: SqlDatetimeSubtractionOperator.java    License: Apache License 2.0 5 votes vote down vote up
public SqlDatetimeSubtractionOperator() {
  super(
      "-",
      SqlKind.MINUS,
      40,
      true,
      ReturnTypes.ARG2_NULLABLE,
      InferTypes.FIRST_KNOWN, OperandTypes.MINUS_DATE_OPERATOR);
}
 
Example 12
Source Project: Bats   Source File: SqlMultisetSetOperator.java    License: Apache License 2.0 5 votes vote down vote up
public SqlMultisetSetOperator(String name, int prec, boolean all) {
  super(
      name,
      SqlKind.OTHER,
      prec,
      true,
      ReturnTypes.MULTISET_NULLABLE,
      InferTypes.FIRST_KNOWN,
      OperandTypes.MULTISET_MULTISET);
  this.all = all;
}
 
Example 13
Source Project: Bats   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 14
Source Project: flink   Source File: ProctimeMaterializeSqlFunction.java    License: Apache License 2.0 5 votes vote down vote up
public ProctimeMaterializeSqlFunction() {
	super(
		"PROCTIME_MATERIALIZE",
		SqlKind.OTHER_FUNCTION,
		ReturnTypes.cascade(
				ReturnTypes.explicit(SqlTypeName.TIMESTAMP),
				SqlTypeTransforms.TO_NULLABLE),
		InferTypes.RETURN_TYPE,
		OperandTypes.family(SqlTypeFamily.TIMESTAMP),
		SqlFunctionCategory.SYSTEM);
}
 
Example 15
Source Project: flink   Source File: StreamRecordTimestampSqlFunction.java    License: Apache License 2.0 5 votes vote down vote up
public StreamRecordTimestampSqlFunction() {
	super(
		"STREAMRECORD_TIMESTAMP",
		SqlKind.OTHER_FUNCTION,
		ReturnTypes.explicit(SqlTypeName.BIGINT),
		InferTypes.RETURN_TYPE,
		OperandTypes.family(SqlTypeFamily.NUMERIC),
		SqlFunctionCategory.SYSTEM);
}
 
Example 16
Source Project: alchemy   Source File: MysqlSideFunction.java    License: Apache License 2.0 5 votes vote down vote up
private String modifySql(SideTable sideTable) throws SqlParseException {
    SqlParser.Config config = SqlParser.configBuilder().setLex(Lex.MYSQL).build();
    SqlParser sqlParser = SqlParser.create(sideTable.getSql(), config);
    SqlNode sqlNode = sqlParser.parseStmt();
    if (SqlKind.SELECT != sqlNode.getKind()) {
        throw new UnsupportedOperationException(
            "MysqlAsyncReqRow only support query sql, sql:" + sideTable.getSql());
    }
    SqlSelect sqlSelect = (SqlSelect)sqlNode;
    SqlNode whereNode = sqlSelect.getWhere();
    SqlBinaryOperator and = new SqlBinaryOperator("AND", SqlKind.AND, 24, true,
        ReturnTypes.BOOLEAN_NULLABLE_OPTIMIZED, InferTypes.BOOLEAN, OperandTypes.BOOLEAN_BOOLEAN);
    List<SqlBasicCall> conditionNodes = createConditionNodes(sideTable.getConditions(), sideTable.getSideAlias());
    List<SqlNode> nodes = new ArrayList<>();
    nodes.addAll(conditionNodes);
    if (whereNode != null) {
        nodes.add(whereNode);
    } else {
        SqlBinaryOperator equal = new SqlBinaryOperator("=", SqlKind.EQUALS, 30, true, ReturnTypes.BOOLEAN_NULLABLE,
            InferTypes.FIRST_KNOWN, OperandTypes.COMPARABLE_UNORDERED_COMPARABLE_UNORDERED);
        SqlBasicCall andEqual
            = new SqlBasicCall(equal, SideParser.createEqualNodes(SqlKind.AND), new SqlParserPos(0, 0));
        nodes.add(andEqual);
    }
    SqlBasicCall sqlBasicCall
        = new SqlBasicCall(and, nodes.toArray(new SqlNode[nodes.size()]), new SqlParserPos(0, 0));
    sqlSelect.setWhere(sqlBasicCall);
    return sqlSelect.toString();
}
 
Example 17
Source Project: kylin-on-parquet-v2   Source File: OLAPAggregateRel.java    License: Apache License 2.0 5 votes vote down vote up
SqlAggFunction createCustomAggFunction(String funcName, RelDataType returnType, Class<?> customAggFuncClz) {
    RelDataTypeFactory typeFactory = getCluster().getTypeFactory();
    SqlIdentifier sqlIdentifier = new SqlIdentifier(funcName, new SqlParserPos(1, 1));
    AggregateFunction aggFunction = AggregateFunctionImpl.create(customAggFuncClz);
    List<RelDataType> argTypes = new ArrayList<RelDataType>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>();
    for (FunctionParameter o : aggFunction.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    return new SqlUserDefinedAggFunction(sqlIdentifier, ReturnTypes.explicit(returnType),
            InferTypes.explicit(argTypes), OperandTypes.family(typeFamilies), aggFunction, false, false,
            typeFactory);
}
 
Example 18
Source Project: dremio-oss   Source File: DremioCatalogReader.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Rest of class is utility functions taken directly from CalciteCatalogReader. This is because that class consider these utilities to be private concerns.
 */
private SqlOperator toOp(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 Predicate<Integer> optional =
      new Predicate<Integer>() {
        @Override
        public boolean apply(Integer input) {
          return function.getParameters().get(input).isOptional();
        }
      };
  final FamilyOperandTypeChecker typeChecker =
      OperandTypes.family(typeFamilies, optional);
  final List<RelDataType> paramTypes = toSql(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, 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 19
Source Project: kylin   Source File: OLAPAggregateRel.java    License: Apache License 2.0 5 votes vote down vote up
SqlAggFunction createCustomAggFunction(String funcName, RelDataType returnType, Class<?> customAggFuncClz) {
    RelDataTypeFactory typeFactory = getCluster().getTypeFactory();
    SqlIdentifier sqlIdentifier = new SqlIdentifier(funcName, new SqlParserPos(1, 1));
    AggregateFunction aggFunction = AggregateFunctionImpl.create(customAggFuncClz);
    List<RelDataType> argTypes = new ArrayList<RelDataType>();
    List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>();
    for (FunctionParameter o : aggFunction.getParameters()) {
        final RelDataType type = o.getType(typeFactory);
        argTypes.add(type);
        typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
    }
    return new SqlUserDefinedAggFunction(sqlIdentifier, ReturnTypes.explicit(returnType),
            InferTypes.explicit(argTypes), OperandTypes.family(typeFamilies), aggFunction, false, false,
            typeFactory);
}
 
Example 20
Source Project: flink   Source File: ProctimeMaterializeSqlFunction.java    License: Apache License 2.0 5 votes vote down vote up
public ProctimeMaterializeSqlFunction() {
	super(
		"PROCTIME_MATERIALIZE",
		SqlKind.OTHER_FUNCTION,
		ReturnTypes.cascade(
				ReturnTypes.explicit(SqlTypeName.TIMESTAMP, 3),
				SqlTypeTransforms.TO_NULLABLE),
		InferTypes.RETURN_TYPE,
		OperandTypes.family(SqlTypeFamily.TIMESTAMP),
		SqlFunctionCategory.SYSTEM);
}
 
Example 21
Source Project: flink   Source File: StreamRecordTimestampSqlFunction.java    License: Apache License 2.0 5 votes vote down vote up
public StreamRecordTimestampSqlFunction() {
	super(
		"STREAMRECORD_TIMESTAMP",
		SqlKind.OTHER_FUNCTION,
		ReturnTypes.explicit(SqlTypeName.BIGINT),
		InferTypes.RETURN_TYPE,
		OperandTypes.family(SqlTypeFamily.NUMERIC),
		SqlFunctionCategory.SYSTEM);
}
 
Example 22
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 23
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 24
Source Project: calcite   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 25
Source Project: calcite   Source File: SqlLiteralChainOperator.java    License: Apache License 2.0 5 votes vote down vote up
SqlLiteralChainOperator() {
  super(
      "$LiteralChain",
      SqlKind.LITERAL_CHAIN,
      80,
      true,

      // precedence tighter than the * and || operators
      ReturnTypes.ARG0,
      InferTypes.FIRST_KNOWN,
      OperandTypes.VARIADIC);
}
 
Example 26
Source Project: calcite   Source File: SqlInOperator.java    License: Apache License 2.0 5 votes vote down vote up
protected SqlInOperator(String name, SqlKind kind) {
  super(name, kind,
      32,
      true,
      ReturnTypes.BOOLEAN_NULLABLE,
      InferTypes.FIRST_KNOWN,
      null);
}
 
Example 27
Source Project: calcite   Source File: SqlMultisetValueConstructor.java    License: Apache License 2.0 5 votes vote down vote up
protected SqlMultisetValueConstructor(String name, SqlKind kind) {
  super(
      name,
      kind, MDX_PRECEDENCE,
      false,
      ReturnTypes.ARG0,
      InferTypes.FIRST_KNOWN,
      OperandTypes.VARIADIC);
}
 
Example 28
Source Project: calcite   Source File: SqlJsonArrayAggAggFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlJsonArrayAggAggFunction(SqlKind kind,
    SqlJsonConstructorNullClause nullClause) {
  super(kind + "_" + nullClause.name(), null, kind, ReturnTypes.VARCHAR_2000,
      InferTypes.ANY_NULLABLE, OperandTypes.family(SqlTypeFamily.ANY),
      SqlFunctionCategory.SYSTEM, false, false, Optionality.OPTIONAL);
  this.nullClause = Objects.requireNonNull(nullClause);
}
 
Example 29
Source Project: calcite   Source File: SqlCastFunction.java    License: Apache License 2.0 5 votes vote down vote up
public SqlCastFunction() {
  super("CAST",
      SqlKind.CAST,
      null,
      InferTypes.FIRST_KNOWN,
      null,
      SqlFunctionCategory.SYSTEM);
}
 
Example 30
Source Project: calcite   Source File: SqlDatetimeSubtractionOperator.java    License: Apache License 2.0 5 votes vote down vote up
public SqlDatetimeSubtractionOperator() {
  super(
      "-",
      SqlKind.MINUS,
      40,
      true,
      ReturnTypes.ARG2_NULLABLE,
      InferTypes.FIRST_KNOWN, OperandTypes.MINUS_DATE_OPERATOR);
}