def execute_prediction(prediction, empty_table_cursor, cursor, case_sensitive,
                       verbose):
  """Executes a single example's prediction(s).

  If more than one prediction is available, the most likely executable
  prediction is used as the "official" prediction.

  Args:
    prediction: A dictionary containing information for a single example's
      prediction.
    empty_table_cursor: The cursor to a database containing no records, to be
      used only to determine whether a query is executable in the database.
    cursor: The sqlite3 database cursor to execute queries on.
    case_sensitive: Boolean indicating whether the execution should be case
      sensitive with respect to string values.
    verbose: Whether to print details about what queries are being executed.

  Returns:
    Tuple containing the highest-ranked executable query, the resulting table,
    and any exception string associated with executing this query.
  """

  # Go through predictions in order of probability and test their executability
  # until you get an executable prediction. If you don't find one, just
  # "predict" the most probable one.
  paired_preds_and_scores = zip(prediction['predictions'], prediction['scores'])
  sorted_by_scores = sorted(
      paired_preds_and_scores, key=lambda x: x[1], reverse=True)

  best_prediction = None
  pred_results = None
  exception_str = None
  execution_time = 0

  if len(sorted_by_scores) > MAX_CANDIDATE:
    sorted_by_scores = sorted_by_scores[:MAX_CANDIDATE]

  for i, (pred, _) in enumerate(sorted_by_scores):
    # Try predicting
    if verbose:
      print('Trying to execute query:\n\t' + pred)
      print('... on empty database')
    temp_exception_str = try_executing_query(pred, empty_table_cursor,
                                             case_sensitive, verbose)[1]

    if temp_exception_str:
      if i == 0:
        # By default, set the prediction to the first (highest-scoring)
        # one.
        best_prediction = pred

        # Get the actual results
        if verbose:
          print('... on actual database')
        pred_results, exception_str, execution_time = try_executing_query(
            pred, cursor, case_sensitive, verbose)
      if exception_str == 'timeout':
        # Technically, this query didn't have a syntax problem, so
        # continue and set this as the best prediction.
        best_prediction = pred

        if verbose:
          print('... on actual database')
        pred_results, exception_str, execution_time = try_executing_query(
            pred, cursor, case_sensitive, verbose)
        break
    else:
      best_prediction = pred
      exception_str = None

      if verbose:
        print('No exception... on actual database')
      pred_results, _, execution_time = try_executing_query(
          pred, cursor, case_sensitive, verbose)
      break

  return best_prediction, pred_results, exception_str, execution_time
Executes a single example's prediction(s).

  If more than one prediction is available, the most likely executable
  prediction is used as the "official" prediction.

  Args:
    prediction: A dictionary containing information for a single example's
      prediction.
    empty_table_cursor: The cursor to a database containing no records, to be
      used only to determine whether a query is executable in the database.
    cursor: The sqlite3 database cursor to execute queries on.
    case_sensitive: Boolean indicating whether the execution should be case
      sensitive with respect to string values.
    verbose: Whether to print details about what queries are being executed.

  Returns:
    Tuple containing the highest-ranked executable query, the resulting table,
    and any exception string associated with executing this query.
  
language/xsp/evaluation/official_evaluation.py