Max

Max( value_1, value_2, ... )
Max( value_1, value_2, ... )
Max( value_1, value_2, ... )
Max( value_1, value_2, ... )
max( value_1, value_2, ... )
Max( value_1, value_2, ... )

Description

The Max function returns the largest value in a list of values.

Types have an order of precedence. When comparing values of different types, they are ranked in the following order, from least to greatest.

  1. Number (integers and decimals: 0.5 < 1 < 1.5 < 2)

  2. Byte

  3. String

  4. Array (ordered lexically, like strings)

  5. Object (ordered lexically, like strings)

  6. Reference

  7. Timestamp

  8. Date

  9. Boolean (false < true)

  10. Null

With this precedence, Strings are always larger than Numbers.

The run time of Max is dependent on the number of elements in the underlying set or page — it’s linear, or O(n). For very large sets or pages, executing Max might result in a query timeout error, or "width" error.

For query "width" errors, the underlying set or page involves more than 100K items. This can happen when using a set function, such as Difference, where more than 100K items need to be considered to produce the set that Max evaluates. To resolve this, use Paginate to limit the set or page size.

For example, instead of:

Max(
  Difference(
    Match(Index("Index1"), "term1"),
    Match(Index("Index2"), "term2")
  )
)

use:

Max(
  Paginate(
    Difference(
      Match(Index("Index1"), "term1"),
      Match(Index("Index2"), "term2")
    ),
    { size: 10000 }
  )
)

This does mean that if the entire set must be evaluated to arrive at the correct result, you would have to page through the Paginate results.

For query timeout errors, you may specify a larger query timeout via the driver that you are using.

Parameters

Parameter Type Definition and Requirements

value

List of Values.

A single Value or a list of Values.

Returns

A value which is the maximum value from the value list.

Examples

The following query executes an array of independent max operations and returns the results in an array. The result array position matches the execution array position. The top operation in the execution array, max of the values 1, 5, and 22, returns a long value of 22 in the top position of the result array.

try
{
    Value result = await client.Query(
        Arr(
            Max(1, 5, 22),
            Max(1, 0, 3, -1),
            Max(-1, 12, 3, -1),
            Max(Arr(10))
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(LongV(22), LongV(3), LongV(12), LongV(10))
result, err := client.Query(
	f.Arr{
		f.Max(1, 5, 22),
		f.Max(1, 0, 3, -1),
		f.Max(-1, 12, 3, -1),
		f.Max(f.Arr{10}),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[22 3 12 10]
System.out.println(
    client.query(
        Arr(
            Max(Value(1), Value(5), Value(22)),
            Max(Value(1), Value(0), Value(3), Value(-1)),
            Max(Value(-1), Value(12), Value(3), Value(-1)),
            Max(Arr(Value(10)))
        )
    ).get());
[22, 3, 12, 10]
client.query(
  [
    q.Max(1, 5, 22),
    q.Max(1, 0, 3, -1),
    q.Max(-1, 12, 3, -1),
    q.Max([10]),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[ 22, 3, 12, 10 ]
result = client.query(
  [
    q.max(1, 5, 22),
    q.max(1, 0, 3, -1),
    q.max(-1, 12, 3, -1),
    q.max([10]),
  ]
)
print(result)
[22, 3, 12, 10]
[
  Max(1, 5, 22),
  Max(1, 0, 3, -1),
  Max(-1, 12, 3, -1),
  Max([10]),
]
[ 22, 3, 12, 10 ]
Query metrics:
  •    bytesIn:  71

  •   bytesOut:  25

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 4ms

  •    retries:   0

The following query uses the same approach as the previous query to demonstrate using Max with various types of values:

try
{
    Value result = await client.Query(
        Arr(
            Max("A", "B", "C", "D"),
            Max(10, 11, "A"),
            Max(Time("1970-01-01T00:00:00Z"), Time("1980-01-01T00:00:00Z")),
            Max(Date("1970-01-01"), Date("1930-01-01")),
            Max("A", 1),
            Max(true, false),
            Max(Obj("x", 10), Obj("x", 11)),
            Max(Arr("A"), Arr("B"), Arr("C")),
            Max(Arr("X"), Arr("A", "B"))
        )
    );

    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
Arr(StringV(D), StringV(A), FaunaTime(1980-01-01T00:00:00Z), FaunaDate(1970-01-01 12:00:00 AM), StringV(A), BooleanV(True), ObjectV(x: LongV(11)), Arr(StringV(C)), Arr(StringV(X)))
result, err := client.Query(
	f.Arr{
		f.Max("A", "B", "C", "D"),
		f.Max(10, 11, "A"),
		f.Max(f.Time("1970-01-01T00:00:00Z"), f.Time("1980-01-01T00:00:00Z")),
		f.Max(f.Date("1970-01-01"), f.Date("1930-01-01")),
		f.Max("A", 1),
		f.Max(true, false),
		f.Max(f.Obj{"x": 10}, f.Obj{"x": 11}),
		f.Max(f.Arr{"A"}, f.Arr{"B"}, f.Arr{"C"}),
		f.Max(f.Arr{"X"}, f.Arr{"A", "B"}),
	})

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
[D A {0 62451129600 <nil>} {0 62135596800 <nil>} A true map[x:11] [C] [X]]
System.out.println(
    client.query(
        Arr(
            Max(Value("A"), Value("B"), Value("C"), Value("D")),
            Max(Value(10), Value(11), Value("A")),
            Max(Time("1970-01-01T00:00:00Z"), Time("1980-01-01T00:00:00Z")),
            Max(Date("1970-01-01"), Date("1930-01-01")),
            Max(Value("A"), Value(1)),
            Max(Value(true), Value(false)),
            Max(Obj("x", Value(10)), Obj("x", Value(11))),
            Max(Arr(Value("A")), Arr(Value("B")), Arr(Value("C"))),
            Max(Arr(Value("X")), Arr(Value("A"), Value("B")))
        )
    ).get()
);
["D", "A", 1980-01-01T00:00:00Z, 1970-01-01, "A", true, {x: 11}, ["C"], ["X"]]
client.query(
  [
    q.Max('A', 'B', 'C', 'D'),
    q.Max(10, 11, 'A'),
    q.Max(q.Time('1970-01-01T00:00:00Z'), q.Time('1980-01-01T00:00:00Z')),
    q.Max(q.Date('1970-01-01'), q.Date('1930-01-01')),
    q.Max('A', 1),
    q.Max(true, false),
    q.Max({ x: 10 }, { x: 11 }),
    q.Max(['A'], ['B'], ['C']),
    q.Max(['X'], ['A', 'B']),
  ]
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
[
  'D',
  'A',
  Time("1980-01-01T00:00:00Z"),
  Date("1970-01-01"),
  'A',
  true,
  { x: 11 },
  [ 'C' ],
  [ 'X' ]
]
result = client.query(
  [
    q.max("A", "B", "C", "D"),
    q.max(10, 11, "A"),
    q.max(q.time("1970-01-01T00:00:00Z"), q.time("1980-01-01T00:00:00Z")),
    q.max(q.date("1970-01-01"), q.date("1930-01-01")),
    q.max("A", 1),
    q.max(True, False),
    q.max({"x": 10}, {"x": 11}),
    q.max(["A"], ["B"], ["C"]),
    q.max(["X"], ["A", "B"]),
  ]
)
print(result)
['D', 'A', FaunaTime('1980-01-01T00:00:00Z'), datetime.date(1970, 1, 1), 'A', True, {'x': 11}, ['C'], ['X']]
[
  Max('A', 'B', 'C', 'D'),
  Max(10, 11, 'A'),
  Max(Time('1970-01-01T00:00:00Z'), Time('1980-01-01T00:00:00Z')),
  Max(Date('1970-01-01'), Date('1930-01-01')),
  Max('A', 1),
  Max(true, false),
  Max({ x: 10 }, { x: 11 }),
  Max(['A'], ['B'], ['C']),
  Max(['X'], ['A', 'B']),
]
[
  'D',
  'A',
  Time("1980-01-01T00:00:00Z"),
  Date("1970-01-01"),
  'A',
  true,
  { x: 11 },
  [ 'C' ],
  [ 'X' ]
]
Query metrics:
  •    bytesIn: 316

  •   bytesOut: 106

  • computeOps:   1

  •    readOps:   0

  •   writeOps:   0

  •  readBytes:   0

  • writeBytes:   0

  •  queryTime: 5ms

  •    retries:   0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!