|
| 1 | +/* |
| 2 | + * Copyright 2021 DataCanvas |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.dingodb.expr.runtime.op.time; |
| 18 | + |
| 19 | +import io.dingodb.expr.common.type.Type; |
| 20 | +import io.dingodb.expr.common.type.Types; |
| 21 | +import io.dingodb.expr.runtime.ExprConfig; |
| 22 | +import io.dingodb.expr.runtime.op.BinaryOp; |
| 23 | +import io.dingodb.expr.runtime.op.OpKey; |
| 24 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 25 | + |
| 26 | +import java.sql.Date; |
| 27 | +import java.sql.Timestamp; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +public final class DateFormat2FunFactory extends DateFormat2Fun { |
| 32 | + private static final long serialVersionUID = -7943722511046298904L; |
| 33 | + |
| 34 | + public static final DateFormat2FunFactory INSTANCE = new DateFormat2FunFactory(); |
| 35 | + |
| 36 | + private final Map<Object, DateFormat2Fun> opMap = new HashMap<>(); |
| 37 | + |
| 38 | + private DateFormat2FunFactory() { |
| 39 | + super(); |
| 40 | + opMap.put(keyOf(Types.ANY, Types.STRING), new DateFormatAnyString()); |
| 41 | + opMap.put(keyOf(Types.DATE, Types.STRING), new DateFormatDateString()); |
| 42 | + opMap.put(keyOf(Types.TIMESTAMP, Types.STRING), new DateFormatTimestampString()); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public BinaryOp getOp(OpKey key) { |
| 47 | + return opMap.get(key); |
| 48 | + } |
| 49 | + |
| 50 | + public static final class DateFormatAnyString extends DateFormat2Fun { |
| 51 | + private static final long serialVersionUID = -1014099593846559663L; |
| 52 | + |
| 53 | + @Override |
| 54 | + protected String evalNonNullValue(@NonNull Object value0, @NonNull Object value1, |
| 55 | + ExprConfig config) { |
| 56 | + if (value0 instanceof Number) { |
| 57 | + return dateFormat((Number) value0, (String) value1); |
| 58 | + } else if (value0 instanceof Timestamp) { |
| 59 | + return dateFormat((Timestamp) value0, (String) value1); |
| 60 | + } else if (value0 instanceof Date) { |
| 61 | + return dateFormat((Date) value0, (String) value1); |
| 62 | + } else { |
| 63 | + return null; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Type getType() { |
| 69 | + return Types.STRING; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public OpKey getKey() { |
| 74 | + return keyOf(Types.ANY, Types.STRING); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static final class DateFormatDateString extends DateFormat2Fun { |
| 79 | + private static final long serialVersionUID = 1697599751798482051L; |
| 80 | + |
| 81 | + @Override |
| 82 | + protected String evalNonNullValue(@NonNull Object value0, @NonNull Object value1, |
| 83 | + ExprConfig config) { |
| 84 | + if (value0 instanceof Number) { |
| 85 | + return dateFormat((Number) value0, (String) value1); |
| 86 | + } else if (value0 instanceof Timestamp) { |
| 87 | + return dateFormat((Timestamp) value0, (String) value1); |
| 88 | + } else if (value0 instanceof Date) { |
| 89 | + return dateFormat((Date) value0, (String) value1); |
| 90 | + } else { |
| 91 | + return null; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Type getType() { |
| 97 | + return Types.STRING; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public OpKey getKey() { |
| 102 | + return keyOf(Types.DATE, Types.STRING); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static final class DateFormatTimestampString extends DateFormat2Fun { |
| 107 | + private static final long serialVersionUID = 9159110028878236722L; |
| 108 | + |
| 109 | + @Override |
| 110 | + protected String evalNonNullValue(@NonNull Object value0, @NonNull Object value1, |
| 111 | + ExprConfig config) { |
| 112 | + if (value0 instanceof Number) { |
| 113 | + return dateFormat((Number) value0, (String) value1); |
| 114 | + } else if (value0 instanceof Timestamp) { |
| 115 | + return dateFormat((Timestamp) value0, (String) value1); |
| 116 | + } else if (value0 instanceof Date) { |
| 117 | + return dateFormat((Date) value0, (String) value1); |
| 118 | + } else { |
| 119 | + return null; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public Type getType() { |
| 125 | + return Types.STRING; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public OpKey getKey() { |
| 130 | + return keyOf(Types.TIMESTAMP, Types.STRING); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments