Search in sources :

Example 11 with DoubleLiteral

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

the class TestComparisonStatsCalculator method symbolToLiteralEqualStats.

@Test
public void symbolToLiteralEqualStats() {
    // Simple case
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("y"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    25.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(2.5).highValue(2.5).nullsFraction(0.0);
    });
    // Literal on the edge of symbol range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("x"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.75).symbolStats("x", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(10.0).highValue(10.0).nullsFraction(0.0);
    });
    // Literal out of symbol range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("y"), new DoubleLiteral("10.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    0.0).symbolStats("y", symbolAssert -> {
        symbolAssert.averageRowSize(0.0).distinctValuesCount(0.0).emptyRange().nullsFraction(1.0);
    });
    // Literal in left open range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("leftOpen"), new DoubleLiteral("2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).symbolStats("leftOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(2.5).highValue(2.5).nullsFraction(0.0);
    });
    // Literal in right open range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("rightOpen"), new DoubleLiteral("-2.5"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).symbolStats("rightOpen", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(-2.5).highValue(-2.5).nullsFraction(0.0);
    });
    // Literal in unknown range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("unknownRange"), new DoubleLiteral("0.0"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).symbolStats("unknownRange", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(0.0).highValue(0.0).nullsFraction(0.0);
    });
    // Literal in empty range
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("emptyRange"), new DoubleLiteral("0.0"))).outputRowsCount(0.0).symbolStats("emptyRange", equalTo(emptyRangeStats));
    // Column with values not representable as double (unknown range)
    assertCalculate(new ComparisonExpression(EQUAL, new SymbolReference("varchar"), new StringLiteral("blah"))).outputRowsCount(// all rows minus nulls divided by distinct values count
    18.0).symbolStats("varchar", symbolAssert -> {
        symbolAssert.averageRowSize(4.0).distinctValuesCount(1.0).lowValue(NEGATIVE_INFINITY).highValue(POSITIVE_INFINITY).nullsFraction(0.0);
    });
}
Also used : ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) StringLiteral(io.prestosql.sql.tree.StringLiteral) SymbolReference(io.prestosql.sql.tree.SymbolReference) DoubleLiteral(io.prestosql.sql.tree.DoubleLiteral) Test(org.testng.annotations.Test)

Example 12 with DoubleLiteral

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

the class TestStarTreeAggregationRule method testBuildSymbolMappings.

@Test
public void testBuildSymbolMappings() {
    RowExpression rowExpression = planBuilder.variable(columnTotalprice.getName(), totalpriceHandle.getType());
    FilterNode filterNode = new FilterNode(newId(), baseTableScan, rowExpression);
    Assignments projectionAssignments = Assignments.builder().put(columnCustkey, planBuilder.variable(columnCustkey.getName(), custkeyHandle.getType())).build();
    ProjectNode projectNode1 = new ProjectNode(newId(), filterNode, projectionAssignments);
    List<ProjectNode> projectNodes = ImmutableList.of(projectNode1);
    AggregationNode countAggNode = new AggregationNode(newId(), projectNode1, ImmutableMap.of(columnCustkey, new AggregationNode.Aggregation(Expressions.call("count", COUNT, DOUBLE, ImmutableList.of(planBuilder.variable(columnOrderkey.getName(), orderkeyHandle.getType()))), ImmutableList.of(planBuilder.variable(columnCustkey.getName(), custkeyHandle.getType())), true, Optional.empty(), Optional.empty(), Optional.empty())), singleGroupingSet(ImmutableList.of(columnOrderkey, columnOrderDate)), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty(), AggregationNode.AggregationType.HASH, Optional.empty());
    Map<String, Object> columnMapping = CubeOptimizerUtil.buildSymbolMappings(countAggNode, projectNodes, Optional.of(filterNode), baseTableScan, baseTableMetadata);
    assertEquals(columnMapping.size(), 4);
    assertEquals(new ArrayList<>(columnMapping.keySet()).toString(), "[orderkey, custkey, totalprice, orderdate]");
    assertTrue(columnMapping.values().containsAll(assignments.values()) && assignments.values().containsAll(columnMapping.values()));
    // expression Literal
    DoubleLiteral testDouble = new DoubleLiteral("1.0");
    Assignments assignment2 = Assignments.builder().put(columnCustkey, OriginalExpressionUtils.castToRowExpression(testDouble)).build();
    ProjectNode projectNode2 = new ProjectNode(newId(), filterNode, assignment2);
    List<ProjectNode> projections2 = ImmutableList.of(projectNode2);
    columnMapping = CubeOptimizerUtil.buildSymbolMappings(countAggNode, projections2, Optional.of(filterNode), baseTableScan, baseTableMetadata);
    assertEquals(new ArrayList(columnMapping.values()).get(1), testDouble);
    // expression Cast/SymbolReference
    Cast testCast = new Cast(SymbolUtils.toSymbolReference(columnCustkey), StandardTypes.INTEGER);
    Assignments assignment3 = Assignments.builder().put(columnCustkey, OriginalExpressionUtils.castToRowExpression(testCast)).build();
    ProjectNode planNode3 = new ProjectNode(newId(), baseTableScan, assignment3);
    List<ProjectNode> projections3 = ImmutableList.of(planNode3);
    columnMapping = CubeOptimizerUtil.buildSymbolMappings(countAggNode, projections3, Optional.of(filterNode), baseTableScan, baseTableMetadata);
    assertEquals(new ArrayList(columnMapping.values()).get(1), custkeyHandle);
    // expression Cast/Literal
    Cast testCast2 = new Cast(testDouble, StandardTypes.DOUBLE);
    Assignments assignment4 = Assignments.builder().put(columnCustkey, OriginalExpressionUtils.castToRowExpression(testCast2)).build();
    ProjectNode planNode4 = new ProjectNode(newId(), baseTableScan, assignment4);
    List<ProjectNode> projections4 = ImmutableList.of(planNode4);
    columnMapping = CubeOptimizerUtil.buildSymbolMappings(countAggNode, projections4, Optional.of(filterNode), baseTableScan, baseTableMetadata);
    assertEquals(new ArrayList(columnMapping.values()).get(1), testDouble);
}
Also used : Cast(io.prestosql.sql.tree.Cast) FilterNode(io.prestosql.spi.plan.FilterNode) Assignments(io.prestosql.spi.plan.Assignments) ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) AggregationNode(io.prestosql.spi.plan.AggregationNode) Matchers.anyString(org.mockito.Matchers.anyString) ProjectNode(io.prestosql.spi.plan.ProjectNode) DoubleLiteral(io.prestosql.sql.tree.DoubleLiteral) BaseRuleTest(io.prestosql.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 13 with DoubleLiteral

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

the class CubeConsole method isSupportedExpression.

/**
 * Gets whether the expression is supported for processing the create cube query
 *
 * @param createCube createCube
 * @return void
 */
private boolean isSupportedExpression(CreateCube createCube, QueryRunner queryRunner, ClientOptions.OutputFormat outputFormat, Runnable schemaChanged, boolean usePager, boolean showProgress, Terminal terminal, PrintStream out, PrintStream errorChannel) {
    boolean supportedExpression = false;
    boolean success = true;
    Optional<Expression> expression = createCube.getWhere();
    if (expression.isPresent()) {
        ImmutableSet.Builder<Identifier> identifierBuilder = new ImmutableSet.Builder<>();
        new DefaultExpressionTraversalVisitor<Void, ImmutableSet.Builder<Identifier>>() {

            @Override
            protected Void visitIdentifier(Identifier node, ImmutableSet.Builder<Identifier> builder) {
                builder.add(node);
                return null;
            }
        }.process(expression.get(), identifierBuilder);
        int sizeIdentifiers = identifierBuilder.build().asList().size();
        if (sizeIdentifiers == SUPPORTED_INDENTIFIER_SIZE) {
            String whereClause = createCube.getWhere().get().toString();
            QualifiedName sourceTableName = createCube.getSourceTableName();
            if (expression.get() instanceof BetweenPredicate) {
                BetweenPredicate betweenPredicate = (BetweenPredicate) (expression.get());
                String columnName = betweenPredicate.getValue().toString();
                String columnDataTypeQuery;
                String catalogName;
                String tableName = sourceTableName.getSuffix();
                checkArgument(tableName.matches("[\\p{Alnum}_]+"), "Invalid table name");
                if (hasInvalidSymbol(columnName)) {
                    return false;
                }
                if (sourceTableName.getPrefix().isPresent() && sourceTableName.getPrefix().get().getPrefix().isPresent()) {
                    catalogName = sourceTableName.getPrefix().get().getPrefix().get().toString();
                    checkArgument(catalogName.matches("[\\p{Alnum}_]+"), "Invalid catalog name");
                    columnDataTypeQuery = String.format(SELECT_DATA_TYPE_STRING, catalogName, tableName, columnName);
                } else if (queryRunner.getSession().getCatalog() != null) {
                    catalogName = queryRunner.getSession().getCatalog();
                    checkArgument(catalogName.matches("[\\p{Alnum}_]+"), "Invalid catalog name");
                    columnDataTypeQuery = String.format(SELECT_DATA_TYPE_STRING, catalogName, tableName, columnName);
                } else {
                    return false;
                }
                if (!processCubeInitialQuery(queryRunner, columnDataTypeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
                    return false;
                }
                String resInitCubeQuery;
                resInitCubeQuery = getResultInitCubeQuery();
                if (resInitCubeQuery != null) {
                    cubeColumnDataType = resInitCubeQuery;
                }
                if (cubeColumnDataType.contains(DATATYPE_DECIMAL)) {
                    cubeColumnDataType = DATATYPE_DECIMAL;
                }
                if (cubeColumnDataType.contains(DATATYPE_VARCHAR)) {
                    cubeColumnDataType = DATATYPE_VARCHAR;
                }
                if (!isSupportedDatatype(cubeColumnDataType)) {
                    return false;
                }
                if (betweenPredicate.getMin() instanceof LongLiteral || betweenPredicate.getMin() instanceof LongLiteral || betweenPredicate.getMin() instanceof TimestampLiteral || betweenPredicate.getMin() instanceof TimestampLiteral || betweenPredicate.getMin() instanceof GenericLiteral || betweenPredicate.getMin() instanceof StringLiteral || betweenPredicate.getMin() instanceof DoubleLiteral) {
                    if (betweenPredicate.getMax() instanceof LongLiteral || betweenPredicate.getMax() instanceof LongLiteral || betweenPredicate.getMax() instanceof TimestampLiteral || betweenPredicate.getMax() instanceof TimestampLiteral || betweenPredicate.getMax() instanceof GenericLiteral || betweenPredicate.getMax() instanceof StringLiteral || betweenPredicate.getMax() instanceof DoubleLiteral) {
                        // initial query to get the total number of distinct column values in the table
                        String countDistinctQuery = String.format(SELECT_COUNT_DISTINCT_FROM_STRING, columnName, sourceTableName.toString(), whereClause);
                        if (!processCubeInitialQuery(queryRunner, countDistinctQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
                            return false;
                        }
                        Long valueCountDistinctQuery = INITIAL_QUERY_RESULT_VALUE;
                        resInitCubeQuery = getResultInitCubeQuery();
                        if (resInitCubeQuery != null) {
                            valueCountDistinctQuery = Long.parseLong(resInitCubeQuery);
                        }
                        if (valueCountDistinctQuery < MAX_BUFFERED_ROWS && valueCountDistinctQuery * rowBufferTempMultiplier < Integer.MAX_VALUE) {
                            supportedExpression = true;
                            rowBufferListSize = (int) ((valueCountDistinctQuery).intValue() * rowBufferTempMultiplier);
                        }
                    }
                }
            }
            if (expression.get() instanceof ComparisonExpression) {
                ComparisonExpression comparisonExpression = (ComparisonExpression) (createCube.getWhere().get());
                ComparisonExpression.Operator operator = comparisonExpression.getOperator();
                Expression left = comparisonExpression.getLeft();
                Expression right = comparisonExpression.getRight();
                if (!(left instanceof SymbolReference) && right instanceof SymbolReference) {
                    comparisonExpression = new ComparisonExpression(operator.flip(), right, left);
                }
                if (left instanceof Literal && !(right instanceof Literal)) {
                    comparisonExpression = new ComparisonExpression(operator.flip(), right, left);
                }
                if (comparisonExpression.getRight() instanceof LongLiteral) {
                    supportedExpression = true;
                }
                String catalogName;
                String tableName = sourceTableName.getSuffix();
                String columnName = comparisonExpression.getLeft().toString();
                String columnDataTypeQuery;
                checkArgument(tableName.matches("[\\p{Alnum}_]+"), "Invalid table name");
                if (hasInvalidSymbol(columnName)) {
                    return false;
                }
                if (sourceTableName.getPrefix().isPresent() && sourceTableName.getPrefix().get().getPrefix().isPresent()) {
                    catalogName = sourceTableName.getPrefix().get().getPrefix().get().toString();
                    checkArgument(catalogName.matches("[\\p{Alnum}_]+"), "Invalid catalog name");
                    columnDataTypeQuery = String.format(SELECT_DATA_TYPE_STRING, catalogName, tableName, columnName);
                } else if (queryRunner.getSession().getCatalog() != null) {
                    catalogName = queryRunner.getSession().getCatalog();
                    checkArgument(catalogName.matches("[\\p{Alnum}_]+"), "Invalid catalog name");
                    columnDataTypeQuery = String.format(SELECT_DATA_TYPE_STRING, catalogName, tableName, columnName);
                } else {
                    return false;
                }
                if (!processCubeInitialQuery(queryRunner, columnDataTypeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
                    return false;
                }
                String resInitCubeQuery;
                resInitCubeQuery = getResultInitCubeQuery();
                if (resInitCubeQuery != null) {
                    cubeColumnDataType = resInitCubeQuery.toLowerCase(Locale.ENGLISH);
                }
                if (cubeColumnDataType.contains(DATATYPE_DECIMAL)) {
                    cubeColumnDataType = DATATYPE_DECIMAL;
                }
                if (cubeColumnDataType.contains(DATATYPE_VARCHAR)) {
                    cubeColumnDataType = DATATYPE_VARCHAR;
                }
                if (!isSupportedDatatype(cubeColumnDataType)) {
                    return false;
                }
                if (comparisonExpression.getRight() instanceof GenericLiteral || comparisonExpression.getRight() instanceof StringLiteral || comparisonExpression.getRight() instanceof DoubleLiteral || comparisonExpression.getRight() instanceof LongLiteral || comparisonExpression.getRight() instanceof TimestampLiteral) {
                    // initial query to get the total number of distinct column values in the table
                    String countDistinctQuery = String.format(SELECT_COUNT_DISTINCT_FROM_STRING, columnName, sourceTableName.toString(), whereClause);
                    if (!processCubeInitialQuery(queryRunner, countDistinctQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
                        return false;
                    }
                    Long valueCountDistinctQuery = INITIAL_QUERY_RESULT_VALUE;
                    resInitCubeQuery = getResultInitCubeQuery();
                    if (resInitCubeQuery != null) {
                        valueCountDistinctQuery = Long.parseLong(resInitCubeQuery);
                    }
                    if (valueCountDistinctQuery < MAX_BUFFERED_ROWS) {
                        supportedExpression = true;
                    }
                }
            }
        }
    }
    return supportedExpression;
}
Also used : BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) TimestampLiteral(io.prestosql.sql.tree.TimestampLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) SymbolReference(io.prestosql.sql.tree.SymbolReference) QualifiedName(io.prestosql.sql.tree.QualifiedName) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Identifier(io.prestosql.sql.tree.Identifier) ImmutableSet(com.google.common.collect.ImmutableSet) StringLiteral(io.prestosql.sql.tree.StringLiteral) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) Literal(io.prestosql.sql.tree.Literal) TimestampLiteral(io.prestosql.sql.tree.TimestampLiteral) DoubleLiteral(io.prestosql.sql.tree.DoubleLiteral) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) LongLiteral(io.prestosql.sql.tree.LongLiteral) StringLiteral(io.prestosql.sql.tree.StringLiteral) DoubleLiteral(io.prestosql.sql.tree.DoubleLiteral)

Aggregations

DoubleLiteral (io.prestosql.sql.tree.DoubleLiteral)13 Test (org.testng.annotations.Test)10 StringLiteral (io.prestosql.sql.tree.StringLiteral)8 LongLiteral (io.prestosql.sql.tree.LongLiteral)6 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)5 SymbolReference (io.prestosql.sql.tree.SymbolReference)5 GenericLiteral (io.prestosql.sql.tree.GenericLiteral)4 DecimalLiteral (io.prestosql.sql.tree.DecimalLiteral)3 Expression (io.prestosql.sql.tree.Expression)3 Slice (io.airlift.slice.Slice)2 RowExpression (io.prestosql.spi.relation.RowExpression)2 BooleanLiteral (io.prestosql.sql.tree.BooleanLiteral)2 Cast (io.prestosql.sql.tree.Cast)2 NullLiteral (io.prestosql.sql.tree.NullLiteral)2 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Maps (com.google.common.collect.Maps)1