Search in sources :

Example 11 with Expression

use of com.hazelcast.sql.impl.expression.Expression in project hazelcast by hazelcast.

the class CreateDagVisitor method onProject.

public Vertex onProject(ProjectPhysicalRel rel) {
    List<Expression<?>> projection = rel.projection(parameterMetadata);
    Vertex vertex = dag.newUniqueVertex("Project", mapUsingServiceP(ServiceFactories.nonSharedService(ctx -> ExpressionUtil.projectionFn(projection, ExpressionEvalContext.from(ctx))), (Function<JetSqlRow, JetSqlRow> projectionFn, JetSqlRow row) -> projectionFn.apply(row)));
    connectInputPreserveCollation(rel, vertex);
    return vertex;
}
Also used : Vertex(com.hazelcast.jet.core.Vertex) Expression(com.hazelcast.sql.impl.expression.Expression) ConstantExpression(com.hazelcast.sql.impl.expression.ConstantExpression) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow)

Example 12 with Expression

use of com.hazelcast.sql.impl.expression.Expression in project hazelcast by hazelcast.

the class IndexResolver method prepareSingleColumnCandidates.

/**
 * Creates a map from the scan column ordinal to expressions that could be potentially used by indexes created over
 * this column.
 *
 * @param expressions             CNF nodes
 * @param allIndexedFieldOrdinals ordinals of all columns that have some indexes. Helps to filter out candidates that
 *                                definitely cannot be used earlier.
 */
private static Map<Integer, List<IndexComponentCandidate>> prepareSingleColumnCandidates(List<RexNode> expressions, QueryParameterMetadata parameterMetadata, Set<Integer> allIndexedFieldOrdinals) {
    Map<Integer, List<IndexComponentCandidate>> res = new HashMap<>();
    // Iterate over each CNF component of the expression.
    for (RexNode expression : expressions) {
        // Try creating a candidate for the expression. The candidate is created iff the expression could be used
        // by some index implementation (SORTED, HASH)
        IndexComponentCandidate candidate = prepareSingleColumnCandidate(expression, parameterMetadata);
        if (candidate == null) {
            // Expression cannot be used by any index implementation, skip
            continue;
        }
        if (!allIndexedFieldOrdinals.contains(candidate.getColumnIndex())) {
            // Therefore, the expression could not be used, skip
            continue;
        }
        // Group candidates by column. E.g. {a>1 AND a<3} is grouped into a single map entry: a->{>1},{<3}
        res.computeIfAbsent(candidate.getColumnIndex(), (k) -> new ArrayList<>()).add(candidate);
    }
    return res;
}
Also used : RangeSet(com.google.common.collect.RangeSet) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) OptUtils.getCluster(com.hazelcast.jet.sql.impl.opt.OptUtils.getCluster) QueryParameterMetadata(com.hazelcast.sql.impl.QueryParameterMetadata) Collections.singletonList(java.util.Collections.singletonList) HazelcastTable(com.hazelcast.jet.sql.impl.schema.HazelcastTable) TypeConverters(com.hazelcast.query.impl.TypeConverters) RexUtil(org.apache.calcite.rex.RexUtil) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) QueryDataTypeUtils(com.hazelcast.sql.impl.type.QueryDataTypeUtils) HASH(com.hazelcast.config.IndexType.HASH) IndexEqualsFilter(com.hazelcast.sql.impl.exec.scan.index.IndexEqualsFilter) RexToExpressionVisitor(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpressionVisitor) PlanNodeFieldTypeProvider(com.hazelcast.sql.impl.plan.node.PlanNodeFieldTypeProvider) RelTraitSet(org.apache.calcite.plan.RelTraitSet) SqlKind(org.apache.calcite.sql.SqlKind) HazelcastTypeUtils(com.hazelcast.jet.sql.impl.validate.types.HazelcastTypeUtils) RexLiteral(org.apache.calcite.rex.RexLiteral) Collection(java.util.Collection) Range(com.google.common.collect.Range) Set(java.util.Set) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Collectors(java.util.stream.Collectors) IndexInFilter(com.hazelcast.sql.impl.exec.scan.index.IndexInFilter) RexInputRef(org.apache.calcite.rex.RexInputRef) List(java.util.List) FullScanLogicalRel(com.hazelcast.jet.sql.impl.opt.logical.FullScanLogicalRel) BoundType(com.google.common.collect.BoundType) RelCollation(org.apache.calcite.rel.RelCollation) MapTableIndex(com.hazelcast.sql.impl.schema.map.MapTableIndex) TRUE(java.lang.Boolean.TRUE) RexCall(org.apache.calcite.rex.RexCall) OptUtils.createRelTable(com.hazelcast.jet.sql.impl.opt.OptUtils.createRelTable) Iterables(com.google.common.collect.Iterables) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) IndexFilter(com.hazelcast.sql.impl.exec.scan.index.IndexFilter) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) HashMap(java.util.HashMap) POSITIVE_INFINITY(com.hazelcast.query.impl.CompositeValue.POSITIVE_INFINITY) RelOptUtil(org.apache.calcite.plan.RelOptUtil) RelOptTable(org.apache.calcite.plan.RelOptTable) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IndexType(com.hazelcast.config.IndexType) BiTuple(com.hazelcast.internal.util.BiTuple) Expression(com.hazelcast.sql.impl.expression.Expression) Nonnull(javax.annotation.Nonnull) ComparableIdentifiedDataSerializable(com.hazelcast.query.impl.ComparableIdentifiedDataSerializable) RelCollations(org.apache.calcite.rel.RelCollations) RexToExpression(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) FALSE(java.lang.Boolean.FALSE) HazelcastRelOptTable(com.hazelcast.jet.sql.impl.schema.HazelcastRelOptTable) RelCollationTraitDef(org.apache.calcite.rel.RelCollationTraitDef) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) SORTED(com.hazelcast.config.IndexType.SORTED) Util.toList(com.hazelcast.jet.impl.util.Util.toList) RexBuilder(org.apache.calcite.rex.RexBuilder) OptUtils(com.hazelcast.jet.sql.impl.opt.OptUtils) IndexScanMapPhysicalRel(com.hazelcast.jet.sql.impl.opt.physical.IndexScanMapPhysicalRel) RelNode(org.apache.calcite.rel.RelNode) Direction(org.apache.calcite.rel.RelFieldCollation.Direction) ASCENDING(org.apache.calcite.rel.RelFieldCollation.Direction.ASCENDING) TreeMap(java.util.TreeMap) NEGATIVE_INFINITY(com.hazelcast.query.impl.CompositeValue.NEGATIVE_INFINITY) ConstantExpression(com.hazelcast.sql.impl.expression.ConstantExpression) DESCENDING(org.apache.calcite.rel.RelFieldCollation.Direction.DESCENDING) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ArrayList(java.util.ArrayList) Util.toList(com.hazelcast.jet.impl.util.Util.toList) RexNode(org.apache.calcite.rex.RexNode)

Example 13 with Expression

use of com.hazelcast.sql.impl.expression.Expression 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);
}
Also used : SqlOperator(org.apache.calcite.sql.SqlOperator) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) TimeUnitRange(org.apache.calcite.avatica.util.TimeUnitRange) HazelcastLikeOperator(com.hazelcast.jet.sql.impl.validate.operators.string.HazelcastLikeOperator) SqlJsonQueryWrapperBehavior(org.apache.calcite.sql.SqlJsonQueryWrapperBehavior) SqlJsonQueryEmptyOrErrorBehavior(org.apache.calcite.sql.SqlJsonQueryEmptyOrErrorBehavior) SearchableExpression(com.hazelcast.sql.impl.expression.SearchableExpression) CastExpression(com.hazelcast.sql.impl.expression.CastExpression) CaseExpression(com.hazelcast.sql.impl.expression.CaseExpression) Expression(com.hazelcast.sql.impl.expression.Expression) SymbolExpression(com.hazelcast.sql.impl.expression.SymbolExpression) ConstantExpression(com.hazelcast.sql.impl.expression.ConstantExpression) SqlTrimFunction(org.apache.calcite.sql.fun.SqlTrimFunction) SymbolExpression(com.hazelcast.sql.impl.expression.SymbolExpression) ExtractField(com.hazelcast.sql.impl.expression.datetime.ExtractField) SqlJsonConstructorNullClause(org.apache.calcite.sql.SqlJsonConstructorNullClause) SqlJsonValueEmptyOrErrorBehavior(org.apache.calcite.sql.SqlJsonValueEmptyOrErrorBehavior) SqlFunction(org.apache.calcite.sql.SqlFunction)

Example 14 with Expression

use of com.hazelcast.sql.impl.expression.Expression in project hazelcast by hazelcast.

the class SortPhysicalRel method fetch.

public Expression<?> fetch(QueryParameterMetadata parameterMetadata) {
    PlanNodeSchema schema = schema(parameterMetadata);
    RexVisitor<Expression<?>> visitor = OptUtils.createRexToExpressionVisitor(schema, parameterMetadata);
    return fetch.accept(visitor);
}
Also used : Expression(com.hazelcast.sql.impl.expression.Expression) PlanNodeSchema(com.hazelcast.sql.impl.plan.node.PlanNodeSchema)

Example 15 with Expression

use of com.hazelcast.sql.impl.expression.Expression in project hazelcast by hazelcast.

the class SortPhysicalRel method offset.

public Expression<?> offset(QueryParameterMetadata parameterMetadata) {
    PlanNodeSchema schema = schema(parameterMetadata);
    RexVisitor<Expression<?>> visitor = OptUtils.createRexToExpressionVisitor(schema, parameterMetadata);
    return offset.accept(visitor);
}
Also used : Expression(com.hazelcast.sql.impl.expression.Expression) PlanNodeSchema(com.hazelcast.sql.impl.plan.node.PlanNodeSchema)

Aggregations

Expression (com.hazelcast.sql.impl.expression.Expression)15 ConstantExpression (com.hazelcast.sql.impl.expression.ConstantExpression)6 ExpressionEvalContext (com.hazelcast.sql.impl.expression.ExpressionEvalContext)6 FunctionEx (com.hazelcast.function.FunctionEx)4 Vertex (com.hazelcast.jet.core.Vertex)4 QueryParameterMetadata (com.hazelcast.sql.impl.QueryParameterMetadata)4 IndexFilterValue (com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue)4 QueryDataType (com.hazelcast.sql.impl.type.QueryDataType)4 List (java.util.List)4 DAG (com.hazelcast.jet.core.DAG)3 ProcessorMetaSupplier (com.hazelcast.jet.core.ProcessorMetaSupplier)3 ExpressionUtil (com.hazelcast.jet.sql.impl.ExpressionUtil)3 RexToExpression (com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression)3 NodeEngine (com.hazelcast.spi.impl.NodeEngine)3 IndexRangeFilter (com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter)3 PlanObjectKey (com.hazelcast.sql.impl.optimizer.PlanObjectKey)3 JetSqlRow (com.hazelcast.sql.impl.row.JetSqlRow)3 Table (com.hazelcast.sql.impl.schema.Table)3 ArrayList (java.util.ArrayList)3 Nullable (javax.annotation.Nullable)3