Search in sources :

Example 11 with QualifiedName

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

the class CubeConsole method createCubeCommand.

/**
 * Process the Create Cube Query
 *
 * @param queryRunner queryRunner
 * @param outputFormat outputFormat
 * @param schemaChanged schemaChanged
 * @param usePager usePager
 * @param schemaChanged schemaChanged
 * @param showProgress showProgress
 * @param terminal terminal
 * @param out out
 * @param errorChannel errorChannel
 * @return boolean after processing the create cube query command.
 */
public boolean createCubeCommand(String query, QueryRunner queryRunner, ClientOptions.OutputFormat outputFormat, Runnable schemaChanged, boolean usePager, boolean showProgress, Terminal terminal, PrintStream out, PrintStream errorChannel) {
    boolean success = true;
    SqlParser parser = new SqlParser();
    QualifiedName cubeName = null;
    try {
        CreateCube createCube = (CreateCube) parser.createStatement(query, new ParsingOptions(ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE));
        cubeName = createCube.getCubeName();
        QualifiedName sourceTableName = createCube.getSourceTableName();
        String whereClause = createCube.getWhere().get().toString();
        Set<FunctionCall> aggregations = createCube.getAggregations();
        List<Identifier> groupingSet = createCube.getGroupingSet();
        List<Property> properties = createCube.getProperties();
        boolean notExists = createCube.isNotExists();
        CreateCube modifiedCreateCube = new CreateCube(cubeName, sourceTableName, groupingSet, aggregations, notExists, properties, Optional.empty(), createCube.getSourceFilter().orElse(null));
        String queryCreateCube = SqlFormatter.formatSql(modifiedCreateCube, Optional.empty());
        if (!console.runQuery(queryRunner, queryCreateCube, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
            return false;
        }
        // we check whether the create cube expression can be processed
        if (isSupportedExpression(createCube, queryRunner, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
            if (createCube.getWhere().get() instanceof BetweenPredicate) {
                // we process the between predicate in the create cube query where clause
                success = processBetweenPredicate(createCube, queryRunner, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel, parser);
            }
            if (createCube.getWhere().get() instanceof ComparisonExpression) {
                // we process the comparison expression in the create cube query where clause
                success = processComparisonExpression(createCube, queryRunner, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel, parser);
            }
        } else {
            // if we donot support processing the create cube query with multiple inserts, then only a single insert is run internally.
            String queryInsert = String.format(INSERT_INTO_CUBE_STRING, cubeName, whereClause);
            success = console.runQuery(queryRunner, queryInsert, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
        }
        if (!success) {
            // roll back mechanism for unsuccessful create cube query
            String dropCubeQuery = String.format(DROP_CUBE_STRING, cubeName);
            console.runQuery(queryRunner, dropCubeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
        }
    } catch (ParsingException e) {
        if (cubeName != null) {
            // roll back mechanism for unsuccessful create cube query
            String dropCubeQuery = String.format(DROP_CUBE_STRING, cubeName);
            console.runQuery(queryRunner, dropCubeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
        }
        System.out.println(e.getMessage());
        Query.renderErrorLocation(query, new ErrorLocation(e.getLineNumber(), e.getColumnNumber()), errorChannel);
        success = false;
    } catch (Exception e) {
        if (cubeName != null) {
            // roll back mechanism for unsuccessful create cube query
            String dropCubeQuery = String.format(DROP_CUBE_STRING, cubeName);
            console.runQuery(queryRunner, dropCubeQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel);
        }
        // Add blank line after progress bar
        System.out.println();
        System.out.println(e.getMessage());
        success = false;
    }
    return success;
}
Also used : ErrorLocation(io.prestosql.client.ErrorLocation) BetweenPredicate(io.prestosql.sql.tree.BetweenPredicate) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) QualifiedName(io.prestosql.sql.tree.QualifiedName) SqlParser(io.prestosql.sql.parser.SqlParser) ParsingException(io.prestosql.sql.parser.ParsingException) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Identifier(io.prestosql.sql.tree.Identifier) CreateCube(io.prestosql.sql.tree.CreateCube) ParsingException(io.prestosql.sql.parser.ParsingException) FunctionCall(io.prestosql.sql.tree.FunctionCall) Property(io.prestosql.sql.tree.Property)

Example 12 with QualifiedName

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

the class TestDropCacheTask method testDropWithNonQualifiedName.

@Test
public void testDropWithNonQualifiedName() {
    QualifiedName tableName = QualifiedName.of(table3);
    QualifiedName fullName = QualifiedName.of(CATALOG_NAME, schema, table3);
    DropCache statement = new DropCache(tableName, true);
    Session session = testSessionBuilder().setTransactionId(transactionManager.beginTransaction(false)).setCatalog(CATALOG_NAME).setSchema(schema).build();
    QueryStateMachine queryStateMachine = createQueryStateMachine("START TRANSACTION", session, transactionManager);
    getFutureValue(new DropCacheTask().execute(statement, createTestTransactionManager(), metadata, new AllowAllAccessControl(), queryStateMachine, Collections.emptyList(), new HeuristicIndexerManager(new FileSystemClientManager(), new HetuMetaStoreManager())));
    assertFalse(SplitCacheMap.getInstance().cacheExists(fullName));
}
Also used : DropCache(io.prestosql.sql.tree.DropCache) AllowAllAccessControl(io.prestosql.security.AllowAllAccessControl) QualifiedName(io.prestosql.sql.tree.QualifiedName) HeuristicIndexerManager(io.prestosql.heuristicindex.HeuristicIndexerManager) HetuMetaStoreManager(io.prestosql.metastore.HetuMetaStoreManager) Session(io.prestosql.Session) FileSystemClientManager(io.prestosql.filesystem.FileSystemClientManager) Test(org.testng.annotations.Test)

Example 13 with QualifiedName

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

the class ImpalaAstBuilder method visitFunctionCall.

@Override
public Node visitFunctionCall(ImpalaSqlParser.FunctionCallContext context) {
    Optional<Expression> filter = visitIfPresent(context.filter(), Expression.class);
    Optional<Window> window = visitIfPresent(context.over(), Window.class);
    Optional<OrderBy> orderBy = Optional.empty();
    if (context.ORDER() != null) {
        orderBy = Optional.of(new OrderBy(visit(context.sortItem(), SortItem.class)));
    }
    QualifiedName name = getQualifiedName(context.qualifiedName());
    boolean distinct = isDistinct(context.setQuantifier());
    if (name.toString().equalsIgnoreCase("if")) {
        check(context.expression().size() == 2 || context.expression().size() == 3, "Invalid number of arguments for 'if' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'if' function", context);
        check(!distinct, "DISTINCT not valid for 'if' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'if' function", context);
        Expression elseExpression = null;
        if (context.expression().size() == 3) {
            elseExpression = (Expression) visit(context.expression(2));
        }
        return new IfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)), elseExpression);
    }
    if (name.toString().equalsIgnoreCase("nullif")) {
        check(context.expression().size() == 2, "Invalid number of arguments for 'nullif' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'nullif' function", context);
        check(!distinct, "DISTINCT not valid for 'nullif' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'nullif' function", context);
        return new NullIfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)));
    }
    if (name.toString().equalsIgnoreCase("coalesce")) {
        check(context.expression().size() >= 2, "The 'coalesce' function must have at least two arguments", context);
        check(!window.isPresent(), "OVER clause not valid for 'coalesce' function", context);
        check(!distinct, "DISTINCT not valid for 'coalesce' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'coalesce' function", context);
        return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
    }
    return new FunctionCall(Optional.of(getLocation(context)), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, false, visit(context.expression(), Expression.class));
}
Also used : Window(io.prestosql.sql.tree.Window) OrderBy(io.prestosql.sql.tree.OrderBy) IfExpression(io.prestosql.sql.tree.IfExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) QualifiedName(io.prestosql.sql.tree.QualifiedName) SortItem(io.prestosql.sql.tree.SortItem) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) FunctionCall(io.prestosql.sql.tree.FunctionCall) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Example 14 with QualifiedName

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

the class DropTableTask method execute.

@Override
public ListenableFuture<?> execute(DropTable statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters, HeuristicIndexerManager heuristicIndexerManager) {
    Session session = stateMachine.getSession();
    QualifiedObjectName fullObjectName = createQualifiedObjectName(session, statement, statement.getTableName());
    QualifiedName tableName = QualifiedName.of(fullObjectName.getCatalogName(), fullObjectName.getSchemaName(), fullObjectName.getObjectName());
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, fullObjectName);
    if (!tableHandle.isPresent()) {
        if (!statement.isExists()) {
            throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
        }
        return immediateFuture(null);
    }
    Optional<CubeMetaStore> optionalCubeMetaStore = this.cubeManager.getMetaStore(STAR_TREE);
    if (optionalCubeMetaStore.isPresent() && optionalCubeMetaStore.get().getMetadataFromCubeName(tableName.toString()).isPresent()) {
        throw new SemanticException(DROP_TABLE_ON_CUBE, statement, "%s is a star-tree cube, drop using DROP CUBE", tableName);
    }
    accessControl.checkCanDropTable(session.getRequiredTransactionId(), session.getIdentity(), fullObjectName);
    if (PropertyService.getBooleanProperty(HetuConstant.SPLIT_CACHE_MAP_ENABLED)) {
        // Check if SplitCacheMap is enabled
        SplitCacheMap splitCacheMap = SplitCacheMap.getInstance();
        if (splitCacheMap.cacheExists(tableName)) {
            splitCacheMap.dropCache(tableName, Optional.empty());
        }
    }
    metadata.dropTable(session, tableHandle.get());
    if (optionalCubeMetaStore.isPresent()) {
        List<CubeMetadata> cubes = optionalCubeMetaStore.get().getMetadataList(tableName.toString());
        for (CubeMetadata cube : cubes) {
            String[] parts = cube.getCubeName().split("\\.");
            Optional<TableHandle> cubeHandle = metadata.getTableHandle(session, createQualifiedObjectName(session, null, QualifiedName.of(parts[0], parts[1], parts[2])));
            try {
                cubeHandle.ifPresent(cubeTable -> metadata.dropTable(session, cubeTable));
                optionalCubeMetaStore.get().removeCube(cube);
            } catch (TableNotFoundException e) {
                // Can happen in concurrent drop table and drop cube calls
                LOG.debug("Tried dropping cube table but it is already dropped", e);
            }
        }
    }
    dropIndices(heuristicIndexerManager, tableName);
    return immediateFuture(null);
}
Also used : QualifiedName(io.prestosql.sql.tree.QualifiedName) CubeMetaStore(io.hetu.core.spi.cube.io.CubeMetaStore) CubeMetadata(io.hetu.core.spi.cube.CubeMetadata) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(io.prestosql.metadata.MetadataUtil.createQualifiedObjectName) TableNotFoundException(io.prestosql.spi.connector.TableNotFoundException) TableHandle(io.prestosql.spi.metadata.TableHandle) Session(io.prestosql.Session) SemanticException(io.prestosql.sql.analyzer.SemanticException)

Example 15 with QualifiedName

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

the class SetSessionTask method execute.

@Override
public ListenableFuture<?> execute(SetSession statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters, HeuristicIndexerManager heuristicIndexerManager) {
    Session session = stateMachine.getSession();
    QualifiedName propertyName = statement.getName();
    List<String> parts = propertyName.getParts();
    if (parts.size() > 2) {
        throw new SemanticException(INVALID_SESSION_PROPERTY, statement, "Invalid session property '%s'", propertyName);
    }
    // validate the property name
    PropertyMetadata<?> propertyMetadata;
    if (parts.size() == 1) {
        accessControl.checkCanSetSystemSessionProperty(session.getIdentity(), parts.get(0));
        propertyMetadata = metadata.getSessionPropertyManager().getSystemSessionPropertyMetadata(parts.get(0)).orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName()));
    } else {
        CatalogName catalogName = metadata.getCatalogHandle(stateMachine.getSession(), parts.get(0)).orElseThrow(() -> new SemanticException(MISSING_CATALOG, statement, "Catalog %s does not exist", parts.get(0)));
        accessControl.checkCanSetCatalogSessionProperty(session.getRequiredTransactionId(), session.getIdentity(), parts.get(0), parts.get(1));
        propertyMetadata = metadata.getSessionPropertyManager().getConnectorSessionPropertyMetadata(catalogName, parts.get(1)).orElseThrow(() -> new SemanticException(INVALID_SESSION_PROPERTY, statement, "Session property %s does not exist", statement.getName()));
    }
    Type type = propertyMetadata.getSqlType();
    Object objectValue;
    try {
        objectValue = evaluatePropertyValue(statement.getValue(), type, session, metadata, parameters);
    } catch (SemanticException e) {
        throw new PrestoException(StandardErrorCode.INVALID_SESSION_PROPERTY, String.format("Unable to set session property '%s' to '%s': %s", propertyName, statement.getValue(), e.getMessage()));
    }
    String value = serializeSessionProperty(type, objectValue);
    // verify the SQL value can be decoded by the property
    propertyMetadata.decode(objectValue);
    stateMachine.addSetSessionProperties(propertyName.toString(), value);
    return immediateFuture(null);
}
Also used : Type(io.prestosql.spi.type.Type) QualifiedName(io.prestosql.sql.tree.QualifiedName) CatalogName(io.prestosql.spi.connector.CatalogName) PrestoException(io.prestosql.spi.PrestoException) SetSession(io.prestosql.sql.tree.SetSession) Session(io.prestosql.Session) SemanticException(io.prestosql.sql.analyzer.SemanticException)

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