Search in sources :

Example 6 with Expression

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

the class IndexResolver method composeRangeFilter.

/**
 * Create the composite range filter from the given per-column filters.
 * <p>
 * If the number of per-column filters if less than the number of index components, then infinite ranges are added
 * to the missing components.
 * <p>
 * Consider that we have two per-column filter as input: {@code {a=1}, {b>2 AND b<3}}.
 * <p>
 * If the index is defined as {@code {a, b}}, then the resulting filter would be {@code {a=1, b>2 AND a=1, b<3}}.
 * <p>
 * If the index is defined as {@code {a, b, c}}, then the resulting filter would be
 * {@code {a=1, b>2, c>NEGATIVE_INFINITY AND a=1, b<3, c<POSITIVE_INFINITY}}.
 *
 * @param filters         all per-column filters
 * @param lastFilter      the last filter (range)
 * @param componentsCount number of components in the filter
 * @return range filter
 */
private static IndexFilter composeRangeFilter(List<IndexFilter> filters, IndexRangeFilter lastFilter, int componentsCount) {
    // Flatten non-terminal components.
    List<Expression> components = new ArrayList<>(filters.size());
    List<Boolean> allowNulls = new ArrayList<>();
    fillNonTerminalComponents(filters, components, allowNulls);
    // Add value of the current filter.
    List<Expression> fromComponents = components;
    List<Expression> toComponents = new ArrayList<>(components);
    List<Boolean> fromAllowNulls = allowNulls;
    List<Boolean> toAllowNulls = new ArrayList<>(fromAllowNulls);
    if (lastFilter.getFrom() != null) {
        fromComponents.add(lastFilter.getFrom().getComponents().get(0));
        fromAllowNulls.add(false);
    } else {
        if (componentsCount == 1) {
            fromComponents.add(ConstantExpression.create(NEGATIVE_INFINITY, QueryDataType.OBJECT));
            fromAllowNulls.add(false);
        } else {
            // In composite indexes null values are not stored separately. Therefore, we need to filter them out.
            fromComponents.add(ConstantExpression.create(null, QueryDataType.OBJECT));
            fromAllowNulls.add(true);
        }
    }
    if (lastFilter.getTo() != null) {
        toComponents.add(lastFilter.getTo().getComponents().get(0));
    } else {
        toComponents.add(ConstantExpression.create(POSITIVE_INFINITY, QueryDataType.OBJECT));
    }
    toAllowNulls.add(false);
    // Fill missing part of the range request.
    addInfiniteRanges(fromComponents, fromAllowNulls, lastFilter.isFromInclusive(), toComponents, toAllowNulls, lastFilter.isToInclusive(), componentsCount);
    return new IndexRangeFilter(new IndexFilterValue(fromComponents, fromAllowNulls), lastFilter.isFromInclusive(), new IndexFilterValue(toComponents, toAllowNulls), lastFilter.isToInclusive());
}
Also used : IndexRangeFilter(com.hazelcast.sql.impl.exec.scan.index.IndexRangeFilter) Expression(com.hazelcast.sql.impl.expression.Expression) RexToExpression(com.hazelcast.jet.sql.impl.opt.physical.visitor.RexToExpression) ConstantExpression(com.hazelcast.sql.impl.expression.ConstantExpression) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) ArrayList(java.util.ArrayList)

Example 7 with Expression

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

the class IndexFilterValueTest method testContent.

@Test
public void testContent() {
    List<Expression> components = singletonList(constant(1, QueryDataType.INT));
    List<Boolean> allowNulls = singletonList(true);
    IndexFilterValue value = new IndexFilterValue(components, allowNulls);
    assertSame(components, value.getComponents());
    assertSame(allowNulls, value.getAllowNulls());
}
Also used : Expression(com.hazelcast.sql.impl.expression.Expression) IndexFilterValue(com.hazelcast.sql.impl.exec.scan.index.IndexFilterValue) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with Expression

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

the class TestAbstractSqlConnector method fullScanReader.

@Nonnull
@Override
public Vertex fullScanReader(@Nonnull DAG dag, @Nonnull Table table_, @Nullable Expression<Boolean> predicate, @Nonnull List<Expression<?>> projection, @Nullable FunctionEx<ExpressionEvalContext, EventTimePolicy<JetSqlRow>> eventTimePolicyProvider) {
    TestTable table = (TestTable) table_;
    List<Object[]> rows = table.rows;
    boolean streaming = table.streaming;
    FunctionEx<Context, TestDataGenerator> createContextFn = ctx -> {
        ExpressionEvalContext evalContext = ExpressionEvalContext.from(ctx);
        EventTimePolicy<JetSqlRow> eventTimePolicy = eventTimePolicyProvider == null ? EventTimePolicy.noEventTime() : eventTimePolicyProvider.apply(evalContext);
        return new TestDataGenerator(rows, predicate, projection, evalContext, eventTimePolicy, streaming);
    };
    ProcessorMetaSupplier pms = createProcessorSupplier(createContextFn);
    return dag.newUniqueVertex(table.toString(), pms);
}
Also used : ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) Context(com.hazelcast.jet.core.Processor.Context) Traverser(com.hazelcast.jet.Traverser) Arrays(java.util.Arrays) PlanObjectKey(com.hazelcast.sql.impl.optimizer.PlanObjectKey) EventTimePolicy(com.hazelcast.jet.core.EventTimePolicy) QueryDataTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeFamily) ArrayList(java.util.ArrayList) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) String.join(java.lang.String.join) Map(java.util.Map) ConstantTableStatistics(com.hazelcast.sql.impl.schema.ConstantTableStatistics) SqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnector) DAG(com.hazelcast.jet.core.DAG) Expression(com.hazelcast.sql.impl.expression.Expression) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) EventTimeMapper(com.hazelcast.jet.core.EventTimeMapper) QueryException(com.hazelcast.sql.impl.QueryException) FunctionEx(com.hazelcast.function.FunctionEx) SqlService(com.hazelcast.sql.SqlService) NodeEngine(com.hazelcast.spi.impl.NodeEngine) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) JetTable(com.hazelcast.jet.sql.impl.schema.JetTable) Traversers(com.hazelcast.jet.Traversers) ExpressionUtil(com.hazelcast.jet.sql.impl.ExpressionUtil) Collectors.joining(java.util.stream.Collectors.joining) Objects(java.util.Objects) Vertex(com.hazelcast.jet.core.Vertex) TableField(com.hazelcast.sql.impl.schema.TableField) List(java.util.List) ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) QueryDataTypeUtils.resolveTypeForTypeFamily(com.hazelcast.sql.impl.type.QueryDataTypeUtils.resolveTypeForTypeFamily) Context(com.hazelcast.jet.core.Processor.Context) MappingField(com.hazelcast.sql.impl.schema.MappingField) SourceBuilder(com.hazelcast.jet.pipeline.SourceBuilder) Table(com.hazelcast.sql.impl.schema.Table) ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) EventTimePolicy(com.hazelcast.jet.core.EventTimePolicy) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) Nonnull(javax.annotation.Nonnull)

Example 9 with Expression

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

the class TestAllTypesSqlConnector method fullScanReader.

@Nonnull
@Override
public Vertex fullScanReader(@Nonnull DAG dag, @Nonnull Table table, @Nullable Expression<Boolean> predicate, @Nonnull List<Expression<?>> projection, @Nullable FunctionEx<ExpressionEvalContext, EventTimePolicy<JetSqlRow>> eventTimePolicyProvider) {
    if (eventTimePolicyProvider != null) {
        throw QueryException.error("Ordering function are not supported for " + TYPE_NAME + " mappings");
    }
    BatchSource<JetSqlRow> source = SourceBuilder.batch("batch", ExpressionEvalContext::from).<JetSqlRow>fillBufferFn((ctx, buf) -> {
        JetSqlRow row = ExpressionUtil.evaluate(predicate, projection, VALUES, ctx);
        if (row != null) {
            buf.add(row);
        }
        buf.close();
    }).build();
    ProcessorMetaSupplier pms = ((BatchSourceTransform<JetSqlRow>) source).metaSupplier;
    return dag.newUniqueVertex(table.toString(), pms);
}
Also used : ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) QueryDataType(com.hazelcast.sql.impl.type.QueryDataType) LocalDateTime(java.time.LocalDateTime) PlanObjectKey(com.hazelcast.sql.impl.optimizer.PlanObjectKey) EventTimePolicy(com.hazelcast.jet.core.EventTimePolicy) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) BigDecimal(java.math.BigDecimal) TEST_SS(com.hazelcast.jet.sql.SqlTestSupport.TEST_SS) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) ConstantTableStatistics(com.hazelcast.sql.impl.schema.ConstantTableStatistics) LocalTime(java.time.LocalTime) SqlConnector(com.hazelcast.jet.sql.impl.connector.SqlConnector) DAG(com.hazelcast.jet.core.DAG) Expression(com.hazelcast.sql.impl.expression.Expression) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) QueryException(com.hazelcast.sql.impl.QueryException) FunctionEx(com.hazelcast.function.FunctionEx) SqlService(com.hazelcast.sql.SqlService) NodeEngine(com.hazelcast.spi.impl.NodeEngine) BatchSource(com.hazelcast.jet.pipeline.BatchSource) Util.toList(com.hazelcast.jet.impl.util.Util.toList) ImmutableMap(com.google.common.collect.ImmutableMap) BatchSourceTransform(com.hazelcast.jet.impl.pipeline.transform.BatchSourceTransform) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) JetTable(com.hazelcast.jet.sql.impl.schema.JetTable) SqlTestSupport(com.hazelcast.jet.sql.SqlTestSupport) ExpressionUtil(com.hazelcast.jet.sql.impl.ExpressionUtil) Objects(java.util.Objects) Vertex(com.hazelcast.jet.core.Vertex) TableField(com.hazelcast.sql.impl.schema.TableField) List(java.util.List) ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) OffsetDateTime(java.time.OffsetDateTime) LocalDate(java.time.LocalDate) UTC(java.time.ZoneOffset.UTC) MappingField(com.hazelcast.sql.impl.schema.MappingField) SourceBuilder(com.hazelcast.jet.pipeline.SourceBuilder) Table(com.hazelcast.sql.impl.schema.Table) JetSqlRow(com.hazelcast.sql.impl.row.JetSqlRow) BatchSourceTransform(com.hazelcast.jet.impl.pipeline.transform.BatchSourceTransform) ProcessorMetaSupplier(com.hazelcast.jet.core.ProcessorMetaSupplier) Nonnull(javax.annotation.Nonnull)

Example 10 with Expression

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

the class SeriesTable method items.

BatchSource<JetSqlRow> items(Expression<Boolean> predicate, List<Expression<?>> projections) {
    List<Expression<?>> argumentExpressions = this.argumentExpressions;
    return SourceBuilder.batch("series", ctx -> {
        ExpressionEvalContext evalContext = ExpressionEvalContext.from(ctx);
        Integer start = evaluate(argumentExpressions.get(0), null, evalContext);
        Integer stop = evaluate(argumentExpressions.get(1), null, evalContext);
        Integer step = evaluate(argumentExpressions.get(2), 1, evalContext);
        if (start == null || stop == null || step == null) {
            throw QueryException.error("Invalid argument of a call to function GENERATE_SERIES" + " - null argument(s)");
        }
        if (step == 0) {
            throw QueryException.error("Invalid argument of a call to function GENERATE_SERIES" + " - step cannot be equal to zero");
        }
        return new DataGenerator(start, stop, step, predicate, projections, evalContext);
    }).fillBufferFn(DataGenerator::fillBuffer).build();
}
Also used : ExpressionEvalContext(com.hazelcast.sql.impl.expression.ExpressionEvalContext) Expression(com.hazelcast.sql.impl.expression.Expression)

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