Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -569,12 +569,19 @@ public Object[] getObjectArray(String colName) {

@Override
public boolean hasValue(int colIndex) {
if (colIndex < 1 || colIndex > currentRecord.length) {
return false;
}
return currentRecord[colIndex - 1] != null;
}

@Override
public boolean hasValue(String colName) {
return hasValue(schema.nameToColumnIndex(colName));
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can add TableSchema hasColumn instead, using try catch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we have hasColumn then it will be two methods call.
we need something like nameToColumnIndexSafe(), WDYT?
But this I think overloads interface a bit.
returning -1 by schema.nameToColumnIndex() also looks not a java way.

Idea behind exception is that application code should know what number of columns are in the result:

  • we let application access schema to check dynamically
  • or application knows it upfront

so checking value for missing column look more like a bug in application.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using an exception has a little bit performance penalty.
Two methods call, are you worried about performance?
In general, we need to specify the expected behavior of our API. I prefer not use exceptions if we can avoid

Copy link
Contributor Author

@chernser chernser Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect reading missing column is be a very specific case.
Ok, I'll implement another safe method to return -1 from schema on unknown column

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but I agree that logic based on exception is bad

return hasValue(schema.nameToColumnIndex(colName));
} catch (NoSuchColumnException e) {
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.clickhouse.client.api.ClientException;
import com.clickhouse.client.api.internal.DataTypeConverter;
import com.clickhouse.client.api.metadata.NoSuchColumnException;
import com.clickhouse.client.api.metadata.TableSchema;
import com.clickhouse.client.api.query.GenericRecord;
import com.clickhouse.client.api.query.NullValueException;
Expand Down Expand Up @@ -310,7 +311,11 @@ public String[] getStringArray(String colName) {

@Override
public boolean hasValue(int colIndex) {
return hasValue(schema.columnIndexToName(colIndex));
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can add TableSchema hasColumn instead, using try catch

return hasValue(schema.columnIndexToName(colIndex));
} catch (NoSuchColumnException e) {
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,41 @@ public void testQueryRecordsReadsAllValues() throws Exception {
}
}

@Test(groups = {"integration"})
public void testSimpleResultSetReadWithBinaryReader() throws Exception {
QuerySettings settings = new QuerySettings().setFormat(ClickHouseFormat.RowBinaryWithNamesAndTypes);

try (QueryResponse response = client.query("SELECT 1 a, null::Nullable(Int32) b", settings).get(3, TimeUnit.SECONDS)) {
ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response);

Assert.assertTrue(reader.hasNext());
reader.next();
Assert.assertEquals(reader.getInteger(1), 1);

Assert.assertTrue(reader.hasValue(1));
Assert.assertFalse(reader.hasValue(2));
Assert.assertFalse(reader.hasValue(3));

Assert.assertTrue(reader.hasValue("a"));
Assert.assertFalse(reader.hasValue("b"));
Assert.assertFalse(reader.hasValue("c"));
}

List<GenericRecord> records = client.queryAll("SELECT 1 a, null::Nullable(Int32) b", settings);

GenericRecord record = records.get(0);

Assert.assertEquals(record.getInteger(1), 1);

Assert.assertTrue(record.hasValue(1));
Assert.assertFalse(record.hasValue(2));
Assert.assertFalse(record.hasValue(3));

Assert.assertTrue(record.hasValue("a"));
Assert.assertFalse(record.hasValue("b"));
Assert.assertFalse(record.hasValue("c"));
}

private final static List<String> NULL_DATASET_COLUMNS = Arrays.asList(
"id UInt32",
"col1 UInt32 NULL",
Expand Down
Loading