Search in sources :

Example 1 with PartitionParser

use of com.netflix.metacat.common.server.partition.parser.PartitionParser in project metacat by Netflix.

the class FilterPartition method evaluatePartitionExpression.

/**
     * Evaluates the given expression.
     * @param partitionExpression expression
     * @param name name
     * @param path partition uri path
     * @param batchid batch id
     * @param values map of values
     * @return true if the expression evaluates to true
     */
public boolean evaluatePartitionExpression(final String partitionExpression, final String name, final String path, final boolean batchid, final Map<String, String> values) {
    if (partitionExpression != null) {
        try {
            if (parser == null) {
                parser = new PartitionParser(new StringReader(partitionExpression));
            } else {
                parser.ReInit(new StringReader(partitionExpression));
            }
            context.clear();
            if (batchid) {
                PartitionUtil.getPartitionKeyValues(path, context);
            }
            PartitionUtil.getPartitionKeyValues(name, context);
            if (values != null) {
                context.putAll(values);
            }
            if (context.size() > 0) {
                return (Boolean) parser.filter().jjtAccept(new PartitionParserEval(context), null);
            } else {
                return false;
            }
        } catch (ParseException | TokenMgrError e) {
            throw new IllegalArgumentException(String.format("Invalid expression: %s", partitionExpression), e);
        } catch (IllegalArgumentException e) {
            throw e;
        } catch (Throwable t) {
            log.warn("Caught unexpected exception during evaluatePartitionExpression,", t);
            return false;
        }
    }
    return true;
}
Also used : PartitionParser(com.netflix.metacat.common.server.partition.parser.PartitionParser) StringReader(java.io.StringReader) TokenMgrError(com.netflix.metacat.common.server.partition.parser.TokenMgrError) PartitionParserEval(com.netflix.metacat.common.server.partition.visitor.PartitionParserEval) ParseException(com.netflix.metacat.common.server.partition.parser.ParseException)

Example 2 with PartitionParser

use of com.netflix.metacat.common.server.partition.parser.PartitionParser in project metacat by Netflix.

the class HiveConnectorFastPartitionService method getDateCreatedSqlCriteria.

private String getDateCreatedSqlCriteria(final String filterExpression) {
    final StringBuilder result = new StringBuilder();
    Collection<String> values = Lists.newArrayList();
    if (!Strings.isNullOrEmpty(filterExpression)) {
        try {
            values = (Collection<String>) new PartitionParser(new StringReader(filterExpression)).filter().jjtAccept(new PartitionParamParserEval(), null);
        } catch (Throwable ignored) {
        //
        }
    }
    for (String value : values) {
        if (result.length() != 0) {
            result.append(" and ");
        }
        result.append(value.replace("dateCreated", "p.CREATE_TIME"));
    }
    return result.toString();
}
Also used : PartitionParser(com.netflix.metacat.common.server.partition.parser.PartitionParser) PartitionParamParserEval(com.netflix.metacat.common.server.partition.visitor.PartitionParamParserEval) StringReader(java.io.StringReader)

Example 3 with PartitionParser

use of com.netflix.metacat.common.server.partition.parser.PartitionParser in project metacat by Netflix.

the class HiveConnectorFastPartitionService method getHandlerResults.

private <T> List<T> getHandlerResults(final String databaseName, final String tableName, final String filterExpression, final List<String> partitionIds, final String sql, final ResultSetHandler<List<T>> resultSetHandler, final Sort sort, final Pageable pageable) {
    List<T> partitions;
    try {
        if (!Strings.isNullOrEmpty(filterExpression)) {
            final PartitionFilterGenerator generator = new PartitionFilterGenerator(getPartitionKeys(databaseName, tableName));
            String filterSql = (String) new PartitionParser(new StringReader(filterExpression)).filter().jjtAccept(generator, null);
            if (generator.isOptimized()) {
                filterSql = generator.getOptimizedSql();
            }
            if (filterSql != null && !filterSql.isEmpty()) {
                filterSql = " and (" + filterSql + ")";
            }
            partitions = gethandlerresults(databaseName, tableName, filterExpression, partitionIds, sql, resultSetHandler, generator.joinSql(), filterSql, generator.getParams(), sort, pageable);
        } else {
            partitions = gethandlerresults(databaseName, tableName, null, partitionIds, sql, resultSetHandler, null, null, null, sort, pageable);
        }
    } catch (Exception e) {
        log.warn("Experiment: Get partitions for for table {} filter {}" + " failed with error {}", tableName, filterExpression, e.getMessage());
        registry.counter(HiveMetrics.CounterHiveExperimentGetTablePartitionsFailure.name()).increment();
        partitions = gethandlerresults(databaseName, tableName, filterExpression, partitionIds, sql, resultSetHandler, null, prepareFilterSql(filterExpression), Lists.newArrayList(), sort, pageable);
    }
    return partitions;
}
Also used : PartitionParser(com.netflix.metacat.common.server.partition.parser.PartitionParser) StringReader(java.io.StringReader) PartitionFilterGenerator(com.netflix.metacat.connector.hive.util.PartitionFilterGenerator) ConnectorException(com.netflix.metacat.common.server.connectors.exception.ConnectorException) SQLException(java.sql.SQLException)

Example 4 with PartitionParser

use of com.netflix.metacat.common.server.partition.parser.PartitionParser in project metacat by Netflix.

the class S3ConnectorPartitionService method getDateCreatedSqlCriteria.

private String getDateCreatedSqlCriteria(final String filterExpression) {
    final StringBuilder result = new StringBuilder();
    Collection<String> values = com.google.common.collect.Lists.newArrayList();
    if (!Strings.isNullOrEmpty(filterExpression)) {
        try {
            values = (Collection<String>) new PartitionParser(new StringReader(filterExpression)).filter().jjtAccept(new PartitionParamParserEval(), null);
        } catch (Throwable ignored) {
        //
        }
    }
    for (String value : values) {
        if (result.length() != 0) {
            result.append(" and ");
        }
        result.append(value.replace("dateCreated", "to_seconds(p.date_created)"));
    }
    return result.toString();
}
Also used : PartitionParser(com.netflix.metacat.common.server.partition.parser.PartitionParser) PartitionParamParserEval(com.netflix.metacat.common.server.partition.visitor.PartitionParamParserEval) StringReader(java.io.StringReader)

Aggregations

PartitionParser (com.netflix.metacat.common.server.partition.parser.PartitionParser)4 StringReader (java.io.StringReader)4 PartitionParamParserEval (com.netflix.metacat.common.server.partition.visitor.PartitionParamParserEval)2 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)1 ParseException (com.netflix.metacat.common.server.partition.parser.ParseException)1 TokenMgrError (com.netflix.metacat.common.server.partition.parser.TokenMgrError)1 PartitionParserEval (com.netflix.metacat.common.server.partition.visitor.PartitionParserEval)1 PartitionFilterGenerator (com.netflix.metacat.connector.hive.util.PartitionFilterGenerator)1 SQLException (java.sql.SQLException)1