use of io.prestosql.sql.tree.InsertCube in project hetu-core by openlookeng.
the class AstBuilder method visitInsertCube.
@Override
public Node visitInsertCube(SqlBaseParser.InsertCubeContext context) {
QualifiedName cubeName = getQualifiedName(context.qualifiedName());
Optional<Expression> optionalExpression = visitIfPresent(context.expression(), Expression.class);
return new InsertCube(getLocation(context), cubeName, optionalExpression, false);
}
use of io.prestosql.sql.tree.InsertCube in project hetu-core by openlookeng.
the class AstBuilder method visitInsertOverwriteCube.
@Override
public Node visitInsertOverwriteCube(SqlBaseParser.InsertOverwriteCubeContext context) {
QualifiedName cubeName = getQualifiedName(context.qualifiedName());
Optional<Expression> optionalExpression = visitIfPresent(context.expression(), Expression.class);
return new InsertCube(getLocation(context), cubeName, optionalExpression, true);
}
use of io.prestosql.sql.tree.InsertCube in project hetu-core by openlookeng.
the class TestSqlParser method testInsertIntoCube.
@Test
public void testInsertIntoCube() {
assertStatement("INSERT INTO CUBE foo WHERE d1 > 10", new InsertCube(QualifiedName.of("foo"), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), false));
assertStatement("INSERT INTO CUBE c1.s1.foo WHERE d1 > 10", new InsertCube(QualifiedName.of("c1", "s1", "foo"), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("d1"), new LongLiteral("10"))), false));
}
use of io.prestosql.sql.tree.InsertCube in project hetu-core by openlookeng.
the class LogicalPlanner method createInsertCubePlan.
private RelationPlan createInsertCubePlan(Analysis analysis, InsertCube insertCubeStatement) {
Analysis.CubeInsert insert = analysis.getCubeInsert().get();
TableMetadata tableMetadata = metadata.getTableMetadata(session, insert.getTarget());
List<ColumnMetadata> visibleTableColumns = tableMetadata.getColumns().stream().filter(column -> !column.isHidden()).collect(toImmutableList());
List<String> visibleTableColumnNames = visibleTableColumns.stream().map(ColumnMetadata::getName).collect(toImmutableList());
RelationPlan plan = createRelationPlan(analysis, insertCubeStatement.getQuery());
Map<String, ColumnHandle> columns = metadata.getColumnHandles(session, insert.getTarget());
Assignments.Builder assignments = Assignments.builder();
for (ColumnMetadata column : tableMetadata.getColumns()) {
if (column.isHidden()) {
continue;
}
Symbol output = planSymbolAllocator.newSymbol(column.getName(), column.getType());
int index = insert.getColumns().indexOf(columns.get(column.getName()));
if (index < 0) {
Expression cast = new Cast(new NullLiteral(), column.getType().getTypeSignature().toString());
assignments.put(output, castToRowExpression(cast));
} else {
Symbol input = plan.getSymbol(index);
Type tableType = column.getType();
Type queryType = planSymbolAllocator.getTypes().get(input);
if (queryType.equals(tableType) || typeCoercion.isTypeOnlyCoercion(queryType, tableType)) {
assignments.put(output, castToRowExpression(toSymbolReference(input)));
} else {
Expression cast = noTruncationCast(toSymbolReference(input), queryType, tableType);
assignments.put(output, castToRowExpression(cast));
}
}
}
ProjectNode projectNode = new ProjectNode(idAllocator.getNextId(), plan.getRoot(), assignments.build());
List<Field> fields = visibleTableColumns.stream().map(column -> Field.newUnqualified(column.getName(), column.getType())).collect(toImmutableList());
Scope scope = Scope.builder().withRelationType(RelationId.anonymous(), new RelationType(fields)).build();
plan = new RelationPlan(projectNode, scope, projectNode.getOutputSymbols());
Optional<NewTableLayout> newTableLayout = metadata.getInsertLayout(session, insert.getTarget());
String catalogName = insert.getTarget().getCatalogName().getCatalogName();
TableStatisticsMetadata statisticsMetadata = metadata.getStatisticsCollectionMetadataForWrite(session, catalogName, tableMetadata.getMetadata());
RelationPlan tableWriterPlan = createTableWriterPlan(analysis, plan, new InsertReference(insert.getTarget(), analysis.isCubeOverwrite()), visibleTableColumnNames, newTableLayout, statisticsMetadata);
Expression rewritten = null;
Set<Identifier> predicateColumns = new HashSet<>();
if (insertCubeStatement.getWhere().isPresent()) {
rewritten = new QueryPlanner(analysis, planSymbolAllocator, idAllocator, buildLambdaDeclarationToSymbolMap(analysis, planSymbolAllocator), metadata, session, namedSubPlan, uniqueIdAllocator).rewriteExpression(tableWriterPlan, insertCubeStatement.getWhere().get(), analysis, buildLambdaDeclarationToSymbolMap(analysis, planSymbolAllocator));
predicateColumns.addAll(ExpressionUtils.getIdentifiers(rewritten));
}
CubeMetadata cubeMetadata = insert.getMetadata();
if (!insertCubeStatement.isOverwrite() && !insertCubeStatement.getWhere().isPresent() && cubeMetadata.getCubeStatus() != CubeStatus.INACTIVE) {
// Means data some data was inserted before, but trying to insert entire dataset
throw new PrestoException(QUERY_REJECTED, "Cannot allow insert. Inserting entire dataset but cube already has partial data");
} else if (insertCubeStatement.getWhere().isPresent()) {
if (!canSupportPredicate(rewritten)) {
throw new PrestoException(QUERY_REJECTED, String.format("Cannot support predicate '%s'", ExpressionFormatter.formatExpression(rewritten, Optional.empty())));
}
if (!insertCubeStatement.isOverwrite() && arePredicatesOverlapping(rewritten, cubeMetadata)) {
throw new PrestoException(QUERY_REJECTED, String.format("Cannot allow insert. Cube already contains data for the given predicate '%s'", ExpressionFormatter.formatExpression(insertCubeStatement.getWhere().get(), Optional.empty())));
}
}
TableHandle sourceTableHandle = insert.getSourceTable();
// At this point it has been verified that source table has not been updated
// so insert into cube should be allowed
LongSupplier tableLastModifiedTimeSupplier = metadata.getTableLastModifiedTimeSupplier(session, sourceTableHandle);
checkState(tableLastModifiedTimeSupplier != null, "Table last modified time is null");
Map<Symbol, Type> predicateColumnsType = predicateColumns.stream().map(identifier -> new Symbol(identifier.getValue())).collect(Collectors.toMap(Function.identity(), symbol -> planSymbolAllocator.getTypes().get(symbol), (key1, ignored) -> key1));
CubeFinishNode cubeFinishNode = new CubeFinishNode(idAllocator.getNextId(), tableWriterPlan.getRoot(), planSymbolAllocator.newSymbol("rows", BIGINT), new CubeUpdateMetadata(tableMetadata.getQualifiedName().toString(), tableLastModifiedTimeSupplier.getAsLong(), rewritten != null ? ExpressionFormatter.formatExpression(rewritten, Optional.empty()) : null, insertCubeStatement.isOverwrite()), predicateColumnsType);
return new RelationPlan(cubeFinishNode, analysis.getScope(insertCubeStatement), cubeFinishNode.getOutputSymbols());
}
use of io.prestosql.sql.tree.InsertCube in project hetu-core by openlookeng.
the class SqlQueryExecution method checkSnapshotSupport.
// Check if snapshot feature conflict with other aspects of the query.
// If any requirement is not met, then proceed as if snapshot was not enabled,
// i.e. session.isSnapshotEnabled() and SystemSessionProperties.isSnapshotEnabled(session) return false
private void checkSnapshotSupport(Session session) {
List<String> reasons = new ArrayList<>();
// Only support create-table-as-select and insert statements
Statement statement = analysis.getStatement();
if (statement instanceof CreateTableAsSelect) {
if (analysis.isCreateTableAsSelectNoOp()) {
// Table already exists. Ask catalog if target table supports snapshot
if (!metadata.isSnapshotSupportedAsOutput(session, analysis.getCreateTableAsSelectNoOpTarget())) {
reasons.add("Only support inserting into tables in Hive with ORC format");
}
} else {
// Ask catalog if new table supports snapshot
Map<String, Object> tableProperties = analysis.getCreateTableMetadata().getProperties();
if (!metadata.isSnapshotSupportedAsNewTable(session, analysis.getTarget().get().getCatalogName(), tableProperties)) {
reasons.add("Only support creating tables in Hive with ORC format");
}
}
} else if (statement instanceof Insert) {
// Ask catalog if target table supports snapshot
if (!metadata.isSnapshotSupportedAsOutput(session, analysis.getInsert().get().getTarget())) {
reasons.add("Only support inserting into tables in Hive with ORC format");
}
} else if (statement instanceof InsertCube) {
reasons.add("INSERT INTO CUBE is not supported, only support CTAS (create table as select) and INSERT INTO (tables) statements");
} else {
reasons.add("Only support CTAS (create table as select) and INSERT INTO (tables) statements");
}
// Doesn't work with the following features
if (SystemSessionProperties.isReuseTableScanEnabled(session) || SystemSessionProperties.isCTEReuseEnabled(session)) {
reasons.add("No support along with reuse_table_scan or cte_reuse_enabled features");
}
// All input tables must support snapshotting
for (TableHandle tableHandle : analysis.getTables()) {
if (!metadata.isSnapshotSupportedAsInput(session, tableHandle)) {
reasons.add("Only support reading from Hive, TPCDS, and TPCH source tables");
break;
}
}
// Must have more than 1 worker
if (nodeScheduler.createNodeSelector(null, false, null).selectableNodeCount() == 1) {
reasons.add("Requires more than 1 worker nodes");
}
if (!snapshotManager.getSnapshotUtils().hasStoreClient()) {
String snapshotProfile = snapshotManager.getSnapshotUtils().getSnapshotProfile();
if (snapshotProfile == null) {
reasons.add("Property hetu.experimental.snapshot.profile is not specified");
} else {
reasons.add("Specified value '" + snapshotProfile + "' for property hetu.experimental.snapshot.profile is not valid");
}
}
if (!reasons.isEmpty()) {
// Disable snapshot support in the session. If this value has been used before this point,
// then we may need to remedy those places to disable snapshot as well. Fortunately,
// most accesses occur before this point, except for classes like ExecutingStatementResource,
// where the "snapshot enabled" info is retrieved and set in ExchangeClient. This is harmless.
// The ExchangeClient may still have snapshotEnabled=true while it's disabled in the session.
// This does not alter ExchangeClient's behavior, because this instance (in coordinator)
// will never receive any marker.
session.disableSnapshot();
String reasonsMessage = "Snapshot feature is disabled: \n" + String.join(". \n", reasons);
warningCollector.add(new PrestoWarning(StandardWarningCode.SNAPSHOT_NOT_SUPPORTED, reasonsMessage));
}
}
Aggregations