This is an automated email from the ASF dual-hosted git repository.

xiangfu0 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new 1fa59ea0adb Render BOOLEAN keys as booleans when selecting a whole 
OPEN_STRUCT column (#20337)
1fa59ea0adb is described below

commit 1fa59ea0adbe01d4f917610b05bf1d9a8eb088a1
Author: RAGHVENDRA KUMAR YADAV <[email protected]>
AuthorDate: Sat Nov 21 02:35:04 2026 -0800

    Render BOOLEAN keys as booleans when selecting a whole OPEN_STRUCT column 
(#20337)
    
    * Render BOOLEAN keys as booleans when selecting a whole OPEN_STRUCT column
    
    A BOOLEAN key is stored as INT 0/1, and the reconstruction behind SELECT
    open_struct_col hands back the stored number, so the document read
    {"flag":1} where col['flag'] returns true.
    
    ProjectionBlock now reads the materialized keys declared BOOLEAN from the
    per-key data sources and renders their 0/1 as false/true. A boolean that 
lives
    in the sparse blob is already a JSON boolean and is unchanged. The
    reconstruction shared with the seal path is not touched.
    
    * Render declared OPEN_STRUCT booleans consistently across storage tiers
    
    Handle sparse numeric values and multi-value children using the logical 
BOOLEAN result conversion. Size the dictionary reader buffer for a materialized 
multi-value child.
    
    Reproduce by selecting the whole OPEN_STRUCT column in 
ProjectionBlockOpenStructTest.testBooleanKeysInWholeDocument, which seals dense 
and sparse boolean fields in one segment.
    
    ---------
    
    Co-authored-by: Xiang Fu <[email protected]>
---
 .../core/operator/blocks/ProjectionBlock.java      | 47 +++++++++++----
 .../blocks/ProjectionBlockOpenStructTest.java      | 69 ++++++++++++++++++++--
 .../openstruct/ImmutableOpenStructDataSource.java  |  4 +-
 3 files changed, 104 insertions(+), 16 deletions(-)

diff --git 
a/pinot-core/src/main/java/org/apache/pinot/core/operator/blocks/ProjectionBlock.java
 
b/pinot-core/src/main/java/org/apache/pinot/core/operator/blocks/ProjectionBlock.java
index 7b9c1d2a625..ba5cab0ab58 100644
--- 
a/pinot-core/src/main/java/org/apache/pinot/core/operator/blocks/ProjectionBlock.java
+++ 
b/pinot-core/src/main/java/org/apache/pinot/core/operator/blocks/ProjectionBlock.java
@@ -21,9 +21,11 @@ package org.apache.pinot.core.operator.blocks;
 import com.fasterxml.jackson.core.type.TypeReference;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import javax.annotation.Nullable;
 import org.apache.pinot.common.request.context.ExpressionContext;
 import org.apache.pinot.core.common.BlockValSet;
@@ -35,7 +37,10 @@ import org.apache.pinot.segment.spi.datasource.MapDataSource;
 import org.apache.pinot.segment.spi.datasource.OpenStructDataSource;
 import 
org.apache.pinot.segment.spi.datasource.OpenStructDataSource.MapValueReader;
 import org.apache.pinot.spi.data.ComplexFieldSpec;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
 import org.apache.pinot.spi.data.OpenStructKeyFlattener;
+import org.apache.pinot.spi.utils.BooleanUtils;
 import org.apache.pinot.spi.utils.BytesUtils;
 import org.apache.pinot.spi.utils.JsonUtils;
 
@@ -93,10 +98,11 @@ public class ProjectionBlock implements ValueBlock {
     int numDocs = getNumDocs();
     int[] docIds = getDocIds();
     String[] documents = new String[numDocs];
+    Set<String> booleanKeys = booleanKeys(openStructDataSource);
     try (MapValueReader reader = openStructDataSource.openMapValueReader()) {
       for (int i = 0; i < numDocs; i++) {
         Map<String, Object> document = reader.getMapValue(docIds[i]);
-        documents[i] = document == null ? "{}" : 
JsonUtils.objectToString(renderDocument(document));
+        documents[i] = document == null ? "{}" : 
JsonUtils.objectToString(renderDocument(document, booleanKeys));
       }
     } catch (IOException e) {
       throw new RuntimeException("Failed to read OPEN_STRUCT column: " + 
column, e);
@@ -111,16 +117,33 @@ public class ProjectionBlock implements ValueBlock {
   /// the shape the source had, so it wins and the paths into it are dropped. 
`.` is an ordinary key character with
   /// no escape, and that is exactly what makes the container's own entry the 
thing that disambiguates: a dotted key
   /// whose prefix is not itself a key was never a path, so it stays a key 
spelled with a dot.
-  private static Map<String, Object> renderDocument(Map<String, Object> 
document) {
+  ///
+  /// The top-level keys use the segment's declared types. Nested objects 
retain their original JSON types.
+  private static Map<String, Object> renderDocument(Map<String, Object> 
document, Set<String> booleanKeys) {
     Map<String, Object> rendered = new LinkedHashMap<>(document.size());
     for (Map.Entry<String, Object> entry : document.entrySet()) {
-      if (!isPathIntoPresentObject(entry.getKey(), document)) {
-        rendered.put(entry.getKey(), renderValue(entry.getValue()));
+      String key = entry.getKey();
+      if (!isPathIntoPresentObject(key, document)) {
+        rendered.put(key, renderValue(entry.getValue(), 
booleanKeys.contains(key)));
       }
     }
     return rendered;
   }
 
+  /// Both dense children and the sparse JSON can contain stored integers for 
declared BOOLEAN keys. On older
+  /// segments a dense child can still report INT, which is also the type 
returned by a direct key projection.
+  private static Set<String> booleanKeys(OpenStructDataSource 
openStructDataSource) {
+    Set<String> booleanKeys = new HashSet<>();
+    for (Map.Entry<String, FieldSpec> entry : 
openStructDataSource.getFieldSpec().getChildFieldSpecs().entrySet()) {
+      String key = entry.getKey();
+      if (entry.getValue().getDataType() == DataType.BOOLEAN && 
(!openStructDataSource.isMaterialized(key)
+          || openStructDataSource.getDataSourceMetadata(key).getDataType() == 
DataType.BOOLEAN)) {
+        booleanKeys.add(key);
+      }
+    }
+    return booleanKeys;
+  }
+
   /// Whether `key` is a path into an object that the document also carries 
whole. `configApi.timeTaken` is, when
   /// `configApi` is an object of the document holding `timeTaken`; a key the 
document literally spells with a dot
   /// is not.
@@ -175,7 +198,7 @@ public class ProjectionBlock implements ValueBlock {
       return text;
     }
     try {
-      return renderValue(JsonUtils.stringToObject(text, 
OBJECT_TYPE_REFERENCE));
+      return renderValue(JsonUtils.stringToObject(text, 
OBJECT_TYPE_REFERENCE), false);
     } catch (IOException e) {
       return text;
     }
@@ -190,16 +213,18 @@ public class ProjectionBlock implements ValueBlock {
   }
 
   /// Values as JSON renders them, recursing so a nested object is cleaned up 
the same way the top level is.
-  /// Only BYTES needs a hand: Jackson would base64 it, while every other way 
of reading this value out of Pinot
-  /// -- `col['key']` included -- gives hex.
+  /// Numeric BOOLEAN values use Pinot's stored representation. BYTES values 
use the same hex encoding as key reads.
   @Nullable
-  private static Object renderValue(@Nullable Object value) {
+  private static Object renderValue(@Nullable Object value, boolean 
booleanKey) {
+    if (booleanKey && value instanceof Number) {
+      return BooleanUtils.toBoolean(value);
+    }
     if (value instanceof byte[] bytes) {
       return BytesUtils.toHexString(bytes);
     }
     if (value instanceof Map<?, ?> map) {
       // Keys re-typed, values left raw: renderDocument renders each one as it 
walks them.
-      return renderDocument(asStringKeyedMap(map));
+      return renderDocument(asStringKeyedMap(map), Set.of());
     }
     if (value instanceof String text) {
       // A container reaches here as the JSON text the flattener stored, so 
inserting it as a string would nest a
@@ -211,14 +236,14 @@ public class ProjectionBlock implements ValueBlock {
     if (value instanceof List<?> list) {
       List<Object> rendered = new ArrayList<>(list.size());
       for (Object element : list) {
-        rendered.add(renderValue(element));
+        rendered.add(renderValue(element, booleanKey));
       }
       return rendered;
     }
     if (value instanceof Object[] array) {
       List<Object> rendered = new ArrayList<>(array.length);
       for (Object element : array) {
-        rendered.add(renderValue(element));
+        rendered.add(renderValue(element, booleanKey));
       }
       return rendered;
     }
diff --git 
a/pinot-core/src/test/java/org/apache/pinot/core/operator/blocks/ProjectionBlockOpenStructTest.java
 
b/pinot-core/src/test/java/org/apache/pinot/core/operator/blocks/ProjectionBlockOpenStructTest.java
index 4cdaef33aca..3d331e8f7dd 100644
--- 
a/pinot-core/src/test/java/org/apache/pinot/core/operator/blocks/ProjectionBlockOpenStructTest.java
+++ 
b/pinot-core/src/test/java/org/apache/pinot/core/operator/blocks/ProjectionBlockOpenStructTest.java
@@ -18,18 +18,36 @@
  */
 package org.apache.pinot.core.operator.blocks;
 
+import java.io.File;
+import java.nio.file.Files;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import org.apache.commons.io.FileUtils;
 import org.apache.pinot.core.common.BlockValSet;
 import org.apache.pinot.core.common.DataBlockCache;
 import org.apache.pinot.core.common.DataFetcher;
+import 
org.apache.pinot.segment.local.indexsegment.immutable.ImmutableSegmentLoader;
+import org.apache.pinot.segment.local.segment.readers.GenericRowRecordReader;
+import org.apache.pinot.segment.local.segment.readers.PinotSegmentUtil;
+import org.apache.pinot.segment.spi.ImmutableSegment;
 import org.apache.pinot.segment.spi.datasource.DataSource;
 import org.apache.pinot.segment.spi.datasource.DataSourceMetadata;
 import org.apache.pinot.segment.spi.datasource.OpenStructDataSource;
 import org.apache.pinot.segment.spi.index.reader.ForwardIndexReader;
-import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.config.table.FieldConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.data.ComplexFieldSpec;
+import org.apache.pinot.spi.data.DimensionFieldSpec;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.apache.pinot.spi.utils.ReadMode;
+import org.apache.pinot.spi.utils.builder.TableConfigBuilder;
 import org.testng.annotations.Test;
 
 import static org.mockito.Mockito.doReturn;
@@ -54,7 +72,10 @@ public class ProjectionBlockOpenStructTest {
 
   /// OPEN_STRUCT parent: an empty index container, so {@code 
getForwardIndex()} is null.
   private static OpenStructDataSource mockOpenStructDataSource() {
-    return mock(OpenStructDataSource.class);
+    OpenStructDataSource dataSource = mock(OpenStructDataSource.class);
+    when(dataSource.getFieldSpec()).thenReturn(new 
ComplexFieldSpec(OPEN_STRUCT_COLUMN, DataType.OPEN_STRUCT, true,
+        Map.of()));
+    return dataSource;
   }
 
   private static DataSource mockPlainDataSource() {
@@ -64,7 +85,7 @@ public class ProjectionBlockOpenStructTest {
     doReturn(forwardIndex).when(dataSource).getForwardIndex();
     DataSourceMetadata metadata = mock(DataSourceMetadata.class);
     when(metadata.isSingleValue()).thenReturn(true);
-    when(metadata.getDataType()).thenReturn(FieldSpec.DataType.LONG);
+    when(metadata.getDataType()).thenReturn(DataType.LONG);
     when(dataSource.getDataSourceMetadata()).thenReturn(metadata);
     return dataSource;
   }
@@ -128,7 +149,7 @@ public class ProjectionBlockOpenStructTest {
   @Test
   public void testSelectingOpenStructParentReadsTheWholeDocument() {
     BlockValSet values = project(Map.of("errors", 3L));
-    assertEquals(values.getValueType(), FieldSpec.DataType.STRING);
+    assertEquals(values.getValueType(), DataType.STRING);
     assertTrue(values.isSingleValue());
     assertEquals(values.getStringValuesSV()[0], "{\"errors\":3}");
   }
@@ -259,6 +280,46 @@ public class ProjectionBlockOpenStructTest {
     assertEquals(project(Map.of("tags", List.of("a", 
"b"))).getStringValuesSV()[0], "{\"tags\":[\"a\",\"b\"]}");
   }
 
+  @Test
+  public void testBooleanKeysInWholeDocument()
+      throws Exception {
+    ComplexFieldSpec parent = new ComplexFieldSpec(OPEN_STRUCT_COLUMN, 
DataType.OPEN_STRUCT, true, Map.of(
+        "flag", new DimensionFieldSpec("flag", DataType.BOOLEAN, true),
+        "off", new DimensionFieldSpec("off", DataType.BOOLEAN, true),
+        "noncanonical", new DimensionFieldSpec("noncanonical", 
DataType.BOOLEAN, true),
+        "denseFlags", new DimensionFieldSpec("denseFlags", DataType.BOOLEAN, 
false),
+        "sparseFlags", new DimensionFieldSpec("sparseFlags", DataType.BOOLEAN, 
false)));
+    Schema schema = new 
Schema.SchemaBuilder().setSchemaName("bools").addField(parent).build();
+    FieldConfig field = new 
FieldConfig.Builder(OPEN_STRUCT_COLUMN).withIndexes(JsonUtils.objectToJsonNode(
+        Map.of("open_struct", Map.of("maxDenseKeys", 3, "denseKeys", 
List.of("flag", "noncanonical", "denseFlags")))))
+        .build();
+    TableConfig table = new 
TableConfigBuilder(TableType.OFFLINE).setTableName("bools")
+        .setFieldConfigList(List.of(field)).build();
+    GenericRow row = new GenericRow();
+    row.putValue(OPEN_STRUCT_COLUMN, Map.of("flag", true, "off", false, 
"noncanonical", 2,
+        "denseFlags", List.of(true, false), "sparseFlags", List.of(false, 
true), "count", 1,
+        "nested", Map.of("flag", 0)));
+
+    File tempDir = Files.createTempDirectory("openStructBoolean").toFile();
+    try {
+      File indexDir = PinotSegmentUtil.createSegment(table, schema, "bools", 
tempDir.getAbsolutePath(),
+          new GenericRowRecordReader(List.of(row)));
+      ImmutableSegment segment = ImmutableSegmentLoader.load(indexDir, 
ReadMode.mmap);
+      try {
+        OpenStructDataSource source = (OpenStructDataSource) 
segment.getDataSource(OPEN_STRUCT_COLUMN);
+        assertEquals(source.getDataSources().keySet(), Set.of("flag", 
"noncanonical", "denseFlags"));
+        String document = projectWith(source).getStringValuesSV()[0];
+        assertEquals(JsonUtils.stringToMap(document), Map.of("flag", true, 
"off", false, "noncanonical", false,
+            "denseFlags", List.of(true, false), "sparseFlags", List.of(false, 
true), "count", 1,
+            "nested", Map.of("flag", 0)));
+      } finally {
+        segment.destroy();
+      }
+    } finally {
+      FileUtils.deleteDirectory(tempDir);
+    }
+  }
+
   /// Projects a single-row block whose OPEN_STRUCT column reconstructs to 
`document`.
   private static BlockValSet project(Map<String, Object> document) {
     OpenStructDataSource openStruct = mockOpenStructDataSource();
diff --git 
a/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/ImmutableOpenStructDataSource.java
 
b/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/ImmutableOpenStructDataSource.java
index f37ea1bae61..0372a420a95 100644
--- 
a/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/ImmutableOpenStructDataSource.java
+++ 
b/pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/ImmutableOpenStructDataSource.java
@@ -313,8 +313,10 @@ public class ImmutableOpenStructDataSource extends 
BaseDataSource implements Ope
       if (fwdReader == null) {
         return null;
       }
+      int maxNumValues = !fwdReader.isSingleValue() && 
fwdReader.isDictionaryEncoded()
+          ? dataSource.getDataSourceMetadata().getMaxNumValuesPerMVEntry() : 0;
       return new PinotSegmentColumnReader(key, fwdReader, 
dataSource.getDictionary(),
-          dataSource.getNullValueVector(), 0);
+          dataSource.getNullValueVector(), maxNumValues);
     }
 
     @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to