def _sample_cell(args, cell_body):
  """Implements the BigQuery sample magic for sampling queries
  The supported sytanx is:
    %%bq sample <args>
     [<inline SQL>]
  Args:
    args: the optional arguments following '%%bq sample'.
    cell_body: optional contents of the cell
  Returns:
    The results of executing the sampling query, or a profile of the sample data.
  """

  env = google.datalab.utils.commands.notebook_environment()
  config = google.datalab.utils.commands.parse_config(cell_body, env, False) or {}
  parameters = config.get('parameters') or []
  if parameters:
    jsonschema.validate({'parameters': parameters}, BigQuerySchema.QUERY_PARAMS_SCHEMA)

  query = None
  table = None
  view = None
  query_params = None

  if args['query']:
    query = google.datalab.utils.commands.get_notebook_item(args['query'])
    if query is None:
      raise Exception('Cannot find query %s.' % args['query'])
    query_params = get_query_parameters(args, cell_body)

  elif args['table']:
    table_name = google.datalab.bigquery.Query.resolve_parameters(args['table'], parameters)
    table = _get_table(table_name)
    if not table:
      raise Exception('Could not find table %s' % args['table'])
  elif args['view']:
    view = google.datalab.utils.commands.get_notebook_item(args['view'])
    if not isinstance(view, bigquery.View):
      raise Exception('Could not find view %s' % args['view'])
  else:
    raise Exception('A query, table, or view is neede to sample')

  # parse comma-separated list of fields
  fields = args['fields'].split(',') if args['fields'] else None
  count = int(args['count']) if args['count'] else None
  percent = int(args['percent']) if args['percent'] else None
  sampling = Sampling._auto(method=args['method'], fields=fields, count=count, percent=percent,
                            key_field=args['key_field'], ascending=(args['order'] == 'ascending'))

  context = google.datalab.utils._utils._construct_context_for_args(args)

  if view:
    query = bigquery.Query.from_view(view)
  elif table:
    query = bigquery.Query.from_table(table)

  if args['profile']:
    results = query.execute(QueryOutput.dataframe(), sampling=sampling,
                            context=context, query_params=query_params).result()
  else:
    results = query.execute(QueryOutput.table(), sampling=sampling, context=context,
                            query_params=query_params).result()

  if args['verbose']:
    print(query.sql)

  if args['profile']:
    return google.datalab.utils.commands.profile_df(results)
  else:
    return results
def _sample_cell ( args , cell_body ) : env = google . datalab . utils . commands . notebook_environment ( ) config = google . datalab . utils . commands . parse_config ( cell_body , env , False ) or { } parameters = config . get ( 'parameters' ) or [ ] if parameters : jsonschema . validate ( { 'parameters' : parameters } , BigQuerySchema . QUERY_PARAMS_SCHEMA ) query = None table = None view = None query_params = None if args [ 'query' ] : query = google . datalab . utils . commands . get_notebook_item ( args [ 'query' ] ) if query is None : raise Exception ( 'Cannot find query %s.' % args [ 'query' ] ) query_params = get_query_parameters ( args , cell_body ) elif args [ 'table' ] : table_name = google . datalab . bigquery . Query . resolve_parameters ( args [ 'table' ] , parameters ) table = _get_table ( table_name ) if not table : raise Exception ( 'Could not find table %s' % args [ 'table' ] ) elif args [ 'view' ] : view = google . datalab . utils . commands . get_notebook_item ( args [ 'view' ] ) if not isinstance ( view , bigquery . View ) : raise Exception ( 'Could not find view %s' % args [ 'view' ] ) else : raise Exception ( 'A query, table, or view is neede to sample' ) fields = args [ 'fields' ] . split ( ',' ) if args [ 'fields' ] else None count = int ( args [ 'count' ] ) if args [ 'count' ] else None percent = int ( args [ 'percent' ] ) if args [ 'percent' ] else None sampling = Sampling . _auto ( method = args [ 'method' ] , fields = fields , count = count , percent = percent , key_field = args [ 'key_field' ] , ascending = ( args [ 'order' ] == 'ascending' ) ) context = google . datalab . utils . _utils . _construct_context_for_args ( args ) if view : query = bigquery . Query . from_view ( view ) elif table : query = bigquery . Query . from_table ( table ) if args [ 'profile' ] : results = query . execute ( QueryOutput . dataframe ( ) , sampling = sampling , context = context , query_params = query_params ) . result ( ) else : results = query . execute ( QueryOutput . table ( ) , sampling = sampling , context = context , query_params = query_params ) . result ( ) if args [ 'verbose' ] : print ( query . sql ) if args [ 'profile' ] : return google . datalab . utils . commands . profile_df ( results ) else : return results
Implements the BigQuery sample magic for sampling queries
  The supported sytanx is:
    %%bq sample <args>
     [<inline SQL>]
  Args:
    args: the optional arguments following '%%bq sample'.
    cell_body: optional contents of the cell
  Returns:
    The results of executing the sampling query, or a profile of the sample data.