FrankChen021 commented on code in PR #19711:
URL: https://github.com/apache/druid/pull/19711#discussion_r3622002875
##########
sql/src/main/java/org/apache/druid/sql/calcite/planner/DruidSqlValidator.java:
##########
@@ -536,20 +540,25 @@ private RelDataType validateTargetType(
// disable sealed mode validation if catalog validation is disabled.
final boolean isStrict = tableMetadata.isSealed();
+ // Base table layouts (which require 'sealed') may declare ingest-time
virtual columns; the columns those virtual
+ // columns consume are allowed in the source even though they are not
stored (e.g. a raw column feeding an
+ // expression that computes a stored clustering column).
+ final Set<String> baseTableVirtualColumnInputs =
getBaseTableVirtualColumnInputs(tableMetadata.baseTableMetadata());
Review Comment:
[P1] Reject source columns shadowed by base-table virtual columns
Validation accepts a direct output such as `tenant_lower` without requiring
the virtual column input `tenant`, because the output is itself a declared
catalog column. During indexing, virtual selectors shadow physical columns of
the same name, so `lower(tenant)` reads a missing input and silently replaces
the supplied clustering/stored value with null. Require the virtual inputs
whenever its output is selected, or reject direct writes to virtual-column
outputs.
##########
server/src/main/java/org/apache/druid/catalog/model/ClusteredValueGroupsBaseTableMetadata.java:
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.druid.catalog.model;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+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.segment.VirtualColumns;
+import org.apache.druid.segment.column.ColumnType;
+import org.apache.druid.utils.CollectionUtils;
+
+import javax.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Catalog layout metadata for {@link
ClusteredValueGroupsBaseTableProjectionSpec} base tables. Declares the
+ * {@link #clusteringColumns} (names of declared catalog columns that rows are
clustered by) and optional
+ * {@link #virtualColumns} that compute stored columns at ingest time;
everything else about the physical spec
+ * (column names, types, and order) is taken from the catalog column list by
{@link #createSpec(List)}. The declared
+ * column order is the physical segment order, so, mirroring the physical
spec, the clustering columns must be
+ * declared as the leading prefix of the column list.
+ */
+@JsonTypeName(ClusteredValueGroupsBaseTableMetadata.TYPE_NAME)
+public class ClusteredValueGroupsBaseTableMetadata implements
DatasourceBaseTableMetadata
+{
+ public static final String TYPE_NAME =
ClusteredValueGroupsBaseTableProjectionSpec.TYPE_NAME;
+
+ private final List<String> clusteringColumns;
+ private final VirtualColumns virtualColumns;
+
+ @JsonCreator
+ public ClusteredValueGroupsBaseTableMetadata(
+ @JsonProperty("clusteringColumns") List<String> clusteringColumns,
+ @JsonProperty("virtualColumns") @Nullable VirtualColumns virtualColumns
+ )
+ {
+ this.clusteringColumns = clusteringColumns == null ?
Collections.emptyList() : clusteringColumns;
+ this.virtualColumns = virtualColumns == null ? VirtualColumns.EMPTY :
virtualColumns;
+ }
+
+ @Override
+ @JsonProperty("type")
+ public String getType()
+ {
+ return TYPE_NAME;
+ }
+
+ @JsonProperty("clusteringColumns")
+ public List<String> getClusteringColumns()
+ {
+ return clusteringColumns;
+ }
+
+ @Override
+ @JsonProperty("virtualColumns")
+ @JsonInclude(JsonInclude.Include.NON_DEFAULT)
+ public VirtualColumns getVirtualColumns()
+ {
+ return virtualColumns;
+ }
+
+ /**
+ * Creates the physical spec from the declared catalog columns, used
verbatim: the declared column order is the
+ * physical segment order, so the clustering columns must be declared as the
leading prefix of the column list (in
+ * {@link #clusteringColumns} order) — anything else is a validation error,
mirroring the physical spec. Column
+ * types come from the catalog column types ({@link Columns#druidType};
untyped columns default to STRING, mirroring
+ * {@link Columns#convertSignature}). Every clustering column must be a
declared column — a clustering column
+ * computed by a virtual column at ingest time is still a stored, queryable
column, so it too must appear in the
+ * column list. All ordering and layout rules (leading prefix, allowed
clustering types, explicit non-clustering
+ * {@code __time}, no duplicates) are enforced by the spec itself.
+ */
+ @Override
+ public ClusteredValueGroupsBaseTableProjectionSpec
createSpec(List<ColumnSpec> columns)
+ {
+ if (CollectionUtils.isNullOrEmpty(columns)) {
+ throw InvalidInput.exception(
+ "Cannot define a [%s] base table without declared columns; the
catalog column list defines the table schema",
+ TYPE_NAME
+ );
+ }
+ final Set<String> declaredNames = new HashSet<>();
+ final List<DimensionSchema> specColumns = new ArrayList<>(columns.size());
+ for (ColumnSpec column : columns) {
+ declaredNames.add(column.name());
+ specColumns.add(toDimensionSchema(column));
+ }
+ for (String clusteringColumn : clusteringColumns) {
+ if (!declaredNames.contains(clusteringColumn)) {
+ throw InvalidInput.exception(
+ "clustering column [%s] is not a declared column; clustering
columns must be declared as the leading"
+ + " prefix of the table's column list, including columns computed
by a virtual column at ingest time"
+ + " (they are stored columns)",
+ clusteringColumn
+ );
+ }
+ }
+ return ClusteredValueGroupsBaseTableProjectionSpec.builder()
+
.virtualColumns(virtualColumns)
+ .columns(specColumns)
+
.clusteringColumns(clusteringColumns)
+ .build();
+ }
+
+ private static DimensionSchema toDimensionSchema(ColumnSpec column)
+ {
+ ColumnType druidType = Columns.druidType(column);
+ if (druidType == null) {
+ druidType = ColumnType.STRING;
+ }
+ return DimensionSchema.getDefaultSchemaForBuiltInType(column.name(),
druidType);
Review Comment:
[P1] Preserve non-scalar catalog types
`getDefaultSchemaForBuiltInType` falls through for ARRAY and COMPLEX types
to `AutoTypeColumnSchema.of`, whose `castToType` is null, so the explicit
catalog type is discarded. For example, an all-null `VARCHAR ARRAY` batch has
no values from which to infer the array type and can be written with a scalar
or otherwise incompatible physical signature. Construct the ingestion schema
with the declared catalog type (and the appropriate complex-type handler)
instead of leaving it untyped.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]