This is an automated email from the ASF dual-hosted git repository. rong pushed a commit to branch convert-on-type-mismatch in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 8e2666a4a56bb965d59f719a36e4d038554bacb2 Author: Steve Yurong Su <[email protected]> AuthorDate: Tue Aug 6 19:15:28 2024 +0800 converter --- .../db/pipe/receiver/converter/ArrayConverter.java | 951 +++++++++++++++++++++ .../db/pipe/receiver/converter/ValueConverter.java | 492 +++++++++++ ...peStatementDataTypeConvertExecutionVisitor.java | 138 ++- .../apache/iotdb/db/utils/TypeInferenceUtils.java | 2 +- 4 files changed, 1492 insertions(+), 91 deletions(-) diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/converter/ArrayConverter.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/converter/ArrayConverter.java new file mode 100644 index 00000000000..0afcb216162 --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/converter/ArrayConverter.java @@ -0,0 +1,951 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.pipe.receiver.converter; + +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.utils.Binary; + +public class ArrayConverter { + + @FunctionalInterface + private interface Converter { + Object convert( + final TSDataType sourceDataType, + final TSDataType targetDataType, + final Object sourceValues); + } + + // BOOLEAN((byte)0), + // INT32((byte)1), + // INT64((byte)2), + // FLOAT((byte)3), + // DOUBLE((byte)4), + // TEXT((byte)5), + // VECTOR((byte)6), + // UNKNOWN((byte)7), + // TIMESTAMP((byte)8), + // DATE((byte)9), + // BLOB((byte)10), + // STRING((byte)11); + + private static final Converter[][] CONVERTER = + new Converter[TSDataType.values().length][TSDataType.values().length]; + + private static final Converter NO_CHANGE_CONVERTER = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + + static { + for (final TSDataType sourceDataType : TSDataType.values()) { + for (final TSDataType targetDataType : TSDataType.values()) { + CONVERTER[sourceDataType.ordinal()][targetDataType.ordinal()] = NO_CHANGE_CONVERTER; + } + } + + // BOOLEAN + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final boolean[] boolValues = (boolean[]) sourceValues; + final int[] intValues = new int[boolValues.length]; + for (int i = 0; i < boolValues.length; i++) { + intValues[i] = ValueConverter.convertBooleanToInt32(boolValues[i]); + } + return intValues; + }; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final boolean[] boolValues = (boolean[]) sourceValues; + final long[] longValues = new long[boolValues.length]; + for (int i = 0; i < boolValues.length; i++) { + longValues[i] = ValueConverter.convertBooleanToInt64(boolValues[i]); + } + return longValues; + }; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final boolean[] boolValues = (boolean[]) sourceValues; + final float[] floatValues = new float[boolValues.length]; + for (int i = 0; i < boolValues.length; i++) { + floatValues[i] = ValueConverter.convertBooleanToFloat(boolValues[i]); + } + return floatValues; + }; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final boolean[] boolValues = (boolean[]) sourceValues; + final double[] doubleValues = new double[boolValues.length]; + for (int i = 0; i < boolValues.length; i++) { + doubleValues[i] = ValueConverter.convertBooleanToDouble(boolValues[i]); + } + return doubleValues; + }; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final boolean[] boolValues = (boolean[]) sourceValues; + final Binary[] textValues = new Binary[boolValues.length]; + for (int i = 0; i < boolValues.length; i++) { + textValues[i] = ValueConverter.convertBooleanToText(boolValues[i]); + } + return textValues; + }; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final boolean[] boolValues = (boolean[]) sourceValues; + final long[] timestampValues = new long[boolValues.length]; + for (int i = 0; i < boolValues.length; i++) { + timestampValues[i] = ValueConverter.convertBooleanToTimestamp(boolValues[i]); + } + return timestampValues; + }; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final boolean[] boolValues = (boolean[]) sourceValues; + final int[] dateValues = new int[boolValues.length]; + for (int i = 0; i < boolValues.length; i++) { + dateValues[i] = ValueConverter.convertBooleanToDate(boolValues[i]); + } + return dateValues; + }; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final boolean[] boolValues = (boolean[]) sourceValues; + final Binary[] blobValues = new Binary[boolValues.length]; + for (int i = 0; i < boolValues.length; i++) { + blobValues[i] = ValueConverter.convertBooleanToBlob(boolValues[i]); + } + return blobValues; + }; + CONVERTER[TSDataType.BOOLEAN.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final boolean[] boolValues = (boolean[]) sourceValues; + final Binary[] stringValues = new Binary[boolValues.length]; + for (int i = 0; i < boolValues.length; i++) { + stringValues[i] = ValueConverter.convertBooleanToString(boolValues[i]); + } + return stringValues; + }; + + // INT32 + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] intValues = (int[]) sourceValues; + final boolean[] boolValues = new boolean[intValues.length]; + for (int i = 0; i < intValues.length; i++) { + boolValues[i] = ValueConverter.convertInt32ToBoolean(intValues[i]); + } + return boolValues; + }; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] intValues = (int[]) sourceValues; + final long[] longValues = new long[intValues.length]; + for (int i = 0; i < intValues.length; i++) { + longValues[i] = ValueConverter.convertInt32ToInt64(intValues[i]); + } + return longValues; + }; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] intValues = (int[]) sourceValues; + final float[] floatValues = new float[intValues.length]; + for (int i = 0; i < intValues.length; i++) { + floatValues[i] = ValueConverter.convertInt32ToFloat(intValues[i]); + } + return floatValues; + }; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] intValues = (int[]) sourceValues; + final double[] doubleValues = new double[intValues.length]; + for (int i = 0; i < intValues.length; i++) { + doubleValues[i] = ValueConverter.convertInt32ToDouble(intValues[i]); + } + return doubleValues; + }; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] intValues = (int[]) sourceValues; + final Binary[] textValues = new Binary[intValues.length]; + for (int i = 0; i < intValues.length; i++) { + textValues[i] = ValueConverter.convertInt32ToText(intValues[i]); + } + return textValues; + }; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] intValues = (int[]) sourceValues; + final long[] timestampValues = new long[intValues.length]; + for (int i = 0; i < intValues.length; i++) { + timestampValues[i] = ValueConverter.convertInt32ToTimestamp(intValues[i]); + } + return timestampValues; + }; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] intValues = (int[]) sourceValues; + final int[] dateValues = new int[intValues.length]; + for (int i = 0; i < intValues.length; i++) { + dateValues[i] = ValueConverter.convertInt32ToDate(intValues[i]); + } + return dateValues; + }; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] intValues = (int[]) sourceValues; + final Binary[] blobValues = new Binary[intValues.length]; + for (int i = 0; i < intValues.length; i++) { + blobValues[i] = ValueConverter.convertInt32ToBlob(intValues[i]); + } + return blobValues; + }; + CONVERTER[TSDataType.INT32.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] intValues = (int[]) sourceValues; + final Binary[] stringValues = new Binary[intValues.length]; + for (int i = 0; i < intValues.length; i++) { + stringValues[i] = ValueConverter.convertInt32ToString(intValues[i]); + } + return stringValues; + }; + + // INT64 + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] longValues = (long[]) sourceValues; + final boolean[] boolValues = new boolean[longValues.length]; + for (int i = 0; i < longValues.length; i++) { + boolValues[i] = ValueConverter.convertInt64ToBoolean(longValues[i]); + } + return boolValues; + }; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] longValues = (long[]) sourceValues; + final int[] intValues = new int[longValues.length]; + for (int i = 0; i < longValues.length; i++) { + intValues[i] = ValueConverter.convertInt64ToInt32(longValues[i]); + } + return intValues; + }; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] longValues = (long[]) sourceValues; + final float[] floatValues = new float[longValues.length]; + for (int i = 0; i < longValues.length; i++) { + floatValues[i] = ValueConverter.convertInt64ToFloat(longValues[i]); + } + return floatValues; + }; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] longValues = (long[]) sourceValues; + final double[] doubleValues = new double[longValues.length]; + for (int i = 0; i < longValues.length; i++) { + doubleValues[i] = ValueConverter.convertInt64ToDouble(longValues[i]); + } + return doubleValues; + }; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] longValues = (long[]) sourceValues; + final Binary[] textValues = new Binary[longValues.length]; + for (int i = 0; i < longValues.length; i++) { + textValues[i] = ValueConverter.convertInt64ToText(longValues[i]); + } + return textValues; + }; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] longValues = (long[]) sourceValues; + final long[] timestampValues = new long[longValues.length]; + for (int i = 0; i < longValues.length; i++) { + timestampValues[i] = ValueConverter.convertInt64ToTimestamp(longValues[i]); + } + return timestampValues; + }; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] longValues = (long[]) sourceValues; + final int[] dateValues = new int[longValues.length]; + for (int i = 0; i < longValues.length; i++) { + dateValues[i] = ValueConverter.convertInt64ToDate(longValues[i]); + } + return dateValues; + }; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] longValues = (long[]) sourceValues; + final Binary[] blobValues = new Binary[longValues.length]; + for (int i = 0; i < longValues.length; i++) { + blobValues[i] = ValueConverter.convertInt64ToBlob(longValues[i]); + } + return blobValues; + }; + CONVERTER[TSDataType.INT64.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] longValues = (long[]) sourceValues; + final Binary[] stringValues = new Binary[longValues.length]; + for (int i = 0; i < longValues.length; i++) { + stringValues[i] = ValueConverter.convertInt64ToString(longValues[i]); + } + return stringValues; + }; + + // FLOAT + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final float[] floatValues = (float[]) sourceValues; + final boolean[] boolValues = new boolean[floatValues.length]; + for (int i = 0; i < floatValues.length; i++) { + boolValues[i] = ValueConverter.convertFloatToBoolean(floatValues[i]); + } + return boolValues; + }; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final float[] floatValues = (float[]) sourceValues; + final int[] intValues = new int[floatValues.length]; + for (int i = 0; i < floatValues.length; i++) { + intValues[i] = ValueConverter.convertFloatToInt32(floatValues[i]); + } + return intValues; + }; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final float[] floatValues = (float[]) sourceValues; + final long[] longValues = new long[floatValues.length]; + for (int i = 0; i < floatValues.length; i++) { + longValues[i] = ValueConverter.convertFloatToInt64(floatValues[i]); + } + return longValues; + }; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final float[] floatValues = (float[]) sourceValues; + final double[] doubleValues = new double[floatValues.length]; + for (int i = 0; i < floatValues.length; i++) { + doubleValues[i] = ValueConverter.convertFloatToDouble(floatValues[i]); + } + return doubleValues; + }; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final float[] floatValues = (float[]) sourceValues; + final Binary[] textValues = new Binary[floatValues.length]; + for (int i = 0; i < floatValues.length; i++) { + textValues[i] = ValueConverter.convertFloatToText(floatValues[i]); + } + return textValues; + }; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final float[] floatValues = (float[]) sourceValues; + final long[] timestampValues = new long[floatValues.length]; + for (int i = 0; i < floatValues.length; i++) { + timestampValues[i] = ValueConverter.convertFloatToTimestamp(floatValues[i]); + } + return timestampValues; + }; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final float[] floatValues = (float[]) sourceValues; + final int[] dateValues = new int[floatValues.length]; + for (int i = 0; i < floatValues.length; i++) { + dateValues[i] = ValueConverter.convertFloatToDate(floatValues[i]); + } + return dateValues; + }; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final float[] floatValues = (float[]) sourceValues; + final Binary[] blobValues = new Binary[floatValues.length]; + for (int i = 0; i < floatValues.length; i++) { + blobValues[i] = ValueConverter.convertFloatToBlob(floatValues[i]); + } + return blobValues; + }; + CONVERTER[TSDataType.FLOAT.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final float[] floatValues = (float[]) sourceValues; + final Binary[] stringValues = new Binary[floatValues.length]; + for (int i = 0; i < floatValues.length; i++) { + stringValues[i] = ValueConverter.convertFloatToString(floatValues[i]); + } + return stringValues; + }; + + // DOUBLE + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final double[] doubleValues = (double[]) sourceValues; + final boolean[] boolValues = new boolean[doubleValues.length]; + for (int i = 0; i < doubleValues.length; i++) { + boolValues[i] = ValueConverter.convertDoubleToBoolean(doubleValues[i]); + } + return boolValues; + }; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final double[] doubleValues = (double[]) sourceValues; + final int[] intValues = new int[doubleValues.length]; + for (int i = 0; i < doubleValues.length; i++) { + intValues[i] = ValueConverter.convertDoubleToInt32(doubleValues[i]); + } + return intValues; + }; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final double[] doubleValues = (double[]) sourceValues; + final long[] longValues = new long[doubleValues.length]; + for (int i = 0; i < doubleValues.length; i++) { + longValues[i] = ValueConverter.convertDoubleToInt64(doubleValues[i]); + } + return longValues; + }; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final double[] doubleValues = (double[]) sourceValues; + final float[] floatValues = new float[doubleValues.length]; + for (int i = 0; i < doubleValues.length; i++) { + floatValues[i] = ValueConverter.convertDoubleToFloat(doubleValues[i]); + } + return floatValues; + }; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final double[] doubleValues = (double[]) sourceValues; + final Binary[] textValues = new Binary[doubleValues.length]; + for (int i = 0; i < doubleValues.length; i++) { + textValues[i] = ValueConverter.convertDoubleToText(doubleValues[i]); + } + return textValues; + }; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final double[] doubleValues = (double[]) sourceValues; + final long[] timestampValues = new long[doubleValues.length]; + for (int i = 0; i < doubleValues.length; i++) { + timestampValues[i] = ValueConverter.convertDoubleToTimestamp(doubleValues[i]); + } + return timestampValues; + }; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final double[] doubleValues = (double[]) sourceValues; + final int[] dateValues = new int[doubleValues.length]; + for (int i = 0; i < doubleValues.length; i++) { + dateValues[i] = ValueConverter.convertDoubleToDate(doubleValues[i]); + } + return dateValues; + }; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final double[] doubleValues = (double[]) sourceValues; + final Binary[] blobValues = new Binary[doubleValues.length]; + for (int i = 0; i < doubleValues.length; i++) { + blobValues[i] = ValueConverter.convertDoubleToBlob(doubleValues[i]); + } + return blobValues; + }; + CONVERTER[TSDataType.DOUBLE.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final double[] doubleValues = (double[]) sourceValues; + final Binary[] stringValues = new Binary[doubleValues.length]; + for (int i = 0; i < doubleValues.length; i++) { + stringValues[i] = ValueConverter.convertDoubleToString(doubleValues[i]); + } + return stringValues; + }; + + // TEXT + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] textValues = (Binary[]) sourceValues; + final boolean[] boolValues = new boolean[textValues.length]; + for (int i = 0; i < textValues.length; i++) { + boolValues[i] = ValueConverter.convertTextToBoolean(textValues[i]); + } + return boolValues; + }; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] textValues = (Binary[]) sourceValues; + final int[] intValues = new int[textValues.length]; + for (int i = 0; i < textValues.length; i++) { + intValues[i] = ValueConverter.convertTextToInt32(textValues[i]); + } + return intValues; + }; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] textValues = (Binary[]) sourceValues; + final long[] longValues = new long[textValues.length]; + for (int i = 0; i < textValues.length; i++) { + longValues[i] = ValueConverter.convertTextToInt64(textValues[i]); + } + return longValues; + }; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] textValues = (Binary[]) sourceValues; + final float[] floatValues = new float[textValues.length]; + for (int i = 0; i < textValues.length; i++) { + floatValues[i] = ValueConverter.convertTextToFloat(textValues[i]); + } + return floatValues; + }; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] textValues = (Binary[]) sourceValues; + final double[] doubleValues = new double[textValues.length]; + for (int i = 0; i < textValues.length; i++) { + doubleValues[i] = ValueConverter.convertTextToDouble(textValues[i]); + } + return doubleValues; + }; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] textValues = (Binary[]) sourceValues; + final long[] timestampValues = new long[textValues.length]; + for (int i = 0; i < textValues.length; i++) { + timestampValues[i] = ValueConverter.convertTextToTimestamp(textValues[i]); + } + return timestampValues; + }; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] textValues = (Binary[]) sourceValues; + final int[] dateValues = new int[textValues.length]; + for (int i = 0; i < textValues.length; i++) { + dateValues[i] = ValueConverter.convertTextToDate(textValues[i]); + } + return dateValues; + }; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] textValues = (Binary[]) sourceValues; + final Binary[] blobValues = new Binary[textValues.length]; + for (int i = 0; i < textValues.length; i++) { + blobValues[i] = ValueConverter.convertTextToBlob(textValues[i]); + } + return blobValues; + }; + CONVERTER[TSDataType.TEXT.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] textValues = (Binary[]) sourceValues; + final Binary[] stringValues = new Binary[textValues.length]; + for (int i = 0; i < textValues.length; i++) { + stringValues[i] = ValueConverter.convertTextToString(textValues[i]); + } + return stringValues; + }; + + // VECTOR + for (int i = 0; i < TSDataType.values().length; i++) { + CONVERTER[TSDataType.VECTOR.ordinal()][i] = NO_CHANGE_CONVERTER; + } + + // UNKNOWN + for (int i = 0; i < TSDataType.values().length; i++) { + CONVERTER[TSDataType.UNKNOWN.ordinal()][i] = NO_CHANGE_CONVERTER; + } + + // TIMESTAMP + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] timestampValues = (long[]) sourceValues; + final boolean[] boolValues = new boolean[timestampValues.length]; + for (int i = 0; i < timestampValues.length; i++) { + boolValues[i] = ValueConverter.convertTimestampToBoolean(timestampValues[i]); + } + return boolValues; + }; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] timestampValues = (long[]) sourceValues; + final int[] intValues = new int[timestampValues.length]; + for (int i = 0; i < timestampValues.length; i++) { + intValues[i] = ValueConverter.convertTimestampToInt32(timestampValues[i]); + } + return intValues; + }; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] timestampValues = (long[]) sourceValues; + final long[] longValues = new long[timestampValues.length]; + for (int i = 0; i < timestampValues.length; i++) { + longValues[i] = ValueConverter.convertTimestampToInt64(timestampValues[i]); + } + return longValues; + }; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] timestampValues = (long[]) sourceValues; + final float[] floatValues = new float[timestampValues.length]; + for (int i = 0; i < timestampValues.length; i++) { + floatValues[i] = ValueConverter.convertTimestampToFloat(timestampValues[i]); + } + return floatValues; + }; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] timestampValues = (long[]) sourceValues; + final double[] doubleValues = new double[timestampValues.length]; + for (int i = 0; i < timestampValues.length; i++) { + doubleValues[i] = ValueConverter.convertTimestampToDouble(timestampValues[i]); + } + return doubleValues; + }; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] timestampValues = (long[]) sourceValues; + final Binary[] textValues = new Binary[timestampValues.length]; + for (int i = 0; i < timestampValues.length; i++) { + textValues[i] = ValueConverter.convertTimestampToText(timestampValues[i]); + } + return textValues; + }; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] timestampValues = (long[]) sourceValues; + final int[] dateValues = new int[timestampValues.length]; + for (int i = 0; i < timestampValues.length; i++) { + dateValues[i] = ValueConverter.convertTimestampToDate(timestampValues[i]); + } + return dateValues; + }; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] timestampValues = (long[]) sourceValues; + final Binary[] blobValues = new Binary[timestampValues.length]; + for (int i = 0; i < timestampValues.length; i++) { + blobValues[i] = ValueConverter.convertTimestampToBlob(timestampValues[i]); + } + return blobValues; + }; + CONVERTER[TSDataType.TIMESTAMP.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final long[] timestampValues = (long[]) sourceValues; + final Binary[] stringValues = new Binary[timestampValues.length]; + for (int i = 0; i < timestampValues.length; i++) { + stringValues[i] = ValueConverter.convertTimestampToString(timestampValues[i]); + } + return stringValues; + }; + + // DATE + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] dateValues = (int[]) sourceValues; + final boolean[] boolValues = new boolean[dateValues.length]; + for (int i = 0; i < dateValues.length; i++) { + boolValues[i] = ValueConverter.convertDateToBoolean(dateValues[i]); + } + return boolValues; + }; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] dateValues = (int[]) sourceValues; + final int[] intValues = new int[dateValues.length]; + for (int i = 0; i < dateValues.length; i++) { + intValues[i] = ValueConverter.convertDateToInt32(dateValues[i]); + } + return intValues; + }; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] dateValues = (int[]) sourceValues; + final long[] longValues = new long[dateValues.length]; + for (int i = 0; i < dateValues.length; i++) { + longValues[i] = ValueConverter.convertDateToInt64(dateValues[i]); + } + return longValues; + }; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] dateValues = (int[]) sourceValues; + final float[] floatValues = new float[dateValues.length]; + for (int i = 0; i < dateValues.length; i++) { + floatValues[i] = ValueConverter.convertDateToFloat(dateValues[i]); + } + return floatValues; + }; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] dateValues = (int[]) sourceValues; + final double[] doubleValues = new double[dateValues.length]; + for (int i = 0; i < dateValues.length; i++) { + doubleValues[i] = ValueConverter.convertDateToDouble(dateValues[i]); + } + return doubleValues; + }; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] dateValues = (int[]) sourceValues; + final Binary[] textValues = new Binary[dateValues.length]; + for (int i = 0; i < dateValues.length; i++) { + textValues[i] = ValueConverter.convertDateToText(dateValues[i]); + } + return textValues; + }; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] dateValues = (int[]) sourceValues; + final long[] timestampValues = new long[dateValues.length]; + for (int i = 0; i < dateValues.length; i++) { + timestampValues[i] = ValueConverter.convertDateToTimestamp(dateValues[i]); + } + return timestampValues; + }; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] dateValues = (int[]) sourceValues; + final Binary[] blobValues = new Binary[dateValues.length]; + for (int i = 0; i < dateValues.length; i++) { + blobValues[i] = ValueConverter.convertDateToBlob(dateValues[i]); + } + return blobValues; + }; + CONVERTER[TSDataType.DATE.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final int[] dateValues = (int[]) sourceValues; + final Binary[] stringValues = new Binary[dateValues.length]; + for (int i = 0; i < dateValues.length; i++) { + stringValues[i] = ValueConverter.convertDateToString(dateValues[i]); + } + return stringValues; + }; + + // BLOB + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] blobValues = (Binary[]) sourceValues; + final boolean[] boolValues = new boolean[blobValues.length]; + for (int i = 0; i < blobValues.length; i++) { + boolValues[i] = ValueConverter.convertBlobToBoolean(blobValues[i]); + } + return boolValues; + }; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] blobValues = (Binary[]) sourceValues; + final int[] intValues = new int[blobValues.length]; + for (int i = 0; i < blobValues.length; i++) { + intValues[i] = ValueConverter.convertBlobToInt32(blobValues[i]); + } + return intValues; + }; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] blobValues = (Binary[]) sourceValues; + final long[] longValues = new long[blobValues.length]; + for (int i = 0; i < blobValues.length; i++) { + longValues[i] = ValueConverter.convertBlobToInt64(blobValues[i]); + } + return longValues; + }; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] blobValues = (Binary[]) sourceValues; + final float[] floatValues = new float[blobValues.length]; + for (int i = 0; i < blobValues.length; i++) { + floatValues[i] = ValueConverter.convertBlobToFloat(blobValues[i]); + } + return floatValues; + }; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] blobValues = (Binary[]) sourceValues; + final double[] doubleValues = new double[blobValues.length]; + for (int i = 0; i < blobValues.length; i++) { + doubleValues[i] = ValueConverter.convertBlobToDouble(blobValues[i]); + } + return doubleValues; + }; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] blobValues = (Binary[]) sourceValues; + final Binary[] textValues = new Binary[blobValues.length]; + for (int i = 0; i < blobValues.length; i++) { + textValues[i] = ValueConverter.convertBlobToText(blobValues[i]); + } + return textValues; + }; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] blobValues = (Binary[]) sourceValues; + final long[] timestampValues = new long[blobValues.length]; + for (int i = 0; i < blobValues.length; i++) { + timestampValues[i] = ValueConverter.convertBlobToTimestamp(blobValues[i]); + } + return timestampValues; + }; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] blobValues = (Binary[]) sourceValues; + final int[] dateValues = new int[blobValues.length]; + for (int i = 0; i < blobValues.length; i++) { + dateValues[i] = ValueConverter.convertBlobToDate(blobValues[i]); + } + return dateValues; + }; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + CONVERTER[TSDataType.BLOB.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] blobValues = (Binary[]) sourceValues; + final Binary[] stringValues = new Binary[blobValues.length]; + for (int i = 0; i < blobValues.length; i++) { + stringValues[i] = ValueConverter.convertBlobToString(blobValues[i]); + } + return stringValues; + }; + + // STRING + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.BOOLEAN.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] stringValues = (Binary[]) sourceValues; + final boolean[] boolValues = new boolean[stringValues.length]; + for (int i = 0; i < stringValues.length; i++) { + boolValues[i] = ValueConverter.convertStringToBoolean(stringValues[i]); + } + return boolValues; + }; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.INT32.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] stringValues = (Binary[]) sourceValues; + final int[] intValues = new int[stringValues.length]; + for (int i = 0; i < stringValues.length; i++) { + intValues[i] = ValueConverter.convertStringToInt32(stringValues[i]); + } + return intValues; + }; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.INT64.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] stringValues = (Binary[]) sourceValues; + final long[] longValues = new long[stringValues.length]; + for (int i = 0; i < stringValues.length; i++) { + longValues[i] = ValueConverter.convertStringToInt64(stringValues[i]); + } + return longValues; + }; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.FLOAT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] stringValues = (Binary[]) sourceValues; + final float[] floatValues = new float[stringValues.length]; + for (int i = 0; i < stringValues.length; i++) { + floatValues[i] = ValueConverter.convertStringToFloat(stringValues[i]); + } + return floatValues; + }; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.DOUBLE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] stringValues = (Binary[]) sourceValues; + final double[] doubleValues = new double[stringValues.length]; + for (int i = 0; i < stringValues.length; i++) { + doubleValues[i] = ValueConverter.convertStringToDouble(stringValues[i]); + } + return doubleValues; + }; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.TEXT.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] stringValues = (Binary[]) sourceValues; + final Binary[] textValues = new Binary[stringValues.length]; + for (int i = 0; i < stringValues.length; i++) { + textValues[i] = ValueConverter.convertStringToText(stringValues[i]); + } + return textValues; + }; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.VECTOR.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.UNKNOWN.ordinal()] = NO_CHANGE_CONVERTER; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.TIMESTAMP.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] stringValues = (Binary[]) sourceValues; + final long[] timestampValues = new long[stringValues.length]; + for (int i = 0; i < stringValues.length; i++) { + timestampValues[i] = ValueConverter.convertStringToTimestamp(stringValues[i]); + } + return timestampValues; + }; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.DATE.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] stringValues = (Binary[]) sourceValues; + final int[] dateValues = new int[stringValues.length]; + for (int i = 0; i < stringValues.length; i++) { + dateValues[i] = ValueConverter.convertStringToDate(stringValues[i]); + } + return dateValues; + }; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.BLOB.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> { + final Binary[] stringValues = (Binary[]) sourceValues; + final Binary[] blobValues = new Binary[stringValues.length]; + for (int i = 0; i < stringValues.length; i++) { + blobValues[i] = ValueConverter.convertStringToBlob(stringValues[i]); + } + return blobValues; + }; + CONVERTER[TSDataType.STRING.ordinal()][TSDataType.STRING.ordinal()] = + (sourceDataType, targetDataType, sourceValues) -> sourceValues; + } + + public static Object convert( + final TSDataType sourceDataType, final TSDataType targetDataType, final Object sourceValues) { + return CONVERTER[sourceDataType.ordinal()][targetDataType.ordinal()].convert( + sourceDataType, targetDataType, sourceValues); + } + + private ArrayConverter() { + // forbidden to construct + } +} diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/converter/ValueConverter.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/converter/ValueConverter.java new file mode 100644 index 00000000000..cd448e6543e --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/converter/ValueConverter.java @@ -0,0 +1,492 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.pipe.receiver.converter; + +import org.apache.iotdb.db.utils.DateTimeUtils; +import org.apache.iotdb.db.utils.TypeInferenceUtils; + +import org.apache.commons.lang3.StringUtils; +import org.apache.tsfile.common.conf.TSFileConfig; +import org.apache.tsfile.utils.Binary; + +import java.time.ZoneId; + +public class ValueConverter { + + ////////////// BOOLEAN ////////////// + + private static final Binary BINARY_TRUE = parseString(Boolean.TRUE.toString()); + private static final Binary BINARY_FALSE = parseString(Boolean.FALSE.toString()); + + public static int convertBooleanToInt32(final boolean value) { + return value ? 1 : 0; + } + + public static long convertBooleanToInt64(final boolean value) { + return value ? 1L : 0L; + } + + public static float convertBooleanToFloat(final boolean value) { + return value ? 1.0f : 0.0f; + } + + public static double convertBooleanToDouble(final boolean value) { + return value ? 1.0 : 0.0; + } + + public static Binary convertBooleanToText(final boolean value) { + return value ? BINARY_TRUE : BINARY_FALSE; + } + + public static long convertBooleanToTimestamp(final boolean value) { + return value ? 1L : 0L; + } + + public static int convertBooleanToDate(final boolean value) { + return value ? 1 : 0; + } + + public static Binary convertBooleanToBlob(final boolean value) { + return value ? BINARY_TRUE : BINARY_FALSE; + } + + public static Binary convertBooleanToString(final boolean value) { + return value ? BINARY_TRUE : BINARY_FALSE; + } + + ///////////// INT32 ////////////// + + public static boolean convertInt32ToBoolean(final int value) { + return value != 0; + } + + public static long convertInt32ToInt64(final int value) { + return value; + } + + public static float convertInt32ToFloat(final int value) { + return value; + } + + public static double convertInt32ToDouble(final int value) { + return value; + } + + public static Binary convertInt32ToText(final int value) { + return parseText(Integer.toString(value)); + } + + public static long convertInt32ToTimestamp(final int value) { + return value; + } + + public static int convertInt32ToDate(final int value) { + return value; + } + + public static Binary convertInt32ToBlob(final int value) { + return parseBlob(Integer.toString(value)); + } + + public static Binary convertInt32ToString(final int value) { + return parseString(Integer.toString(value)); + } + + ///////////// INT64 ////////////// + + public static boolean convertInt64ToBoolean(final long value) { + return value != 0; + } + + public static int convertInt64ToInt32(final long value) { + return (int) value; + } + + public static float convertInt64ToFloat(final long value) { + return value; + } + + public static double convertInt64ToDouble(final long value) { + return value; + } + + public static Binary convertInt64ToText(final long value) { + return parseText(Long.toString(value)); + } + + public static long convertInt64ToTimestamp(final long value) { + return value; + } + + public static int convertInt64ToDate(final long value) { + return (int) value; + } + + public static Binary convertInt64ToBlob(final long value) { + return parseBlob(Long.toString(value)); + } + + public static Binary convertInt64ToString(final long value) { + return parseString(Long.toString(value)); + } + + ///////////// FLOAT ////////////// + + public static boolean convertFloatToBoolean(final float value) { + return value != 0; + } + + public static int convertFloatToInt32(final float value) { + return (int) value; + } + + public static long convertFloatToInt64(final float value) { + return (long) value; + } + + public static double convertFloatToDouble(final float value) { + return value; + } + + public static Binary convertFloatToText(final float value) { + return parseText(Float.toString(value)); + } + + public static long convertFloatToTimestamp(final float value) { + return (long) value; + } + + public static int convertFloatToDate(final float value) { + return (int) value; + } + + public static Binary convertFloatToBlob(final float value) { + return parseBlob(Float.toString(value)); + } + + public static Binary convertFloatToString(final float value) { + return parseString(Float.toString(value)); + } + + ///////////// DOUBLE ////////////// + + public static boolean convertDoubleToBoolean(final double value) { + return value != 0; + } + + public static int convertDoubleToInt32(final double value) { + return (int) value; + } + + public static long convertDoubleToInt64(final double value) { + return (long) value; + } + + public static float convertDoubleToFloat(final double value) { + return (float) value; + } + + public static Binary convertDoubleToText(final double value) { + return parseText(Double.toString(value)); + } + + public static long convertDoubleToTimestamp(final double value) { + return (long) value; + } + + public static int convertDoubleToDate(final double value) { + return (int) value; + } + + public static Binary convertDoubleToBlob(final double value) { + return parseBlob(Double.toString(value)); + } + + public static Binary convertDoubleToString(final double value) { + return parseString(Double.toString(value)); + } + + ///////////// TEXT ////////////// + + public static boolean convertTextToBoolean(final Binary value) { + return Boolean.parseBoolean(value.toString()); + } + + public static int convertTextToInt32(final Binary value) { + return parseInteger(value.toString()); + } + + public static long convertTextToInt64(final Binary value) { + return parseLong(value.toString()); + } + + public static float convertTextToFloat(final Binary value) { + return parseFloat(value.toString()); + } + + public static double convertTextToDouble(final Binary value) { + return parseDouble(value.toString()); + } + + public static long convertTextToTimestamp(final Binary value) { + return parseTimestamp(value.toString()); + } + + public static int convertTextToDate(final Binary value) { + return parseDate(value.toString()); + } + + public static Binary convertTextToBlob(final Binary value) { + return parseBlob(value.toString()); + } + + public static Binary convertTextToString(final Binary value) { + return value; + } + + ///////////// TIMESTAMP ////////////// + + public static boolean convertTimestampToBoolean(final long value) { + return value != 0; + } + + public static int convertTimestampToInt32(final long value) { + return (int) value; + } + + public static long convertTimestampToInt64(final long value) { + return value; + } + + public static float convertTimestampToFloat(final long value) { + return value; + } + + public static double convertTimestampToDouble(final long value) { + return value; + } + + public static Binary convertTimestampToText(final long value) { + return parseText(Long.toString(value)); + } + + public static int convertTimestampToDate(final long value) { + return (int) value; + } + + public static Binary convertTimestampToBlob(final long value) { + return parseBlob(Long.toString(value)); + } + + public static Binary convertTimestampToString(final long value) { + return parseString(Long.toString(value)); + } + + ///////////// DATE ////////////// + + public static boolean convertDateToBoolean(final int value) { + return value != 0; + } + + public static int convertDateToInt32(final int value) { + return value; + } + + public static long convertDateToInt64(final int value) { + return value; + } + + public static float convertDateToFloat(final int value) { + return value; + } + + public static double convertDateToDouble(final int value) { + return value; + } + + public static Binary convertDateToText(final int value) { + return parseText(Integer.toString(value)); + } + + public static long convertDateToTimestamp(final int value) { + return value; + } + + public static Binary convertDateToBlob(final int value) { + return parseBlob(Integer.toString(value)); + } + + public static Binary convertDateToString(final int value) { + return parseString(Integer.toString(value)); + } + + ///////////// BLOB ////////////// + + public static boolean convertBlobToBoolean(final Binary value) { + return Boolean.parseBoolean(value.toString()); + } + + public static int convertBlobToInt32(final Binary value) { + return parseInteger(value.toString()); + } + + public static long convertBlobToInt64(final Binary value) { + return parseLong(value.toString()); + } + + public static float convertBlobToFloat(final Binary value) { + return parseFloat(value.toString()); + } + + public static double convertBlobToDouble(final Binary value) { + return parseDouble(value.toString()); + } + + public static long convertBlobToTimestamp(final Binary value) { + return parseTimestamp(value.toString()); + } + + public static int convertBlobToDate(final Binary value) { + return parseDate(value.toString()); + } + + public static Binary convertBlobToString(final Binary value) { + return value; + } + + public static Binary convertBlobToText(final Binary value) { + return value; + } + + ///////////// STRING ////////////// + + public static boolean convertStringToBoolean(final Binary value) { + return Boolean.parseBoolean(value.toString()); + } + + public static int convertStringToInt32(final Binary value) { + return parseInteger(value.toString()); + } + + public static long convertStringToInt64(final Binary value) { + return parseLong(value.toString()); + } + + public static float convertStringToFloat(final Binary value) { + return parseFloat(value.toString()); + } + + public static double convertStringToDouble(final Binary value) { + return parseDouble(value.toString()); + } + + public static long convertStringToTimestamp(final Binary value) { + return parseTimestamp(value.toString()); + } + + public static int convertStringToDate(final Binary value) { + return parseDate(value.toString()); + } + + public static Binary convertStringToBlob(final Binary value) { + return parseBlob(value.toString()); + } + + public static Binary convertStringToText(final Binary value) { + return value; + } + + ///////////// UTILS ////////////// + + private static Binary parseBlob(final String value) { + return new Binary(value, TSFileConfig.STRING_CHARSET); + } + + private static int parseInteger(final String value) { + try { + return Integer.parseInt(value); + } catch (Exception e) { + return 0; + } + } + + private static long parseLong(final String value) { + try { + return Long.parseLong(value); + } catch (Exception e) { + return 0L; + } + } + + private static float parseFloat(final String value) { + try { + return Float.parseFloat(value); + } catch (Exception e) { + return 0.0f; + } + } + + private static double parseDouble(final String value) { + try { + return Double.parseDouble(value); + } catch (Exception e) { + return 0.0d; + } + } + + private static long parseTimestamp(final String value) { + if (value == null || value.isEmpty()) { + return 0L; + } + try { + return TypeInferenceUtils.isNumber(value) + ? Long.parseLong(value) + : DateTimeUtils.parseDateTimeExpressionToLong( + StringUtils.trim(value), ZoneId.systemDefault()); + } catch (final Exception e) { + return 0L; + } + } + + private static int parseDate(final String value) { + if (value == null || value.isEmpty()) { + return 0; + } + try { + return TypeInferenceUtils.isNumber(value) + ? Integer.parseInt(value) + : DateTimeUtils.parseDateExpressionToInt(StringUtils.trim(value)); + } catch (final Exception e) { + return 0; + } + } + + private static Binary parseString(final String value) { + return new Binary(value, TSFileConfig.STRING_CHARSET); + } + + private static Binary parseText(final String value) { + return new Binary(value, TSFileConfig.STRING_CHARSET); + } + + private ValueConverter() { + // forbidden to construct + } +} diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/visitor/PipeStatementDataTypeConvertExecutionVisitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/visitor/PipeStatementDataTypeConvertExecutionVisitor.java index 67600ff7b5f..fc6e8339b3d 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/visitor/PipeStatementDataTypeConvertExecutionVisitor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/receiver/visitor/PipeStatementDataTypeConvertExecutionVisitor.java @@ -23,6 +23,13 @@ import org.apache.iotdb.common.rpc.thrift.TSStatus; import org.apache.iotdb.db.queryengine.plan.statement.Statement; import org.apache.iotdb.db.queryengine.plan.statement.StatementNode; import org.apache.iotdb.db.queryengine.plan.statement.StatementVisitor; +import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertMultiTabletsStatement; +import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowStatement; +import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowsOfOneDeviceStatement; +import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertRowsStatement; +import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertStatement; +import org.apache.iotdb.db.queryengine.plan.statement.crud.InsertTabletStatement; +import org.apache.iotdb.db.queryengine.plan.statement.crud.LoadTsFileStatement; import java.util.Optional; import java.util.function.Consumer; @@ -46,94 +53,45 @@ public class PipeStatementDataTypeConvertExecutionVisitor return Optional.empty(); } - // - // @Override - // public TSStatus visitLoadFile( - // final LoadTsFileStatement loadTsFileStatement, final Exception context) { - // if (context instanceof LoadRuntimeOutOfMemoryException) { - // return new TSStatus( - // TSStatusCode.PIPE_RECEIVER_TEMPORARY_UNAVAILABLE_EXCEPTION.getStatusCode()) - // .setMessage(context.getMessage()); - // } else if (context instanceof SemanticException) { - // return new TSStatus(TSStatusCode.PIPE_RECEIVER_USER_CONFLICT_EXCEPTION.getStatusCode()) - // .setMessage(context.getMessage()); - // } - // return visitStatement(loadTsFileStatement, context); - // } - // - // @Override - // public TSStatus visitCreateTimeseries( - // final CreateTimeSeriesStatement statement, final Exception context) { - // return visitGeneralCreateTimeSeries(statement, context); - // } - // - // @Override - // public TSStatus visitCreateAlignedTimeseries( - // final CreateAlignedTimeSeriesStatement statement, final Exception context) { - // return visitGeneralCreateTimeSeries(statement, context); - // } - // - // @Override - // public TSStatus visitCreateMultiTimeseries( - // final CreateMultiTimeSeriesStatement statement, final Exception context) { - // return visitGeneralCreateTimeSeries(statement, context); - // } - // - // @Override - // public TSStatus visitInternalCreateTimeseries( - // final InternalCreateTimeSeriesStatement statement, final Exception context) { - // return visitGeneralCreateTimeSeries(statement, context); - // } - // - // @Override - // public TSStatus visitInternalCreateMultiTimeSeries( - // final InternalCreateMultiTimeSeriesStatement statement, final Exception context) { - // return visitGeneralCreateTimeSeries(statement, context); - // } - // - // private TSStatus visitGeneralCreateTimeSeries( - // final Statement statement, final Exception context) { - // if (context instanceof SemanticException) { - // return new TSStatus(TSStatusCode.PIPE_RECEIVER_USER_CONFLICT_EXCEPTION.getStatusCode()) - // .setMessage(context.getMessage()); - // } else if (isAutoCreateConflict(context)) { - // return new TSStatus(TSStatusCode.PIPE_RECEIVER_USER_CONFLICT_EXCEPTION.getStatusCode()) - // .setMessage(context.getCause().getMessage()); - // } - // return visitStatement(statement, context); - // } - // - // @Override - // public TSStatus visitActivateTemplate( - // final ActivateTemplateStatement activateTemplateStatement, final Exception context) { - // return visitGeneralActivateTemplate(activateTemplateStatement, context); - // } - // - // @Override - // public TSStatus visitBatchActivateTemplate( - // final BatchActivateTemplateStatement batchActivateTemplateStatement, - // final Exception context) { - // return visitGeneralActivateTemplate(batchActivateTemplateStatement, context); - // } - // - // // InternalBatchActivateTemplateNode is converted to BatchActivateTemplateStatement - // // No need to handle InternalBatchActivateTemplateStatement - // - // private TSStatus visitGeneralActivateTemplate( - // final Statement activateTemplateStatement, final Exception context) { - // if (context instanceof MetadataException || context instanceof StatementAnalyzeException) { - // return new TSStatus(TSStatusCode.PIPE_RECEIVER_USER_CONFLICT_EXCEPTION.getStatusCode()) - // .setMessage(context.getMessage()); - // } else if (isAutoCreateConflict(context)) { - // return new TSStatus(TSStatusCode.PIPE_RECEIVER_USER_CONFLICT_EXCEPTION.getStatusCode()) - // .setMessage(context.getCause().getMessage()); - // } - // return visitStatement(activateTemplateStatement, context); - // } - // - // private boolean isAutoCreateConflict(final Exception e) { - // return e instanceof RuntimeException - // && e.getCause() instanceof IoTDBException - // && e.getCause().getMessage().contains("already been created as database"); - // } + @Override + public Optional<TSStatus> visitLoadFile( + final LoadTsFileStatement loadTsFileStatement, final Exception exception) { + return visitStatement(loadTsFileStatement, exception); + } + + @Override + public Optional<TSStatus> visitInsert( + InsertStatement insertStatement, final Exception exception) { + return visitStatement(insertStatement, exception); + } + + @Override + public Optional<TSStatus> visitInsertTablet( + InsertTabletStatement insertTabletStatement, final Exception exception) { + return visitStatement(insertTabletStatement, exception); + } + + @Override + public Optional<TSStatus> visitInsertRow( + InsertRowStatement insertRowStatement, final Exception exception) { + return visitStatement(insertRowStatement, exception); + } + + @Override + public Optional<TSStatus> visitInsertRows( + InsertRowsStatement insertRowsStatement, final Exception exception) { + return visitStatement(insertRowsStatement, exception); + } + + @Override + public Optional<TSStatus> visitInsertMultiTablets( + InsertMultiTabletsStatement insertMultiTabletsStatement, final Exception exception) { + return visitStatement(insertMultiTabletsStatement, exception); + } + + @Override + public Optional<TSStatus> visitInsertRowsOfOneDevice( + InsertRowsOfOneDeviceStatement insertRowsOfOneDeviceStatement, final Exception exception) { + return visitStatement(insertRowsOfOneDeviceStatement, exception); + } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/TypeInferenceUtils.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/TypeInferenceUtils.java index 165ea4b8b0f..6666114a1c2 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/TypeInferenceUtils.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/TypeInferenceUtils.java @@ -49,7 +49,7 @@ public class TypeInferenceUtils { return s.length() >= 3 && s.startsWith("X'") && s.endsWith("'"); } - static boolean isNumber(String s) { + public static boolean isNumber(String s) { if (s == null || s.equals("NaN")) { return false; }
