Search in sources :

Example 6 with QualifiedName

use of io.prestosql.sql.tree.QualifiedName in project hetu-core by openlookeng.

the class TestSqlParser method testCreateTableAsSelect.

@Test
public void testCreateTableAsSelect() {
    Query query = simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("t")));
    Query querySelectColumn = simpleQuery(selectList(new Identifier("a")), table(QualifiedName.of("t")));
    Query querySelectColumns = simpleQuery(selectList(new Identifier("a"), new Identifier("b")), table(QualifiedName.of("t")));
    QualifiedName table = QualifiedName.of("foo");
    assertStatement("CREATE TABLE foo AS SELECT * FROM t", new CreateTableAsSelect(table, query, false, ImmutableList.of(), true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS foo AS SELECT * FROM t", new CreateTableAsSelect(table, query, true, ImmutableList.of(), true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS foo(x) AS SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS foo(x,y) AS SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, true, ImmutableList.of(), true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE foo AS SELECT * FROM t WITH NO DATA", new CreateTableAsSelect(table, query, false, ImmutableList.of(), false, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) AS SELECT a FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) AS SELECT a,b FROM t WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, ImmutableList.of(), false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    List<Property> properties = ImmutableList.of(new Property(new Identifier("string"), new StringLiteral("bar")), new Property(new Identifier("long"), new LongLiteral("42")), new Property(new Identifier("computed"), new FunctionCall(QualifiedName.of("concat"), ImmutableList.of(new StringLiteral("ban"), new StringLiteral("ana")))), new Property(new Identifier("a"), new ArrayConstructor(ImmutableList.of(new StringLiteral("v1"), new StringLiteral("v2")))));
    assertStatement("CREATE TABLE foo " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t", new CreateTableAsSelect(table, query, false, properties, true, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t", new CreateTableAsSelect(table, querySelectColumn, false, properties, true, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t", new CreateTableAsSelect(table, querySelectColumns, false, properties, true, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE foo " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, query, false, properties, false, Optional.empty(), Optional.empty()));
    assertStatement("CREATE TABLE foo(x) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.empty()));
    assertStatement("CREATE TABLE foo(x,y) " + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.empty()));
    assertStatement("CREATE TABLE foo COMMENT 'test'" + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT * FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, query, false, properties, false, Optional.empty(), Optional.of("test")));
    assertStatement("CREATE TABLE foo(x) COMMENT 'test'" + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumn, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"))), Optional.of("test")));
    assertStatement("CREATE TABLE foo(x,y) COMMENT 'test'" + "WITH ( string = 'bar', long = 42, computed = 'ban' || 'ana', a  = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.of("test")));
    assertStatement("CREATE TABLE foo(x,y) COMMENT 'test'" + "WITH ( \"string\" = 'bar', \"long\" = 42, computed = 'ban' || 'ana', a = ARRAY[ 'v1', 'v2' ] ) " + "AS " + "SELECT a,b FROM t " + "WITH NO DATA", new CreateTableAsSelect(table, querySelectColumns, false, properties, false, Optional.of(ImmutableList.of(new Identifier("x"), new Identifier("y"))), Optional.of("test")));
}
Also used : Identifier(io.prestosql.sql.tree.Identifier) QueryUtil.quotedIdentifier(io.prestosql.sql.QueryUtil.quotedIdentifier) QueryUtil.simpleQuery(io.prestosql.sql.QueryUtil.simpleQuery) Query(io.prestosql.sql.tree.Query) WithQuery(io.prestosql.sql.tree.WithQuery) StringLiteral(io.prestosql.sql.tree.StringLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) QualifiedName(io.prestosql.sql.tree.QualifiedName) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect) ArrayConstructor(io.prestosql.sql.tree.ArrayConstructor) AllColumns(io.prestosql.sql.tree.AllColumns) FunctionCall(io.prestosql.sql.tree.FunctionCall) Property(io.prestosql.sql.tree.Property) Test(org.testng.annotations.Test)

Example 7 with QualifiedName

use of io.prestosql.sql.tree.QualifiedName in project hetu-core by openlookeng.

the class QueryRewriter method rewriteCreateTableAsSelect.

private Query rewriteCreateTableAsSelect(Connection connection, Query query, CreateTableAsSelect statement) throws SQLException, QueryRewriteException {
    QualifiedName temporaryTableName = generateTemporaryTableName(statement.getName());
    Statement rewritten = new CreateTableAsSelect(temporaryTableName, statement.getQuery(), statement.isNotExists(), statement.getProperties(), statement.isWithData(), statement.getColumnAliases(), Optional.empty());
    String createTableAsSql = formatSql(rewritten, Optional.empty());
    String checksumSql = checksumSql(getColumns(connection, statement), temporaryTableName);
    String dropTableSql = dropTableSql(temporaryTableName);
    return new Query(query.getCatalog(), query.getSchema(), ImmutableList.of(createTableAsSql), checksumSql, ImmutableList.of(dropTableSql), query.getUsername(), query.getPassword(), query.getSessionProperties());
}
Also used : Statement(io.prestosql.sql.tree.Statement) QualifiedName(io.prestosql.sql.tree.QualifiedName) CreateTableAsSelect(io.prestosql.sql.tree.CreateTableAsSelect)

Example 8 with QualifiedName

use of io.prestosql.sql.tree.QualifiedName in project hetu-core by openlookeng.

the class QueryRewriter method rewriteInsertQuery.

private Query rewriteInsertQuery(Connection connection, Query query, Insert statement) throws SQLException, QueryRewriteException {
    QualifiedName temporaryTableName = generateTemporaryTableName(statement.getTarget());
    Statement createTemporaryTable = new CreateTable(temporaryTableName, ImmutableList.of(new LikeClause(statement.getTarget(), Optional.of(INCLUDING))), true, ImmutableList.of(), Optional.empty());
    String createTemporaryTableSql = formatSql(createTemporaryTable, Optional.empty());
    String insertSql = formatSql(new Insert(temporaryTableName, statement.getColumns(), statement.getQuery(), statement.getOverwrite()), Optional.empty());
    String checksumSql = checksumSql(getColumnsForTable(connection, query.getCatalog(), query.getSchema(), statement.getTarget().toString()), temporaryTableName);
    String dropTableSql = dropTableSql(temporaryTableName);
    return new Query(query.getCatalog(), query.getSchema(), ImmutableList.of(createTemporaryTableSql, insertSql), checksumSql, ImmutableList.of(dropTableSql), query.getUsername(), query.getPassword(), query.getSessionProperties());
}
Also used : LikeClause(io.prestosql.sql.tree.LikeClause) Statement(io.prestosql.sql.tree.Statement) QualifiedName(io.prestosql.sql.tree.QualifiedName) CreateTable(io.prestosql.sql.tree.CreateTable) Insert(io.prestosql.sql.tree.Insert)

Example 9 with QualifiedName

use of io.prestosql.sql.tree.QualifiedName in project hetu-core by openlookeng.

the class RelationPlanner method visitJoin.

@Override
protected RelationPlan visitJoin(Join node, Void context) {
    // TODO: translate the RIGHT join into a mirrored LEFT join when we refactor (@martint)
    RelationPlan leftPlan = process(node.getLeft(), context);
    Optional<Unnest> unnest = getUnnest(node.getRight());
    if (unnest.isPresent()) {
        if (node.getType() != Join.Type.CROSS && node.getType() != Join.Type.IMPLICIT) {
            throw notSupportedException(unnest.get(), "UNNEST on other than the right side of CROSS JOIN");
        }
        return planCrossJoinUnnest(leftPlan, node, unnest.get());
    }
    Optional<Lateral> lateral = getLateral(node.getRight());
    if (lateral.isPresent()) {
        return planLateralJoin(node, leftPlan, lateral.get());
    }
    RelationPlan rightPlan = process(node.getRight(), context);
    if (node.getCriteria().isPresent() && node.getCriteria().get() instanceof JoinUsing) {
        return planJoinUsing(node, leftPlan, rightPlan);
    }
    PlanBuilder leftPlanBuilder = initializePlanBuilder(leftPlan);
    PlanBuilder rightPlanBuilder = initializePlanBuilder(rightPlan);
    // NOTE: symbols must be in the same order as the outputDescriptor
    List<Symbol> outputSymbols = ImmutableList.<Symbol>builder().addAll(leftPlan.getFieldMappings()).addAll(rightPlan.getFieldMappings()).build();
    ImmutableList.Builder<JoinNode.EquiJoinClause> equiClauses = ImmutableList.builder();
    List<Expression> complexJoinExpressions = new ArrayList<>();
    List<Expression> postInnerJoinConditions = new ArrayList<>();
    if (node.getType() != Join.Type.CROSS && node.getType() != Join.Type.IMPLICIT) {
        Expression criteria = analysis.getJoinCriteria(node);
        RelationType left = analysis.getOutputDescriptor(node.getLeft());
        RelationType right = analysis.getOutputDescriptor(node.getRight());
        List<Expression> leftComparisonExpressions = new ArrayList<>();
        List<Expression> rightComparisonExpressions = new ArrayList<>();
        List<ComparisonExpression.Operator> joinConditionComparisonOperators = new ArrayList<>();
        for (Expression conjunct : ExpressionUtils.extractConjuncts(criteria)) {
            conjunct = ExpressionUtils.normalize(conjunct);
            if (!isEqualComparisonExpression(conjunct) && node.getType() != INNER) {
                complexJoinExpressions.add(conjunct);
                continue;
            }
            Set<QualifiedName> dependencies = SymbolsExtractor.extractNames(conjunct, analysis.getColumnReferences());
            if (dependencies.stream().allMatch(left::canResolve) || dependencies.stream().allMatch(right::canResolve)) {
                // If the conjunct can be evaluated entirely with the inputs on either side of the join, add
                // it to the list complex expressions and let the optimizers figure out how to push it down later.
                complexJoinExpressions.add(conjunct);
            } else if (conjunct instanceof ComparisonExpression) {
                Expression firstExpression = ((ComparisonExpression) conjunct).getLeft();
                Expression secondExpression = ((ComparisonExpression) conjunct).getRight();
                ComparisonExpression.Operator comparisonOperator = ((ComparisonExpression) conjunct).getOperator();
                Set<QualifiedName> firstDependencies = SymbolsExtractor.extractNames(firstExpression, analysis.getColumnReferences());
                Set<QualifiedName> secondDependencies = SymbolsExtractor.extractNames(secondExpression, analysis.getColumnReferences());
                if (firstDependencies.stream().allMatch(left::canResolve) && secondDependencies.stream().allMatch(right::canResolve)) {
                    leftComparisonExpressions.add(firstExpression);
                    rightComparisonExpressions.add(secondExpression);
                    joinConditionComparisonOperators.add(comparisonOperator);
                } else if (firstDependencies.stream().allMatch(right::canResolve) && secondDependencies.stream().allMatch(left::canResolve)) {
                    leftComparisonExpressions.add(secondExpression);
                    rightComparisonExpressions.add(firstExpression);
                    joinConditionComparisonOperators.add(comparisonOperator.flip());
                } else {
                    // the case when we mix symbols from both left and right join side on either side of condition.
                    complexJoinExpressions.add(conjunct);
                }
            } else {
                complexJoinExpressions.add(conjunct);
            }
        }
        leftPlanBuilder = subqueryPlanner.handleSubqueries(leftPlanBuilder, leftComparisonExpressions, node);
        rightPlanBuilder = subqueryPlanner.handleSubqueries(rightPlanBuilder, rightComparisonExpressions, node);
        // Add projections for join criteria
        leftPlanBuilder = leftPlanBuilder.appendProjections(leftComparisonExpressions, planSymbolAllocator, idAllocator);
        rightPlanBuilder = rightPlanBuilder.appendProjections(rightComparisonExpressions, planSymbolAllocator, idAllocator);
        for (int i = 0; i < leftComparisonExpressions.size(); i++) {
            if (joinConditionComparisonOperators.get(i) == ComparisonExpression.Operator.EQUAL) {
                Symbol leftSymbol = leftPlanBuilder.translate(leftComparisonExpressions.get(i));
                Symbol rightSymbol = rightPlanBuilder.translate(rightComparisonExpressions.get(i));
                equiClauses.add(new JoinNode.EquiJoinClause(leftSymbol, rightSymbol));
            } else {
                Expression leftExpression = leftPlanBuilder.rewrite(leftComparisonExpressions.get(i));
                Expression rightExpression = rightPlanBuilder.rewrite(rightComparisonExpressions.get(i));
                postInnerJoinConditions.add(new ComparisonExpression(joinConditionComparisonOperators.get(i), leftExpression, rightExpression));
            }
        }
    }
    PlanNode root = new JoinNode(idAllocator.getNextId(), JoinNodeUtils.typeConvert(node.getType()), leftPlanBuilder.getRoot(), rightPlanBuilder.getRoot(), equiClauses.build(), ImmutableList.<Symbol>builder().addAll(leftPlanBuilder.getRoot().getOutputSymbols()).addAll(rightPlanBuilder.getRoot().getOutputSymbols()).build(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of());
    if (node.getType() != INNER) {
        for (Expression complexExpression : complexJoinExpressions) {
            Set<InPredicate> inPredicates = subqueryPlanner.collectInPredicateSubqueries(complexExpression, node);
            if (!inPredicates.isEmpty()) {
                InPredicate inPredicate = Iterables.getLast(inPredicates);
                throw notSupportedException(inPredicate, "IN with subquery predicate in join condition");
            }
        }
        // subqueries can be applied only to one side of join - left side is selected in arbitrary way
        leftPlanBuilder = subqueryPlanner.handleUncorrelatedSubqueries(leftPlanBuilder, complexJoinExpressions, node);
    }
    RelationPlan intermediateRootRelationPlan = new RelationPlan(root, analysis.getScope(node), outputSymbols);
    TranslationMap translationMap = new TranslationMap(intermediateRootRelationPlan, analysis, lambdaDeclarationToSymbolMap);
    translationMap.setFieldMappings(outputSymbols);
    translationMap.putExpressionMappingsFrom(leftPlanBuilder.getTranslations());
    translationMap.putExpressionMappingsFrom(rightPlanBuilder.getTranslations());
    if (node.getType() != INNER && !complexJoinExpressions.isEmpty()) {
        Expression joinedFilterCondition = ExpressionUtils.and(complexJoinExpressions);
        Expression rewrittenFilterCondition = translationMap.rewrite(joinedFilterCondition);
        root = new JoinNode(idAllocator.getNextId(), JoinNodeUtils.typeConvert(node.getType()), leftPlanBuilder.getRoot(), rightPlanBuilder.getRoot(), equiClauses.build(), ImmutableList.<Symbol>builder().addAll(leftPlanBuilder.getRoot().getOutputSymbols()).addAll(rightPlanBuilder.getRoot().getOutputSymbols()).build(), Optional.of(castToRowExpression(rewrittenFilterCondition)), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of());
    }
    if (node.getType() == INNER) {
        // rewrite all the other conditions using output symbols from left + right plan node.
        PlanBuilder rootPlanBuilder = new PlanBuilder(translationMap, root);
        rootPlanBuilder = subqueryPlanner.handleSubqueries(rootPlanBuilder, complexJoinExpressions, node);
        for (Expression expression : complexJoinExpressions) {
            postInnerJoinConditions.add(rootPlanBuilder.rewrite(expression));
        }
        root = rootPlanBuilder.getRoot();
        Expression postInnerJoinCriteria;
        if (!postInnerJoinConditions.isEmpty()) {
            postInnerJoinCriteria = ExpressionUtils.and(postInnerJoinConditions);
            root = new FilterNode(idAllocator.getNextId(), root, castToRowExpression(postInnerJoinCriteria));
        }
    }
    return new RelationPlan(root, analysis.getScope(node), outputSymbols);
}
Also used : ReuseExchangeOperator(io.prestosql.spi.operator.ReuseExchangeOperator) Set(java.util.Set) AggregationNode.singleGroupingSet(io.prestosql.spi.plan.AggregationNode.singleGroupingSet) HashSet(java.util.HashSet) Symbol(io.prestosql.spi.plan.Symbol) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) FilterNode(io.prestosql.spi.plan.FilterNode) ArrayList(java.util.ArrayList) PlanNode(io.prestosql.spi.plan.PlanNode) RelationType(io.prestosql.sql.analyzer.RelationType) Unnest(io.prestosql.sql.tree.Unnest) Lateral(io.prestosql.sql.tree.Lateral) LateralJoinNode(io.prestosql.sql.planner.plan.LateralJoinNode) JoinNode(io.prestosql.spi.plan.JoinNode) QualifiedName(io.prestosql.sql.tree.QualifiedName) JoinUsing(io.prestosql.sql.tree.JoinUsing) InPredicate(io.prestosql.sql.tree.InPredicate) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression)

Example 10 with QualifiedName

use of io.prestosql.sql.tree.QualifiedName in project hetu-core by openlookeng.

the class CubeConsole method processBetweenPredicate.

/**
 * Process the Create Cube Query with Between Predicate in where clause.
 *
 * @param createCube createCube
 * @param queryRunner queryRunner
 * @param outputFormat outputFormat
 * @param schemaChanged schemaChanged
 * @param usePager usePager
 * @param showProgress showProgress
 * @param terminal terminal
 * @param out out
 * @param errorChannel errorChannel
 * @param parser parser
 * @return boolean
 */
private boolean processBetweenPredicate(CreateCube createCube, QueryRunner queryRunner, ClientOptions.OutputFormat outputFormat, Runnable schemaChanged, boolean usePager, boolean showProgress, Terminal terminal, PrintStream out, PrintStream errorChannel, SqlParser parser) {
    String whereClause = createCube.getWhere().get().toString();
    QualifiedName sourceTableName = createCube.getSourceTableName();
    QualifiedName cubeName = createCube.getCubeName();
    BetweenPredicate betweenPredicate = (BetweenPredicate) (createCube.getWhere().get());
    Expression betweenPredicateValue = betweenPredicate.getValue();
    String columnName = betweenPredicate.getValue().toString();
    BetweenPredicate betweenExpression = ((BetweenPredicate) createCube.getWhere().get());
    Expression left = betweenExpression.getMin();
    Expression right = betweenExpression.getMax();
    // Run Query
    String rowCountsDistinctValuesQuery = String.format(SELECT_COLUMN_ROW_COUNT_FROM_STRING, columnName, sourceTableName.toString(), whereClause, columnName, columnName);
    if (!processCubeInitialQuery(queryRunner, rowCountsDistinctValuesQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
        return false;
    }
    List<List<?>> bufferIterationItems = getListRowBufferIterationItems();
    if (bufferIterationItems != null) {
        // this loop process the multiple insert query statements
        int end = bufferIterationItems.size() - 1;
        for (int i = 0; i <= end; i++) {
            List<?> rowBufferItems = bufferIterationItems.get(i);
            Expression finalPredicate;
            String queryInsert;
            String minItem = rowBufferItems.get(INDEX_AT_MIN_POSITION).toString();
            String maxItem = rowBufferItems.get(INDEX_AT_MAX_POSITION).toString();
            Expression finalPredicateMinExp = null;
            Expression finalPredicateMaxExp = null;
            switch(cubeColumnDataType) {
                case DATATYPE_DOUBLE:
                    {
                        double parsedMinVal = Double.parseDouble(minItem);
                        double parsedMaxVal = Double.parseDouble(maxItem);
                        double userMinVal = Double.parseDouble(extractQuotedValue(left.toString()));
                        double userMaxVal = Double.parseDouble(extractQuotedValue(right.toString()));
                        if (parsedMinVal > userMinVal) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(DATATYPE_DOUBLE + QUOTE_STRING + parsedMinVal + QUOTE_STRING, new ParsingOptions());
                        }
                        if (parsedMaxVal < userMaxVal) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(DATATYPE_DOUBLE + QUOTE_STRING + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
                case DATATYPE_REAL:
                    {
                        double parsedMinVal = Double.parseDouble(minItem);
                        double parsedMaxVal = Double.parseDouble(maxItem);
                        double userMinVal = Double.parseDouble(extractQuotedValue(left.toString()));
                        double userMaxVal = Double.parseDouble(extractQuotedValue(right.toString()));
                        if (parsedMinVal > userMinVal) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(DATATYPE_REAL + QUOTE_STRING + parsedMinVal + QUOTE_STRING, new ParsingOptions());
                        }
                        if (parsedMaxVal < userMaxVal) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(DATATYPE_REAL + QUOTE_STRING + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
                case DATATYPE_DECIMAL:
                    {
                        double parsedMinVal = Double.parseDouble(minItem);
                        double parsedMaxVal = Double.parseDouble(maxItem);
                        double userMinVal = Double.parseDouble(extractQuotedValue(left.toString()));
                        double userMaxVal = Double.parseDouble(extractQuotedValue(right.toString()));
                        if (parsedMinVal > userMinVal) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(DATATYPE_DECIMAL + QUOTE_STRING + parsedMinVal + QUOTE_STRING, new ParsingOptions());
                        }
                        if (parsedMaxVal < userMaxVal) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(DATATYPE_DECIMAL + QUOTE_STRING + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
                case DATATYPE_DATE:
                    {
                        LocalDate parsedMinVal = LocalDate.parse(minItem);
                        LocalDate parsedMaxVal = LocalDate.parse(maxItem);
                        LocalDate userMinVal = LocalDate.parse(extractQuotedValue(left.toString()));
                        LocalDate userMaxVal = LocalDate.parse(extractQuotedValue(right.toString()));
                        if (parsedMinVal.compareTo(userMinVal) > 0) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(DATATYPE_DATE_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
                        }
                        if (parsedMaxVal.compareTo(userMaxVal) < 0) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(DATATYPE_DATE_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
                case DATATYPE_TIMESTAMP:
                    {
                        Timestamp parsedMinVal = Timestamp.valueOf(minItem);
                        Timestamp parsedMaxVal = Timestamp.valueOf(maxItem);
                        Timestamp userMinVal = Timestamp.valueOf(extractQuotedValue(left.toString()));
                        Timestamp userMaxVal = Timestamp.valueOf(extractQuotedValue(right.toString()));
                        if (parsedMinVal.compareTo(userMinVal) > 0) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(DATATYPE_TIMESTAMP_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
                        }
                        if (parsedMaxVal.compareTo(userMaxVal) < 0) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(DATATYPE_TIMESTAMP_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
                case DATATYPE_TINYINT:
                    {
                        int parsedMinVal = Integer.parseInt(minItem);
                        int parsedMaxVal = Integer.parseInt(maxItem);
                        int userMinVal = Integer.parseInt(extractQuotedValue(left.toString()));
                        int userMaxVal = Integer.parseInt(extractQuotedValue(right.toString()));
                        if (parsedMinVal > userMinVal) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(DATATYPE_TINYINT_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
                        }
                        if (parsedMaxVal < userMaxVal) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(DATATYPE_TINYINT_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
                case DATATYPE_BIGINT:
                    {
                        BigInteger parsedMinVal = new BigInteger(minItem);
                        BigInteger parsedMaxVal = new BigInteger(maxItem);
                        BigInteger userMinVal = new BigInteger(extractQuotedValue(left.toString()));
                        BigInteger userMaxVal = new BigInteger(extractQuotedValue(right.toString()));
                        if (parsedMinVal.compareTo(userMinVal) > 0) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(DATATYPE_BIGINT_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
                        }
                        if (parsedMaxVal.compareTo(userMaxVal) < 0) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(DATATYPE_BIGINT_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
                case DATATYPE_SMALLINT:
                    {
                        int parsedMinVal = Integer.parseInt(minItem);
                        int parsedMaxVal = Integer.parseInt(maxItem);
                        int userMinVal = Integer.parseInt(extractQuotedValue(left.toString()));
                        int userMaxVal = Integer.parseInt(extractQuotedValue(right.toString()));
                        if (parsedMinVal > userMinVal) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(DATATYPE_SMALLINT_QUOTE + parsedMinVal + QUOTE_STRING, new ParsingOptions());
                        }
                        if (parsedMaxVal < userMaxVal) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(DATATYPE_SMALLINT_QUOTE + parsedMaxVal + QUOTE_STRING, new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
                case DATATYPE_VARCHAR:
                    {
                        String userMinVal = extractQuotedValue(left.toString());
                        String userMaxVal = extractQuotedValue(right.toString());
                        if (minItem.compareTo(userMinVal) > 0) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(QUOTE_STRING + minItem + QUOTE_STRING, new ParsingOptions());
                        }
                        if (maxItem.compareTo(userMaxVal) < 0) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(QUOTE_STRING + maxItem + QUOTE_STRING, new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
                default:
                    {
                        int parsedMinVal = Integer.parseInt(minItem);
                        int parsedMaxVal = Integer.parseInt(maxItem);
                        int userMinVal = Integer.parseInt(extractQuotedValue(left.toString()));
                        int userMaxVal = Integer.parseInt(extractQuotedValue(right.toString()));
                        if (parsedMinVal > userMinVal) {
                            finalPredicateMinExp = left;
                        } else {
                            finalPredicateMinExp = parser.createExpression(rowBufferItems.get(INDEX_AT_MIN_POSITION).toString(), new ParsingOptions());
                        }
                        if (parsedMaxVal < userMaxVal) {
                            finalPredicateMaxExp = right;
                        } else {
                            finalPredicateMaxExp = parser.createExpression(rowBufferItems.get(INDEX_AT_MAX_POSITION).toString(), new ParsingOptions());
                        }
                        finalPredicate = new BetweenPredicate(betweenPredicateValue, finalPredicateMinExp, finalPredicateMaxExp);
                        break;
                    }
            }
            queryInsert = String.format(INSERT_INTO_CUBE_STRING, cubeName, finalPredicate);
            if (!console.runQuery(queryRunner, queryInsert, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
                return false;
            }
        }
    } else {
        // if the range is within the processing size limit then we run a single insert query only
        String queryInsert = String.format(INSERT_INTO_CUBE_STRING, cubeName, whereClause);
        return console.runQuery(queryRunner, queryInsert, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
    }
    return true;
}
Also used : BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) QualifiedName(io.prestosql.sql.tree.QualifiedName) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) List(java.util.List) LocalDate(java.time.LocalDate) Timestamp(java.sql.Timestamp)

Aggregations

QualifiedName (io.prestosql.sql.tree.QualifiedName)30 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)13 Expression (io.prestosql.sql.tree.Expression)11 Test (org.testng.annotations.Test)10 Identifier (io.prestosql.sql.tree.Identifier)9 FunctionCall (io.prestosql.sql.tree.FunctionCall)8 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)8 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)7 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)7 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)7 LongLiteral (io.prestosql.sql.tree.LongLiteral)7 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)7 SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)7 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)6 IfExpression (io.prestosql.sql.tree.IfExpression)6 InListExpression (io.prestosql.sql.tree.InListExpression)6 NotExpression (io.prestosql.sql.tree.NotExpression)6 NullIfExpression (io.prestosql.sql.tree.NullIfExpression)6 SearchedCaseExpression (io.prestosql.sql.tree.SearchedCaseExpression)6 SimpleCaseExpression (io.prestosql.sql.tree.SimpleCaseExpression)6