Search in sources :

Example 6 with BetweenPredicate

use of io.prestosql.sql.tree.BetweenPredicate 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)

Example 7 with BetweenPredicate

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

the class TestSqlParser method testInsertOverwriteCube.

@Test
public void testInsertOverwriteCube() {
    assertStatement("INSERT OVERWRITE CUBE foo WHERE d1 BETWEEN 1012020 AND 31012020", new InsertCube(QualifiedName.of("foo"), Optional.of(new BetweenPredicate(new Identifier("d1"), new LongLiteral("1012020"), new LongLiteral("31012020"))), true));
    assertStatement("INSERT OVERWRITE CUBE c1.s1.foo WHERE d1 BETWEEN 1012020 AND 31012020", new InsertCube(QualifiedName.of("c1", "s1", "foo"), Optional.of(new BetweenPredicate(new Identifier("d1"), new LongLiteral("1012020"), new LongLiteral("31012020"))), true));
}
Also used : Identifier(io.prestosql.sql.tree.Identifier) QueryUtil.quotedIdentifier(io.prestosql.sql.QueryUtil.quotedIdentifier) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) LongLiteral(io.prestosql.sql.tree.LongLiteral) InsertCube(io.prestosql.sql.tree.InsertCube) Test(org.testng.annotations.Test)

Example 8 with BetweenPredicate

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

the class TestSqlParser method testBetween.

@Test
public void testBetween() {
    assertExpression("1 BETWEEN 2 AND 3", new BetweenPredicate(new LongLiteral("1"), new LongLiteral("2"), new LongLiteral("3")));
    assertExpression("1 NOT BETWEEN 2 AND 3", new NotExpression(new BetweenPredicate(new LongLiteral("1"), new LongLiteral("2"), new LongLiteral("3"))));
}
Also used : BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) LongLiteral(io.prestosql.sql.tree.LongLiteral) NotExpression(io.prestosql.sql.tree.NotExpression) Test(org.testng.annotations.Test)

Example 9 with BetweenPredicate

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

the class TestEffectivePredicateExtractor method testValues.

@Test
public void testValues() {
    TypeProvider types = TypeProvider.copyOf(ImmutableMap.<Symbol, Type>builder().put(A, BIGINT).put(B, BIGINT).build());
    // one column
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)))), types, typeAnalyzer), new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))));
    // one column with null
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(bigintLiteralRowExpression(2)), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), or(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new IsNullPredicate(AE)));
    // all nulls
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))))), types, typeAnalyzer), new IsNullPredicate(AE));
    // many rows
    List<List<RowExpression>> rows = IntStream.range(0, 500).mapToObj(TestEffectivePredicateExtractor::bigintLiteralRowExpression).map(ImmutableList::of).collect(toImmutableList());
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), rows), types, typeAnalyzer), new BetweenPredicate(AE, bigintLiteral(0), bigintLiteral(499)));
    // multiple columns
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), bigintLiteralRowExpression(100)), ImmutableList.of(bigintLiteralRowExpression(2), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(new InPredicate(AE, new InListExpression(ImmutableList.of(bigintLiteral(1), bigintLiteral(2)))), new InPredicate(BE, new InListExpression(ImmutableList.of(bigintLiteral(100), bigintLiteral(200))))));
    // multiple columns with null
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString()))), ImmutableList.of(castToRowExpression(new Cast(new NullLiteral(), BIGINT.toString())), bigintLiteralRowExpression(200)))), types, typeAnalyzer), and(or(new ComparisonExpression(EQUAL, AE, bigintLiteral(1)), new IsNullPredicate(AE)), or(new ComparisonExpression(EQUAL, BE, bigintLiteral(200)), new IsNullPredicate(BE))));
    // non-deterministic
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A, B), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1), castToRowExpression(new FunctionCall(QualifiedName.of("rand"), ImmutableList.of()))))), types, typeAnalyzer), new ComparisonExpression(EQUAL, AE, bigintLiteral(1)));
    // non-constant
    assertEquals(effectivePredicateExtractor.extract(SESSION, new ValuesNode(newId(), ImmutableList.of(A), ImmutableList.of(ImmutableList.of(bigintLiteralRowExpression(1)), ImmutableList.of(castToRowExpression(BE)))), types, typeAnalyzer), TRUE_LITERAL);
}
Also used : Cast(io.prestosql.sql.tree.Cast) ValuesNode(io.prestosql.spi.plan.ValuesNode) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) Symbol(io.prestosql.spi.plan.Symbol) InListExpression(io.prestosql.sql.tree.InListExpression) InPredicate(io.prestosql.sql.tree.InPredicate) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) IsNullPredicate(io.prestosql.sql.tree.IsNullPredicate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) NullLiteral(io.prestosql.sql.tree.NullLiteral) Test(org.testng.annotations.Test)

Aggregations

BetweenPredicate (io.prestosql.sql.tree.BetweenPredicate)9 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)7 LongLiteral (io.prestosql.sql.tree.LongLiteral)5 Expression (io.prestosql.sql.tree.Expression)4 Identifier (io.prestosql.sql.tree.Identifier)4 QualifiedName (io.prestosql.sql.tree.QualifiedName)4 Test (org.testng.annotations.Test)4 ParsingOptions (io.prestosql.sql.parser.ParsingOptions)3 FunctionCall (io.prestosql.sql.tree.FunctionCall)3 StringLiteral (io.prestosql.sql.tree.StringLiteral)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 QueryUtil.quotedIdentifier (io.prestosql.sql.QueryUtil.quotedIdentifier)2 CreateCube (io.prestosql.sql.tree.CreateCube)2 DoubleLiteral (io.prestosql.sql.tree.DoubleLiteral)2 GenericLiteral (io.prestosql.sql.tree.GenericLiteral)2 InListExpression (io.prestosql.sql.tree.InListExpression)2 Literal (io.prestosql.sql.tree.Literal)2 NotExpression (io.prestosql.sql.tree.NotExpression)2 Property (io.prestosql.sql.tree.Property)2