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.

To work around this, you may specify a larger query timeout via the driver that you are using.

Parameters

Argument 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 query below 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.

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);
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', err))
[ 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]
println(Await.result(
    client.query(
        Arr(
            Max(1, 5, 22),
            Max(1, 0, 3, -1),
            Max(-1, 12, 3, -1),
            Max(Arr(10))
        )
    ),
    5.seconds
))
[22, 3, 12, 10]

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

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);
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', err))
[
  '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']]
println(Await.result(
    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"))
        )
    ),
    5.seconds
))
["D", "A", 1980-01-01T00:00:00Z, 1970-01-01, "A", true, {x: 11}, ["C"], ["X"]]

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!