-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[UUID 2/8] UUID ingest and segment storage #18870
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
xiangfu0
wants to merge
1
commit into
apache:master
Choose a base branch
from
xiangfu0:uuid-split/02-ingest-storage
base: master
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.
+1,025
−89
Open
Changes from all commits
Commits
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
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
147 changes: 147 additions & 0 deletions
147
pinot-core/src/test/java/org/apache/pinot/core/util/SegmentProcessorAvroUtilsTest.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,147 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.pinot.core.util; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.List; | ||
| import org.apache.avro.LogicalTypes; | ||
| import org.apache.avro.Schema; | ||
| import org.apache.avro.SchemaBuilder; | ||
| import org.apache.avro.file.DataFileReader; | ||
| import org.apache.avro.file.DataFileWriter; | ||
| import org.apache.avro.generic.GenericData; | ||
| import org.apache.avro.generic.GenericDatumReader; | ||
| import org.apache.avro.generic.GenericDatumWriter; | ||
| import org.apache.avro.generic.GenericRecord; | ||
| import org.apache.commons.io.FileUtils; | ||
| import org.apache.pinot.spi.data.FieldSpec.DataType; | ||
| import org.apache.pinot.spi.data.readers.GenericRow; | ||
| import org.apache.pinot.spi.utils.UuidUtils; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertTrue; | ||
|
|
||
|
|
||
| public class SegmentProcessorAvroUtilsTest { | ||
|
|
||
| /// convertGenericRowToAvroRecord keeps a UUID value as its raw 16-byte form (it does NOT render a string here); the | ||
| /// rendering is deferred to the uuid Conversion on the writer's data model. Plain BYTES columns are still wrapped as | ||
| /// ByteBuffer. | ||
| @Test | ||
| public void testConvertGenericRowToAvroRecordKeepsUuidBytesRaw() { | ||
| Schema uuidSchema = LogicalTypes.uuid().addToSchema(Schema.create(Schema.Type.STRING)); | ||
| Schema recordSchema = SchemaBuilder.record("record").fields() | ||
| .name("uuidCol").type(uuidSchema).noDefault() | ||
| .name("bytesCol").type().bytesType().noDefault() | ||
| .endRecord(); | ||
|
|
||
| byte[] uuidBytes = UuidUtils.toBytes("12345678-1234-1234-1234-1234567890ab"); | ||
| byte[] rawBytes = {1, 2, 3, 4}; | ||
| GenericRow row = new GenericRow(); | ||
| row.putValue("uuidCol", uuidBytes); | ||
| row.putValue("bytesCol", rawBytes); | ||
|
|
||
| GenericData.Record record = new GenericData.Record(recordSchema); | ||
| SegmentProcessorAvroUtils.convertGenericRowToAvroRecord(row, record); | ||
|
|
||
| assertTrue(record.get("uuidCol") instanceof byte[], "UUID must be left as raw byte[] for the uuid Conversion"); | ||
| assertEquals((byte[]) record.get("uuidCol"), uuidBytes); | ||
| assertEquals(record.get("bytesCol"), ByteBuffer.wrap(rawBytes), "BYTES byte[] must be wrapped as ByteBuffer"); | ||
| } | ||
|
|
||
| /// End-to-end: a GenericDatumWriter built with getAvroDataModel() serializes the raw 16-byte UUID values as their | ||
| /// canonical string (via the registered uuid Conversion) for both SV and MV, and the on-disk value reads back as | ||
| /// that string with a vanilla reader. Without the registered Conversion the write would fail (a ByteBuffer/byte[] | ||
| /// cannot be written to a string{logicalType:uuid} field). | ||
| @Test | ||
| public void testUuidRoundTripThroughAvroDataModel() | ||
| throws Exception { | ||
| org.apache.pinot.spi.data.Schema pinotSchema = new org.apache.pinot.spi.data.Schema.SchemaBuilder() | ||
| .setSchemaName("uuidSchema") | ||
| .addSingleValueDimension("uuidSv", DataType.UUID) | ||
| .addMultiValueDimension("uuidMv", DataType.UUID) | ||
| .build(); | ||
| Schema avroSchema = SegmentProcessorAvroUtils.convertPinotSchemaToAvroSchema(pinotSchema); | ||
|
|
||
| String svCanonical = "12345678-1234-1234-1234-1234567890ab"; | ||
| String mvCanonical = "550e8400-e29b-41d4-a716-446655440000"; | ||
| GenericRow row = new GenericRow(); | ||
| row.putValue("uuidSv", UuidUtils.toBytes(svCanonical)); | ||
| row.putValue("uuidMv", new Object[]{UuidUtils.toBytes(mvCanonical)}); | ||
| GenericData.Record record = | ||
| SegmentProcessorAvroUtils.convertGenericRowToAvroRecord(row, new GenericData.Record(avroSchema)); | ||
|
|
||
| File tmp = File.createTempFile("uuidRoundTrip", ".avro"); | ||
| try { | ||
| GenericData model = SegmentProcessorAvroUtils.getAvroDataModel(); | ||
| try (DataFileWriter<GenericData.Record> writer = | ||
| new DataFileWriter<>(new GenericDatumWriter<>(avroSchema, model))) { | ||
| writer.create(avroSchema, tmp); | ||
| writer.append(record); | ||
| } | ||
| // Read back with a vanilla reader: the on-disk representation must be the canonical UUID string. | ||
| try (DataFileReader<GenericRecord> reader = new DataFileReader<>(tmp, new GenericDatumReader<>(avroSchema))) { | ||
| assertTrue(reader.hasNext()); | ||
| GenericRecord read = reader.next(); | ||
| assertEquals(read.get("uuidSv").toString(), svCanonical, "SV UUID must serialize as canonical string"); | ||
| List<?> mv = (List<?>) read.get("uuidMv"); | ||
| assertEquals(mv.size(), 1); | ||
| assertEquals(mv.get(0).toString(), mvCanonical, "MV UUID element must serialize as canonical string"); | ||
| } | ||
| // Read back WITH the data model: the uuid Conversion's fromCharSequence must reconstruct the raw 16-byte value. | ||
| try (DataFileReader<GenericRecord> reader = new DataFileReader<>(tmp, | ||
| new GenericDatumReader<>(avroSchema, avroSchema, SegmentProcessorAvroUtils.getAvroDataModel()))) { | ||
| GenericRecord read = reader.next(); | ||
| assertEquals((byte[]) read.get("uuidSv"), UuidUtils.toBytes(svCanonical), | ||
| "reading with the model must convert the uuid string back to raw bytes"); | ||
| assertEquals((byte[]) ((List<?>) read.get("uuidMv")).get(0), UuidUtils.toBytes(mvCanonical), | ||
| "MV uuid element must convert back to raw bytes when reading with the model"); | ||
| } | ||
| } finally { | ||
| FileUtils.deleteQuietly(tmp); | ||
| } | ||
| } | ||
|
|
||
| /// convertPinotSchemaToAvroSchema must emit SV UUID as string{logicalType:uuid} and MV UUID as | ||
| /// array<string{logicalType:uuid}>, which is what the uuid Conversion above pairs with. | ||
| @Test | ||
| public void testConvertPinotSchemaToAvroSchemaEmitsUuidLogicalType() { | ||
| org.apache.pinot.spi.data.Schema pinotSchema = new org.apache.pinot.spi.data.Schema.SchemaBuilder() | ||
| .setSchemaName("uuidSchema") | ||
| .addSingleValueDimension("uuidSv", DataType.UUID) | ||
| .addMultiValueDimension("uuidMv", DataType.UUID) | ||
| .build(); | ||
|
|
||
| Schema avroSchema = SegmentProcessorAvroUtils.convertPinotSchemaToAvroSchema(pinotSchema); | ||
|
|
||
| Schema svFieldSchema = avroSchema.getField("uuidSv").schema(); | ||
| assertEquals(svFieldSchema.getType(), Schema.Type.STRING, "SV UUID must be string{logicalType:uuid}"); | ||
| assertEquals(LogicalTypes.fromSchemaIgnoreInvalid(svFieldSchema), LogicalTypes.uuid(), | ||
| "SV UUID must carry the uuid logical type"); | ||
|
|
||
| Schema mvFieldSchema = avroSchema.getField("uuidMv").schema(); | ||
| assertEquals(mvFieldSchema.getType(), Schema.Type.ARRAY, "MV UUID must be emitted as an array"); | ||
| Schema mvElementSchema = mvFieldSchema.getElementType(); | ||
| assertEquals(mvElementSchema.getType(), Schema.Type.STRING, "MV UUID elements must be string{logicalType:uuid}"); | ||
| assertEquals(LogicalTypes.fromSchemaIgnoreInvalid(mvElementSchema), LogicalTypes.uuid(), | ||
| "MV UUID elements must carry the uuid logical type"); | ||
| } | ||
| } |
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
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.