You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/02/11 12:37:11 UTC

[GitHub] [flink] twalthr commented on a change in pull request #18648: [FLINK-25845][table] Introduce EXECUTE PLAN

twalthr commented on a change in pull request #18648:
URL: https://github.com/apache/flink/pull/18648#discussion_r804556222



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/api/internal/TableEnvironmentInternalTest.java
##########
@@ -93,6 +97,23 @@ public void testExecutePlan() throws Exception {
         assertResult(data, sinkPath);
     }
 
+    @Test
+    public void testExecutePlanSql() throws Exception {

Review comment:
       Rename this class to `CompiledPlanITCase extends JsonPlanTestBase` and move it to `org.apache.flink.table.api`

##########
File path: flink-table/flink-sql-parser/src/main/codegen/includes/parserImpls.ftl
##########
@@ -1787,6 +1787,24 @@ SqlNode SqlExecute() :
     }
 }
 
+/**
+* Parses a execute plan statement.

Review comment:
       nit: `an`

##########
File path: flink-table/flink-sql-parser/src/main/java/org/apache/flink/sql/parser/dml/SqlExecutePlan.java
##########
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.sql.parser.dml;
+
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlCharStringLiteral;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.util.NlsString;
+
+import javax.annotation.Nonnull;
+
+import java.util.Collections;
+import java.util.List;
+
+/** AST node for {@code EXECUTE PLAN 'planfile'}. */
+public class SqlExecutePlan extends SqlCall {

Review comment:
       `@Internal` or default scoped

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/api/internal/TableEnvironmentInternalTest.java
##########
@@ -114,6 +118,195 @@ public void testExecutePlanSql() throws Exception {
         assertResult(data, sinkPath);
     }
 
+    @Test
+    public void testCompilePlan() throws Exception {
+        Path planPath =
+                Paths.get(URI.create(getTempDirPath("plan")).getPath(), "plan.json")
+                        .toAbsolutePath();
+        FileUtils.createParentDirectories(planPath.toFile());
+
+        List<String> data = Arrays.asList("1,1,hi", "2,1,hello", "3,2,hello world");

Review comment:
       dedup code in this class

##########
File path: flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/TableEnvironmentImpl.java
##########
@@ -791,6 +796,52 @@ public TableResult executePlan(CompiledPlan plan) {
         return executeInternal(transformations, sinkIdentifierNames);
     }
 
+    private CompiledPlan compilePlanAndWrite(
+            String filePath, boolean ifNotExists, Operation operation) {
+        File file = Paths.get(filePath).toFile();
+        if (file.exists()) {
+            if (ifNotExists) {
+                try {
+                    return loadPlan(PlanReference.fromFile(filePath));
+                } catch (IOException e) {
+                    throw new TableException("Cannot load the plan file '" + filePath + "'", e);
+                }
+            }
+
+            if (!tableConfig.getConfiguration().get(TableConfigOptions.PLAN_FORCE_RECOMPILE)) {

Review comment:
       Let's move this into `compiledPlan.writeToFile`/into the planner? Then the force overwrite is valid everywhere.

##########
File path: flink-table/flink-sql-parser/src/main/java/org/apache/flink/sql/parser/dml/SqlCompileAndExecutePlan.java
##########
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.sql.parser.dml;
+
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlCharStringLiteral;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.util.NlsString;
+
+import javax.annotation.Nonnull;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * AST node for {@code COMPILE AND EXECUTE PLAN 'planfile' FOR [DML]}. DML can be either a {@link
+ * RichSqlInsert} or a {@link SqlStatementSet}.
+ */
+public class SqlCompileAndExecutePlan extends SqlCall {
+
+    public static final SqlSpecialOperator OPERATOR =
+            new SqlSpecialOperator("CompilePlanAndExecute", SqlKind.OTHER);

Review comment:
       btw it is totally valid to use SQL syntax for names including spaces, so the name can be `COMPILE AND EXECUTE PLAN`, check other operators such as `IS NULL` etc.

##########
File path: flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/internal/TableEnvironmentImpl.java
##########
@@ -791,6 +796,52 @@ public TableResult executePlan(CompiledPlan plan) {
         return executeInternal(transformations, sinkIdentifierNames);
     }
 
+    private CompiledPlan compilePlanAndWrite(
+            String filePath, boolean ifNotExists, Operation operation) {
+        File file = Paths.get(filePath).toFile();
+        if (file.exists()) {
+            if (ifNotExists) {
+                try {
+                    return loadPlan(PlanReference.fromFile(filePath));
+                } catch (IOException e) {
+                    throw new TableException("Cannot load the plan file '" + filePath + "'", e);

Review comment:
       TBH SQL should be a thin wrapper around Table API. We can also use `TableException` for `loadPlan` and make the nice `Cannot load plan file...` for all users consistently. Checked exceptions are annoying anyway and it should be obvious that a lot of stuff can go wrong (not only IO with files but also catalog etc.).

##########
File path: flink-table/flink-sql-parser/src/main/java/org/apache/flink/sql/parser/ddl/SqlCompilePlan.java
##########
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.sql.parser.ddl;
+
+import org.apache.flink.sql.parser.dml.RichSqlInsert;
+import org.apache.flink.sql.parser.dml.SqlStatementSet;
+
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlCharStringLiteral;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.util.NlsString;
+
+import javax.annotation.Nonnull;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * AST node for {@code COMPILE PLAN 'planfile' [IF NOT EXISTS] FOR [DML]}. DML can be either a
+ * {@link RichSqlInsert} or a {@link SqlStatementSet}.
+ */
+public class SqlCompilePlan extends SqlCall {
+
+    public static final SqlSpecialOperator OPERATOR =
+            new SqlSpecialOperator("CompilePlan", SqlKind.OTHER);
+
+    private final SqlCharStringLiteral planFile;
+    private final boolean ifNotExists;
+    private SqlNode operand;
+
+    public SqlCompilePlan(
+            SqlParserPos pos, SqlCharStringLiteral planFile, boolean ifNotExists, SqlNode operand) {
+        super(pos);
+        this.planFile = planFile;
+        this.ifNotExists = ifNotExists;
+        this.operand = checkOperand(operand);
+    }
+
+    public String getPlanFile() {
+        return planFile.getValueAs(NlsString.class).getValue();
+    }
+
+    public boolean isIfNotExists() {
+        return ifNotExists;
+    }
+
+    @Nonnull
+    @Override
+    public SqlOperator getOperator() {
+        return OPERATOR;
+    }
+
+    @Nonnull
+    @Override
+    public List<SqlNode> getOperandList() {
+        return Collections.singletonList(operand);
+    }
+
+    @Override
+    public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
+        writer.keyword("COMPILE PLAN");
+        planFile.unparse(writer, leftPrec, rightPrec);
+        if (isIfNotExists()) {
+            writer.keyword("IF NOT EXISTS");
+        }
+        writer.keyword("FOR");
+        operand.unparse(writer, leftPrec, rightPrec);
+    }
+
+    @Override
+    public void setOperand(int i, SqlNode operand) {
+        if (i == 0) {
+            this.operand = checkOperand(operand);
+        } else {
+            throw new UnsupportedOperationException(
+                    "SqlCompilePlan supports only one operand with index 0");
+        }
+    }
+
+    private SqlNode checkOperand(SqlNode operand) {
+        if (!(operand instanceof RichSqlInsert || operand instanceof SqlStatementSet)) {
+            throw new UnsupportedOperationException(
+                    "SqlCompilePlan supports only RichSqlInsert or SqlStatementSet as operand");

Review comment:
       Could a user see this? If yes, remove internal class names and write the exception like: `COMPILE PLAN supports only INSERT INTO or STATEMENT SET definitions.`

##########
File path: flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/operations/command/ExecutePlanOperation.java
##########
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.operations.command;
+
+import org.apache.flink.annotation.Experimental;
+import org.apache.flink.table.operations.Operation;
+
+/** Operation to describe an EXECUTE PLAN statement. */
+@Experimental

Review comment:
       `@Internal` for now

##########
File path: flink-table/flink-sql-parser/src/main/java/org/apache/flink/sql/parser/ddl/SqlCompilePlan.java
##########
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.sql.parser.ddl;
+
+import org.apache.flink.sql.parser.dml.RichSqlInsert;
+import org.apache.flink.sql.parser.dml.SqlStatementSet;
+
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlCharStringLiteral;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.util.NlsString;
+
+import javax.annotation.Nonnull;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * AST node for {@code COMPILE PLAN 'planfile' [IF NOT EXISTS] FOR [DML]}. DML can be either a
+ * {@link RichSqlInsert} or a {@link SqlStatementSet}.
+ */
+public class SqlCompilePlan extends SqlCall {
+
+    public static final SqlSpecialOperator OPERATOR =
+            new SqlSpecialOperator("CompilePlan", SqlKind.OTHER);
+
+    private final SqlCharStringLiteral planFile;
+    private final boolean ifNotExists;
+    private SqlNode operand;
+
+    public SqlCompilePlan(
+            SqlParserPos pos, SqlCharStringLiteral planFile, boolean ifNotExists, SqlNode operand) {
+        super(pos);
+        this.planFile = planFile;
+        this.ifNotExists = ifNotExists;
+        this.operand = checkOperand(operand);
+    }
+
+    public String getPlanFile() {
+        return planFile.getValueAs(NlsString.class).getValue();

Review comment:
       Take a look at `org.apache.flink.sql.parser.ddl.SqlTableColumn.SqlMetadataColumn#getMetadataAlias` there are different ways of extracting a string from SQL parser. Esp. for paths we should also support line breaks with the SQL string concatenate operator like `||`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org