-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBigQueryBaseArray.java
More file actions
178 lines (156 loc) · 5.92 KB
/
BigQueryBaseArray.java
File metadata and controls
178 lines (156 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright 2023 Google LLC
*
* Licensed 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
*
* https://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 com.google.cloud.bigquery.jdbc;
import static com.google.cloud.bigquery.Field.Mode.REPEATED;
import static com.google.cloud.bigquery.jdbc.BigQueryBaseStruct.isStruct;
import static com.google.cloud.bigquery.jdbc.BigQueryErrorMessage.CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED;
import static com.google.cloud.bigquery.jdbc.BigQueryErrorMessage.INVALID_ARRAY;
import com.google.cloud.Tuple;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.Field.Mode;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.exception.BigQueryJdbcSqlFeatureNotSupportedException;
import java.lang.reflect.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Struct;
import java.util.Arrays;
import java.util.Base64;
import java.util.Map;
import java.util.stream.Collectors;
/**
* An abstract implementation of {@link java.sql.Array} used as a base class for {@link
* BigQueryArrowArray} and {@link BigQueryJsonArray}. An Array value is a transaction-duration
* reference to an SQL ARRAY value.
*/
abstract class BigQueryBaseArray implements java.sql.Array {
protected final BigQueryJdbcResultSetLogger LOG;
protected final boolean arrayOfStruct;
private boolean valid;
protected Field schema;
BigQueryBaseArray(Field schema, BigQueryJdbcResultSetLogger log) {
this.LOG = log;
this.schema = schema;
this.arrayOfStruct = isStruct(schema);
this.valid = true;
}
@Override
public final String getBaseTypeName() {
LOG.finestTrace("getBaseTypeName");
ensureValid();
return this.schema.getType().getStandardType().name();
}
@Override
public final int getBaseType() {
LOG.finestTrace("getBaseType");
ensureValid();
return BigQueryJdbcTypeMappings.standardSQLToJavaSqlTypesMapping.get(
schema.getType().getStandardType());
}
@Override
public final Object getArray(Map<String, Class<?>> map) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
}
@Override
public final Object getArray(long index, int count, Map<String, Class<?>> map)
throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
}
@Override
public final ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
}
@Override
public final ResultSet getResultSet(long index, int count, Map<String, Class<?>> map)
throws SQLException {
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
}
protected Object getArrayInternal(int fromIndex, int toIndexExclusive) {
LOG.finestTrace("getArrayInternal");
Class<?> targetClass = getTargetClass();
int size = toIndexExclusive - fromIndex;
Object javaArray = Array.newInstance(targetClass, size);
for (int index = 0; index < size; index++) {
Array.set(javaArray, index, getCoercedValue(fromIndex + index));
}
return javaArray;
}
protected void ensureValid() throws IllegalStateException {
LOG.finestTrace("ensureValid");
if (!this.valid) {
IllegalStateException ex = new IllegalStateException(INVALID_ARRAY);
LOG.severe(INVALID_ARRAY, ex);
throw ex;
}
}
protected void markInvalid() {
LOG.finestTrace("markInvalid");
this.schema = null;
this.valid = false;
}
protected Field singleElementSchema() {
LOG.finestTrace("singleElementSchema");
return this.schema.toBuilder().setMode(Mode.REQUIRED).build();
}
protected Tuple<Integer, Integer> createRange(long index, int count, int size)
throws IllegalStateException {
LOG.finestTrace("createRange");
// jdbc array follows 1 based array indexing
long normalisedFromIndex = index - 1;
if (normalisedFromIndex + count > size) {
IllegalArgumentException ex =
new IllegalArgumentException(
String.format(
"The array index is out of range: %d, number of elements: %d.",
index + count, size));
LOG.severe(ex.getMessage(), ex);
throw ex;
}
long toIndex = normalisedFromIndex + count;
return Tuple.of((int) normalisedFromIndex, (int) toIndex);
}
protected Class<?> getTargetClass() {
LOG.finestTrace("getTargetClass");
return this.arrayOfStruct
? Struct.class
: BigQueryJdbcTypeMappings.standardSQLToJavaTypeMapping.get(
this.schema.getType().getStandardType());
}
abstract Object getCoercedValue(int index);
static boolean isArray(Field currentSchema) {
return currentSchema.getMode() == REPEATED;
}
@Override
public String toString() {
try {
Object[] array = (Object[]) getArray();
if (array == null) {
return "null";
}
if (this.schema.getType().getStandardType() == StandardSQLTypeName.BYTES) {
return Arrays.stream(array)
.map(
element ->
element == null ? "null" : Base64.getEncoder().encodeToString((byte[]) element))
.collect(Collectors.joining(", ", "[", "]"));
}
return Arrays.deepToString(array);
} catch (SQLException e) {
LOG.warning("Error converting array to string");
return "[Error converting array to string: " + e.getMessage() + "]";
}
}
}