use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project hazelcast by hazelcast.
the class RexToExpression method convertCall.
/**
* Converts a {@link RexCall} to {@link Expression}.
*
* @param call the call to convert.
* @return the resulting expression.
* @throws QueryException if the given {@link RexCall} can't be
* converted.
*/
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:MethodLength", "checkstyle:ReturnCount", "checkstyle:NPathComplexity", "checkstyle:MagicNumber" })
public static Expression<?> convertCall(RexCall call, Expression<?>[] operands) {
SqlOperator operator = call.getOperator();
QueryDataType resultType = HazelcastTypeUtils.toHazelcastType(call.getType());
switch(operator.getKind()) {
case DEFAULT:
return ConstantExpression.create(null, resultType);
case CAST:
if (operands[0].getType().equals(resultType)) {
// nullable ones after the conversion.
return operands[0];
}
return CastExpression.create(operands[0], resultType);
case AND:
return AndPredicate.create(operands);
case OR:
return OrPredicate.create(operands);
case NOT:
return NotPredicate.create(operands[0]);
case PLUS:
return PlusFunction.create(operands[0], operands[1], resultType);
case MINUS:
return MinusFunction.create(operands[0], operands[1], resultType);
case TIMES:
return MultiplyFunction.create(operands[0], operands[1], resultType);
case DIVIDE:
return DivideFunction.create(operands[0], operands[1], resultType);
case MOD:
return RemainderFunction.create(operands[0], operands[1], resultType);
case MINUS_PREFIX:
return UnaryMinusFunction.create(operands[0], resultType);
case PLUS_PREFIX:
return operands[0];
case FLOOR:
return FloorCeilFunction.create(operands[0], resultType, false);
case CEIL:
return FloorCeilFunction.create(operands[0], resultType, true);
case EQUALS:
return ComparisonPredicate.create(operands[0], operands[1], ComparisonMode.EQUALS);
case NOT_EQUALS:
return ComparisonPredicate.create(operands[0], operands[1], ComparisonMode.NOT_EQUALS);
case GREATER_THAN:
return ComparisonPredicate.create(operands[0], operands[1], ComparisonMode.GREATER_THAN);
case GREATER_THAN_OR_EQUAL:
return ComparisonPredicate.create(operands[0], operands[1], ComparisonMode.GREATER_THAN_OR_EQUAL);
case LESS_THAN:
return ComparisonPredicate.create(operands[0], operands[1], ComparisonMode.LESS_THAN);
case LESS_THAN_OR_EQUAL:
return ComparisonPredicate.create(operands[0], operands[1], ComparisonMode.LESS_THAN_OR_EQUAL);
case SEARCH:
return SearchPredicate.create(operands[0], operands[1]);
case IS_TRUE:
return IsTruePredicate.create(operands[0]);
case IS_NOT_TRUE:
return IsNotTruePredicate.create(operands[0]);
case IS_FALSE:
return IsFalsePredicate.create(operands[0]);
case IS_NOT_FALSE:
return IsNotFalsePredicate.create(operands[0]);
case IS_NULL:
return IsNullPredicate.create(operands[0]);
case IS_NOT_NULL:
return IsNotNullPredicate.create(operands[0]);
case LIKE:
boolean negated = ((HazelcastLikeOperator) operator).isNegated();
Expression<?> escape1 = operands.length == 2 ? null : operands[2];
return LikeFunction.create(operands[0], operands[1], escape1, negated);
case TRIM:
assert operands.length == 3;
assert operands[0] instanceof SymbolExpression;
SqlTrimFunction.Flag trimFlag = ((SymbolExpression) operands[0]).getSymbol();
return TrimFunction.create(operands[2], operands[1], trimFlag.getLeft() == 1, trimFlag.getRight() == 1);
case EXTRACT:
assert operands.length == 2;
assert operands[0] instanceof SymbolExpression;
TimeUnitRange field = ((SymbolExpression) operands[0]).getSymbol();
ExtractField extractField = convertField(field);
return ExtractFunction.create(operands[1], extractField);
case CASE:
return CaseExpression.create(operands);
case COALESCE:
return CaseExpression.coalesce(operands);
case NULLIF:
return CaseExpression.nullif(operands[0], operands[1]);
case OTHER:
if (operator == HazelcastSqlOperatorTable.CONCAT) {
assert operands.length == 2;
return ConcatFunction.create(operands[0], operands[1]);
}
if (operator == HazelcastSqlOperatorTable.NOT_LIKE) {
assert ((HazelcastLikeOperator) operator).isNegated();
Expression<?> escape2 = operands.length == 2 ? null : operands[2];
return LikeFunction.create(operands[0], operands[1], escape2, true);
}
break;
case POSITION:
case OTHER_FUNCTION:
SqlFunction function = (SqlFunction) operator;
// Math.
if (function == HazelcastSqlOperatorTable.POWER) {
assert operands.length == 2;
return DoubleBiFunction.create(operands[0], operands[1], DoubleBiFunction.POWER);
} else if (function == HazelcastSqlOperatorTable.SQUARE) {
return DoubleFunction.create(operands[0], DoubleFunction.SQUARE);
} else if (function == HazelcastSqlOperatorTable.SQRT) {
return DoubleFunction.create(operands[0], DoubleFunction.SQRT);
} else if (function == HazelcastSqlOperatorTable.CBRT) {
return DoubleFunction.create(operands[0], DoubleFunction.CBRT);
} else if (function == HazelcastSqlOperatorTable.COS) {
return DoubleFunction.create(operands[0], DoubleFunction.COS);
} else if (function == HazelcastSqlOperatorTable.SIN) {
return DoubleFunction.create(operands[0], DoubleFunction.SIN);
} else if (function == HazelcastSqlOperatorTable.TAN) {
return DoubleFunction.create(operands[0], DoubleFunction.TAN);
} else if (function == HazelcastSqlOperatorTable.COT) {
return DoubleFunction.create(operands[0], DoubleFunction.COT);
} else if (function == HazelcastSqlOperatorTable.ACOS) {
return DoubleFunction.create(operands[0], DoubleFunction.ACOS);
} else if (function == HazelcastSqlOperatorTable.ASIN) {
return DoubleFunction.create(operands[0], DoubleFunction.ASIN);
} else if (function == HazelcastSqlOperatorTable.ATAN) {
return DoubleFunction.create(operands[0], DoubleFunction.ATAN);
} else if (function == HazelcastSqlOperatorTable.ATAN2) {
assert operands.length == 2;
return DoubleBiFunction.create(operands[0], operands[1], DoubleBiFunction.ATAN2);
} else if (function == HazelcastSqlOperatorTable.EXP) {
return DoubleFunction.create(operands[0], DoubleFunction.EXP);
} else if (function == HazelcastSqlOperatorTable.LN) {
return DoubleFunction.create(operands[0], DoubleFunction.LN);
} else if (function == HazelcastSqlOperatorTable.LOG10) {
return DoubleFunction.create(operands[0], DoubleFunction.LOG10);
} else if (function == HazelcastSqlOperatorTable.RAND) {
return RandFunction.create(operands.length == 0 ? null : operands[0]);
} else if (function == HazelcastSqlOperatorTable.ABS) {
return AbsFunction.create(operands[0], resultType);
} else if (function == SqlStdOperatorTable.PI) {
return ConstantExpression.create(Math.PI, resultType);
} else if (function == HazelcastSqlOperatorTable.SIGN) {
return SignFunction.create(operands[0], resultType);
} else if (function == HazelcastSqlOperatorTable.DEGREES) {
return DoubleFunction.create(operands[0], DoubleFunction.DEGREES);
} else if (function == HazelcastSqlOperatorTable.RADIANS) {
return DoubleFunction.create(operands[0], DoubleFunction.RADIANS);
} else if (function == HazelcastSqlOperatorTable.ROUND) {
return RoundTruncateFunction.create(operands[0], operands.length == 1 ? null : operands[1], resultType, false);
} else if (function == HazelcastSqlOperatorTable.TRUNCATE) {
return RoundTruncateFunction.create(operands[0], operands.length == 1 ? null : operands[1], resultType, true);
}
if (function == CHAR_LENGTH || function == CHARACTER_LENGTH || function == LENGTH) {
return CharLengthFunction.create(operands[0]);
} else if (function == HazelcastSqlOperatorTable.UPPER) {
return UpperFunction.create(operands[0]);
} else if (function == HazelcastSqlOperatorTable.LOWER) {
return LowerFunction.create(operands[0]);
} else if (function == HazelcastSqlOperatorTable.INITCAP) {
return InitcapFunction.create(operands[0]);
} else if (function == HazelcastSqlOperatorTable.ASCII) {
return AsciiFunction.create(operands[0]);
} else if (function == HazelcastSqlOperatorTable.SUBSTRING) {
Expression<?> input = operands[0];
Expression<?> start = operands[1];
Expression<?> length = operands.length > 2 ? operands[2] : null;
return SubstringFunction.create(input, start, length);
} else if (function == HazelcastSqlOperatorTable.LTRIM) {
return TrimFunction.create(operands[0], null, true, false);
} else if (function == HazelcastSqlOperatorTable.RTRIM) {
return TrimFunction.create(operands[0], null, false, true);
} else if (function == HazelcastSqlOperatorTable.BTRIM) {
return TrimFunction.create(operands[0], null, true, true);
} else if (function == HazelcastSqlOperatorTable.REPLACE) {
return ReplaceFunction.create(operands[0], operands[1], operands[2]);
} else if (function == HazelcastSqlOperatorTable.POSITION) {
Expression<?> start = operands.length > 2 ? operands[2] : null;
return PositionFunction.create(operands[0], operands[1], start);
} else if (function == HazelcastSqlOperatorTable.TO_TIMESTAMP_TZ) {
return ToTimestampTzFunction.create(operands[0]);
} else if (function == HazelcastSqlOperatorTable.TO_EPOCH_MILLIS) {
return ToEpochMillisFunction.create(operands[0]);
} else if (function == HazelcastSqlOperatorTable.CONCAT_WS) {
return ConcatWSFunction.create(operands);
} else if (function == HazelcastSqlOperatorTable.JSON_QUERY) {
final SqlJsonQueryWrapperBehavior wrapperBehavior = ((SymbolExpression) operands[2]).getSymbol();
final SqlJsonQueryEmptyOrErrorBehavior onEmpty = ((SymbolExpression) operands[3]).getSymbol();
final SqlJsonQueryEmptyOrErrorBehavior onError = ((SymbolExpression) operands[4]).getSymbol();
return JsonQueryFunction.create(operands[0], operands[1], wrapperBehavior, onEmpty, onError);
} else if (function == HazelcastJsonParseFunction.INSTANCE) {
return JsonParseFunction.create(operands[0]);
} else if (function == HazelcastSqlOperatorTable.JSON_VALUE) {
final SqlJsonValueEmptyOrErrorBehavior onEmpty = ((SymbolExpression) operands[4]).getSymbol();
final SqlJsonValueEmptyOrErrorBehavior onError = ((SymbolExpression) operands[5]).getSymbol();
return JsonValueFunction.create(operands[0], operands[1], operands[2], operands[3], resultType, onEmpty, onError);
} else if (function == HazelcastSqlOperatorTable.JSON_OBJECT) {
final SqlJsonConstructorNullClause nullClause = ((SymbolExpression) operands[0]).getSymbol();
final Expression<?>[] fields = Arrays.copyOfRange(operands, 1, operands.length);
return JsonObjectFunction.create(fields, nullClause);
} else if (function == HazelcastSqlOperatorTable.JSON_ARRAY) {
final SqlJsonConstructorNullClause nullClause = ((SymbolExpression) operands[0]).getSymbol();
final Expression<?>[] fields = Arrays.copyOfRange(operands, 1, operands.length);
return JsonArrayFunction.create(fields, nullClause);
}
break;
default:
break;
}
throw QueryException.error("Unsupported operator: " + operator);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project hive by apache.
the class HiveSubQueryRemoveRule method rewriteScalar.
private RexNode rewriteScalar(RelMetadataQuery mq, RexSubQuery e, Set<CorrelationId> variablesSet, RelBuilder builder, int offset, int inputCount, boolean isCorrScalarAgg) {
// if scalar query has aggregate and no windowing and no gby avoid adding sq_count_check
// since it is guaranteed to produce at most one row
Double maxRowCount = mq.getMaxRowCount(e.rel);
boolean shouldIntroSQCountCheck = maxRowCount == null || maxRowCount > 1.0;
if (shouldIntroSQCountCheck) {
builder.push(e.rel);
// returns single row/column
builder.aggregate(builder.groupKey(), builder.count(false, "cnt"));
SqlFunction countCheck = new SqlFunction("sq_count_check", SqlKind.OTHER_FUNCTION, ReturnTypes.BOOLEAN, InferTypes.RETURN_TYPE, OperandTypes.NUMERIC, SqlFunctionCategory.USER_DEFINED_FUNCTION);
// we create FILTER (sq_count_check(count())) instead of PROJECT because RelFieldTrimmer
// ends up getting rid of Project since it is not used further up the tree
// sq_count_check returns true when subquery returns single row, else it fails
builder.filter(builder.call(countCheck, builder.field("cnt")));
if (!variablesSet.isEmpty()) {
builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);
} else {
builder.join(JoinRelType.INNER, builder.literal(true), variablesSet);
}
offset++;
}
if (isCorrScalarAgg) {
// Transformation :
// Outer Query Left Join (inner query) on correlated predicate
// and preserve rows only from left side.
builder.push(e.rel);
final List<RexNode> parentQueryFields = new ArrayList<>();
parentQueryFields.addAll(builder.fields());
// id is appended since there could be multiple scalar subqueries and FILTER
// is created using field name
String indicator = "trueLiteral";
parentQueryFields.add(builder.alias(builder.literal(true), indicator));
builder.project(parentQueryFields);
builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);
final ImmutableList.Builder<RexNode> operands = ImmutableList.builder();
RexNode literal;
if (isAggZeroOnEmpty(e)) {
// since count has a return type of BIG INT we need to make a literal of type big int
// relbuilder's literal doesn't allow this
literal = e.rel.getCluster().getRexBuilder().makeBigintLiteral(new BigDecimal(0));
} else {
literal = e.rel.getCluster().getRexBuilder().makeNullLiteral(getAggTypeForScalarSub(e));
}
operands.add((builder.isNull(builder.field(indicator))), literal);
operands.add(field(builder, 1, builder.fields().size() - 2));
return builder.call(SqlStdOperatorTable.CASE, operands.build());
}
// Transformation is to left join for correlated predicates and inner join otherwise,
// but do a count on inner side before that to make sure it generates atmost 1 row.
builder.push(e.rel);
builder.join(JoinRelType.LEFT, builder.literal(true), variablesSet);
return field(builder, inputCount, offset);
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project hive by apache.
the class DataSketchesFunctions method registerAsHiveFunction.
private void registerAsHiveFunction(SketchFunctionDescriptor sfd) {
if (sfd != null && sfd.getReturnRelDataType().isPresent()) {
SqlFunction cdfFn = new HiveSqlFunction(sfd.name, SqlKind.OTHER_FUNCTION, ReturnTypes.explicit(sfd.getReturnRelDataType().get()), InferTypes.ANY_NULLABLE, OperandTypes.family(), SqlFunctionCategory.USER_DEFINED_FUNCTION, true, false);
sfd.setCalciteFunction(cdfFn);
}
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project hive by apache.
the class DruidSqlOperatorConverter method getDefaultMap.
public static final Map<SqlOperator, org.apache.calcite.adapter.druid.DruidSqlOperatorConverter> getDefaultMap() {
if (druidOperatorMap == null) {
druidOperatorMap = new HashMap<SqlOperator, org.apache.calcite.adapter.druid.DruidSqlOperatorConverter>();
DruidQuery.DEFAULT_OPERATORS_LIST.stream().forEach(op -> druidOperatorMap.put(op.calciteOperator(), op));
// Override Hive specific operators
druidOperatorMap.putAll(Maps.asMap(HiveFloorDate.ALL_FUNCTIONS, (Function<SqlFunction, org.apache.calcite.adapter.druid.DruidSqlOperatorConverter>) input -> new FloorOperatorConversion()));
druidOperatorMap.putAll(Maps.asMap(HiveExtractDate.ALL_FUNCTIONS, (Function<SqlFunction, org.apache.calcite.adapter.druid.DruidSqlOperatorConverter>) input -> new ExtractOperatorConversion()));
druidOperatorMap.put(HiveConcat.INSTANCE, new DirectOperatorConversion(HiveConcat.INSTANCE, "concat"));
druidOperatorMap.put(SqlStdOperatorTable.SUBSTRING, new DruidSqlOperatorConverter.DruidSubstringOperatorConversion());
druidOperatorMap.put(SqlStdOperatorTable.IS_NULL, new UnaryFunctionOperatorConversion(SqlStdOperatorTable.IS_NULL, "isnull"));
druidOperatorMap.put(SqlStdOperatorTable.IS_NOT_NULL, new UnaryFunctionOperatorConversion(SqlStdOperatorTable.IS_NOT_NULL, "notnull"));
druidOperatorMap.put(HiveTruncSqlOperator.INSTANCE, new DruidDateTruncOperatorConversion());
druidOperatorMap.put(HiveToDateSqlOperator.INSTANCE, new DruidToDateOperatorConversion());
druidOperatorMap.put(HiveFromUnixTimeSqlOperator.INSTANCE, new DruidFormUnixTimeOperatorConversion());
druidOperatorMap.put(HiveToUnixTimestampSqlOperator.INSTANCE, new DruidUnixTimestampOperatorConversion());
druidOperatorMap.put(HiveDateAddSqlOperator.INSTANCE, new DruidDateArithmeticOperatorConversion(1, HiveDateAddSqlOperator.INSTANCE));
druidOperatorMap.put(HiveDateSubSqlOperator.INSTANCE, new DruidDateArithmeticOperatorConversion(-1, HiveDateSubSqlOperator.INSTANCE));
}
return druidOperatorMap;
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.sql.SqlFunction in project calcite by apache.
the class AggVisitor method visit.
public Void visit(SqlCall call) {
final SqlOperator operator = call.getOperator();
// If nested aggregates disallowed or found an aggregate at invalid level
if (operator.isAggregator() && !(operator instanceof SqlAbstractGroupFunction) && !operator.requiresOver()) {
if (delegate != null) {
return operator.acceptCall(delegate, call);
}
if (aggregate) {
return found(call);
}
}
if (group && operator.isGroup()) {
return found(call);
}
// User-defined function may not be resolved yet.
if (operator instanceof SqlFunction) {
final SqlFunction sqlFunction = (SqlFunction) operator;
if (sqlFunction.getFunctionType().isUserDefinedNotSpecificFunction()) {
final List<SqlOperator> list = Lists.newArrayList();
opTab.lookupOperatorOverloads(sqlFunction.getSqlIdentifier(), sqlFunction.getFunctionType(), SqlSyntax.FUNCTION, list);
for (SqlOperator operator2 : list) {
if (operator2.isAggregator() && !operator2.requiresOver()) {
// level
if (aggregate) {
found(call);
}
}
}
}
}
if (call.isA(SqlKind.QUERY)) {
// don't traverse into queries
return null;
}
if (call.getKind() == SqlKind.OVER) {
if (over) {
return found(call);
} else {
// an aggregate function over a window is not an aggregate!
return null;
}
}
return super.visit(call);
}
Aggregations