-
Notifications
You must be signed in to change notification settings - Fork 631
feat(client-v2): decode QBit in the Native format #2983
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
Open
polyglotAI-bot
wants to merge
4
commits into
main
Choose a base branch
from
polyglot/client-v2-qbit-native-read
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
af35872
feat(client-v2): decode QBit in the Native format
polyglotAI-bot 688cf07
Address review: rename readQBitColumn->readQBitNative, shorten javadoc
polyglotAI-bot 9b0eaaa
Address review: narrow QBit Native support to float element types, gu…
polyglotAI-bot f71744e
Address review: raise QBit Native decode coverage, shorten comments, …
polyglotAI-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...t-v2/src/test/java/com/clickhouse/client/api/data_formats/NativeFormatReaderQBitTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package com.clickhouse.client.api.data_formats; | ||
|
|
||
| import com.clickhouse.client.api.ClientException; | ||
| import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader; | ||
| import com.clickhouse.client.api.query.QuerySettings; | ||
| import com.clickhouse.data.format.BinaryStreamUtils; | ||
|
|
||
| import org.testng.Assert; | ||
| import org.testng.annotations.DataProvider; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * QBit handling in {@link NativeFormatReader}, driven by Native-format blocks captured from ClickHouse | ||
| * 26.5.1 and by minimal hand-built block headers. These run without a live server, so the block-level | ||
| * routing (decode a plain float QBit, reject every other shape) stays covered on the coverage build, | ||
| * whose older server skips the QBit integration tests. | ||
| */ | ||
| public class NativeFormatReaderQBitTest { | ||
|
|
||
| // Full single-row Native blocks (SELECT CAST(<vec> AS QBit(<type>)) FORMAT Native), one per decodable | ||
| // element type, with the vector each encodes. | ||
| @DataProvider(name = "decodableQBitBlocks") | ||
| public static Object[][] decodableQBitBlocks() { | ||
| return new Object[][] { | ||
| {"Float32", | ||
| "010103766563105142697428466c6f617433322c2033290206010101010101010404000000000000000000000000000000000000000000", | ||
| false, new float[] {1f, -2f, 3.5f}, null}, | ||
| {"Float64", | ||
| "010103766563105142697428466c6f617436342c20332902060101010101010101010104040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", | ||
| true, null, new double[] {1d, -2d, 3.5d}}, | ||
| {"BFloat16", | ||
| "01010376656311514269742842466c6f617431362c20382900fe01010101e1995500000000000000", | ||
| false, new float[] {1f, 2f, 4f, 8f, 16f, 32f, 64f, 128f}, null}, | ||
| }; | ||
| } | ||
|
|
||
| @Test(dataProvider = "decodableQBitBlocks") | ||
| public void testDecodesPlainQBitColumn(String label, String blockHex, boolean isDouble, | ||
| float[] expectedFloat, double[] expectedDouble) { | ||
| NativeFormatReader reader = nativeReader(fromHex(blockHex)); | ||
|
|
||
| Assert.assertNotNull(reader.next(), label); | ||
| if (isDouble) { | ||
| Assert.assertEquals(reader.getDoubleArray("vec"), expectedDouble, label); | ||
| } else { | ||
| Assert.assertEquals(reader.getFloatArray("vec"), expectedFloat, label); | ||
| } | ||
| Assert.assertNull(reader.next(), label); | ||
| } | ||
|
|
||
| // QBit shapes the Native reader cannot decode: nested inside a container, strided, wrapped in Nullable, | ||
| // or a non-float element type. Each must be rejected up front instead of misreading the block. | ||
| @DataProvider(name = "undecodableQBitTypes") | ||
| public static Object[][] undecodableQBitTypes() { | ||
| return new Object[][] { | ||
| {"m", "Map(String, QBit(Float32, 3))"}, | ||
| {"vec", "QBit(Float32, 4, 2)"}, | ||
| {"vec", "Nullable(QBit(Float32, 3))"}, | ||
| {"vec", "QBit(Int8, 3)"}, | ||
| }; | ||
| } | ||
|
|
||
| @Test(dataProvider = "undecodableQBitTypes") | ||
| public void testRejectsUndecodableQBitColumn(String colName, String colType) throws IOException { | ||
| // Only the block header (column name + type) is needed: readBlock rejects the column before reading | ||
| // any payload. | ||
| byte[] block = nativeBlockHeader(colName, colType); | ||
| ClientException ex = Assert.expectThrows(ClientException.class, () -> nativeReader(block)); | ||
| Assert.assertTrue(ex.getMessage().contains("QBit") && ex.getMessage().contains("Native"), | ||
| colType + ": " + ex.getMessage()); | ||
| } | ||
|
|
||
| private static NativeFormatReader nativeReader(byte[] block) { | ||
| return new NativeFormatReader(new ByteArrayInputStream(block), | ||
| new QuerySettings().setUseTimeZone("UTC"), | ||
| new BinaryStreamReader.CachingByteBufferAllocator()); | ||
| } | ||
|
|
||
| private static byte[] nativeBlockHeader(String columnName, String columnType) throws IOException { | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| BinaryStreamUtils.writeVarInt(out, 1); // one column | ||
| BinaryStreamUtils.writeVarInt(out, 1); // one row | ||
| BinaryStreamUtils.writeString(out, columnName); | ||
| BinaryStreamUtils.writeString(out, columnType); | ||
| return out.toByteArray(); | ||
| } | ||
|
|
||
| private static byte[] fromHex(String hex) { | ||
| byte[] out = new byte[hex.length() / 2]; | ||
| for (int i = 0; i < out.length; i++) { | ||
| out[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); | ||
| } | ||
| return out; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.