Skip to content

Commit

Permalink
optimize find column index
Browse files Browse the repository at this point in the history
  • Loading branch information
Okapist committed Jul 8, 2023
1 parent c25ffc6 commit 1912cea
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.*;

import com.clickhouse.client.ClickHouseConfig;
import com.clickhouse.client.ClickHouseResponse;
Expand All @@ -48,6 +42,7 @@ public class ClickHouseResultSet extends AbstractResultSet {
private Iterator<ClickHouseRecord> rowCursor;
private int rowNumber;
private int lastReadColumn; // 1-based
private final Map<String, Integer> columnIndexCache = Collections.synchronizedMap(new HashMap<>());

protected final String database;
protected final String table;
Expand Down Expand Up @@ -215,6 +210,15 @@ public int findColumn(String columnLabel) throws SQLException {
throw SqlExceptionUtils.clientError("Non-empty column label is required");
}

Integer index = columnIndexCache.get(columnLabel);
if(index == null) {
index = findColumnIndex(columnLabel);
columnIndexCache.putIfAbsent(columnLabel, index);
}
return index;
}

private int findColumnIndex(String columnLabel) throws SQLException {
int index = 0;
for (ClickHouseColumn c : columns) {
index++;
Expand Down

0 comments on commit 1912cea

Please sign in to comment.