def METHOD_NAME(response):
    results = response["results"]
    rows = []
    field_orders = {}
    for row in results:
        record = {}
        rows.append(record)
        for order, col in enumerate(row):
            if col["field"] == "@ptr":
                continue
            field = col["field"]
            record[field] = col["value"]
            field_orders[field] = max(field_orders.get(field, -1), order)
    fields = sorted(field_orders, key=lambda f: field_orders[f])
    cols = [
        {
            "name": f,
            "type": "datetime" if f == "@timestamp" else "string",
            "friendly_name": f,
        }
        for f in fields
    ]
    return {
        "columns": cols,
        "rows": rows,
        "metadata": {"data_scanned": response["statistics"]["bytesScanned"]},
    }