Class: Google::Cloud::Datastore::Dataset::AggregateQueryResults

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/datastore/dataset/aggregate_query_results.rb

Overview

AggregateQueryResults

An AggregateQueryResult object is a representation for a result of an AggregateQuery or a GqlQuery.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aggregate_fieldsHash{String => Integer, Float}

The result of the aggregation query, returned as a hash of key-value pairs. The key is the alias of the aggregate function, and the value is the result of the aggregation.

The alias of the aggregate function can be:

  • an aggregate literal "sum", "avg", or "count"
  • a custom aggregate alias

Returns:

  • (Hash{String => Integer, Float})


52
53
54
# File 'lib/google/cloud/datastore/dataset/aggregate_query_results.rb', line 52

def aggregate_fields
  @aggregate_fields
end

#explain_metricsGoogle::Cloud::Datastore::V1::ExplainMetrics?

Query explain metrics. This is only present when the explain_options are provided to Google::Cloud::Datastore::Dataset#run_aggregation. It is sent only once with the response.

Returns:

  • (Google::Cloud::Datastore::V1::ExplainMetrics, nil)


65
66
67
# File 'lib/google/cloud/datastore/dataset/aggregate_query_results.rb', line 65

def explain_metrics
  @explain_metrics
end

#explain_optionsGoogle::Cloud::Datastore::V1::ExplainOptions?

The options for query explanation.

This is a copy of the input parameter supplied to the Google::Cloud::Datastore::Dataset#run_aggregation function.

Returns:

  • (Google::Cloud::Datastore::V1::ExplainOptions, nil)


73
74
75
# File 'lib/google/cloud/datastore/dataset/aggregate_query_results.rb', line 73

def explain_options
  @explain_options
end

#read_timeGoogle::Protobuf::Timestamp

The time when the query was executed.

Returns:

  • (Google::Protobuf::Timestamp)


58
59
60
# File 'lib/google/cloud/datastore/dataset/aggregate_query_results.rb', line 58

def read_time
  @read_time
end

Instance Method Details

#get(aggregate_alias = nil) ⇒ Integer, ...

Retrieves the aggregate data.

if the aggregate_alias does not exist.

Examples:

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get

Alias an aggregate query

require "google/cloud/datastore"

datastore = Google::Cloud::Datastore.new

query = Google::Cloud::Datastore::Query.new
query.kind("Task")
     .where("done", "=", false)

Create an aggregate query
aggregate_query = query.aggregate_query
                       .add_count aggregate_alias: 'total'

aggregate_query_results = dataset.run_aggregation aggregate_query
puts aggregate_query_results.get('total')

Parameters:

  • aggregate_alias (String) (defaults to: nil)

    The alias used to access the aggregate value. For an AggregateQuery with a single aggregate field, this parameter can be omitted.

Returns:

  • (Integer, Float, nil)

    The aggregate value. Returns nil



120
121
122
123
124
125
126
# File 'lib/google/cloud/datastore/dataset/aggregate_query_results.rb', line 120

def get aggregate_alias = nil
  if @aggregate_fields.count > 1 && aggregate_alias.nil?
    raise ArgumentError, "Required param aggregate_alias for AggregateQuery with multiple aggregate fields"
  end
  aggregate_alias ||= @aggregate_fields.keys.first
  @aggregate_fields[aggregate_alias]
end