<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def from_bigquery(sql):
 """Create a ConfusionMatrix from a BigQuery table or query. Args: sql: Can be one of: A SQL query string. A Bigquery table string. A Query object defined with '%%bq query --name [query_name]'. The query results or table must include "target", "predicted" columns. Returns: A ConfusionMatrix that can be plotted. Raises: ValueError if query results or table does not include 'target' or 'predicted' columns. """

    if isinstance(sql, bq.Query):
      sql = sql._expanded_sql()

    parts = sql.split('.')
    if len(parts) == 1 or len(parts) > 3 or any(' ' in x for x in parts):
      sql = '(' + sql + ')'  # query, not a table name
    else:
      sql = '`' + sql + '`'  # table name

    query = bq.Query(
        'SELECT target, predicted, count(*) as count FROM %s group by target, predicted' % sql)
    df = query.execute().result().to_dataframe()
    labels = sorted(set(df['target']) | set(df['predicted']))
    labels_count = len(labels)
    df['target'] = [labels.index(x) for x in df['target']]
    df['predicted'] = [labels.index(x) for x in df['predicted']]
    cm = [[0] * labels_count for i in range(labels_count)]
    for index, row in df.iterrows():
      cm[row['target']][row['predicted']] = row['count']
    return ConfusionMatrix(cm, labels)