3 import static java.lang.Math.toIntExact;
5 import java.math.BigDecimal;
7 import java.util.ArrayList;
8 import java.util.Arrays;
12 import ai.heavy.thrift.server.*;
20 throw new SQLException(
"Elements[] cannot be null");
24 Class<?> elements_class = elements.getClass().getComponentType();
42 checkClass(elements_class, java.sql.Time.class);
45 checkClass(elements_class, java.sql.Timestamp.class);
48 checkClass(elements_class, java.sql.Date.class);
69 throw new AssertionError(
type.toString());
80 return HeavyAIType.toJava(
type);
89 public Object
getArray(
long start,
int size)
throws SQLException {
91 return Arrays.copyOfRange(
elements, toIntExact(start), size);
100 public ResultSet
getResultSet(
long start,
int size)
throws SQLException {
103 ArrayList<TColumnType> columnTypes =
new ArrayList<>(2);
105 new TTypeInfo(TDatumType.BIGINT, TEncodingType.NONE,
false,
false, 0, 0, 0);
106 columnTypes.add(
new TColumnType(
"INDEX", idxType,
false,
"",
false,
false, 0));
109 int precision = (
type == TDatumType.TIMESTAMP ||
type == TDatumType.TIME
110 ||
type == TDatumType.DATE
113 TTypeInfo valueType =
114 new TTypeInfo(
type, TEncodingType.NONE,
true,
false, precision, 0, 0);
115 columnTypes.add(
new TColumnType(
"VALUE", valueType,
false,
"",
false,
false, 1));
117 Long[] indexes =
new Long[size];
119 for (
int i = 0; i < size; ++i) {
120 indexes[i] = (long) (i + 1);
122 TColumnData idxData =
new TColumnData(Arrays.asList(indexes), null, null, null);
123 ArrayList<Boolean> idxNulls =
new ArrayList<>(size);
124 for (
int i = 0; i < size; ++i) {
125 idxNulls.add(Boolean.FALSE);
128 TColumnData valuesData;
129 Long[] int_values =
new Long[size];
131 String[] string_values =
new String[size];
132 boolean is_real =
false, is_string =
false;
133 ArrayList<Boolean> valueNulls =
new ArrayList<>(size);
134 for (
int i = toIntExact(start); i < start + size; ++i) {
136 valueNulls.add(
true);
138 valueNulls.add(
false);
141 int_values[i] = ((Byte)
elements[i]).longValue();
144 int_values[i] = ((Short)
elements[i]).longValue();
153 int_values[i] =
elements[i] == Boolean.TRUE ? 1l : 0l;
159 int_values[i] = ((Timestamp)
elements[i]).getTime();
170 real_values[i] = ((BigDecimal)
elements[i]).doubleValue();
180 case MULTILINESTRING:
184 string_values[i] = (String)
elements[i];
187 throw new AssertionError(
type.toString());
192 valuesData =
new TColumnData(null, Arrays.asList(real_values), null, null);
193 }
else if (is_string) {
194 valuesData =
new TColumnData(null, null, Arrays.asList(string_values), null);
196 valuesData =
new TColumnData(Arrays.asList(int_values), null, null, null);
199 ArrayList<TColumn> columns =
new ArrayList<>(2);
200 columns.add(
new TColumn(idxData, idxNulls));
201 columns.add(
new TColumn(valuesData, valueNulls));
202 TRowSet rowSet =
new TRowSet(columnTypes, null, columns,
true);
203 TQueryResult
result =
new TQueryResult(rowSet, 0, 0,
"",
"",
true, TQueryType.READ);
208 public void free() throws SQLException {
222 case MULTILINESTRING:
228 StringBuilder sb =
new StringBuilder(
"{");
233 sb.append(
"NULL").
append(
", ");
236 if (elements.length > 0) {
237 sb.delete(sb.length() - 2, sb.length());
240 return sb.toString();
243 String arr_str = Arrays.toString(
elements);
244 return "{" + arr_str.substring(1, arr_str.length() - 1) +
"}";
251 public ResultSet
getResultSet(
long start,
int size, Map<String, Class<?>> map)
252 throws SQLException {
253 throw new UnsupportedOperationException(
"Not supported yet,"
254 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
255 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
256 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
260 public ResultSet
getResultSet(Map<String, Class<?>> map) throws SQLException {
261 throw new UnsupportedOperationException(
"Not supported yet,"
262 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
263 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
264 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
268 public Object
getArray(
long start,
int size, Map<String, Class<?>> map)
269 throws SQLException {
270 throw new UnsupportedOperationException(
"Not supported yet,"
271 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
272 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
273 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
277 public Object
getArray(Map<String, Class<?>> map) throws SQLException {
278 throw new UnsupportedOperationException(
"Not supported yet,"
279 +
" line:" +
new Throwable().getStackTrace()[0].getLineNumber()
280 +
" class:" +
new Throwable().getStackTrace()[0].getClassName()
281 +
" method:" +
new Throwable().getStackTrace()[0].getMethodName());
284 private void checkSize(
int start,
int size)
throws SQLException {
285 if (start < 0 || start >=
elements.length || start + size >
elements.length) {
286 throw new SQLException(
"Array length = " +
Integer.toString(
elements.length)
287 +
", slice start index = " + Integer.toString(start)
288 +
", slice length = " +
Integer.toString(size));
292 private void checkClass(Class<?> given, Class<?> expected)
throws SQLException {
293 if (!expected.isAssignableFrom(given)) {
294 throw new SQLException(
"For array of " +
getBaseTypeName() +
", elements of type "
295 + expected +
" are expected. Got " + given +
" instead");
Object getArray(long start, int size)
void checkSize(int start, int size)
size_t append(FILE *f, const size_t size, const int8_t *buf)
Appends the specified number of bytes to the end of the file f from buf.
HeavyAIArray(TDatumType type, Object[] elements)
ResultSet getResultSet(long start, int size, Map< String, Class<?>> map)
Object getArray(Map< String, Class<?>> map)
ResultSet getResultSet(Map< String, Class<?>> map)
void checkClass(Class<?> given, Class<?> expected)
Object getArray(long start, int size, Map< String, Class<?>> map)
ResultSet getResultSet(long start, int size)