use of org.apache.calcite.sql.type.SqlTypeFamily in project hazelcast by hazelcast.
the class HazelcastSqlToRelConverter method convertLiteral.
/**
* Convert a literal taking into account the type that we assigned to it during validation.
* Otherwise Apache Calcite will try to deduce literal type again, leading to incorrect exposed types.
* <p>
* For example, {@code [x:BIGINT > 1]} is interpreted as {@code [x:BIGINT > 1:BIGINT]} during the validation.
* If this method is not invoked, Apache Calcite will convert it to {[@code x:BIGINT > 1:TINYINT]} instead.
*/
private RexNode convertLiteral(SqlLiteral literal, RelDataTypeFactory typeFactory) {
RelDataType type = validator.getValidatedNodeType(literal);
Object value;
if (HazelcastTypeUtils.isIntervalType(type) && !SqlUtil.isNullLiteral(literal, false)) {
// Normalize interval literals to YEAR-MONTH or DAY-SECOND literals.
value = literal.getValueAs(BigDecimal.class);
SqlTypeFamily family = type.getSqlTypeName().getFamily();
if (family == SqlTypeFamily.INTERVAL_YEAR_MONTH) {
type = typeFactory.createSqlIntervalType(INTERVAL_YEAR_MONTH);
} else {
assert family == SqlTypeFamily.INTERVAL_DAY_TIME;
type = typeFactory.createSqlIntervalType(INTERVAL_DAY_SECOND);
}
} else {
value = literal.getValue();
}
return getRexBuilder().makeLiteral(value, type, true);
}
use of org.apache.calcite.sql.type.SqlTypeFamily in project flink by apache.
the class HiveParserSqlFunctionConverter method getUDFInfo.
private static CalciteUDFInfo getUDFInfo(String hiveUdfName, List<RelDataType> calciteArgTypes, RelDataType calciteRetType) {
CalciteUDFInfo udfInfo = new CalciteUDFInfo();
udfInfo.udfName = hiveUdfName;
String[] nameParts = hiveUdfName.split("\\.");
if (nameParts.length > 1) {
udfInfo.identifier = new SqlIdentifier(Arrays.stream(nameParts).collect(Collectors.toList()), new SqlParserPos(0, 0));
}
udfInfo.returnTypeInference = ReturnTypes.explicit(calciteRetType);
udfInfo.operandTypeInference = InferTypes.explicit(calciteArgTypes);
List<SqlTypeFamily> typeFamily = new ArrayList<>();
for (RelDataType argType : calciteArgTypes) {
typeFamily.add(Util.first(argType.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
}
udfInfo.operandTypeChecker = OperandTypes.family(Collections.unmodifiableList(typeFamily));
return udfInfo;
}
use of org.apache.calcite.sql.type.SqlTypeFamily in project calcite by apache.
the class SqlWindow method validateFrameBoundary.
private void validateFrameBoundary(SqlNode bound, boolean isRows, SqlTypeFamily orderTypeFam, SqlValidator validator, SqlValidatorScope scope) {
if (null == bound) {
return;
}
bound.validate(validator, scope);
switch(bound.getKind()) {
case LITERAL:
// "CURRENT_ROW","unbounded preceding" & "unbounded following"
break;
case OTHER:
case FOLLOWING:
case PRECEDING:
assert bound instanceof SqlCall;
final SqlNode boundVal = ((SqlCall) bound).operand(0);
// values, but allow zero.
if (isRows) {
if (boundVal instanceof SqlNumericLiteral) {
final SqlNumericLiteral boundLiteral = (SqlNumericLiteral) boundVal;
if ((!boundLiteral.isExact()) || (boundLiteral.getScale() != 0) || (0 > boundLiteral.longValue(true))) {
// true == throw if not exact (we just tested that - right?)
throw validator.newValidationError(boundVal, RESOURCE.rowMustBeNonNegativeIntegral());
}
} else {
// Allow expressions in ROWS clause
}
}
// and order by type are compatible
if (orderTypeFam != null && !isRows) {
RelDataType bndType = validator.deriveType(scope, boundVal);
SqlTypeFamily bndTypeFam = bndType.getSqlTypeName().getFamily();
switch(orderTypeFam) {
case NUMERIC:
if (SqlTypeFamily.NUMERIC != bndTypeFam) {
throw validator.newValidationError(boundVal, RESOURCE.orderByRangeMismatch());
}
break;
case DATE:
case TIME:
case TIMESTAMP:
if (SqlTypeFamily.INTERVAL_DAY_TIME != bndTypeFam && SqlTypeFamily.INTERVAL_YEAR_MONTH != bndTypeFam) {
throw validator.newValidationError(boundVal, RESOURCE.orderByRangeMismatch());
}
break;
default:
throw validator.newValidationError(boundVal, RESOURCE.orderByDataTypeProhibitsRange());
}
}
break;
default:
throw new AssertionError("Unexpected node type");
}
}
use of org.apache.calcite.sql.type.SqlTypeFamily in project flink by apache.
the class SqlCastFunction method getMonotonicity.
@Override
public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
final RelDataType castFromType = call.getOperandType(0);
final RelDataTypeFamily castFromFamily = castFromType.getFamily();
final Collator castFromCollator = castFromType.getCollation() == null ? null : castFromType.getCollation().getCollator();
final RelDataType castToType = call.getOperandType(1);
final RelDataTypeFamily castToFamily = castToType.getFamily();
final Collator castToCollator = castToType.getCollation() == null ? null : castToType.getCollation().getCollator();
if (!Objects.equals(castFromCollator, castToCollator)) {
// Cast between types compared with different collators: not monotonic.
return SqlMonotonicity.NOT_MONOTONIC;
} else if (castFromFamily instanceof SqlTypeFamily && castToFamily instanceof SqlTypeFamily && nonMonotonicCasts.containsEntry(castFromFamily, castToFamily)) {
return SqlMonotonicity.NOT_MONOTONIC;
} else {
return call.getOperandMonotonicity(0);
}
}
use of org.apache.calcite.sql.type.SqlTypeFamily in project calcite by apache.
the class CalciteCatalogReader method toOp.
/**
* 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 Predicate<Integer> optional = new PredicateImpl<Integer>() {
public boolean test(Integer input) {
return function.getParameters().get(input).isOptional();
}
};
final FamilyOperandTypeChecker typeChecker = OperandTypes.family(typeFamilies, optional);
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, 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);
}
}
Aggregations