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

The following examples show how to use org.apache.calcite.sql.type.InferTypes. 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: SqlLikeOperator.java    From calcite with 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 File: MysqlSideFunction.java    From alchemy with 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 File: SqlLikeOperator.java    From Bats with 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 File: SqlPosixRegexOperator.java    From calcite with 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 File: SqlDatePartFunction.java    From calcite with 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 #6
Source File: OLAPAggregateRel.java    From kylin-on-parquet-v2 with 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 #7
Source File: DremioCatalogReader.java    From dremio-oss with 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 #8
Source File: SqlAsOperator.java    From Bats with 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 #9
Source File: ProctimeMaterializeSqlFunction.java    From flink with 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 #10
Source File: StreamRecordTimestampSqlFunction.java    From flink with 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 #11
Source File: CalciteCatalogReader.java    From calcite with 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 #12
Source File: SqlAsOperator.java    From calcite with 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 #13
Source File: SqlMultisetSetOperator.java    From calcite with 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 #14
Source File: OLAPAggregateRel.java    From kylin with 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 #15
Source File: SqlLiteralChainOperator.java    From calcite with 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 #16
Source File: SqlInOperator.java    From calcite with 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 #17
Source File: SqlMultisetValueConstructor.java    From calcite with 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 #18
Source File: SqlJsonArrayAggAggFunction.java    From calcite with 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 #19
Source File: SqlCastFunction.java    From calcite with Apache License 2.0 5 votes vote down vote up
public SqlCastFunction() {
  super("CAST",
      SqlKind.CAST,
      null,
      InferTypes.FIRST_KNOWN,
      null,
      SqlFunctionCategory.SYSTEM);
}
 
Example #20
Source File: SqlDatetimeSubtractionOperator.java    From calcite with 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 #21
Source File: StreamRecordTimestampSqlFunction.java    From flink with 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 File: SqlInOperator.java    From Bats with 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 #23
Source File: SqlMultisetValueConstructor.java    From Bats with 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 #24
Source File: SqlLiteralChainOperator.java    From Bats with 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 #25
Source File: SqlDatePartFunction.java    From Bats with 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 #26
Source File: SqlCastFunction.java    From Bats with Apache License 2.0 5 votes vote down vote up
public SqlCastFunction() {
  super(
      "CAST",
      SqlKind.CAST,
      null,
      InferTypes.FIRST_KNOWN,
      null,
      SqlFunctionCategory.SYSTEM);
}
 
Example #27
Source File: SqlDatetimeSubtractionOperator.java    From Bats with 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 #28
Source File: SqlMultisetSetOperator.java    From Bats with 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 #29
Source File: MysqlSideFunction.java    From alchemy with 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 #30
Source File: CalciteCatalogReader.java    From Bats with 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);
  }
}