Search in sources :

Example 1 with TimestampLiteral

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

the class TestSqlParser method testLiterals.

@Test
public void testLiterals() {
    assertExpression("TIME" + " 'abc'", new TimeLiteral("abc"));
    assertExpression("TIMESTAMP" + " 'abc'", new TimestampLiteral("abc"));
    assertExpression("INTERVAL '33' day", new IntervalLiteral("33", Sign.POSITIVE, IntervalField.DAY, Optional.empty()));
    assertExpression("INTERVAL '33' day to second", new IntervalLiteral("33", Sign.POSITIVE, IntervalField.DAY, Optional.of(IntervalField.SECOND)));
    assertExpression("CHAR 'abc'", new CharLiteral("abc"));
}
Also used : IntervalLiteral(io.prestosql.sql.tree.IntervalLiteral) TimestampLiteral(io.prestosql.sql.tree.TimestampLiteral) CharLiteral(io.prestosql.sql.tree.CharLiteral) TimeLiteral(io.prestosql.sql.tree.TimeLiteral) Test(org.testng.annotations.Test)

Example 2 with TimestampLiteral

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

TimestampLiteral (io.prestosql.sql.tree.TimestampLiteral)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 BetweenPredicate (io.prestosql.sql.tree.BetweenPredicate)1 CharLiteral (io.prestosql.sql.tree.CharLiteral)1 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)1 DoubleLiteral (io.prestosql.sql.tree.DoubleLiteral)1 Expression (io.prestosql.sql.tree.Expression)1 GenericLiteral (io.prestosql.sql.tree.GenericLiteral)1 Identifier (io.prestosql.sql.tree.Identifier)1 IntervalLiteral (io.prestosql.sql.tree.IntervalLiteral)1 Literal (io.prestosql.sql.tree.Literal)1 LongLiteral (io.prestosql.sql.tree.LongLiteral)1 QualifiedName (io.prestosql.sql.tree.QualifiedName)1 StringLiteral (io.prestosql.sql.tree.StringLiteral)1 SymbolReference (io.prestosql.sql.tree.SymbolReference)1 TimeLiteral (io.prestosql.sql.tree.TimeLiteral)1 Test (org.testng.annotations.Test)1