use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project calcite by apache.
the class Strong method createPolicyMap.
private static Map<SqlKind, Policy> createPolicyMap() {
EnumMap<SqlKind, Policy> map = new EnumMap<>(SqlKind.class);
map.put(SqlKind.INPUT_REF, Policy.AS_IS);
map.put(SqlKind.LOCAL_REF, Policy.AS_IS);
map.put(SqlKind.DYNAMIC_PARAM, Policy.AS_IS);
map.put(SqlKind.OTHER_FUNCTION, Policy.AS_IS);
// The following types of expressions could potentially be custom.
map.put(SqlKind.CASE, Policy.AS_IS);
map.put(SqlKind.DECODE, Policy.AS_IS);
// NULLIF(1, NULL) yields 1, but NULLIF(1, 1) yields NULL
map.put(SqlKind.NULLIF, Policy.AS_IS);
// COALESCE(NULL, 2) yields 2
map.put(SqlKind.COALESCE, Policy.AS_IS);
map.put(SqlKind.NVL, Policy.AS_IS);
// FALSE AND NULL yields FALSE
// TRUE AND NULL yields NULL
map.put(SqlKind.AND, Policy.AS_IS);
// TRUE OR NULL yields TRUE
// FALSE OR NULL yields NULL
map.put(SqlKind.OR, Policy.AS_IS);
// Expression types with custom handlers.
map.put(SqlKind.LITERAL, Policy.CUSTOM);
map.put(SqlKind.EXISTS, Policy.NOT_NULL);
map.put(SqlKind.IS_DISTINCT_FROM, Policy.NOT_NULL);
map.put(SqlKind.IS_NOT_DISTINCT_FROM, Policy.NOT_NULL);
map.put(SqlKind.IS_NULL, Policy.NOT_NULL);
map.put(SqlKind.IS_NOT_NULL, Policy.NOT_NULL);
map.put(SqlKind.IS_TRUE, Policy.NOT_NULL);
map.put(SqlKind.IS_NOT_TRUE, Policy.NOT_NULL);
map.put(SqlKind.IS_FALSE, Policy.NOT_NULL);
map.put(SqlKind.IS_NOT_FALSE, Policy.NOT_NULL);
map.put(SqlKind.IS_DISTINCT_FROM, Policy.NOT_NULL);
map.put(SqlKind.IS_NOT_DISTINCT_FROM, Policy.NOT_NULL);
map.put(SqlKind.NOT, Policy.ANY);
map.put(SqlKind.EQUALS, Policy.ANY);
map.put(SqlKind.NOT_EQUALS, Policy.ANY);
map.put(SqlKind.LESS_THAN, Policy.ANY);
map.put(SqlKind.LESS_THAN_OR_EQUAL, Policy.ANY);
map.put(SqlKind.GREATER_THAN, Policy.ANY);
map.put(SqlKind.GREATER_THAN_OR_EQUAL, Policy.ANY);
map.put(SqlKind.LIKE, Policy.ANY);
map.put(SqlKind.SIMILAR, Policy.ANY);
map.put(SqlKind.PLUS_PREFIX, Policy.ANY);
map.put(SqlKind.MINUS_PREFIX, Policy.ANY);
map.put(SqlKind.PLUS, Policy.ANY);
map.put(SqlKind.PLUS_PREFIX, Policy.ANY);
map.put(SqlKind.MINUS, Policy.ANY);
map.put(SqlKind.MINUS_PREFIX, Policy.ANY);
map.put(SqlKind.TIMES, Policy.ANY);
map.put(SqlKind.DIVIDE, Policy.ANY);
map.put(SqlKind.CAST, Policy.ANY);
map.put(SqlKind.REINTERPRET, Policy.ANY);
map.put(SqlKind.TRIM, Policy.ANY);
map.put(SqlKind.LTRIM, Policy.ANY);
map.put(SqlKind.RTRIM, Policy.ANY);
map.put(SqlKind.CEIL, Policy.ANY);
map.put(SqlKind.FLOOR, Policy.ANY);
map.put(SqlKind.EXTRACT, Policy.ANY);
map.put(SqlKind.GREATEST, Policy.ANY);
map.put(SqlKind.LEAST, Policy.ANY);
map.put(SqlKind.TIMESTAMP_ADD, Policy.ANY);
map.put(SqlKind.TIMESTAMP_DIFF, Policy.ANY);
// Assume that any other expressions cannot be simplified.
for (SqlKind k : Iterables.concat(SqlKind.EXPRESSION, SqlKind.AGGREGATE)) {
if (!map.containsKey(k)) {
map.put(k, Policy.AS_IS);
}
}
return map;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project calcite by apache.
the class DruidExpressions method toDruidExpression.
/**
* Translates Calcite rexNode to Druid Expression when possible
* @param rexNode rexNode to convert to a Druid Expression
* @param inputRowType input row type of the rexNode to translate
* @param druidRel Druid query
*
* @return Druid Expression or null when can not convert the RexNode
*/
@Nullable
public static String toDruidExpression(final RexNode rexNode, final RelDataType inputRowType, final DruidQuery druidRel) {
SqlKind kind = rexNode.getKind();
SqlTypeName sqlTypeName = rexNode.getType().getSqlTypeName();
if (kind == SqlKind.INPUT_REF) {
final RexInputRef ref = (RexInputRef) rexNode;
final String columnName = inputRowType.getFieldNames().get(ref.getIndex());
if (columnName == null) {
return null;
}
if (druidRel.getDruidTable().timestampFieldName.equals(columnName)) {
return DruidExpressions.fromColumn(DruidTable.DEFAULT_TIMESTAMP_COLUMN);
}
return DruidExpressions.fromColumn(columnName);
}
if (rexNode instanceof RexCall) {
final SqlOperator operator = ((RexCall) rexNode).getOperator();
final DruidSqlOperatorConverter conversion = druidRel.getOperatorConversionMap().get(operator);
if (conversion == null) {
// unknown operator can not translate
return null;
} else {
return conversion.toDruidExpression(rexNode, inputRowType, druidRel);
}
}
if (kind == SqlKind.LITERAL) {
// Translate literal.
if (RexLiteral.isNullLiteral(rexNode)) {
// case the filter/project might yield to unknown let Calcite deal with this for now
return null;
} else if (SqlTypeName.NUMERIC_TYPES.contains(sqlTypeName)) {
return DruidExpressions.numberLiteral((Number) RexLiteral.value(rexNode));
} else if (SqlTypeFamily.INTERVAL_DAY_TIME == sqlTypeName.getFamily()) {
// Calcite represents DAY-TIME intervals in milliseconds.
final long milliseconds = ((Number) RexLiteral.value(rexNode)).longValue();
return DruidExpressions.numberLiteral(milliseconds);
} else if (SqlTypeFamily.INTERVAL_YEAR_MONTH == sqlTypeName.getFamily()) {
// Calcite represents YEAR-MONTH intervals in months.
final long months = ((Number) RexLiteral.value(rexNode)).longValue();
return DruidExpressions.numberLiteral(months);
} else if (SqlTypeName.STRING_TYPES.contains(sqlTypeName)) {
return DruidExpressions.stringLiteral(RexLiteral.stringValue(rexNode));
} else if (SqlTypeName.TIMESTAMP == sqlTypeName || SqlTypeName.DATE == sqlTypeName || SqlTypeName.TIME_WITH_LOCAL_TIME_ZONE == sqlTypeName) {
return DruidExpressions.numberLiteral(DruidDateTimeUtils.literalValue(rexNode, TimeZone.getTimeZone(druidRel.getConnectionConfig().timeZone())).getMillisSinceEpoch());
} else if (SqlTypeName.BOOLEAN == sqlTypeName) {
return DruidExpressions.numberLiteral(RexLiteral.booleanValue(rexNode) ? 1 : 0);
}
}
// Not Literal/InputRef/RexCall or unknown type?
return null;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project calcite by apache.
the class RexSimplify method simplifyIs.
private RexNode simplifyIs(RexCall call) {
final SqlKind kind = call.getKind();
final RexNode a = call.getOperands().get(0);
final RexNode simplified = simplifyIs2(kind, a);
if (simplified != null) {
return simplified;
}
return call;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by axbaretto.
the class DrillReduceAggregatesRule method reduceAgg.
private RexNode reduceAgg(Aggregate oldAggRel, AggregateCall oldCall, List<AggregateCall> newCalls, Map<AggregateCall, RexNode> aggCallMapping, List<RexNode> inputExprs) {
final SqlAggFunction sqlAggFunction = DrillCalciteWrapperUtility.extractSqlOperatorFromWrapper(oldCall.getAggregation());
if (sqlAggFunction instanceof SqlSumAggFunction) {
// case COUNT(x) when 0 then null else SUM0(x) end
return reduceSum(oldAggRel, oldCall, newCalls, aggCallMapping);
}
if (sqlAggFunction instanceof SqlAvgAggFunction) {
final SqlKind subtype = sqlAggFunction.getKind();
switch(subtype) {
case AVG:
// replace original AVG(x) with SUM(x) / COUNT(x)
return reduceAvg(oldAggRel, oldCall, newCalls, aggCallMapping);
case STDDEV_POP:
// / COUNT(x))
return reduceStddev(oldAggRel, oldCall, true, true, newCalls, aggCallMapping, inputExprs);
case STDDEV_SAMP:
// / CASE COUNT(x) WHEN 1 THEN NULL ELSE COUNT(x) - 1 END)
return reduceStddev(oldAggRel, oldCall, false, true, newCalls, aggCallMapping, inputExprs);
case VAR_POP:
// / COUNT(x)
return reduceStddev(oldAggRel, oldCall, true, false, newCalls, aggCallMapping, inputExprs);
case VAR_SAMP:
// / CASE COUNT(x) WHEN 1 THEN NULL ELSE COUNT(x) - 1 END
return reduceStddev(oldAggRel, oldCall, false, false, newCalls, aggCallMapping, inputExprs);
default:
throw Util.unexpected(subtype);
}
} else {
// anything else: preserve original call
RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder();
final int nGroups = oldAggRel.getGroupCount();
List<RelDataType> oldArgTypes = new ArrayList<>();
List<Integer> ordinals = oldCall.getArgList();
assert ordinals.size() <= inputExprs.size();
for (int ordinal : ordinals) {
oldArgTypes.add(inputExprs.get(ordinal).getType());
}
return rexBuilder.addAggCall(oldCall, nGroups, oldAggRel.indicator, newCalls, aggCallMapping, oldArgTypes);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlKind in project drill by axbaretto.
the class RewriteAsBinaryOperators method visitCall.
@Override
public RexNode visitCall(RexCall call) {
SqlOperator op = call.getOperator();
SqlKind kind = op.getKind();
RelDataType type = call.getType();
if (kind == SqlKind.OR || kind == SqlKind.AND) {
if (call.getOperands().size() > 2) {
List<RexNode> children = new ArrayList<>(call.getOperands());
RexNode left = children.remove(0).accept(this);
RexNode right = builder.makeCall(type, op, children).accept(this);
return builder.makeCall(type, op, ImmutableList.of(left, right));
}
}
return builder.makeCall(type, op, visitChildren(call));
}
Aggregations