|
7 | 7 | #endif |
8 | 8 |
|
9 | 9 | // This header is generated at build time. See CMakeLists.txt. |
| 10 | +#include <vector> |
| 11 | + |
10 | 12 | #include "com_kuzudb_Native.h" |
11 | 13 | #include "common/constants.h" |
12 | 14 | #include "common/exception/exception.h" |
@@ -796,6 +798,37 @@ JNIEXPORT void JNICALL Java_com_kuzudb_Native_kuzu_1value_1destroy(JNIEnv* env, |
796 | 798 | delete v; |
797 | 799 | } |
798 | 800 |
|
| 801 | +JNIEXPORT jobject JNICALL Java_com_kuzudb_Native_kuzu_1create_1list___3Lcom_kuzudb_Value_2( |
| 802 | + JNIEnv* env, jclass, jobjectArray listValues) { |
| 803 | + jsize len = env->GetArrayLength(listValues); |
| 804 | + if (len == 0) { |
| 805 | + return nullptr; |
| 806 | + } |
| 807 | + |
| 808 | + std::vector<std::unique_ptr<Value>> children; |
| 809 | + for (jsize i = 0; i < len; ++i) { |
| 810 | + Value* element = getValue(env, env->GetObjectArrayElement(listValues, i)); |
| 811 | + children.emplace_back(element->copy()); |
| 812 | + } |
| 813 | + LogicalType childType = children[0]->getDataType().copy(); |
| 814 | + |
| 815 | + Value* listValue = new Value(LogicalType::LIST(std::move(childType)), std::move(children)); |
| 816 | + return createJavaObject(env, listValue, J_C_Value, J_C_Value_F_v_ref); |
| 817 | +} |
| 818 | + |
| 819 | +JNIEXPORT jobject JNICALL Java_com_kuzudb_Native_kuzu_1create_1list__Lcom_kuzudb_DataType_2J( |
| 820 | + JNIEnv* env, jclass, jobject dataType, jlong numElements) { |
| 821 | + LogicalType* logicalType = getDataType(env, dataType); |
| 822 | + |
| 823 | + std::vector<std::unique_ptr<Value>> children; |
| 824 | + for (jlong i = 0; i < numElements; ++i) { |
| 825 | + children.emplace_back(std::make_unique<Value>(Value::createDefaultValue(*logicalType))); |
| 826 | + } |
| 827 | + |
| 828 | + Value* listValue = new Value(LogicalType::LIST(logicalType->copy()), std::move(children)); |
| 829 | + return createJavaObject(env, listValue, J_C_Value, J_C_Value_F_v_ref); |
| 830 | +} |
| 831 | + |
799 | 832 | JNIEXPORT jlong JNICALL Java_com_kuzudb_Native_kuzu_1value_1get_1list_1size(JNIEnv* env, jclass, |
800 | 833 | jobject thisValue) { |
801 | 834 | Value* v = getValue(env, thisValue); |
@@ -1124,6 +1157,71 @@ JNIEXPORT jstring JNICALL Java_com_kuzudb_Native_kuzu_1rel_1val_1to_1string(JNIE |
1124 | 1157 | return ret; |
1125 | 1158 | } |
1126 | 1159 |
|
| 1160 | +JNIEXPORT jobject JNICALL Java_com_kuzudb_Native_kuzu_1create_1map(JNIEnv* env, jclass, |
| 1161 | + jobjectArray keys, jobjectArray values) { |
| 1162 | + jsize len = env->GetArrayLength(keys); |
| 1163 | + KU_ASSERT(env->GetArrayLength(values) == len); |
| 1164 | + KU_ASSERT(len > 0); |
| 1165 | + |
| 1166 | + std::optional<LogicalType> keyType; |
| 1167 | + std::optional<LogicalType> valueType; |
| 1168 | + |
| 1169 | + std::vector<std::unique_ptr<Value>> children; |
| 1170 | + for (jsize i = 0; i < len; ++i) { |
| 1171 | + auto key = getValue(env, env->GetObjectArrayElement(keys, i))->copy(); |
| 1172 | + auto value = getValue(env, env->GetObjectArrayElement(values, i))->copy(); |
| 1173 | + |
| 1174 | + if (!keyType.has_value()) { |
| 1175 | + keyType = key->getDataType().copy(); |
| 1176 | + valueType = value->getDataType().copy(); |
| 1177 | + } else { |
| 1178 | + KU_ASSERT(valueType.has_value()); |
| 1179 | + if (key->getDataType() != *keyType || value->getDataType() != *valueType) { |
| 1180 | + return nullptr; |
| 1181 | + } |
| 1182 | + } |
| 1183 | + |
| 1184 | + std::vector<StructField> structFields; |
| 1185 | + structFields.emplace_back(InternalKeyword::MAP_KEY, keyType->copy()); |
| 1186 | + structFields.emplace_back(InternalKeyword::MAP_VALUE, valueType->copy()); |
| 1187 | + |
| 1188 | + decltype(children) structVals; |
| 1189 | + structVals.emplace_back(std::move(key)); |
| 1190 | + structVals.emplace_back(std::move(value)); |
| 1191 | + children.emplace_back(std::make_unique<Value>(LogicalType::STRUCT(std::move(structFields)), |
| 1192 | + std::move(structVals))); |
| 1193 | + } |
| 1194 | + |
| 1195 | + KU_ASSERT(keyType.has_value()); |
| 1196 | + KU_ASSERT(valueType.has_value()); |
| 1197 | + Value* mapValue = new Value(LogicalType::MAP(std::move(*keyType), std::move(*valueType)), |
| 1198 | + std::move(children)); |
| 1199 | + return createJavaObject(env, mapValue, J_C_Value, J_C_Value_F_v_ref); |
| 1200 | +} |
| 1201 | + |
| 1202 | +JNIEXPORT jobject JNICALL Java_com_kuzudb_Native_kuzu_1create_1struct(JNIEnv* env, jclass, |
| 1203 | + jobjectArray fieldNames, jobjectArray fieldValues) { |
| 1204 | + jsize len = env->GetArrayLength(fieldNames); |
| 1205 | + KU_ASSERT(env->GetArrayLength(fieldValues) == len); |
| 1206 | + KU_ASSERT(len > 0); |
| 1207 | + |
| 1208 | + std::vector<std::unique_ptr<Value>> children; |
| 1209 | + auto structFields = std::vector<StructField>{}; |
| 1210 | + for (jsize i = 0; i < len; ++i) { |
| 1211 | + auto fieldName = std::string(env->GetStringUTFChars( |
| 1212 | + reinterpret_cast<jstring>(env->GetObjectArrayElement(fieldNames, i)), JNI_FALSE)); |
| 1213 | + auto fieldValue = getValue(env, env->GetObjectArrayElement(fieldValues, i))->copy(); |
| 1214 | + auto fieldType = fieldValue->getDataType().copy(); |
| 1215 | + |
| 1216 | + structFields.emplace_back(std::move(fieldName), std::move(fieldType)); |
| 1217 | + children.push_back(std::move(fieldValue)); |
| 1218 | + } |
| 1219 | + |
| 1220 | + Value* structValue = |
| 1221 | + new Value(LogicalType::STRUCT(std::move(structFields)), std::move(children)); |
| 1222 | + return createJavaObject(env, structValue, J_C_Value, J_C_Value_F_v_ref); |
| 1223 | +} |
| 1224 | + |
1127 | 1225 | JNIEXPORT jstring JNICALL Java_com_kuzudb_Native_kuzu_1value_1get_1struct_1field_1name(JNIEnv* env, |
1128 | 1226 | jclass, jobject thisSV, jlong index) { |
1129 | 1227 | auto* sv = getValue(env, thisSV); |
|
0 commit comments