Search in sources :

Example 16 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class CreateViewTask method execute.

@Override
public ListenableFuture<?> execute(CreateView statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters, HeuristicIndexerManager heuristicIndexerManager) {
    Session session = stateMachine.getSession();
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    accessControl.checkCanCreateView(session.getRequiredTransactionId(), session.getIdentity(), name);
    String sql = getFormattedSql(statement.getQuery(), sqlParser, Optional.of(parameters));
    Analysis analysis = analyzeStatement(statement, session, metadata, accessControl, parameters, stateMachine.getWarningCollector());
    List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ViewColumn(field.getName().get(), field.getType().getTypeSignature())).collect(toImmutableList());
    // use DEFINER security by default
    Optional<String> owner = Optional.of(session.getUser());
    if (statement.getSecurity().orElse(null) == INVOKER) {
        owner = Optional.empty();
    }
    ConnectorViewDefinition definition = new ConnectorViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, owner, !owner.isPresent());
    metadata.createView(session, name, definition, statement.isReplace());
    return immediateFuture(null);
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ConnectorViewDefinition(io.prestosql.spi.connector.ConnectorViewDefinition) TransactionManager(io.prestosql.transaction.TransactionManager) SqlParser(io.prestosql.sql.parser.SqlParser) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) Inject(javax.inject.Inject) INVOKER(io.prestosql.sql.tree.CreateView.Security.INVOKER) Statement(io.prestosql.sql.tree.Statement) WarningCollector(io.prestosql.execution.warnings.WarningCollector) ViewColumn(io.prestosql.spi.connector.ConnectorViewDefinition.ViewColumn) Objects.requireNonNull(java.util.Objects.requireNonNull) Session(io.prestosql.Session) HeuristicIndexerManager(io.prestosql.heuristicindex.HeuristicIndexerManager) Futures.immediateFuture(com.google.common.util.concurrent.Futures.immediateFuture) AccessControl(io.prestosql.security.AccessControl) SqlFormatterUtil.getFormattedSql(io.prestosql.sql.SqlFormatterUtil.getFormattedSql) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Metadata(io.prestosql.metadata.Metadata) Analyzer(io.prestosql.sql.analyzer.Analyzer) CubeManager(io.prestosql.cube.CubeManager) List(java.util.List) CreateView(io.prestosql.sql.tree.CreateView) FeaturesConfig(io.prestosql.sql.analyzer.FeaturesConfig) Optional(java.util.Optional) Analysis(io.prestosql.sql.analyzer.Analysis) MetadataUtil.createQualifiedObjectName(io.prestosql.metadata.MetadataUtil.createQualifiedObjectName) Expression(io.prestosql.sql.tree.Expression) Analysis(io.prestosql.sql.analyzer.Analysis) ViewColumn(io.prestosql.spi.connector.ConnectorViewDefinition.ViewColumn) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(io.prestosql.metadata.MetadataUtil.createQualifiedObjectName) Session(io.prestosql.Session) ConnectorViewDefinition(io.prestosql.spi.connector.ConnectorViewDefinition)

Example 17 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class ReloadCubeConsole method reload.

public boolean reload(String query, QueryRunner queryRunner, ClientOptions.OutputFormat outputFormat, Runnable schemaChanged, boolean usePager, boolean showProgress, Terminal terminal, PrintStream out, PrintStream errorChannel) throws UnsupportedEncodingException {
    SqlParser parser = new SqlParser();
    ReloadCube reloadCube = (ReloadCube) parser.createStatement(query, new ParsingOptions(ParsingOptions.DecimalLiteralTreatment.AS_DOUBLE));
    if (!checkCubeName(queryRunner, reloadCube, reloadCube.getCubeName())) {
        return false;
    }
    String cubeTableName = this.catalogName + "." + this.schemaName + "." + this.objectName;
    final Charset charset = StandardCharsets.UTF_8;
    ByteArrayOutputStream stringOutputStream = new ByteArrayOutputStream();
    String showCreateCubeQuery = "SHOW CREATE CUBE " + cubeTableName.toString();
    if (!console.runQuery(queryRunner, showCreateCubeQuery, ClientOptions.OutputFormat.CSV, schemaChanged, false, showProgress, terminal, new PrintStream(stringOutputStream, true, charset.name()), errorChannel)) {
        return false;
    }
    this.newQuery = stringOutputStream.toString().replace("\"\"", "\"").trim();
    this.newQuery = this.newQuery.substring(1, this.newQuery.length() - 1);
    String dropQuery = "DROP CUBE " + cubeTableName.toString();
    if (!console.runQuery(queryRunner, dropQuery, outputFormat, schemaChanged, usePager, showProgress, terminal, out, errorChannel)) {
        return false;
    }
    return true;
}
Also used : PrintStream(java.io.PrintStream) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) SqlParser(io.prestosql.sql.parser.SqlParser) Charset(java.nio.charset.Charset) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReloadCube(io.prestosql.sql.tree.ReloadCube)

Example 18 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class LogicalPlanner method arePredicatesOverlapping.

private boolean arePredicatesOverlapping(Expression inputNewDataPredicate, CubeMetadata cubeMetadata) {
    // Cannot do this check inside StatementAnalyzer because predicate expressions have not been rewritten by then.
    Expression newDataPredicate = inputNewDataPredicate;
    TypeProvider types = planSymbolAllocator.getTypes();
    newDataPredicate = ExpressionUtils.rewriteIdentifiersToSymbolReferences(newDataPredicate);
    ExpressionDomainTranslator.ExtractionResult decomposedNewDataPredicate = ExpressionDomainTranslator.fromPredicate(metadata, session, newDataPredicate, types);
    if (cubeMetadata.getCubeStatus() == CubeStatus.INACTIVE) {
        // Inactive cubes are empty. So inserts should be allowed.
        return false;
    }
    CubeFilter cubeFilter = cubeMetadata.getCubeFilter();
    if (cubeFilter == null || cubeFilter.getCubePredicate() == null) {
        // Means Cube was created for entire dataset.
        return true;
    }
    SqlParser sqlParser = new SqlParser();
    Expression cubePredicateAsExpr = sqlParser.createExpression(cubeFilter.getCubePredicate(), createParsingOptions(session));
    cubePredicateAsExpr = ExpressionUtils.rewriteIdentifiersToSymbolReferences(cubePredicateAsExpr);
    ExpressionDomainTranslator.ExtractionResult decomposedCubePredicate = ExpressionDomainTranslator.fromPredicate(metadata, session, cubePredicateAsExpr, types);
    return decomposedCubePredicate.getTupleDomain().overlaps(decomposedNewDataPredicate.getTupleDomain());
}
Also used : ConstantExpression(io.prestosql.spi.relation.ConstantExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) IfExpression(io.prestosql.sql.tree.IfExpression) CubeFilter(io.hetu.core.spi.cube.CubeFilter) SqlParser(io.prestosql.sql.parser.SqlParser)

Example 19 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class TestWindowNode method getJsonCodec.

private JsonCodec<WindowNode> getJsonCodec() throws Exception {
    Module module = binder -> {
        SqlParser sqlParser = new SqlParser();
        TypeManager typeManager = new TestingTypeManager();
        binder.install(new JsonModule());
        binder.install(new HandleJsonModule());
        binder.bind(SqlParser.class).toInstance(sqlParser);
        binder.bind(TypeManager.class).toInstance(typeManager);
        configBinder(binder).bindConfig(FeaturesConfig.class);
        newSetBinder(binder, Type.class);
        jsonBinder(binder).addSerializerBinding(Slice.class).to(SliceSerializer.class);
        jsonBinder(binder).addDeserializerBinding(Slice.class).to(SliceDeserializer.class);
        jsonBinder(binder).addDeserializerBinding(Type.class).to(TestingTypeDeserializer.class);
        jsonBinder(binder).addSerializerBinding(Expression.class).to(Serialization.ExpressionSerializer.class);
        jsonBinder(binder).addDeserializerBinding(Expression.class).to(Serialization.ExpressionDeserializer.class);
        jsonBinder(binder).addDeserializerBinding(FunctionCall.class).to(Serialization.FunctionCallDeserializer.class);
        jsonBinder(binder).addKeySerializerBinding(VariableReferenceExpression.class).to(Serialization.VariableReferenceExpressionSerializer.class);
        jsonBinder(binder).addKeyDeserializerBinding(VariableReferenceExpression.class).to(Serialization.VariableReferenceExpressionDeserializer.class);
        jsonCodecBinder(binder).bindJsonCodec(WindowNode.class);
    };
    Bootstrap app = new Bootstrap(ImmutableList.of(module));
    Injector injector = app.strictConfig().doNotInitializeLogging().quiet().initialize();
    return injector.getInstance(new Key<JsonCodec<WindowNode>>() {
    });
}
Also used : Module(com.google.inject.Module) SliceDeserializer(io.prestosql.server.SliceDeserializer) Slice(io.airlift.slice.Slice) Key(com.google.inject.Key) Serialization(io.prestosql.sql.Serialization) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) SqlParser(io.prestosql.sql.parser.SqlParser) JsonBinder.jsonBinder(io.airlift.json.JsonBinder.jsonBinder) SortOrder(io.prestosql.spi.block.SortOrder) Types(io.prestosql.spi.sql.expression.Types) CallExpression(io.prestosql.spi.relation.CallExpression) ImmutableList(com.google.common.collect.ImmutableList) FunctionCall(io.prestosql.sql.tree.FunctionCall) Expressions.call(io.prestosql.sql.relational.Expressions.call) OrderingScheme(io.prestosql.spi.plan.OrderingScheme) Map(java.util.Map) Multibinder.newSetBinder(com.google.inject.multibindings.Multibinder.newSetBinder) SliceSerializer(io.prestosql.server.SliceSerializer) Type(io.prestosql.spi.type.Type) BIGINT(io.prestosql.spi.type.BigintType.BIGINT) Symbol(io.prestosql.spi.plan.Symbol) PlanNodeId(io.prestosql.spi.plan.PlanNodeId) JsonCodecBinder.jsonCodecBinder(io.airlift.json.JsonCodecBinder.jsonCodecBinder) ImmutableSet(com.google.common.collect.ImmutableSet) TypeSignatureProvider.fromTypes(io.prestosql.sql.analyzer.TypeSignatureProvider.fromTypes) TestingTypeManager(io.prestosql.spi.type.TestingTypeManager) ImmutableMap(com.google.common.collect.ImmutableMap) MetadataManager.createTestMetadataManager(io.prestosql.metadata.MetadataManager.createTestMetadataManager) BeforeClass(org.testng.annotations.BeforeClass) TypeManager(io.prestosql.spi.type.TypeManager) Set(java.util.Set) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) UUID(java.util.UUID) TestingTypeDeserializer(io.prestosql.spi.type.TestingTypeDeserializer) Injector(com.google.inject.Injector) FunctionHandle(io.prestosql.spi.function.FunctionHandle) PlanSymbolAllocator(io.prestosql.sql.planner.PlanSymbolAllocator) ValuesNode(io.prestosql.spi.plan.ValuesNode) WindowNode(io.prestosql.spi.plan.WindowNode) Bootstrap(io.airlift.bootstrap.Bootstrap) FeaturesConfig(io.prestosql.sql.analyzer.FeaturesConfig) Optional(java.util.Optional) ConfigBinder.configBinder(io.airlift.configuration.ConfigBinder.configBinder) Expression(io.prestosql.sql.tree.Expression) JsonCodec(io.airlift.json.JsonCodec) JsonModule(io.airlift.json.JsonModule) HandleJsonModule(io.prestosql.metadata.HandleJsonModule) WindowNode(io.prestosql.spi.plan.WindowNode) FeaturesConfig(io.prestosql.sql.analyzer.FeaturesConfig) SqlParser(io.prestosql.sql.parser.SqlParser) SliceSerializer(io.prestosql.server.SliceSerializer) JsonModule(io.airlift.json.JsonModule) HandleJsonModule(io.prestosql.metadata.HandleJsonModule) Type(io.prestosql.spi.type.Type) SliceDeserializer(io.prestosql.server.SliceDeserializer) TestingTypeDeserializer(io.prestosql.spi.type.TestingTypeDeserializer) JsonCodec(io.airlift.json.JsonCodec) HandleJsonModule(io.prestosql.metadata.HandleJsonModule) Injector(com.google.inject.Injector) TestingTypeManager(io.prestosql.spi.type.TestingTypeManager) TypeManager(io.prestosql.spi.type.TypeManager) Bootstrap(io.airlift.bootstrap.Bootstrap) Module(com.google.inject.Module) JsonModule(io.airlift.json.JsonModule) HandleJsonModule(io.prestosql.metadata.HandleJsonModule) TestingTypeManager(io.prestosql.spi.type.TestingTypeManager)

Example 20 with SqlParser

use of io.prestosql.sql.parser.SqlParser in project hetu-core by openlookeng.

the class TestHiveSqlMigrate method setup.

@BeforeClass
public void setup() throws Exception {
    sqlParser = new SqlParser();
    parsingOptions = new io.prestosql.sql.parser.ParsingOptions(DecimalLiteralTreatment.AS_DECIMAL);
    SessionProperties sessionProperties = new SessionProperties();
    sessionProperties.setSourceType(SqlSyntaxType.HIVE);
    sessionProperties.setParsingOptions(false);
    sqlConverter = SqlConverterFactory.getSqlConverter(sessionProperties);
}
Also used : SqlParser(io.prestosql.sql.parser.SqlParser) ParsingOptions(io.prestosql.sql.parser.ParsingOptions) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

SqlParser (io.prestosql.sql.parser.SqlParser)29 Expression (io.prestosql.sql.tree.Expression)11 ParsingOptions (io.prestosql.sql.parser.ParsingOptions)10 TypeAnalyzer (io.prestosql.sql.planner.TypeAnalyzer)6 Optional (java.util.Optional)6 BeforeClass (org.testng.annotations.BeforeClass)6 CubeFilter (io.hetu.core.spi.cube.CubeFilter)5 Metadata (io.prestosql.metadata.Metadata)5 Symbol (io.prestosql.spi.plan.Symbol)5 FeaturesConfig (io.prestosql.sql.analyzer.FeaturesConfig)5 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)5 FunctionCall (io.prestosql.sql.tree.FunctionCall)5 List (java.util.List)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Session (io.prestosql.Session)4 CubeManager (io.prestosql.cube.CubeManager)4 CallExpression (io.prestosql.spi.relation.CallExpression)4 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)4 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)4