org.apache.kylin.query.relnode.OLAPContext Java Examples

The following examples show how to use org.apache.kylin.query.relnode.OLAPContext. 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: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private void resetRealizationInContext(OLAPContext olapContext) {
    IRealization realization = olapContext.realization;
    if (realization == null) {
        return;
    }
    KylinConfig config = getConfig();
    HybridInstance hybridInstance = HybridManager.getInstance(config).getHybridInstance(realization.getName());
    if (hybridInstance != null) {
        olapContext.realization = hybridInstance;
        return;
    }
    CubeInstance cubeInstance = CubeManager.getInstance(config).getCube(realization.getName());
    if (cubeInstance != null) {
        olapContext.realization = cubeInstance;
    }
}
 
Example #2
Source File: LookupTableEnumerator.java    From Kylin with Apache License 2.0 6 votes vote down vote up
public LookupTableEnumerator(OLAPContext olapContext) {

        //TODO: assuming LookupTableEnumerator is handled by a cube
        CubeInstance cube = (CubeInstance) olapContext.realization;

        String lookupTableName = olapContext.firstTableScan.getTableName();
        DimensionDesc dim = cube.getDescriptor().findDimensionByTable(lookupTableName);
        if (dim == null)
            throw new IllegalStateException("No dimension with derived columns found for lookup table " + lookupTableName + ", cube desc " + cube.getDescriptor());

        CubeManager cubeMgr = CubeManager.getInstance(cube.getConfig());
        LookupStringTable table = cubeMgr.getLookupTable(cube.getLatestReadySegment(), dim);
        this.allRows = table.getAllRows();

        OLAPTable olapTable = (OLAPTable) olapContext.firstTableScan.getOlapTable();
        this.colDescs = olapTable.getExposedColumns();
        this.current = new Object[colDescs.size()];

        reset();
    }
 
Example #3
Source File: AdjustForWeeklyMatchedRealization.java    From Kylin with Apache License 2.0 6 votes vote down vote up
private static void convertAggregationToDimension(OLAPContext olapContext, Collection<FunctionDesc> availableAggregations, String factTableName) {
    Iterator<FunctionDesc> it = olapContext.aggregations.iterator();
    while (it.hasNext()) {
        FunctionDesc functionDesc = it.next();
        if (!availableAggregations.contains(functionDesc)) {
            // try to convert the metric to dimension to see if it works
            TblColRef col = functionDesc.selectTblColRef(olapContext.metricsColumns, factTableName);
            functionDesc.setDimensionAsMetric(true);
            olapContext.rewriteFields.remove(functionDesc.getRewriteFieldName());
            if (col != null) {
                olapContext.metricsColumns.remove(col);
                olapContext.groupByColumns.add(col);
            }
            logger.info("Adjust OLAPContext for " + functionDesc);
        }
    }
}
 
Example #4
Source File: QueryService.java    From kylin with Apache License 2.0 6 votes vote down vote up
private void resetRealizationInContext(OLAPContext olapContext) {
    IRealization realization = olapContext.realization;
    if (realization == null) {
        return;
    }
    KylinConfig config = getConfig();
    HybridInstance hybridInstance = HybridManager.getInstance(config).getHybridInstance(realization.getName());
    if (hybridInstance != null) {
        olapContext.realization = hybridInstance;
        return;
    }
    CubeInstance cubeInstance = CubeManager.getInstance(config).getCube(realization.getName());
    if (cubeInstance != null) {
        olapContext.realization = cubeInstance;
    }
}
 
Example #5
Source File: QueryService.java    From kylin with Apache License 2.0 6 votes vote down vote up
private SQLResponse queryWithSqlMassage(SQLRequest sqlRequest) throws Exception {
    SQLResponse fakeResponse = QueryUtil.tableauIntercept(sqlRequest.getSql());
    if (null != fakeResponse) {
        logger.debug("Return fake response, is exception? " + fakeResponse.getIsException());
        return fakeResponse;
    }

    String correctedSql = QueryUtil.massageSql(sqlRequest);
    if (correctedSql.equals(sqlRequest.getSql()) == false)
        logger.info("The corrected query: " + correctedSql);

    // add extra parameters into olap context, like acceptPartial
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put(OLAPContext.PRM_ACCEPT_PARTIAL_RESULT, String.valueOf(sqlRequest.isAcceptPartial()));
    OLAPContext.setParameters(parameters);

    return execute(correctedSql, sqlRequest);
}
 
Example #6
Source File: AdjustForWeeklyMatchedRealization.java    From Kylin with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(List<IRealization> realizations, OLAPContext olapContext) {
    if (realizations.size() > 0) {
        IRealization first = realizations.get(0);

        if (first instanceof CubeInstance) {
            CubeInstance cube = (CubeInstance) first;
            adjustOLAPContextIfNecessary(cube, olapContext);
        }

        if (first instanceof IIInstance) {
            IIInstance ii = (IIInstance) first;
            adjustOLAPContextIfNecessary(ii, olapContext);
        }
    }
}
 
Example #7
Source File: OLAPQuery.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
public Enumerator<Object[]> enumerator() {
    OLAPContext olapContext = OLAPContext.getThreadLocalContextById(contextId);
    switch (type) {
    case OLAP:
        return BackdoorToggles.getPrepareOnly() ? new EmptyEnumerator()
                : new OLAPEnumerator(olapContext, optiqContext);
    case LOOKUP_TABLE:
        return BackdoorToggles.getPrepareOnly() ? new EmptyEnumerator() : new LookupTableEnumerator(olapContext);
    case COL_DICT:
        return BackdoorToggles.getPrepareOnly() ? new EmptyEnumerator() : new DictionaryEnumerator(olapContext);
    case HIVE:
        return BackdoorToggles.getPrepareOnly() ? new EmptyEnumerator() : new HiveEnumerator(olapContext);
    default:
        throw new IllegalArgumentException("Wrong type " + type + "!");
    }
}
 
Example #8
Source File: OLAPQuery.java    From kylin with Apache License 2.0 6 votes vote down vote up
public Enumerator<Object[]> enumerator() {
    OLAPContext olapContext = OLAPContext.getThreadLocalContextById(contextId);
    switch (type) {
    case OLAP:
        return BackdoorToggles.getPrepareOnly() ? new EmptyEnumerator()
                : new OLAPEnumerator(olapContext, optiqContext);
    case LOOKUP_TABLE:
        return BackdoorToggles.getPrepareOnly() ? new EmptyEnumerator() : new LookupTableEnumerator(olapContext);
    case COL_DICT:
        return BackdoorToggles.getPrepareOnly() ? new EmptyEnumerator() : new DictionaryEnumerator(olapContext);
    case HIVE:
        return BackdoorToggles.getPrepareOnly() ? new EmptyEnumerator() : new HiveEnumerator(olapContext);
    default:
        throw new IllegalArgumentException("Wrong type " + type + "!");
    }
}
 
Example #9
Source File: QueryRouter.java    From Kylin with Apache License 2.0 6 votes vote down vote up
public static IRealization selectRealization(OLAPContext olapContext) throws NoRealizationFoundException {

        ProjectManager prjMgr = ProjectManager.getInstance(olapContext.olapSchema.getConfig());
        String factTableName = olapContext.firstTableScan.getTableName();
        String projectName = olapContext.olapSchema.getProjectName();
        List<IRealization> realizations = Lists.newArrayList(prjMgr.getRealizationsByTable(projectName, factTableName));
        logger.info("Find candidates by table " + factTableName + " and project=" + projectName + " : " + StringUtils.join(realizations, ","));

        //rule based realization selection, rules might reorder realizations or remove specific realization
        RoutingRule.applyRules(realizations, olapContext);

        if (realizations.size() == 0) {
            throw new NoRealizationFoundException("Can't find any realization. Please confirm with providers. SQL digest: " + olapContext.getSQLDigest().toString());
        }

        logger.info("The realizations remaining: ");
        logger.info(RoutingRule.getPrintableText(realizations));
        logger.info("The realization being chosen: " + realizations.get(0).getName());

        return realizations.get(0);
    }
 
Example #10
Source File: QueryService.java    From Kylin with Apache License 2.0 6 votes vote down vote up
protected SQLResponse executeQuery(String sql, SQLRequest sqlRequest) throws Exception {
    sql = sql.trim().replace(";", "");

    int limit = sqlRequest.getLimit();
    if (limit > 0 && !sql.toLowerCase().contains("limit")) {
        sql += (" LIMIT " + limit);
    }

    int offset = sqlRequest.getOffset();
    if (offset > 0 && !sql.toLowerCase().contains("offset")) {
        sql += (" OFFSET " + offset);
    }

    // add extra parameters into olap context, like acceptPartial
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put(OLAPContext.PRM_ACCEPT_PARTIAL_RESULT, String.valueOf(sqlRequest.isAcceptPartial()));
    OLAPContext.setParameters(parameters);

    return execute(sql, sqlRequest);
}
 
Example #11
Source File: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private SQLResponse queryWithSqlMassage(SQLRequest sqlRequest) throws Exception {
    SQLResponse fakeResponse = QueryUtil.tableauIntercept(sqlRequest.getSql());
    if (null != fakeResponse) {
        logger.debug("Return fake response, is exception? " + fakeResponse.getIsException());
        return fakeResponse;
    }

    String correctedSql = QueryUtil.massageSql(sqlRequest);
    if (correctedSql.equals(sqlRequest.getSql()) == false)
        logger.info("The corrected query: " + correctedSql);

    // add extra parameters into olap context, like acceptPartial
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put(OLAPContext.PRM_ACCEPT_PARTIAL_RESULT, String.valueOf(sqlRequest.isAcceptPartial()));
    OLAPContext.setParameters(parameters);

    return execute(correctedSql, sqlRequest);
}
 
Example #12
Source File: TableLevelACL.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static Set<String> getTableWithSchema(List<OLAPContext> contexts) {
    // all tables with DB, Like DB.TABLE
    Set<String> tableWithSchema = new HashSet<>();
    for (OLAPContext context : contexts) {
        Set<TblColRef> allColumns = context.allColumns;
        for (TblColRef tblColRef : allColumns) {
            tableWithSchema.add(tblColRef.getTableWithSchema());
        }
    }
    return tableWithSchema;
}
 
Example #13
Source File: StreamTableInterceptor.java    From kylin with Apache License 2.0 5 votes vote down vote up
private Set<String> getAllTblsWithSchema(List<OLAPContext> contexts) {
    // all tables with DB, Like DB.TABLE
    Set<String> tableWithSchema = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    for (OLAPContext context : contexts) {
        for (OLAPTableScan tableScan : context.allTableScans) {
            tableWithSchema.add(tableScan.getTableRef().getTableIdentity());
        }
    }
    return tableWithSchema;
}
 
Example #14
Source File: QueryRouter.java    From kylin with Apache License 2.0 5 votes vote down vote up
private static void collectIncapableReason(OLAPContext olapContext, List<Candidate> candidates) {
    for (Candidate candidate : candidates) {
        if (!candidate.getCapability().capable) {
            RealizationCheck.IncapableReason reason = RealizationCheck.IncapableReason
                    .create(candidate.getCapability().incapableCause);
            if (reason != null)
                olapContext.realizationCheck.addIncapableCube(candidate.getRealization(), reason);
        } else {
            olapContext.realizationCheck.addCapableCube(candidate.getRealization());
        }
    }
}
 
Example #15
Source File: TableInterceptor.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
protected Set<String> getIdentifierBlackList(List<OLAPContext> contexts) {
    String project = getProject(contexts);
    String username = getUser(contexts);

    return TableACLManager
            .getInstance(KylinConfig.getInstanceFromEnv())
            .getTableACLByCache(project)
            .getTableBlackList(username, AclPermissionUtil.getCurrentUserGroups());
}
 
Example #16
Source File: RealizationChooser.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void selectRealization(List<OLAPContext> contexts) {
    // try different model for different context

    for (OLAPContext ctx : contexts) {
        ctx.realizationCheck = new RealizationCheck();
        attemptSelectRealization(ctx);
        Preconditions.checkNotNull(ctx.realization);
    }
}
 
Example #17
Source File: RealizationChooser.java    From kylin with Apache License 2.0 5 votes vote down vote up
private static Map<String, String> matches(DataModelDesc model, OLAPContext ctx) {
    Map<String, String> result = Maps.newHashMap();

    TableRef firstTable = ctx.firstTableScan.getTableRef();

    Map<String, String> matchUp = null;

    if (ctx.joins.isEmpty() && model.isLookupTable(firstTable.getTableIdentity())) {
        // one lookup table
        String modelAlias = model.findFirstTable(firstTable.getTableIdentity()).getAlias();
        matchUp = ImmutableMap.of(firstTable.getAlias(), modelAlias);
    } else if (ctx.joins.size() != ctx.allTableScans.size() - 1) {
        // has hanging tables
        ctx.realizationCheck.addModelIncapableReason(model,
                RealizationCheck.IncapableReason.create(RealizationCheck.IncapableType.MODEL_BAD_JOIN_SEQUENCE));
        throw new IllegalStateException("Please adjust the sequence of join tables. " + toErrorMsg(ctx));
    } else {
        // normal big joins
        if (ctx.joinsTree == null) {
            ctx.joinsTree = new JoinsTree(firstTable, ctx.joins);
        }
        matchUp = ctx.joinsTree.matches(model.getJoinsTree(), result);
    }

    if (matchUp == null) {
        ctx.realizationCheck.addModelIncapableReason(model,
                RealizationCheck.IncapableReason.create(RealizationCheck.IncapableType.MODEL_UNMATCHED_JOIN));
        return null;
    }

    result.putAll(matchUp);

    ctx.realizationCheck.addCapableModel(model, result);
    return result;
}
 
Example #18
Source File: RealizationPriorityRule.java    From Kylin with Apache License 2.0 5 votes vote down vote up
public void apply(List<IRealization> realizations, OLAPContext olapContext) {

        Collections.sort(realizations, new Comparator<IRealization>() {
            @Override
            public int compare(IRealization o1, IRealization o2) {
                int i1 = priorities.get(o1.getType());
                int i2 = priorities.get(o2.getType());
                return i1 - i2;
            }
        });
    }
 
Example #19
Source File: TableInterceptor.java    From kylin with Apache License 2.0 5 votes vote down vote up
private Set<String> getAllTblsWithSchema(List<OLAPContext> contexts) {
    // all tables with DB, Like DB.TABLE
    Set<String> tableWithSchema = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    for (OLAPContext context : contexts) {
        for (OLAPTableScan tableScan : context.allTableScans) {
            tableWithSchema.add(tableScan.getTableRef().getTableIdentity());
        }
    }
    return tableWithSchema;
}
 
Example #20
Source File: TableLevelACL.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void tableFilter(List<OLAPContext> contexts, List<String> tableBlackList) {
    Set<String> tableWithSchema = getTableWithSchema(contexts);
    for (String tbl : tableBlackList) {
        if (tableWithSchema.contains(tbl.toUpperCase(Locale.ROOT))) {
            //                throw new kylin.AccessDeniedException("table:" + tbl);
            System.out.println("Access table:" + tbl + " denied");
        }
    }
}
 
Example #21
Source File: CubesSortRule.java    From Kylin with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(List<IRealization> realizations, OLAPContext olapContext) {

    // sort cube candidates, 0) the cost indicator, 1) the lesser header
    // columns the better, 2) the lesser body columns the better
    List<Integer> items = super.findRealizationsOf(realizations, RealizationType.CUBE);
    PartialSorter.partialSort(realizations, items, new Comparator<IRealization>() {
        @Override
        public int compare(IRealization o1, IRealization o2) {
            CubeInstance c1 = (CubeInstance) o1;
            CubeInstance c2 = (CubeInstance) o2;
            int comp = 0;
            comp = c1.getCost() - c2.getCost();
            if (comp != 0) {
                return comp;
            }

            CubeDesc schema1 = c1.getDescriptor();
            CubeDesc schema2 = c2.getDescriptor();

            comp = schema1.listDimensionColumnsIncludingDerived().size() - schema2.listDimensionColumnsIncludingDerived().size();
            if (comp != 0)
                return comp;

            comp = schema1.getMeasures().size() - schema2.getMeasures().size();
            return comp;
        }
    });

}
 
Example #22
Source File: RemoveUncapableRealizationsRule.java    From Kylin with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(List<IRealization> realizations, OLAPContext olapContext) {
    for (Iterator<IRealization> iterator = realizations.iterator(); iterator.hasNext();) {
        IRealization realization = iterator.next();
        if (!realization.isCapable(olapContext.getSQLDigest())) {
            iterator.remove();
        }
    }
}
 
Example #23
Source File: QueryService.java    From kylin with Apache License 2.0 5 votes vote down vote up
private static PreparedContext createPreparedContext(String project, String sql) throws Exception {
    Connection conn = QueryConnection.getConnection(project);
    PreparedStatement preparedStatement = conn.prepareStatement(sql);
    Collection<OLAPContext> olapContexts = OLAPContext.getThreadLocalContexts();
    // If the preparedContext is first initialized, then set the borrowed tag to false
    for (OLAPContext olapContext : olapContexts) {
        olapContext.isBorrowedContext = false;
    }
    return new PreparedContext(conn, preparedStatement, olapContexts);
}
 
Example #24
Source File: QueryInterceptor.java    From kylin with Apache License 2.0 5 votes vote down vote up
private void intercept(List<OLAPContext> contexts, Collection<String> blackList) {
    if (blackList.isEmpty()) {
        return;
    }

    Collection<String> queryCols = getQueryIdentifiers(contexts);
    for (String id : blackList) {
        if (queryCols.contains(id.toUpperCase(Locale.ROOT))) {
            throw new AccessDeniedException(getIdentifierType() + ":" + id);
        }
    }
}
 
Example #25
Source File: QueryInterceptor.java    From kylin with Apache License 2.0 5 votes vote down vote up
public void intercept(List<OLAPContext> contexts) {
    if (!isEnabled()) {
        return;
    }
    Collection<String> userIdentifierBlackList = getIdentifierBlackList(contexts);
    intercept(contexts, userIdentifierBlackList);
}
 
Example #26
Source File: QueryInterceptor.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public void intercept(List<OLAPContext> contexts) {
    if (!isEnabled()) {
        return;
    }
    Collection<String> userIdentifierBlackList = getIdentifierBlackList(contexts);
    intercept(contexts, userIdentifierBlackList);
}
 
Example #27
Source File: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private static PreparedContext createPreparedContext(String project, String sql) throws Exception {
    Connection conn = QueryConnection.getConnection(project);
    PreparedStatement preparedStatement = conn.prepareStatement(sql);
    Collection<OLAPContext> olapContexts = OLAPContext.getThreadLocalContexts();
    // If the preparedContext is first initialized, then set the borrowed tag to false
    for (OLAPContext olapContext : olapContexts) {
        olapContext.isBorrowedContext = false;
    }
    return new PreparedContext(conn, preparedStatement, olapContexts);
}
 
Example #28
Source File: NExecAndComp.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private static Dataset<Row> queryWithKap(String prj, String joinType, Pair<String, String> pair,
                                         Map<String, CompareEntity> compareEntityMap) {

    compareEntityMap.putIfAbsent(pair.getFirst(), new CompareEntity());
    final CompareEntity entity = compareEntityMap.get(pair.getFirst());
    entity.setSql(pair.getFirst());
    Dataset<Row> rowDataset = queryFromCube(prj, changeJoinType(pair.getSecond(), joinType));
    entity.setOlapContexts(OLAPContext.getThreadLocalContexts());
    OLAPContext.clearThreadLocalContexts();
    return rowDataset;
}
 
Example #29
Source File: AdjustForWeeklyMatchedRealization.java    From Kylin with Apache License 2.0 5 votes vote down vote up
private static void adjustOLAPContextIfNecessary(CubeInstance cube, OLAPContext olapContext) {
    if (CubeCapabilityChecker.check(cube, olapContext.getSQLDigest(), false))
        return;

    CubeDesc cubeDesc = cube.getDescriptor();
    Collection<FunctionDesc> cubeFuncs = cubeDesc.listAllFunctions();
    convertAggregationToDimension(olapContext, cubeFuncs, cubeDesc.getFactTable());
}
 
Example #30
Source File: TableLevelACL.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void columnFilter(List<OLAPContext> contexts, List<String> columnBlackList) {
    List<String> allColWithTblAndSchema = getAllColWithTblAndSchema(contexts);
    for (String tbl : columnBlackList) {
        if (allColWithTblAndSchema.contains(tbl.toUpperCase(Locale.ROOT))) {
            //                throw new kylin.AccessDeniedException("table:" + tbl);
            System.out.println("Access table:" + tbl + " denied");
        }
    }
}