Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(java): replace org.json with gson to resolve the jar conflict with spark 3.5.1 #3340

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions java/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.questdb</groupId>
Expand Down
82 changes: 53 additions & 29 deletions java/core/src/main/java/com/lancedb/lance/FragmentMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
*/
package com.lancedb.lance;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.annotations.SerializedName;
import org.apache.arrow.util.Preconditions;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -25,8 +28,6 @@
/** Metadata of a Fragment in the dataset. Matching to lance Fragment. */
public class FragmentMetadata implements Serializable {
private static final long serialVersionUID = -5886811251944130460L;
private static final String ID_KEY = "id";
private static final String PHYSICAL_ROWS_KEY = "physical_rows";
private final String jsonMetadata;
private final int id;
private final long physicalRows;
Expand Down Expand Up @@ -66,17 +67,13 @@ public String toString() {
*/
public static FragmentMetadata fromJson(String jsonMetadata) {
Preconditions.checkNotNull(jsonMetadata);
JSONObject metadata = new JSONObject(jsonMetadata);
if (!metadata.has(ID_KEY) || !metadata.has(PHYSICAL_ROWS_KEY)) {
throw new IllegalArgumentException(
String.format(
"Fragment metadata must have {} and {} but is {}",
ID_KEY,
PHYSICAL_ROWS_KEY,
jsonMetadata));
Gson gson = new Gson();
try {
Fragment fragment = gson.fromJson(jsonMetadata, Fragment.class);
return new FragmentMetadata(jsonMetadata, fragment.getId(), fragment.getPhysicalRows());
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return new FragmentMetadata(
jsonMetadata, metadata.getInt(ID_KEY), metadata.getLong(PHYSICAL_ROWS_KEY));
}

/**
Expand All @@ -87,22 +84,49 @@ public static FragmentMetadata fromJson(String jsonMetadata) {
*/
public static List<FragmentMetadata> fromJsonArray(String jsonMetadata) {
Preconditions.checkNotNull(jsonMetadata);
JSONArray metadatas = new JSONArray(jsonMetadata);
List<FragmentMetadata> fragmentMetadataList = new ArrayList<>();
for (Object object : metadatas) {
JSONObject metadata = (JSONObject) object;
if (!metadata.has(ID_KEY) || !metadata.has(PHYSICAL_ROWS_KEY)) {
throw new IllegalArgumentException(
String.format(
"Fragment metadata must have {} and {} but is {}",
ID_KEY,
PHYSICAL_ROWS_KEY,
jsonMetadata));
Gson gson = new Gson();
JsonParser parser = new JsonParser();
try {
JsonArray fragments = parser.parse(jsonMetadata).getAsJsonArray();
List<FragmentMetadata> fragmentMetadataList = new ArrayList<>();
for (JsonElement fragmentE : fragments) {
Fragment fragment = gson.fromJson(fragmentE, Fragment.class);
fragmentMetadataList.add(
new FragmentMetadata(
fragmentE.toString(), fragment.getId(), fragment.getPhysicalRows()));
}
fragmentMetadataList.add(
new FragmentMetadata(
metadata.toString(), metadata.getInt(ID_KEY), metadata.getLong(PHYSICAL_ROWS_KEY)));
return fragmentMetadataList;
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}

public static class Fragment {
@SerializedName("id")
private int id;

@SerializedName("physical_rows")
private long physicalRows;

public Fragment(int id, long physicalRows) {
this.id = id;
this.physicalRows = physicalRows;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public long getPhysicalRows() {
return physicalRows;
}

public void setPhysicalRows(long physicalRows) {
this.physicalRows = physicalRows;
}
return fragmentMetadataList;
}
}
6 changes: 3 additions & 3 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@
<version>5.10.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Loading