|
| 1 | +package com.clickhouse.utils; |
| 2 | + |
| 3 | +import com.clickhouse.client.api.data_formats.internal.SerializerUtils; |
| 4 | +import com.clickhouse.data.ClickHouseDataType; |
| 5 | +import com.clickhouse.data.format.BinaryStreamUtils; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.OutputStream; |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.time.LocalDate; |
| 11 | +import java.time.ZoneId; |
| 12 | +import java.time.ZonedDateTime; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +//import static com.clickhouse.client.api.data_formats.internal.SerializerUtils.writeDate; |
| 17 | + |
| 18 | +public class Serialize { |
| 19 | + |
| 20 | + public static boolean writePrimitiveValuePreamble(OutputStream out, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 21 | + // since it is primitive we always have a value that is not null |
| 22 | + if (defaultsSupport) { |
| 23 | + SerializerUtils.writeNonNull(out); |
| 24 | + if (isNullable) { |
| 25 | + SerializerUtils.writeNonNull(out); |
| 26 | + } |
| 27 | + } else if (isNullable) { |
| 28 | + SerializerUtils.writeNonNull(out); |
| 29 | + } |
| 30 | + return true; |
| 31 | + } |
| 32 | + public static boolean writeValuePreamble(OutputStream out, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column, Object value) throws IOException { |
| 33 | + if (defaultsSupport) { |
| 34 | + if (value != null) { |
| 35 | + SerializerUtils.writeNonNull(out); |
| 36 | + if (isNullable) { |
| 37 | + SerializerUtils.writeNonNull(out); |
| 38 | + } |
| 39 | + } else { |
| 40 | + if (hasDefault) { |
| 41 | + SerializerUtils.writeNull(out); |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + if (isNullable) { |
| 46 | + SerializerUtils.writeNonNull(out); |
| 47 | + SerializerUtils.writeNull(out); |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + if (dataType == ClickHouseDataType.Array) { |
| 52 | + SerializerUtils.writeNonNull(out); |
| 53 | + } else if (dataType != ClickHouseDataType.Dynamic) { |
| 54 | + throw new IllegalArgumentException(String.format("An attempt to write null into not nullable column '%s'", column)); |
| 55 | + } |
| 56 | + } |
| 57 | + } else if (isNullable) { |
| 58 | + if (value == null) { |
| 59 | + SerializerUtils.writeNull(out); |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + SerializerUtils.writeNonNull(out); |
| 64 | + } else if (value == null) { |
| 65 | + if (dataType == ClickHouseDataType.Array) { |
| 66 | + SerializerUtils.writeNonNull(out); |
| 67 | + } else if (dataType != ClickHouseDataType.Dynamic) { |
| 68 | + throw new IllegalArgumentException(String.format("An attempt to write null into not nullable column '%s'", column)); |
| 69 | + } |
| 70 | + } |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + public static String convertToString(Object value) { |
| 75 | + return java.lang.String.valueOf(value); |
| 76 | + } |
| 77 | + |
| 78 | + public static Integer convertToInteger(Object value) { |
| 79 | + if (value instanceof Integer) { |
| 80 | + return (Integer) value; |
| 81 | + } else if (value instanceof Number) { |
| 82 | + return ((Number) value).intValue(); |
| 83 | + } else if (value instanceof String) { |
| 84 | + return Integer.parseInt((String) value); |
| 85 | + } else if (value instanceof Boolean) { |
| 86 | + return ((Boolean) value) ? 1 : 0; |
| 87 | + } else { |
| 88 | + throw new IllegalArgumentException("Cannot convert " + value + " to Integer"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static Map<ClickHouseDataType, Method> mapClickHouseTypeToMethod() { |
| 93 | + Map<ClickHouseDataType, Method> map = new HashMap<>(); |
| 94 | + for (Method method : Serialize.class.getMethods()) { |
| 95 | + String name = method.getName(); |
| 96 | + if (name.startsWith("write")) { |
| 97 | + String chType = name.substring("write".length()); |
| 98 | + try { |
| 99 | + ClickHouseDataType type = ClickHouseDataType.valueOf(chType); |
| 100 | + map.put(type, method); |
| 101 | + } catch (IllegalArgumentException e) { |
| 102 | + System.out.println(e.getMessage()); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return map; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * |
| 111 | + */ |
| 112 | + |
| 113 | + // Method structure write[ClickHouse Type](OutputStream, Java type, ... ) |
| 114 | + // Date support |
| 115 | + public static void writeDate(OutputStream out, LocalDate value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 116 | + System.out.println("writeDate"); |
| 117 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 118 | + SerializerUtils.writeDate(out, value, ZoneId.of("UTC")); // TODO: check |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public static void writeDate(OutputStream out, ZonedDateTime value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 123 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 124 | + SerializerUtils.writeDate(out, value, ZoneId.of("UTC")); // TODO: check |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // clickhouse type String support |
| 129 | + public static void writeString(OutputStream out, String value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 130 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 131 | + BinaryStreamUtils.writeString(out, convertToString(value)); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public static void writeFixedString(OutputStream out, String value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, int size, String column) throws IOException { |
| 136 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 137 | + BinaryStreamUtils.writeFixedString(out, convertToString(value), size); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // Int8 |
| 142 | + public static void writeInt8(OutputStream out, Byte value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 143 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 144 | + BinaryStreamUtils.writeInt8(out, convertToInteger(value)); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // Int16 |
| 149 | + public static void writeInt16(OutputStream out, Short value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 150 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 151 | + BinaryStreamUtils.writeInt16(out, convertToInteger(value)); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + // Int32 |
| 156 | + public static void writeInt32(OutputStream out, Integer value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 157 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 158 | + BinaryStreamUtils.writeInt32(out, convertToInteger(value)); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // Int64 |
| 163 | + public static void writeInt64(OutputStream out, Long value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 164 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 165 | + BinaryStreamUtils.writeInt64(out, convertToInteger(value)); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + // Float32 |
| 170 | + public static void writeFloat32(OutputStream out, Float value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 171 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 172 | + BinaryStreamUtils.writeFloat32(out, value); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Float64 |
| 177 | + public static void writeFloat64(OutputStream out, Double value, boolean defaultsSupport, boolean isNullable, ClickHouseDataType dataType, boolean hasDefault, String column) throws IOException { |
| 178 | + if (writeValuePreamble(out, defaultsSupport, isNullable, dataType, hasDefault, column, value)) { |
| 179 | + BinaryStreamUtils.writeFloat64(out, value); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments