use of org.apache.calcite.sql.SqlFunction in project flink-mirror by flink-ci.
the class SqlValidatorImpl method handleUnresolvedFunction.
public CalciteException handleUnresolvedFunction(SqlCall call, SqlFunction unresolvedFunction, List<RelDataType> argTypes, List<String> argNames) {
// For builtins, we can give a better error message
final List<SqlOperator> overloads = new ArrayList<>();
opTab.lookupOperatorOverloads(unresolvedFunction.getNameAsId(), null, SqlSyntax.FUNCTION, overloads, catalogReader.nameMatcher());
if (overloads.size() == 1) {
SqlFunction fun = (SqlFunction) overloads.get(0);
if ((fun.getSqlIdentifier() == null) && (fun.getSyntax() != SqlSyntax.FUNCTION_ID)) {
final int expectedArgCount = fun.getOperandCountRange().getMin();
throw newValidationError(call, RESOURCE.invalidArgCount(call.getOperator().getName(), expectedArgCount));
}
}
AssignableOperandTypeChecker typeChecking = new AssignableOperandTypeChecker(argTypes, argNames);
String signature = typeChecking.getAllowedSignatures(unresolvedFunction, unresolvedFunction.getName());
throw newValidationError(call, RESOURCE.validatorUnknownFunction(signature));
}
use of org.apache.calcite.sql.SqlFunction in project druid by apache.
the class ExpressionsTest method testRoundWithNanShouldRoundTo0.
@Test
public void testRoundWithNanShouldRoundTo0() {
final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
testHelper.testExpression(roundFunction, testHelper.makeInputRef("nan"), DruidExpression.ofExpression(ColumnType.DOUBLE, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.DOUBLE, "nan"))), 0D);
testHelper.testExpression(roundFunction, testHelper.makeInputRef("fnan"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "fnan"))), 0D);
}
use of org.apache.calcite.sql.SqlFunction in project druid by apache.
the class ExpressionsTest method testRoundWithInfinityShouldRoundTo0.
@Test
public void testRoundWithInfinityShouldRoundTo0() {
final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
// CHECKSTYLE.OFF: Regexp
testHelper.testExpression(roundFunction, testHelper.makeInputRef("inf"), DruidExpression.ofExpression(ColumnType.DOUBLE, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.DOUBLE, "inf"))), Double.MAX_VALUE);
testHelper.testExpression(roundFunction, testHelper.makeInputRef("-inf"), DruidExpression.ofExpression(ColumnType.DOUBLE, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.DOUBLE, "-inf"))), -1 * Double.MAX_VALUE);
testHelper.testExpression(roundFunction, testHelper.makeInputRef("finf"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "finf"))), Double.MAX_VALUE);
testHelper.testExpression(roundFunction, testHelper.makeInputRef("-finf"), DruidExpression.ofExpression(ColumnType.FLOAT, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.FLOAT, "-finf"))), -1 * Double.MAX_VALUE);
// CHECKSTYLE.ON: Regexp
}
use of org.apache.calcite.sql.SqlFunction in project druid by apache.
the class ExpressionsTest method testRoundWithInvalidArgument.
@Test
public void testRoundWithInvalidArgument() {
final SqlFunction roundFunction = new RoundOperatorConversion().calciteOperator();
if (!NullHandling.sqlCompatible()) {
expectException(IAE.class, "The first argument to the function[round] should be integer or double type but got the type: STRING");
}
testHelper.testExpression(roundFunction, testHelper.makeInputRef("s"), DruidExpression.ofExpression(ColumnType.STRING, DruidExpression.functionCall("round"), ImmutableList.of(DruidExpression.ofColumn(ColumnType.STRING, "s"))), NullHandling.sqlCompatible() ? null : "IAE Exception");
}
use of org.apache.calcite.sql.SqlFunction in project Mycat2 by MyCATApache.
the class RelJson method toJson.
private Object toJson(RexNode node) {
final Map<String, Object> map;
switch(node.getKind()) {
case FIELD_ACCESS:
map = jsonBuilder.map();
final RexFieldAccess fieldAccess = (RexFieldAccess) node;
map.put("field", fieldAccess.getField().getName());
map.put("expr", toJson(fieldAccess.getReferenceExpr()));
return map;
case LITERAL:
final RexLiteral literal = (RexLiteral) node;
Object value = literal.getValue3();
map = jsonBuilder.map();
if (value instanceof ByteString || value instanceof Duration || value instanceof Period || value instanceof Temporal) {
if (value instanceof Period) {
Period period = (Period) value;
switch(literal.getTypeName()) {
case INTERVAL_DAY:
value = period.getDays();
break;
case INTERVAL_WEEK:
value = period.getDays() / 7;
break;
case INTERVAL_MONTH:
value = period.getMonths();
break;
case INTERVAL_YEAR_MONTH:
value = period.getMonths();
break;
case INTERVAL_QUARTER:
value = period.getMonths() / 3;
break;
case INTERVAL_YEAR:
value = period.getYears();
break;
default:
}
map.put("literal", value);
map.put("class", value.getClass().getName());
} else // else if (value instanceof Duration) {
// switch (literal.getTypeName()){
// case INTERVAL_DAY_HOUR:
// case INTERVAL_DAY_MINUTE:
// case INTERVAL_DAY_SECOND:
// case INTERVAL_HOUR:
// case INTERVAL_HOUR_MINUTE:
// case INTERVAL_HOUR_SECOND:
// case INTERVAL_MINUTE:
// case INTERVAL_MINUTE_SECOND:
// case INTERVAL_SECOND:
// case INTERVAL_MICROSECOND:
// case INTERVAL_SECOND_MICROSECOND:
// }
// }
{
map.put("literal", value.toString());
map.put("class", value.getClass().getName());
}
} else {
map.put("literal", RelEnumTypes.fromEnum(value));
}
map.put("type", toJson(node.getType()));
return map;
case INPUT_REF:
map = jsonBuilder.map();
map.put("input", ((RexSlot) node).getIndex());
map.put("name", ((RexSlot) node).getName());
return map;
case LOCAL_REF:
map = jsonBuilder.map();
map.put("input", ((RexSlot) node).getIndex());
map.put("name", ((RexSlot) node).getName());
map.put("type", toJson(node.getType()));
return map;
case CORREL_VARIABLE:
map = jsonBuilder.map();
map.put("correl", ((RexCorrelVariable) node).getName());
map.put("type", toJson(node.getType()));
return map;
case DYNAMIC_PARAM:
map = jsonBuilder.map();
map.put("name", ((RexDynamicParam) node).getName());
map.put("index", ((RexDynamicParam) node).getIndex());
map.put("type", toJson(node.getType()));
return map;
default:
if (node instanceof RexCall) {
final RexCall call = (RexCall) node;
map = jsonBuilder.map();
map.put("op", toJson(call.getOperator()));
final List<Object> list = jsonBuilder.list();
for (RexNode operand : call.getOperands()) {
list.add(toJson(operand));
}
map.put("operands", list);
switch(node.getKind()) {
case CAST:
map.put("type", toJson(node.getType()));
}
if (call.getOperator() instanceof SqlFunction) {
if (((SqlFunction) call.getOperator()).getFunctionType().isUserDefined()) {
SqlOperator op = call.getOperator();
map.put("class", op.getClass().getName());
map.put("type", toJson(node.getType()));
map.put("deterministic", op.isDeterministic());
map.put("dynamic", op.isDynamicFunction());
}
}
if (call instanceof RexOver) {
RexOver over = (RexOver) call;
map.put("distinct", over.isDistinct());
map.put("type", toJson(node.getType()));
map.put("window", toJson(over.getWindow()));
}
if (call instanceof Window.RexWinAggCall) {
Window.RexWinAggCall rexWinAggCall = (Window.RexWinAggCall) call;
final int ordinal = rexWinAggCall.ordinal;
final boolean distinct = rexWinAggCall.distinct;
final boolean ignoreNulls = rexWinAggCall.ignoreNulls;
map.put("ordinal", toJson(ordinal));
map.put("distinct", toJson(distinct));
map.put("window", toJson(ignoreNulls));
}
return map;
}
throw new UnsupportedOperationException("unknown rex " + node);
}
}
Aggregations