wuchong commented on a change in pull request #15089:
URL: https://github.com/apache/flink/pull/15089#discussion_r589249112



##########
File path: 
flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/utils/SqlScriptReader.java
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.client.cli.utils;
+
+import javax.annotation.Nullable;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * A utility to read and parse content of a SQL script. The SQL script is 
located in "resources/sql"
+ * path and in the "xx.q" file name pattern. The SQL script is executed and 
tested by {@link
+ * org.apache.flink.table.client.cli.CliClientITCase}.
+ */
+public final class SqlScriptReader implements AutoCloseable {
+
+    private final BufferedReader reader;
+    private String currentLine;
+
+    public static List<TestSqlStatement> parseSqlScript(String in) {
+        try (SqlScriptReader sqlReader = new SqlScriptReader(in)) {
+            return sqlReader.parseSqlScript();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private SqlScriptReader(String input) {
+        this.reader = new BufferedReader(new StringReader(input));
+    }
+
+    private List<TestSqlStatement> parseSqlScript() throws IOException {
+        List<TestSqlStatement> specs = new ArrayList<>();
+        TestSqlStatement spec;
+        while ((spec = readNext()) != null) {
+            specs.add(spec);
+        }
+        return specs;
+    }
+
+    private void readLine() throws IOException {
+        this.currentLine = reader.readLine();
+    }
+
+    private @Nullable TestSqlStatement readNext() throws IOException {
+        StringBuilder commentLines = new StringBuilder();
+        StringBuilder sqlLines = new StringBuilder();
+        ReadingStatus status = ReadingStatus.BEGINNING;
+        readLine();
+        while (currentLine != null) {
+            switch (status) {

Review comment:
       That would get an invalid `TestSqlStatement`, for example, 
   
   1) invalid `sql` string if there is no `;` at the end.
   2) next comment and sql string will be recognized as result content if there 
is no `!flag` after result content
   3) ...
   
   No matter which case, the script comparing will fail, and you can get the 
mismatch from the assertion, and then can easily fix the invalid sql script. 
   
   




----------------------------------------------------------------
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.

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


Reply via email to