-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix: clustered segment catalog spec complex handling #19842
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| import com.google.common.primitives.Doubles; | ||
| import com.google.common.primitives.Floats; | ||
| import org.apache.druid.common.guava.GuavaUtils; | ||
| import org.apache.druid.data.input.impl.DimensionSchema; | ||
| import org.apache.druid.data.input.impl.DimensionSchema.MultiValueHandling; | ||
| import org.apache.druid.error.DruidException; | ||
| import org.apache.druid.java.util.common.IAE; | ||
|
|
@@ -130,17 +131,44 @@ private DimensionHandlerUtils() | |
| } | ||
|
|
||
| if (capabilities.is(ValueType.COMPLEX) && capabilities.getComplexTypeName() != null) { | ||
| DimensionHandlerProvider provider = DIMENSION_HANDLER_PROVIDERS.get(capabilities.getComplexTypeName()); | ||
| if (provider == null) { | ||
| throw new ISE("Can't find DimensionHandlerProvider for typeName [%s]", capabilities.getComplexTypeName()); | ||
| } | ||
| return provider.get(dimensionName); | ||
| return getHandlerForComplexType(dimensionName, capabilities.getComplexTypeName()); | ||
| } | ||
|
|
||
| // Return a StringDimensionHandler by default (null columns will be treated as String typed) | ||
| return new StringDimensionHandler(dimensionName, multiValueHandling, true, false); | ||
| } | ||
|
|
||
| /** | ||
| * The {@link DimensionHandler} registered for a complex type. Complex columns are stored by type-specific handlers, | ||
| * so a type contributed by an extension becomes storable as soon as that extension registers one. | ||
| * | ||
| * @throws ISE if no handler is registered for the type, which usually means the extension defining it is not loaded | ||
| */ | ||
| public static DimensionHandler<?, ?, ?> getHandlerForComplexType(String dimensionName, String complexTypeName) | ||
| { | ||
| final DimensionHandlerProvider provider = DIMENSION_HANDLER_PROVIDERS.get(complexTypeName); | ||
| if (provider == null) { | ||
| throw new ISE("Can't find DimensionHandlerProvider for typeName [%s]", complexTypeName); | ||
| } | ||
| return provider.get(dimensionName); | ||
| } | ||
|
|
||
| /** | ||
| * The {@link DimensionSchema} to use when storing a complex column of the given type, for callers that have a | ||
| * declared type rather than an existing column. Handlers are free to consult the {@link ColumnCapabilities} they | ||
| * are given, so a default set describing the type is supplied on the caller's behalf. | ||
| * | ||
| * @throws ISE if no handler is registered for the type, which usually means the extension defining it is not loaded | ||
| */ | ||
| public static DimensionSchema getComplexDimensionSchema(String dimensionName, ColumnType type) | ||
| { | ||
| if (!type.is(ValueType.COMPLEX) || type.getComplexTypeName() == null) { | ||
| throw new IAE("Type [%s] is not a named complex type", type); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using |
||
| } | ||
| return getHandlerForComplexType(dimensionName, type.getComplexTypeName()) | ||
| .getDimensionSchema(ColumnCapabilitiesImpl.createDefault().setType(type)); | ||
| } | ||
|
|
||
| public static List<ColumnType> getValueTypesFromDimensionSpecs(List<DimensionSpec> dimSpecs) | ||
| { | ||
| List<ColumnType> types = new ArrayList<>(dimSpecs.size()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,11 +26,14 @@ | |
| import org.apache.druid.data.input.impl.ClusteredValueGroupsBaseTableProjectionSpec; | ||
| import org.apache.druid.data.input.impl.DimensionSchema; | ||
| import org.apache.druid.error.InvalidInput; | ||
| import org.apache.druid.java.util.common.ISE; | ||
| import org.apache.druid.segment.AutoTypeColumnSchema; | ||
| import org.apache.druid.segment.DimensionHandlerUtils; | ||
| import org.apache.druid.segment.NestedDataColumnSchema; | ||
| import org.apache.druid.segment.VirtualColumns; | ||
| import org.apache.druid.segment.column.ColumnHolder; | ||
| import org.apache.druid.segment.column.ColumnType; | ||
| import org.apache.druid.segment.column.ValueType; | ||
| import org.apache.druid.utils.CollectionUtils; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
@@ -212,16 +215,54 @@ private DimensionSchema toDimensionSchema(ColumnSpec column, @Nullable Dimension | |
| if (ColumnType.NESTED_DATA.equals(druidType)) { | ||
| return new NestedDataColumnSchema(column.name(), NestedDataColumnSchema.DEFAULT_FORMAT_VERSION); | ||
| } | ||
| // Other complex types cannot be ingested into a clustered base table: there is no dimension handler for them, | ||
| // and clustered base tables have no aggregators to produce them. | ||
| if (druidType.is(ValueType.COMPLEX)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove the special case for |
||
| return complexDimensionSchema(column.name(), druidType); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Reject handler schemas that change the declared type This path accepts the provider's DimensionSchema without checking that schema.getColumnType() equals the catalog's declared complex type. The new test demonstrates the problem by declaring COMPLEX but producing a DoubleDimensionSchema. During ingestion, DimensionSchema.getDimensionHandler() then selects the double handler from that returned schema, so complex values fail conversion or are stored as a type that contradicts the sealed catalog schema. Apply the same type-consistency check used for custom schemas before accepting the provider result. |
||
| } | ||
| throw InvalidInput.exception( | ||
| "column [%s] has unsupported type [%s] for a clustered base table; supported types are primitive, primitive" | ||
| + " array, and COMPLEX<json> columns", | ||
| "column [%s] has unsupported type [%s] for a clustered base table", | ||
| column.name(), | ||
| druidType | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve a complex column through its registered {@link org.apache.druid.segment.DimensionHandler}, so that any | ||
| * complex type which can be stored as a dimension may be declared, including types contributed by extensions. The | ||
| * handler is looked up by the complex type name, so the schema it returns is specific to the declared type. | ||
| * <p> | ||
| * The returned schema is checked against the declared type before it is accepted. A schema selects its own handler | ||
| * at ingest time (via {@link DimensionSchema#getDimensionHandler()}, which reads | ||
| * {@link DimensionSchema#getColumnType()}), so a schema of some other type would quietly store the column as that | ||
| * type instead, contradicting the declared schema that queries are validated and coerced against. | ||
| */ | ||
| private static DimensionSchema complexDimensionSchema(String name, ColumnType druidType) | ||
| { | ||
| final DimensionSchema schema; | ||
| try { | ||
| schema = DimensionHandlerUtils.getComplexDimensionSchema(name, druidType); | ||
| } | ||
| catch (ISE e) { | ||
| // No handler is registered for this complex type, which usually means the extension that defines it is not | ||
| // loaded on whichever service is validating the spec. | ||
| throw InvalidInput.exception( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the errors in DimensionHandlerUtils are made more friendly then this catch + rethrow won't be needed. |
||
| "column [%s] has type [%s], which cannot be stored as a dimension of a clustered base table; if this type" | ||
| + " comes from an extension, check that the extension is loaded", | ||
| name, | ||
| druidType | ||
| ); | ||
| } | ||
| if (!druidType.equals(schema.getColumnType())) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Reject handler schemas that rename the column The new guard validates only the returned schema's type. A provider can still return the correct complex type under a different column name, which createSpec accepts verbatim. Downstream clustered ingestion then reads row.getRaw(schema.getName()), silently storing nulls for the declared catalog column and exposing the provider-chosen name instead. Validate name.equals(schema.getName()) alongside the type. |
||
| throw InvalidInput.exception( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Push this check up to |
||
| "column [%s] has type [%s], but the dimension handler registered for that type produced a schema of type" | ||
| + " [%s]; a column cannot be stored as a type other than the one it declares", | ||
| name, | ||
| druidType, | ||
| schema.getColumnType() | ||
| ); | ||
| } | ||
| return schema; | ||
| } | ||
|
|
||
| private void validateColumnSchemaCustomization( | ||
| ColumnSpec column, | ||
| DimensionSchema customSchema, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this error was pre-existing, but still, it's a funny error for someone to get if they provide an invalid complex type. Consider rewording it to include
dimensionName, to be anInvalidInput, and to say something more user friendly likeComplex type[%s] for dimension[%s] is not a valid type.