@SuppressWarnings({"squid:S3776"}) // Suppress high Cognitive Complexity warning
  private static int executeQuery(CliContext ctx, IoTDBConnection connection, String cmd) {
    int executeStatus = CODE_OK;
    long startTime = System.currentTimeMillis();
    try (Statement statement = connection.createStatement()) {
      ZoneId zoneId = ZoneId.of(connection.getTimeZone());
      statement.setFetchSize(fetchSize);
      boolean hasResultSet = statement.execute(cmd.trim());
      long costTime = System.currentTimeMillis() - startTime;
      if (hasResultSet) {
        // print the result
        try (ResultSet resultSet = statement.getResultSet()) {
          ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
          int columnLength = resultSetMetaData.getColumnCount();
          List<Integer> maxSizeList = new ArrayList<>(columnLength);
          List<List<String>> lists =
              cacheResult(ctx, resultSet, maxSizeList, columnLength, resultSetMetaData, zoneId);
          output(ctx, lists, maxSizeList);
          ctx.getPrinter().println(String.format("It costs %.3fs", costTime / 1000.0));
          while (!isReachEnd) {
            if (continuePrint) {
              maxSizeList = new ArrayList<>(columnLength);
              lists =
                  cacheResult(ctx, resultSet, maxSizeList, columnLength, resultSetMetaData, zoneId);
              output(ctx, lists, maxSizeList);
              continue;
            }
            ctx.getPrinter()
                .println("This display 1000 rows. Press ENTER to show more, input 'q' to quit.");

            BufferedReader br = new BufferedReader(new InputStreamReader(ctx.getIn()));
            try {
              String line;
              if (ctx.isDisableCliHistory()) {
                line = ctx.getLineReader().readLine();
              } else {
                line = br.readLine();
              }
              if ("".equals(line)) {
                maxSizeList = new ArrayList<>(columnLength);
                lists =
                    cacheResult(
                        ctx, resultSet, maxSizeList, columnLength, resultSetMetaData, zoneId);
                output(ctx, lists, maxSizeList);
              } else {
                break;
              }
            } catch (Exception e) {
              ctx.getPrinter().printException(e);
              executeStatus = CODE_ERROR;
            }
          }
          // output tracing activity
          if (((IoTDBJDBCResultSet) resultSet).isSetTracingInfo()) {
            maxSizeList = new ArrayList<>(2);
            lists = cacheTracingInfo(resultSet, maxSizeList);
            outputTracingInfo(ctx, lists, maxSizeList);
          }
        }
      } else {
        ctx.getPrinter().println("Msg: " + SUCCESS_MESSAGE);
      }
    } catch (Exception e) {
      ctx.getPrinter().println("Msg: " + e);
      executeStatus = CODE_ERROR;
    } finally {
      updateSqlDialectAndUsingDatabase(
          connection.getParams().getSqlDialect(), connection.getParams().getDb().orElse(null));
      resetArgs();
    }
    return executeStatus;
  }
If cli has not specified a zoneId, it will be set to cli's system timezone by default otherwise
for insert and query accuracy cli should set timezone the same for all sessions.

@param specialCmd
@param cmd
@param connection
@return execute result code
iotdb-client/cli/src/main/java/org/apache/iotdb/cli/AbstractCli.java