Debian Codegraph @

src/main/org/h2/expression/function/MathFunction1.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 org.h2.api.ErrorCode;
9 import org.h2.engine.SessionLocal;
10 import org.h2.expression.Expression;
11 import org.h2.expression.TypedValueExpression;
12 import org.h2.message.DbException;
13 import org.h2.value.TypeInfo;
14 import org.h2.value.Value;
15 import org.h2.value.ValueDouble;
16 import org.h2.value.ValueNull;
17
18 /**
19 * A math function with one argument and DOUBLE PRECISION result.
20 */
21 public final class MathFunction1 extends Function1 {
22
23 // Trigonometric functions
24
25 /**
26 * SIN().
27 */
28 public static final int SIN = 0;
29
30 /**
31 * COS().
32 */
33 public static final int COS = SIN + 1;
34
35 /**
36 * TAN().
37 */
38 public static final int TAN = COS + 1;
39
40 /**
41 * COT() (non-standard).
42 */
43 public static final int COT = TAN + 1;
44
45 /**
46 * SINH().
47 */
48 public static final int SINH = COT + 1;
49
50 /**
51 * COSH().
52 */
53 public static final int COSH = SINH + 1;
54
55 /**
56 * TANH().
57 */
58 public static final int TANH = COSH + 1;
59
60 /**
61 * ASIN().
62 */
63 public static final int ASIN = TANH + 1;
64
65 /**
66 * ACOS().
67 */
68 public static final int ACOS = ASIN + 1;
69
70 /**
71 * ATAN().
72 */
73 public static final int ATAN = ACOS + 1;
74
75 // Logarithm functions
76
77 /**
78 * LOG10().
79 */
80 public static final int LOG10 = ATAN + 1;
81
82 /**
83 * LN().
84 */
85 public static final int LN = LOG10 + 1;
86
87 // Exponential function
88
89 /**
90 * EXP().
91 */
92 public static final int EXP = LN + 1;
93
94 // Square root
95
96 /**
97 * SQRT().
98 */
99 public static final int SQRT = EXP + 1;
100
101 // Other non-standard
102
103 /**
104 * DEGREES() (non-standard).
105 */
106 public static final int DEGREES = SQRT + 1;
107
108 /**
109 * RADIANS() (non-standard).
110 */
111 public static final int RADIANS = DEGREES + 1;
112
113 private static final String[] NAMES = { //
114 "SIN", "COS", "TAN", "COT", "SINH", "COSH", "TANH", "ASIN", "ACOS", "ATAN", //
115 "LOG10", "LN", "EXP", "SQRT", "DEGREES", "RADIANS" //
116 };
117
118 private final int function;
119
120 public MathFunction1(Expression arg, int function) {
121 super(arg);
122 this.function = function;
123 }
124
125 @Override
126 public Value getValue(SessionLocal session) {
127 Value v = arg.getValue(session);
128 if (v == ValueNull.INSTANCE) {
129 return ValueNull.INSTANCE;
130 }
131 double d = v.getDouble();
132 switch (function) {
133 case SIN:
134 d = Math.sin(d);
135 break;
136 case COS:
137 d = Math.cos(d);
138 break;
139 case TAN:
140 d = Math.tan(d);
141 break;
142 case COT:
143 d = Math.tan(d);
144 if (d == 0.0) {
145 throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getTraceSQL());
146 }
147 d = 1d / d;
148 break;
149 case SINH:
150 d = Math.sinh(d);
151 break;
152 case COSH:
153 d = Math.cosh(d);
154 break;
155 case TANH:
156 d = Math.tanh(d);
157 break;
158 case ASIN:
159 d = Math.asin(d);
160 break;
161 case ACOS:
162 d = Math.acos(d);
163 break;
164 case ATAN:
165 d = Math.atan(d);
166 break;
167 case LOG10:
168 if (d <= 0) {
169 throw DbException.getInvalidValueException("LOG10() argument", d);
170 }
171 d = Math.log10(d);
172 break;
173 case LN:
174 if (d <= 0) {
175 throw DbException.getInvalidValueException("LN() argument", d);
176 }
177 d = Math.log(d);
178 break;
179 case EXP:
180 d = Math.exp(d);
181 break;
182 case SQRT:
183 d = Math.sqrt(d);
184 break;
185 case DEGREES:
186 d = Math.toDegrees(d);
187 break;
188 case RADIANS:
189 d = Math.toRadians(d);
190 break;
191 default:
192 throw DbException.getInternalError("function=" + function);
193 }
194 return ValueDouble.get(d);
195 }
196
197 @Override
198 public Expression optimize(SessionLocal session) {
199 arg = arg.optimize(session);
200 type = TypeInfo.TYPE_DOUBLE;
201 if (arg.isConstant()) {
202 return TypedValueExpression.getTypedIfNull(getValue(session), type);
203 }
204 return this;
205 }
206
207 @Override
208 public String getName() {
209 return NAMES[function];
210 }
211
212 }