org.apache.kylin.metadata.filter.TupleFilter Java Examples

The following examples show how to use org.apache.kylin.metadata.filter.TupleFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DerivedFilterTranslator.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private static TupleFilter buildInFilter(TblColRef[] hostCols, Set<Array<String>> satisfyingHostRecords) {
    if (satisfyingHostRecords.size() == 0) {
        return ConstantTupleFilter.FALSE;
    }

    int hn = hostCols.length;
    if (hn == 1) {
        CompareTupleFilter in = new CompareTupleFilter(FilterOperatorEnum.IN);
        in.addChild(new ColumnTupleFilter(hostCols[0]));
        in.addChild(new ConstantTupleFilter(asValues(satisfyingHostRecords)));
        return in;
    } else {
        LogicalTupleFilter or = new LogicalTupleFilter(FilterOperatorEnum.OR);
        for (Array<String> rec : satisfyingHostRecords) {
            LogicalTupleFilter and = new LogicalTupleFilter(FilterOperatorEnum.AND);
            for (int i = 0; i < hn; i++) {
                CompareTupleFilter eq = new CompareTupleFilter(FilterOperatorEnum.EQ);
                eq.addChild(new ColumnTupleFilter(hostCols[i]));
                eq.addChild(new ConstantTupleFilter(rec.data[i]));
                and.addChild(eq);
            }
            or.addChild(and);
        }
        return or;
    }
}
 
Example #2
Source File: GTCubeStorageQueryRequest.java    From kylin with Apache License 2.0 6 votes vote down vote up
public GTCubeStorageQueryRequest(Cuboid cuboid, Set<TblColRef> dimensions, //
        Set<TblColRef> groups, List<TblColRef> dynGroups, List<TupleExpression> dynGroupExprs, //
        Set<TblColRef> filterCols, Set<FunctionDesc> metrics, List<DynamicFunctionDesc> dynFuncs, //
        TupleFilter filter, TupleFilter havingFilter, StorageContext context) {
    this.cuboid = cuboid;
    this.dimensions = dimensions;
    this.groups = groups;
    this.dynGroups = dynGroups;
    this.dynGroupExprs = dynGroupExprs;
    this.filterCols = filterCols;
    this.metrics = metrics;
    this.dynFuncs = dynFuncs;
    this.filter = filter;
    this.havingFilter = havingFilter;
    this.context = context;
}
 
Example #3
Source File: SegmentPrunerTest.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Test
public void testPruneSegWithFilterIN() {
    // legacy cube segments does not have DimensionRangeInfo, but with TSRange can do some pruning
    CubeInstance cube = CubeManager.getInstance(getTestConfig())
            .getCube("test_kylin_cube_without_slr_left_join_ready_2_segments");
    TblColRef col = cube.getModel().findColumn("TEST_KYLIN_FACT.CAL_DT");
    CubeSegment seg = cube.getSegments(SegmentStatusEnum.READY).get(0);
    TSRange tsRange = seg.getTSRange();
    String start = DateFormat.formatToTimeStr(tsRange.start.v, "yyyy-MM-dd");
    CubeSegment seg2 = cube.getSegments(SegmentStatusEnum.READY).get(1);
    TSRange tsRange2 = seg2.getTSRange();
    try (SetAndUnsetSystemProp sns = new SetAndUnsetSystemProp("kylin.query.skip-empty-segments", "false")) {

        {
            TupleFilter inFilter = new ConstantTupleFilter(Sets.newHashSet(start,
                    DateFormat.formatToTimeStr(tsRange2.end.v + 1000 * 60 * 60 * 24L, "yyyy-MM-dd")));
            TupleFilter filter = compare(col, FilterOperatorEnum.IN, inFilter);
            SegmentPruner segmentPruner = new SegmentPruner(filter);
            Assert.assertTrue(segmentPruner.check(seg));
            Assert.assertFalse(segmentPruner.check(seg2));

        }
    }
}
 
Example #4
Source File: StreamingSegmentManagerTest.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Test
public void testOneValueAggregation() {
    genEvents(80000);
    StreamingCubeDataSearcher searcher = streamingSegmentManager.getSearcher();
    String startTimeStr = "2018-07-30 20:00:00";
    long startTime = DateFormat.stringToMillis(startTimeStr);
    String endTimeStr = "2018-07-30 20:04:00";
    long endTime = DateFormat.stringToMillis(endTimeStr);
    CompareTupleFilter filter1 = testHelper.buildCompareFilter("STREAMING_V2_TABLE.MINUTE_START",
        FilterOperatorEnum.GTE, startTimeStr);
    CompareTupleFilter filter2 = testHelper.buildCompareFilter("STREAMING_V2_TABLE.MINUTE_START",
        FilterOperatorEnum.LT, endTimeStr);
    TupleFilter filter = testHelper.buildAndFilter(filter1, filter2);
    Set<FunctionDesc> metrics = Sets.newHashSet(testHelper.simulateCountMetric());
    Set<TblColRef> dimensions = testHelper.simulateDimensions("STREAMING_V2_TABLE.MINUTE_START");
    Set<TblColRef> groups = Sets.newHashSet();
    StreamingSearchContext searchRequest = new StreamingSearchContext(cubeDesc, dimensions, groups, metrics, filter,
        null);
    IStreamingSearchResult segmentResults1 = searcher.doSearch(searchRequest, -1, true);
    for (Record record : segmentResults1) {
        long minStart = Long.valueOf(record.getDimensions()[0]);
        assertTrue(startTime <= minStart && minStart < endTime);
        System.out.println(record);
    }
}
 
Example #5
Source File: StreamingBuiltInFunctionTransformer.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(IEvaluatableTuple tuple, IFilterCodeSystem cs) {
    // extract tuple value
    Object tupleValue = null;
    for (TupleFilter filter : this.children) {
        if (!isConstant(filter)) {
            filter.evaluate(tuple, cs);
            tupleValue = filter.getValues().iterator().next();
        }
    }
    if (tupleValue == null || cs.isNull(tupleValue)) {
        return false;
    }

    try {
        return (Boolean) buildInFunctionTupleFilter.invokeFunction(tupleValue);
    } catch (Exception e) {
        logger.error("error when invoke build in function", e);
        return false;
    }
}
 
Example #6
Source File: DictGridTableTest.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void verifyConvertFilterConstants4() {
    GTInfo info = table.getInfo();

    TableDesc extTable = TableDesc.mockup("ext");
    TblColRef extColA = TblColRef.mockup(extTable, 1, "A", "timestamp");
    TblColRef extColB = TblColRef.mockup(extTable, 2, "B", "integer");

    CompareTupleFilter fComp1 = compare(extColA, FilterOperatorEnum.GT, "2015-01-14");
    CompareTupleFilter fComp2 = compare(extColB, FilterOperatorEnum.IN, "9", "10", "15");
    LogicalTupleFilter filter = and(fComp1, fComp2);

    List<TblColRef> colMapping = Lists.newArrayList();
    colMapping.add(extColA);
    colMapping.add(extColB);

    // $1 in ("9", "10", "15") has only "10" left
    TupleFilter newFilter = GTUtil.convertFilterColumnsAndConstants(filter, info, colMapping, null);
    assertEquals(
            "AND [UNKNOWN_MODEL:NULL.GT_MOCKUP_TABLE.0 GT [\\x00\\x00\\x01J\\xE5\\xBD\\x5C\\x00], UNKNOWN_MODEL:NULL.GT_MOCKUP_TABLE.1 IN [\\x00]]",
            newFilter.toString());
}
 
Example #7
Source File: DictGridTableTest.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void verifyConvertFilterConstants1() {
    GTInfo info = table.getInfo();

    TableDesc extTable = TableDesc.mockup("ext");
    TblColRef extColA = TblColRef.mockup(extTable, 1, "A", "timestamp");
    TblColRef extColB = TblColRef.mockup(extTable, 2, "B", "integer");

    CompareTupleFilter fComp1 = compare(extColA, FilterOperatorEnum.GT, "2015-01-14");
    CompareTupleFilter fComp2 = compare(extColB, FilterOperatorEnum.EQ, "10");
    LogicalTupleFilter filter = and(fComp1, fComp2);

    List<TblColRef> colMapping = Lists.newArrayList();
    colMapping.add(extColA);
    colMapping.add(extColB);

    TupleFilter newFilter = GTUtil.convertFilterColumnsAndConstants(filter, info, colMapping, null);
    assertEquals(
            "AND [UNKNOWN_MODEL:NULL.GT_MOCKUP_TABLE.0 GT [\\x00\\x00\\x01J\\xE5\\xBD\\x5C\\x00], UNKNOWN_MODEL:NULL.GT_MOCKUP_TABLE.1 EQ [\\x00]]",
            newFilter.toString());
}
 
Example #8
Source File: InvertIndexSearcher.java    From kylin with Apache License 2.0 6 votes vote down vote up
public EvalResult doEval(TupleFilter filter) {
    if (filter == null)
        return EvalResult.ALL_MATCH;

    if (filter instanceof LogicalTupleFilter)
        return doEvalLogical((LogicalTupleFilter) filter);

    if (filter instanceof CompareTupleFilter)
        return doEvalCompare((CompareTupleFilter) filter);

    if (filter instanceof ConstantTupleFilter) {
        if (filter.getValues().size() == 0) {
            return new EvalResult();
        } else if (filter.getValues().size() > 0) {
            return EvalResult.ALL_MATCH;
        }
    }

    return EvalResult.ALL_MATCH; // unable to evaluate
}
 
Example #9
Source File: TupleFilterVisitor.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public TupleFilter visitLiteral(RexLiteral literal) {
    String strValue = null;
    Object literalValue = literal.getValue();
    if (literalValue instanceof NlsString) {
        strValue = ((NlsString) literalValue).getValue();
    } else if (literalValue instanceof GregorianCalendar) {
        GregorianCalendar g = (GregorianCalendar) literalValue;
        strValue = Long.toString(g.getTimeInMillis());
    } else if (literalValue instanceof TimeUnitRange) {
        // Extract(x from y) in where clause
        strValue = ((TimeUnitRange) literalValue).name();
    } else if (literalValue == null) {
        strValue = null;
    } else {
        strValue = literalValue.toString();
    }
    TupleFilter filter = new ConstantTupleFilter(strValue);
    return filter;
}
 
Example #10
Source File: StreamingSegmentManagerTest.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testOneValueAggregation() {
    genEvents(80000);
    StreamingCubeDataSearcher searcher = streamingSegmentManager.getSearcher();
    String startTimeStr = "2018-07-30 20:00:00";
    long startTime = DateFormat.stringToMillis(startTimeStr);
    String endTimeStr = "2018-07-30 20:04:00";
    long endTime = DateFormat.stringToMillis(endTimeStr);
    CompareTupleFilter filter1 = testHelper.buildCompareFilter("STREAMING_V2_TABLE.MINUTE_START",
        FilterOperatorEnum.GTE, startTimeStr);
    CompareTupleFilter filter2 = testHelper.buildCompareFilter("STREAMING_V2_TABLE.MINUTE_START",
        FilterOperatorEnum.LT, endTimeStr);
    TupleFilter filter = testHelper.buildAndFilter(filter1, filter2);
    Set<FunctionDesc> metrics = Sets.newHashSet(testHelper.simulateCountMetric());
    Set<TblColRef> dimensions = testHelper.simulateDimensions("STREAMING_V2_TABLE.MINUTE_START");
    Set<TblColRef> groups = Sets.newHashSet();
    StreamingSearchContext searchRequest = new StreamingSearchContext(cubeDesc, dimensions, groups, metrics, filter,
        null);
    IStreamingSearchResult segmentResults1 = searcher.doSearch(searchRequest, -1, true);
    for (Record record : segmentResults1) {
        long minStart = Long.valueOf(record.getDimensions()[0]);
        assertTrue(startTime <= minStart && minStart < endTime);
        System.out.println(record);
    }
}
 
Example #11
Source File: Functions.java    From kylin with Apache License 2.0 6 votes vote down vote up
public static TupleFilter getFunctionTupleFilter(String name) {
    if (name == null) {
        throw new IllegalStateException("Function name cannot be null");
    }

    name = name.toUpperCase(Locale.ROOT);

    if (SUPPORTED_UDF.containsKey(name)) {
        try {
            return (TupleFilter) SUPPORTED_UDF.get(name).getConstructor().newInstance();
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException("Failed to on constructing FunctionTupleFilter for " + name);
        }
    }

    return new BuiltInFunctionTupleFilter(name);

}
 
Example #12
Source File: CaseTupleExpression.java    From kylin with Apache License 2.0 6 votes vote down vote up
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(operator.toString());
    sb.append("(");
    boolean ifFirst = true;
    for (Pair<TupleFilter, TupleExpression> whenEntry : whenList) {
        if (ifFirst) {
            ifFirst = false;
        } else {
            sb.append(",");
        }
        sb.append(whenEntry.getFirst().toString());
        sb.append(",");
        sb.append(whenEntry.getSecond().toString());
    }
    if (elseExpr != null) {
        sb.append(",");
        sb.append(elseExpr.toString());
    }
    sb.append(")");
    return sb.toString();
}
 
Example #13
Source File: StreamingCubeDataSearcherPerfTest.java    From kylin with Apache License 2.0 6 votes vote down vote up
private void scan(int time) throws IOException {
    System.out.println("start " + time + " scan");
    Stopwatch sw = Stopwatch.createUnstarted();
    sw.start();
    Set<TblColRef> dimensions = testHelper.simulateDimensions("STREAMING_V2_TABLE.SITE");
    Set<TblColRef> groups = testHelper.simulateDimensions();
    Set<FunctionDesc> metrics = Sets.newHashSet(testHelper.simulateCountMetric());
    long startTime = DateFormat.stringToMillis("2018-07-30 07:00:00");
    long endTime = DateFormat.stringToMillis("2018-07-30 08:00:00");
    TupleFilter filter = testHelper.buildTimeRangeFilter("STREAMING_V2_TABLE.MINUTE_START",
            String.valueOf(startTime), String.valueOf(endTime));
    StreamingSearchContext searchRequest = new StreamingSearchContext(parsedStreamingCubeInfo.cubeDesc, dimensions,
            groups, metrics, null, null);
    IStreamingSearchResult searchResult = searcher.doSearch(searchRequest, 0L, true);
    long scanRowCnt = 0;
    for (Record record : searchResult) {
        scanRowCnt++;
    }
    sw.stop();
    long takeTime = sw.elapsed(MILLISECONDS);
    System.out.println(time + " search finished, scan row cnt:" + scanRowCnt + ", took:" + takeTime
            + ",numRowsPerSec:" + scanRowCnt * 1000 / takeTime);
}
 
Example #14
Source File: CubeEnumerator.java    From Kylin with Apache License 2.0 6 votes vote down vote up
private void bindVariable(TupleFilter filter) {
    if (filter == null) {
        return;
    }

    for (TupleFilter childFilter : filter.getChildren()) {
        bindVariable(childFilter);
    }

    if (filter instanceof CompareTupleFilter && optiqContext != null) {
        CompareTupleFilter compFilter = (CompareTupleFilter) filter;
        for (Map.Entry<String, String> entry : compFilter.getVariables().entrySet()) {
            String variable = entry.getKey();
            Object value = optiqContext.get(variable);
            if (value != null) {
                compFilter.bindVariable(variable, value.toString());
            }

        }
    }
}
 
Example #15
Source File: OLAPFilterRel.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public void implementOLAP(OLAPImplementor implementor) {
    implementor.fixSharedOlapTableScan(this);
    implementor.visitChild(getInput(), this);

    this.columnRowType = buildColumnRowType();
    this.context = implementor.getContext();

    // only translate where clause and don't translate having clause
    if (!context.afterAggregate) {
        translateFilter(context);
    } else {
        context.afterHavingClauseFilter = true;

        TupleFilterVisitor visitor = new TupleFilterVisitor(this.columnRowType);
        TupleFilter havingFilter = this.condition.accept(visitor);
        if (context.havingFilter == null)
            context.havingFilter = havingFilter;
    }
}
 
Example #16
Source File: CaseTupleExpression.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(IFilterCodeSystem<?> cs, ByteBuffer buffer) {
    BytesUtil.writeVInt(whenList.size(), buffer);
    for (Pair<TupleFilter, TupleExpression> whenEntry : whenList) {
        byte[] whenBytes = TupleFilterSerializer.serialize(whenEntry.getFirst(), cs);
        BytesUtil.writeByteArray(whenBytes, buffer);

        byte[] thenBytes = TupleExpressionSerializer.serialize(whenEntry.getSecond(), cs);
        BytesUtil.writeByteArray(thenBytes, buffer);
    }
    if (elseExpr != null) {
        BytesUtil.writeVInt(1, buffer);
        byte[] elseBytes = TupleExpressionSerializer.serialize(elseExpr, cs);
        BytesUtil.writeByteArray(elseBytes, buffer);
    } else {
        BytesUtil.writeVInt(-1, buffer);
    }
}
 
Example #17
Source File: ExpressionCountDistributor.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public TupleExpression visitCaseCall(CaseTupleExpression caseExpr) {
    List<Pair<TupleFilter, TupleExpression>> whenList = Lists
            .newArrayListWithExpectedSize(caseExpr.getWhenList().size());
    for (Pair<TupleFilter, TupleExpression> entry : caseExpr.getWhenList()) {
        TupleFilter filter = entry.getFirst();
        TupleExpression expression = visitIndependent(entry.getSecond());
        whenList.add(new Pair<>(filter, expression));
    }
    TupleExpression elseExpr = null;
    if (caseExpr.getElseExpr() != null) {
        elseExpr = visitIndependent(caseExpr.getElseExpr());
    }

    if (ifToCnt) {
        ifToCnt = ExpressionColCollector.collectMeasureColumns(caseExpr).isEmpty();
    }
    return new CaseTupleExpression(caseExpr.getDataType(), whenList, elseExpr);
}
 
Example #18
Source File: ScanRangePlannerBase.java    From kylin with Apache License 2.0 6 votes vote down vote up
protected TupleFilter flattenToOrAndFilter(TupleFilter filter) {
    if (filter == null)
        return null;

    TupleFilter flatFilter = filter.flatFilter();

    // normalize to OR-AND filter
    if (flatFilter.getOperator() == TupleFilter.FilterOperatorEnum.AND) {
        LogicalTupleFilter f = new LogicalTupleFilter(TupleFilter.FilterOperatorEnum.OR);
        f.addChild(flatFilter);
        flatFilter = f;
    }

    if (flatFilter.getOperator() != TupleFilter.FilterOperatorEnum.OR)
        throw new IllegalStateException();

    return flatFilter;
}
 
Example #19
Source File: ITStorageTest.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private int search(List<TblColRef> groups, List<FunctionDesc> aggregations, TupleFilter filter, StorageContext context) {
    int count = 0;
    ITupleIterator iterator = null;
    try {
        SQLDigest sqlDigest = new SQLDigest("default.test_kylin_fact", /*allCol*/ Collections.<TblColRef> emptySet(), /*join*/ null, //
                groups, /*subqueryJoinParticipants*/ Sets.<TblColRef> newHashSet(), //
                /*dynamicGroupByColumns*/ Collections.<TblColRef, TupleExpression> emptyMap(), //
                /*groupByExpression*/ false, //
                /*metricCol*/ Collections.<TblColRef> emptySet(), aggregations, /*aggrSqlCalls*/ Collections.<SQLCall> emptyList(), //
                /*dynamicAggregations*/ Collections.<DynamicFunctionDesc> emptyList(), //
                /*runtimeDimensionColumns*/ Collections.<TblColRef> emptySet(), //
                /*runtimeMetricColumns*/ Collections.<TblColRef> emptySet(), //
                /*filter col*/ Collections.<TblColRef> emptySet(), filter, null, //
                /*sortCol*/ new ArrayList<TblColRef>(), new ArrayList<SQLDigest.OrderEnum>(), false, false, false, new HashSet<MeasureDesc>());
        iterator = storageEngine.search(context, sqlDigest, mockup.newTupleInfo(groups, aggregations));
        while (iterator.hasNext()) {
            ITuple tuple = iterator.next();
            System.out.println("Tuple = " + tuple);
            count++;
        }
    } finally {
        if (iterator != null)
            iterator.close();
    }
    return count;
}
 
Example #20
Source File: TupleExpressionSerializerTest.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerialization() {
    TblColRef colD = TblColRef.mockup(t, 1, "C1", "decimal");
    TblColRef colM = TblColRef.mockup(t, 2, "C2", "string");
    BigDecimal value = BigDecimal.valueOf(10L);

    ColumnTupleFilter colFilter = new ColumnTupleFilter(colD);
    ConstantTupleFilter constFilter = new ConstantTupleFilter("col");
    CompareTupleFilter compareFilter = new CompareTupleFilter(TupleFilter.FilterOperatorEnum.EQ);
    compareFilter.addChild(colFilter);
    compareFilter.addChild(constFilter);

    ColumnTupleExpression colTuple = new ColumnTupleExpression(colM);
    NumberTupleExpression constTuple = new NumberTupleExpression(value);

    Pair<TupleFilter, TupleExpression> whenEntry = new Pair<TupleFilter, TupleExpression>(compareFilter, colTuple);
    CaseTupleExpression caseTuple = new CaseTupleExpression(Lists.newArrayList(whenEntry), constTuple);

    byte[] result = TupleExpressionSerializer.serialize(caseTuple, StringCodeSystem.INSTANCE);

    TupleExpression desTuple = TupleExpressionSerializer.deserialize(result, StringCodeSystem.INSTANCE);
    assertEquals(caseTuple, desTuple);
}
 
Example #21
Source File: GTCubeStorageQueryBase.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
protected Set<CompareTupleFilter> findSingleValuesCompFilters(TupleFilter filter) {
    Collection<? extends TupleFilter> toCheck;
    if (filter instanceof CompareTupleFilter) {
        toCheck = Collections.singleton(filter);
    } else if (filter instanceof LogicalTupleFilter && filter.getOperator() == FilterOperatorEnum.AND) {
        toCheck = filter.getChildren();
    } else {
        return Collections.emptySet();
    }

    Set<CompareTupleFilter> result = Sets.newHashSet();
    for (TupleFilter f : toCheck) {
        if (f instanceof CompareTupleFilter) {
            CompareTupleFilter compFilter = (CompareTupleFilter) f;
            // is COL=const ?
            if (compFilter.getOperator() == FilterOperatorEnum.EQ && compFilter.getValues().size() == 1
                    && compFilter.getColumn() != null) {
                result.add(compFilter);
            }
        }
    }
    return result;
}
 
Example #22
Source File: GTCubeStorageQueryBase.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected TupleFilter translateDerived(TupleFilter filter, Set<TblColRef> collector) {
    if (filter == null)
        return filter;

    if (filter instanceof CompareTupleFilter) {
        return translateDerivedInCompare((CompareTupleFilter) filter, collector);
    }

    List<TupleFilter> children = (List<TupleFilter>) filter.getChildren();
    List<TupleFilter> newChildren = Lists.newArrayListWithCapacity(children.size());
    boolean modified = false;
    for (TupleFilter child : children) {
        TupleFilter translated = translateDerived(child, collector);
        newChildren.add(translated);
        if (child != translated)
            modified = true;
    }
    if (modified) {
        filter = replaceChildren(filter, newChildren);
    }
    return filter;
}
 
Example #23
Source File: CubeStorageEngine.java    From Kylin with Apache License 2.0 6 votes vote down vote up
private TupleFilter translateDerivedInCompare(CompareTupleFilter compf, Set<TblColRef> collector) {
    if (compf.getColumn() == null || compf.getValues().isEmpty())
        return compf;

    TblColRef derived = compf.getColumn();
    if (cubeDesc.isDerived(derived) == false)
        return compf;

    DeriveInfo hostInfo = cubeDesc.getHostInfo(derived);
    CubeManager cubeMgr = CubeManager.getInstance(this.cubeInstance.getConfig());
    CubeSegment seg = cubeInstance.getLatestReadySegment();
    LookupStringTable lookup = cubeMgr.getLookupTable(seg, hostInfo.dimension);
    Pair<TupleFilter, Boolean> translated = DerivedFilterTranslator.translate(lookup, hostInfo, compf);
    TupleFilter translatedFilter = translated.getFirst();
    boolean loosened = translated.getSecond();
    if (loosened) {
        collectColumnsRecursively(compf, collector);
    }
    return translatedFilter;
}
 
Example #24
Source File: InvertIndexSearcher.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
public EvalResult doEval(TupleFilter filter) {
    if (filter == null)
        return EvalResult.ALL_MATCH;

    if (filter instanceof LogicalTupleFilter)
        return doEvalLogical((LogicalTupleFilter) filter);

    if (filter instanceof CompareTupleFilter)
        return doEvalCompare((CompareTupleFilter) filter);

    if (filter instanceof ConstantTupleFilter) {
        if (filter.getValues().size() == 0) {
            return new EvalResult();
        } else if (filter.getValues().size() > 0) {
            return EvalResult.ALL_MATCH;
        }
    }

    return EvalResult.ALL_MATCH; // unable to evaluate
}
 
Example #25
Source File: FragmentSearchResult.java    From kylin with Apache License 2.0 6 votes vote down vote up
public FilteredAndAggregatedRecords(Iterator<RawRecord> sourceRecords, ResponseResultSchema schema, ColumnarRecordCodec recordDecoder,
                                    TupleFilter filter, Set<TblColRef> groups, TupleFilter havingFilter) {
    this.sourceRecords = sourceRecords;
    this.schema = schema;
    this.recordDecoder = recordDecoder;
    this.filter = filter;
    this.havingFilter = havingFilter;
    this.groupIndexes = new int[groups.size()];
    int i = 0;
    for (TblColRef group : groups) {
        groupIndexes[i] = schema.getIndexOfDimension(group);
        i++;
    }
    if (groupIndexes.length == 0) {
        this.aggrCache = new OneValueAggregationCache();
    } else {
        this.aggrCache = new TreeMapAggregationCache();
    }
}
 
Example #26
Source File: StreamingBuiltInFunctionTransformer.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(IEvaluatableTuple tuple, IFilterCodeSystem cs) {
    // extract tuple value
    Object tupleValue = null;
    for (TupleFilter filter : this.children) {
        if (!isConstant(filter)) {
            filter.evaluate(tuple, cs);
            tupleValue = filter.getValues().iterator().next();
        }
    }
    if (tupleValue == null || cs.isNull(tupleValue)) {
        return false;
    }

    try {
        return (Boolean) buildInFunctionTupleFilter.invokeFunction(tupleValue);
    } catch (Exception e) {
        logger.error("error when invoke build in function", e);
        return false;
    }
}
 
Example #27
Source File: CubeStorageEngine.java    From Kylin with Apache License 2.0 5 votes vote down vote up
private List<HBaseKeyRange> buildScanRanges(TupleFilter flatFilter, Collection<TblColRef> dimensionColumns) {

        List<HBaseKeyRange> result = Lists.newArrayList();

        // build row key range for each cube segment
        for (CubeSegment cubeSeg : cubeInstance.getSegments(SegmentStatusEnum.READY)) {

            // consider derived (lookup snapshot), filter on dimension may
            // differ per segment
            List<Collection<ColumnValueRange>> orAndDimRanges = translateToOrAndDimRanges(flatFilter, cubeSeg);
            if (orAndDimRanges == null) { // has conflict
                continue;
            }

            List<HBaseKeyRange> scanRanges = Lists.newArrayListWithCapacity(orAndDimRanges.size());
            for (Collection<ColumnValueRange> andDimRanges : orAndDimRanges) {
                HBaseKeyRange rowKeyRange = new HBaseKeyRange(dimensionColumns, andDimRanges, cubeSeg, cubeDesc);
                scanRanges.add(rowKeyRange);
            }

            List<HBaseKeyRange> mergedRanges = mergeOverlapRanges(scanRanges);
            mergedRanges = mergeTooManyRanges(mergedRanges);
            result.addAll(mergedRanges);
        }

        dropUnhitSegments(result);

        return result;
    }
 
Example #28
Source File: TupleExpressionVisitor.java    From kylin with Apache License 2.0 5 votes vote down vote up
private CaseTupleExpression getCaseTupleExpression(RexCall call) {
    List<Pair<TupleFilter, TupleExpression>> whenList = Lists
            .newArrayListWithExpectedSize(call.operands.size() / 2);
    TupleExpression elseExpr = null;

    TupleFilterVisitor filterVistor = new TupleFilterVisitor(inputRowType);
    for (int i = 0; i < call.operands.size() - 1; i += 2) {
        if (call.operands.get(i) instanceof RexCall) {
            RexCall whenCall = (RexCall) call.operands.get(i);
            CompareTupleFilter.CompareResultType compareResultType = RexUtil.getCompareResultType(whenCall);
            if (compareResultType == CompareTupleFilter.CompareResultType.AlwaysTrue) {
                elseExpr = call.operands.get(i + 1).accept(this);
                break;
            } else if (compareResultType == CompareTupleFilter.CompareResultType.AlwaysFalse) {
                continue;
            }
            TupleFilter whenFilter = whenCall.accept(filterVistor);
            whenFilter = new FilterOptimizeTransformer().transform(whenFilter);

            TupleExpression thenExpr = call.operands.get(i + 1).accept(this);
            whenList.add(new Pair<>(whenFilter, thenExpr));
        }
    }
    if (elseExpr == null && call.operands.size() % 2 == 1) {
        RexNode elseNode = call.operands.get(call.operands.size() - 1);
        if (!(elseNode instanceof RexLiteral && ((RexLiteral) elseNode).getValue() == null)) {
            elseExpr = elseNode.accept(this);
        }
    }

    DataType dataType = DataType.getType(OLAPTable.DATATYPE_MAPPING.get(call.type.getSqlTypeName()));
    CaseTupleExpression tuple = new CaseTupleExpression(dataType, whenList, elseExpr);
    tuple.setDigest(call.toString());
    return tuple;
}
 
Example #29
Source File: StreamingCubeDataSearcher.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public ITupleIterator search(TupleInfo returnTupleInfo, TupleFilter filter, TupleFilter havingFilter,
        Set<TblColRef> dimensions, Set<TblColRef> groups, Set<FunctionDesc> metrics, boolean allowStorageAggregation) {
    StreamingSearchContext searchRequest = new StreamingSearchContext(cubeDesc, dimensions, groups, metrics,
            filter, havingFilter);
    IStreamingSearchResult searchResult = doSearch(searchRequest, -1, allowStorageAggregation);
    StreamingTupleConverter tupleConverter = new StreamingTupleConverter(searchRequest.getRespResultSchema(),
            returnTupleInfo);
    return transformToTupleIterator(tupleConverter, searchResult, returnTupleInfo);
}
 
Example #30
Source File: FilterSerializeTest.java    From Kylin with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerialize05() {
    ColumnDesc column = new ColumnDesc();

    TblColRef colRef = new TblColRef(column);
    List<TblColRef> groups = new ArrayList<TblColRef>();
    groups.add(colRef);
    TupleFilter filter = buildCompareFilter(groups, 0);

    byte[] bytes = TupleFilterSerializer.serialize(filter);
    TupleFilter newFilter = TupleFilterSerializer.deserialize(bytes);

    compareFilter(filter, newFilter);
}