Debian Codegraph @

src/main/org/h2/expression/function/RandFunction.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.expression.function;
7
8 import java.util.Random;
9
10 import org.h2.engine.SessionLocal;
11 import org.h2.expression.Expression;
12 import org.h2.expression.ExpressionVisitor;
13 import org.h2.message.DbException;
14 import org.h2.util.MathUtils;
15 import org.h2.value.TypeInfo;
16 import org.h2.value.Value;
17 import org.h2.value.ValueDouble;
18 import org.h2.value.ValueNull;
19 import org.h2.value.ValueUuid;
20 import org.h2.value.ValueVarbinary;
21
22 /**
23 * A RAND, SECURE_RAND, or RANDOM_UUID function.
24 */
25 public final class RandFunction extends Function0_1 {
26
27 /**
28 * RAND() (non-standard).
29 */
30 public static final int RAND = 0;
31
32 /**
33 * SECURE_RAND() (non-standard).
34 */
35 public static final int SECURE_RAND = RAND + 1;
36
37 /**
38 * RANDOM_UUID() (non-standard).
39 */
40 public static final int RANDOM_UUID = SECURE_RAND + 1;
41
42 private static final String[] NAMES = { //
43 "RAND", "SECURE_RAND", "RANDOM_UUID" //
44 };
45
46 private final int function;
47
48 public RandFunction(Expression arg, int function) {
49 super(arg);
50 this.function = function;
51 }
52
53 @Override
54 public Value getValue(SessionLocal session) {
55 Value v;
56 if (arg != null) {
57 v = arg.getValue(session);
58 if (v == ValueNull.INSTANCE) {
59 return ValueNull.INSTANCE;
60 }
61 } else {
62 v = null;
63 }
64 switch (function) {
65 case RAND: {
66 Random random = session.getRandom();
67 if (v != null) {
68 random.setSeed(v.getInt());
69 }
70 v = ValueDouble.get(random.nextDouble());
71 break;
72 }
73 case SECURE_RAND:
74 v = ValueVarbinary.getNoCopy(MathUtils.secureRandomBytes(v.getInt()));
75 break;
76 case RANDOM_UUID:
77 v = ValueUuid.getNewRandom();
78 break;
79 default:
80 throw DbException.getInternalError("function=" + function);
81 }
82 return v;
83 }
84
85 @Override
86 public Expression optimize(SessionLocal session) {
87 if (arg != null) {
88 arg = arg.optimize(session);
89 }
90 switch (function) {
91 case RAND:
92 type = TypeInfo.TYPE_DOUBLE;
93 break;
94 case SECURE_RAND: {
95 Value v;
96 type = arg.isConstant() && (v = arg.getValue(session)) != ValueNull.INSTANCE
97 ? TypeInfo.getTypeInfo(Value.VARBINARY, Math.max(v.getInt(), 1), 0, null)
98 : TypeInfo.TYPE_VARBINARY;
99 break;
100 }
101 case RANDOM_UUID:
102 type = TypeInfo.TYPE_UUID;
103 break;
104 default:
105 throw DbException.getInternalError("function=" + function);
106 }
107 return this;
108 }
109
110 @Override
111 public boolean isEverything(ExpressionVisitor visitor) {
112 switch (visitor.getType()) {
113 case ExpressionVisitor.DETERMINISTIC:
114 return false;
115 }
116 return super.isEverything(visitor);
117 }
118
119 @Override
120 public String getName() {
121 return NAMES[function];
122 }
123
124 }