Search in sources :

Example 1 with SinkModifyOperation

use of org.apache.flink.table.operations.SinkModifyOperation in project flink by apache.

the class HiveParserDMLHelper method createInsertOperation.

public Operation createInsertOperation(HiveParserCalcitePlanner analyzer, RelNode queryRelNode) throws SemanticException {
    HiveParserQB topQB = analyzer.getQB();
    QBMetaData qbMetaData = topQB.getMetaData();
    // decide the dest table
    Map<String, Table> nameToDestTable = qbMetaData.getNameToDestTable();
    Map<String, Partition> nameToDestPart = qbMetaData.getNameToDestPartition();
    // for now we only support inserting to a single table
    Preconditions.checkState(nameToDestTable.size() <= 1 && nameToDestPart.size() <= 1, "Only support inserting to 1 table");
    Table destTable;
    String insClauseName;
    if (!nameToDestTable.isEmpty()) {
        insClauseName = nameToDestTable.keySet().iterator().next();
        destTable = nameToDestTable.values().iterator().next();
    } else if (!nameToDestPart.isEmpty()) {
        insClauseName = nameToDestPart.keySet().iterator().next();
        destTable = nameToDestPart.values().iterator().next().getTable();
    } else {
        // happens for INSERT DIRECTORY
        throw new SemanticException("INSERT DIRECTORY is not supported");
    }
    // decide static partition specs
    Map<String, String> staticPartSpec = new LinkedHashMap<>();
    if (destTable.isPartitioned()) {
        List<String> partCols = HiveCatalog.getFieldNames(destTable.getTTable().getPartitionKeys());
        if (!nameToDestPart.isEmpty()) {
            // static partition
            Partition destPart = nameToDestPart.values().iterator().next();
            Preconditions.checkState(partCols.size() == destPart.getValues().size(), "Part cols and static spec doesn't match");
            for (int i = 0; i < partCols.size(); i++) {
                staticPartSpec.put(partCols.get(i), destPart.getValues().get(i));
            }
        } else {
            // dynamic partition
            Map<String, String> spec = qbMetaData.getPartSpecForAlias(insClauseName);
            if (spec != null) {
                for (String partCol : partCols) {
                    String val = spec.get(partCol);
                    if (val != null) {
                        staticPartSpec.put(partCol, val);
                    }
                }
            }
        }
    }
    // decide whether it's overwrite
    boolean overwrite = topQB.getParseInfo().getInsertOverwriteTables().keySet().stream().map(String::toLowerCase).collect(Collectors.toSet()).contains(destTable.getDbName() + "." + destTable.getTableName());
    Tuple4<ObjectIdentifier, QueryOperation, Map<String, String>, Boolean> insertOperationInfo = createInsertOperationInfo(queryRelNode, destTable, staticPartSpec, analyzer.getDestSchemaForClause(insClauseName), overwrite);
    return new SinkModifyOperation(catalogManager.getTableOrError(insertOperationInfo.f0), insertOperationInfo.f1, insertOperationInfo.f2, insertOperationInfo.f3, Collections.emptyMap());
}
Also used : Partition(org.apache.hadoop.hive.ql.metadata.Partition) Table(org.apache.hadoop.hive.ql.metadata.Table) HiveParserQB(org.apache.flink.table.planner.delegation.hive.copy.HiveParserQB) SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) LinkedHashMap(java.util.LinkedHashMap) QBMetaData(org.apache.hadoop.hive.ql.parse.QBMetaData) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) PlannerQueryOperation(org.apache.flink.table.planner.operations.PlannerQueryOperation) QueryOperation(org.apache.flink.table.operations.QueryOperation)

Example 2 with SinkModifyOperation

use of org.apache.flink.table.operations.SinkModifyOperation in project flink by apache.

the class TableEnvironmentImpl method extractSinkIdentifierNames.

/**
 * extract sink identifier names from {@link ModifyOperation}s and deduplicate them with {@link
 * #deduplicateSinkIdentifierNames(List)}.
 */
private List<String> extractSinkIdentifierNames(List<ModifyOperation> operations) {
    List<String> tableNames = new ArrayList<>(operations.size());
    for (ModifyOperation operation : operations) {
        if (operation instanceof SinkModifyOperation) {
            String fullName = ((SinkModifyOperation) operation).getContextResolvedTable().getIdentifier().asSummaryString();
            tableNames.add(fullName);
        } else {
            throw new UnsupportedOperationException("Unsupported operation: " + operation);
        }
    }
    return deduplicateSinkIdentifierNames(tableNames);
}
Also used : SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) ArrayList(java.util.ArrayList) ModifyOperation(org.apache.flink.table.operations.ModifyOperation) SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) CollectModifyOperation(org.apache.flink.table.operations.CollectModifyOperation)

Example 3 with SinkModifyOperation

use of org.apache.flink.table.operations.SinkModifyOperation in project flink by apache.

the class SqlToOperationConverterTest method checkAlterTableCompact.

private void checkAlterTableCompact(Operation operation, Map<String, String> staticPartitions) {
    assertThat(operation).isInstanceOf(SinkModifyOperation.class);
    SinkModifyOperation modifyOperation = (SinkModifyOperation) operation;
    assertThat(modifyOperation.getStaticPartitions()).containsExactlyInAnyOrderEntriesOf(staticPartitions);
    assertThat(modifyOperation.isOverwrite()).isFalse();
    assertThat(modifyOperation.getDynamicOptions()).containsEntry(TestManagedTableFactory.ENRICHED_KEY, TestManagedTableFactory.ENRICHED_VALUE);
    ContextResolvedTable contextResolvedTable = modifyOperation.getContextResolvedTable();
    assertThat(contextResolvedTable.getIdentifier()).isEqualTo(ObjectIdentifier.of("cat1", "db1", "tb1"));
    assertThat(modifyOperation.getChild()).isInstanceOf(SourceQueryOperation.class);
    SourceQueryOperation child = (SourceQueryOperation) modifyOperation.getChild();
    assertThat(child.getChildren()).isEmpty();
    assertThat(child.getDynamicOptions()).containsEntry("k", "v");
    assertThat(child.getDynamicOptions()).containsEntry(TestManagedTableFactory.ENRICHED_KEY, TestManagedTableFactory.ENRICHED_VALUE);
}
Also used : SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation) ContextResolvedTable(org.apache.flink.table.catalog.ContextResolvedTable)

Example 4 with SinkModifyOperation

use of org.apache.flink.table.operations.SinkModifyOperation in project flink by apache.

the class SqlToOperationConverterTest method testSqlInsertWithDynamicTableOptions.

@Test
public void testSqlInsertWithDynamicTableOptions() {
    final String sql = "insert into t1 /*+ OPTIONS('k1'='v1', 'k2'='v2') */\n" + "select a, b, c, d from t2";
    FlinkPlannerImpl planner = getPlannerBySqlDialect(SqlDialect.DEFAULT);
    final CalciteParser parser = getParserBySqlDialect(SqlDialect.DEFAULT);
    Operation operation = parse(sql, planner, parser);
    assertThat(operation).isInstanceOf(SinkModifyOperation.class);
    SinkModifyOperation sinkModifyOperation = (SinkModifyOperation) operation;
    Map<String, String> dynamicOptions = sinkModifyOperation.getDynamicOptions();
    assertThat(dynamicOptions).isNotNull();
    assertThat(dynamicOptions.size()).isEqualTo(2);
    assertThat(dynamicOptions.toString()).isEqualTo("{k1=v1, k2=v2}");
}
Also used : SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) FlinkPlannerImpl(org.apache.flink.table.planner.calcite.FlinkPlannerImpl) OperationMatchers.isCreateTableOperation(org.apache.flink.table.planner.utils.OperationMatchers.isCreateTableOperation) DropDatabaseOperation(org.apache.flink.table.operations.ddl.DropDatabaseOperation) SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) AlterTableOptionsOperation(org.apache.flink.table.operations.ddl.AlterTableOptionsOperation) AlterTableDropConstraintOperation(org.apache.flink.table.operations.ddl.AlterTableDropConstraintOperation) UseCatalogOperation(org.apache.flink.table.operations.UseCatalogOperation) UseDatabaseOperation(org.apache.flink.table.operations.UseDatabaseOperation) CreateViewOperation(org.apache.flink.table.operations.ddl.CreateViewOperation) ShowJarsOperation(org.apache.flink.table.operations.command.ShowJarsOperation) AlterDatabaseOperation(org.apache.flink.table.operations.ddl.AlterDatabaseOperation) QueryOperation(org.apache.flink.table.operations.QueryOperation) EndStatementSetOperation(org.apache.flink.table.operations.EndStatementSetOperation) UseModulesOperation(org.apache.flink.table.operations.UseModulesOperation) ShowFunctionsOperation(org.apache.flink.table.operations.ShowFunctionsOperation) CreateDatabaseOperation(org.apache.flink.table.operations.ddl.CreateDatabaseOperation) SetOperation(org.apache.flink.table.operations.command.SetOperation) LoadModuleOperation(org.apache.flink.table.operations.LoadModuleOperation) Operation(org.apache.flink.table.operations.Operation) ShowModulesOperation(org.apache.flink.table.operations.ShowModulesOperation) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation) UnloadModuleOperation(org.apache.flink.table.operations.UnloadModuleOperation) CreateTableOperation(org.apache.flink.table.operations.ddl.CreateTableOperation) RemoveJarOperation(org.apache.flink.table.operations.command.RemoveJarOperation) BeginStatementSetOperation(org.apache.flink.table.operations.BeginStatementSetOperation) AddJarOperation(org.apache.flink.table.operations.command.AddJarOperation) AlterTableAddConstraintOperation(org.apache.flink.table.operations.ddl.AlterTableAddConstraintOperation) ExplainOperation(org.apache.flink.table.operations.ExplainOperation) ResetOperation(org.apache.flink.table.operations.command.ResetOperation) StatementSetOperation(org.apache.flink.table.operations.StatementSetOperation) AlterTableRenameOperation(org.apache.flink.table.operations.ddl.AlterTableRenameOperation) CalciteParser(org.apache.flink.table.planner.parse.CalciteParser) Test(org.junit.Test)

Example 5 with SinkModifyOperation

use of org.apache.flink.table.operations.SinkModifyOperation in project flink by apache.

the class SqlToOperationConverterTest method testSqlInsertWithStaticPartition.

@Test
public void testSqlInsertWithStaticPartition() {
    final String sql = "insert into t1 partition(a=1) select b, c, d from t2";
    FlinkPlannerImpl planner = getPlannerBySqlDialect(SqlDialect.DEFAULT);
    final CalciteParser parser = getParserBySqlDialect(SqlDialect.DEFAULT);
    Operation operation = parse(sql, planner, parser);
    assertThat(operation).isInstanceOf(SinkModifyOperation.class);
    SinkModifyOperation sinkModifyOperation = (SinkModifyOperation) operation;
    final Map<String, String> expectedStaticPartitions = new HashMap<>();
    expectedStaticPartitions.put("a", "1");
    assertThat(sinkModifyOperation.getStaticPartitions()).isEqualTo(expectedStaticPartitions);
}
Also used : SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) HashMap(java.util.HashMap) FlinkPlannerImpl(org.apache.flink.table.planner.calcite.FlinkPlannerImpl) OperationMatchers.isCreateTableOperation(org.apache.flink.table.planner.utils.OperationMatchers.isCreateTableOperation) DropDatabaseOperation(org.apache.flink.table.operations.ddl.DropDatabaseOperation) SinkModifyOperation(org.apache.flink.table.operations.SinkModifyOperation) AlterTableOptionsOperation(org.apache.flink.table.operations.ddl.AlterTableOptionsOperation) AlterTableDropConstraintOperation(org.apache.flink.table.operations.ddl.AlterTableDropConstraintOperation) UseCatalogOperation(org.apache.flink.table.operations.UseCatalogOperation) UseDatabaseOperation(org.apache.flink.table.operations.UseDatabaseOperation) CreateViewOperation(org.apache.flink.table.operations.ddl.CreateViewOperation) ShowJarsOperation(org.apache.flink.table.operations.command.ShowJarsOperation) AlterDatabaseOperation(org.apache.flink.table.operations.ddl.AlterDatabaseOperation) QueryOperation(org.apache.flink.table.operations.QueryOperation) EndStatementSetOperation(org.apache.flink.table.operations.EndStatementSetOperation) UseModulesOperation(org.apache.flink.table.operations.UseModulesOperation) ShowFunctionsOperation(org.apache.flink.table.operations.ShowFunctionsOperation) CreateDatabaseOperation(org.apache.flink.table.operations.ddl.CreateDatabaseOperation) SetOperation(org.apache.flink.table.operations.command.SetOperation) LoadModuleOperation(org.apache.flink.table.operations.LoadModuleOperation) Operation(org.apache.flink.table.operations.Operation) ShowModulesOperation(org.apache.flink.table.operations.ShowModulesOperation) SourceQueryOperation(org.apache.flink.table.operations.SourceQueryOperation) UnloadModuleOperation(org.apache.flink.table.operations.UnloadModuleOperation) CreateTableOperation(org.apache.flink.table.operations.ddl.CreateTableOperation) RemoveJarOperation(org.apache.flink.table.operations.command.RemoveJarOperation) BeginStatementSetOperation(org.apache.flink.table.operations.BeginStatementSetOperation) AddJarOperation(org.apache.flink.table.operations.command.AddJarOperation) AlterTableAddConstraintOperation(org.apache.flink.table.operations.ddl.AlterTableAddConstraintOperation) ExplainOperation(org.apache.flink.table.operations.ExplainOperation) ResetOperation(org.apache.flink.table.operations.command.ResetOperation) StatementSetOperation(org.apache.flink.table.operations.StatementSetOperation) AlterTableRenameOperation(org.apache.flink.table.operations.ddl.AlterTableRenameOperation) CalciteParser(org.apache.flink.table.planner.parse.CalciteParser) Test(org.junit.Test)

Aggregations

SinkModifyOperation (org.apache.flink.table.operations.SinkModifyOperation)7 QueryOperation (org.apache.flink.table.operations.QueryOperation)4 SourceQueryOperation (org.apache.flink.table.operations.SourceQueryOperation)4 ContextResolvedTable (org.apache.flink.table.catalog.ContextResolvedTable)2 ObjectIdentifier (org.apache.flink.table.catalog.ObjectIdentifier)2 BeginStatementSetOperation (org.apache.flink.table.operations.BeginStatementSetOperation)2 EndStatementSetOperation (org.apache.flink.table.operations.EndStatementSetOperation)2 ExplainOperation (org.apache.flink.table.operations.ExplainOperation)2 LoadModuleOperation (org.apache.flink.table.operations.LoadModuleOperation)2 Operation (org.apache.flink.table.operations.Operation)2 ShowFunctionsOperation (org.apache.flink.table.operations.ShowFunctionsOperation)2 ShowModulesOperation (org.apache.flink.table.operations.ShowModulesOperation)2 StatementSetOperation (org.apache.flink.table.operations.StatementSetOperation)2 UnloadModuleOperation (org.apache.flink.table.operations.UnloadModuleOperation)2 UseCatalogOperation (org.apache.flink.table.operations.UseCatalogOperation)2 UseDatabaseOperation (org.apache.flink.table.operations.UseDatabaseOperation)2 UseModulesOperation (org.apache.flink.table.operations.UseModulesOperation)2 AddJarOperation (org.apache.flink.table.operations.command.AddJarOperation)2 RemoveJarOperation (org.apache.flink.table.operations.command.RemoveJarOperation)2 ResetOperation (org.apache.flink.table.operations.command.ResetOperation)2