Skip to content

Commit 6b6e19d

Browse files
morningmanclaude
andcommitted
[fix](nereids) Keep VARBINARY casts left for BE working in scans, IN subqueries and BE folding
### What problem does this PR solve? Issue Number: None Related PR: #68968 Problem Summary: FE leaves a cast of VARBINARY bytes to a string type for BE when no FE string literal can carry its result: bytes that are not valid UTF-8, or text with a NUL byte or a supplementary character. Master always folded such a cast to the hex text of the bytes, so three places had never met it in a plan: - BE: ScanOperator digests every conjunct for the condition cache, which is on by default, and VLiteral::get_digest hashed the literal's column, which ColumnVarbinary does not support. WHERE s = CAST(X'FF' AS STRING) failed with "update_xxHash_with_value is not supported for ColumnVarbinary". A literal whose type contains VARBINARY now returns digest 0, which keeps its conjunct out of the condition cache, as for an IN predicate. - FE: CheckAfterRewrite rejected a hash or mark join condition with any VARBINARY sub-expression, so CAST(X'610062' AS STRING) IN (SELECT s FROM t) on a nullable s failed with "varbinary type could not in join equal conditions". It now rejects a condition only when a comparison in it has a VARBINARY operand. - FE: with enable_fold_constant_by_be=true, BE folded such a cast when it was the largest constant part of a predicate, and FoldConstantRuleOnBE.getResultExpression decoded the result as UTF-8 with U+FFFD for invalid bytes, so WHERE s = CAST(X'FF' AS STRING) became s = '�'. shouldSkipFold now skips a cast from a type that contains VARBINARY, as it already skips from_base64 for the same reason. The .out of test_binary_hex_function recorded the folded hex text for sql_25 (61 for X'61'), while its sql_24 already expected the bytes for the same values read from a column; it is regenerated. ### Release note None ### Check List (For Author) - Test: Regression test (test_varbinary_cast_fold runs every query with FE folding, with FE and BE folding, and without folding, with the condition cache on; test_binary_hex_function regenerated) and Unit Test (FE: FoldConstantRuleOnBETest.testSkipCastFromVarBinary, VarBinarySqlSupportTest.testJoinConditionComparingStringsCastFromBinary; BE: TEST_VEXPR.VarbinaryLiteralHasNoConditionCacheDigest) - Behavior changed: Yes. A scan conjunct with a VARBINARY literal does not use the condition cache, and a cast from VARBINARY is not folded on BE. - Does this need documentation: No Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 0c522c3 commit 6b6e19d

10 files changed

Lines changed: 295 additions & 58 deletions

File tree

‎be/src/exprs/vliteral.cpp‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@
2626
#include <algorithm>
2727
#include <ostream>
2828

29+
#include "core/assert_cast.h"
2930
#include "core/block/block.h"
3031
#include "core/column/column.h"
32+
#include "core/data_type/data_type_array.h"
3133
#include "core/data_type/data_type_decimal.h"
34+
#include "core/data_type/data_type_map.h"
35+
#include "core/data_type/data_type_nullable.h"
36+
#include "core/data_type/data_type_struct.h"
3237
#include "core/field.h"
3338
#include "core/types.h"
3439
#include "exprs/aggregate/aggregate_function.h"
@@ -37,6 +42,30 @@ namespace doris {
3742

3843
class VExprContext;
3944

45+
namespace {
46+
47+
bool contains_varbinary_type(const DataTypePtr& input) {
48+
const auto type = remove_nullable(input);
49+
switch (type->get_primitive_type()) {
50+
case TYPE_VARBINARY:
51+
return true;
52+
case TYPE_ARRAY:
53+
return contains_varbinary_type(assert_cast<const DataTypeArray&>(*type).get_nested_type());
54+
case TYPE_MAP: {
55+
const auto& map = assert_cast<const DataTypeMap&>(*type);
56+
return contains_varbinary_type(map.get_key_type()) ||
57+
contains_varbinary_type(map.get_value_type());
58+
}
59+
case TYPE_STRUCT:
60+
return std::ranges::any_of(assert_cast<const DataTypeStruct&>(*type).get_elements(),
61+
contains_varbinary_type);
62+
default:
63+
return false;
64+
}
65+
}
66+
67+
} // namespace
68+
4069
Status VLiteral::prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) {
4170
RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
4271
return Status::OK();
@@ -100,6 +129,11 @@ bool VLiteral::equals(const VExpr& other) {
100129
}
101130

102131
uint64_t VLiteral::get_digest(uint64_t seed) const {
132+
// ColumnVarbinary does not hash. Zero keeps the conjunct out of the condition cache, as it does for an IN
133+
// predicate; FE leaves such literals in a scan conjunct when it cannot fold their cast to a string.
134+
if (contains_varbinary_type(_data_type)) {
135+
return 0;
136+
}
103137
_column_ptr->update_xxHash_with_value(0, 1, seed, nullptr);
104138
return seed;
105139
}

‎be/test/exprs/vexpr_test.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@
3434
#include <string>
3535
#include <type_traits>
3636

37+
#include "common/cast_set.h"
3738
#include "common/object_pool.h"
3839
#include "core/block/block.h"
3940
#include "core/column/column_nothing.h"
4041
#include "core/column/column_vector.h"
42+
#include "core/data_type/data_type_array.h"
4143
#include "core/data_type/data_type_factory.hpp"
4244
#include "core/data_type/data_type_nothing.h"
45+
#include "core/data_type/data_type_nullable.h"
4346
#include "core/data_type/data_type_number.h"
47+
#include "core/data_type/data_type_string.h"
48+
#include "core/data_type/data_type_varbinary.h"
4449
#include "core/data_type/define_primitive_type.h"
4550
#include "core/field.h"
4651
#include "core/types.h"
@@ -50,9 +55,11 @@
5055
#include "core/value/timestamptz_value.h"
5156
#include "core/value/vdatetime_value.h"
5257
#include "exec/common/util.hpp"
58+
#include "exec/scan/scanner.h"
5359
#include "exprs/function/cast/cast_to_date_or_datetime_impl.hpp"
5460
#include "exprs/function/cast/cast_to_datetimev2_impl.hpp"
5561
#include "exprs/function/cast/cast_to_datev2_impl.hpp"
62+
#include "exprs/vcast_expr.h"
5663
#include "exprs/vexpr_context.h"
5764
#include "exprs/vliteral.h"
5865
#include "gtest/gtest_pred_impl.h"
@@ -833,6 +840,44 @@ TEST(TEST_VEXPR, LITERALTEST) {
833840
config::allow_zero_date = false;
834841
}
835842

843+
// ColumnVarbinary does not hash, so a literal with a VARBINARY value reports no condition cache digest instead
844+
// of failing the scan. FE leaves such a literal in a scan conjunct for CAST(X'FF' AS STRING), whose bytes it
845+
// cannot fold into a string literal.
846+
TEST(TEST_VEXPR, VarbinaryLiteralHasNoConditionCacheDigest) {
847+
using namespace doris;
848+
const std::string bytes("\xFF\0a", 3);
849+
const auto binary = Field::create_field<TYPE_VARBINARY>(
850+
StringView(bytes.data(), cast_set<uint32_t>(bytes.size())));
851+
const DataTypePtr binary_type = std::make_shared<DataTypeVarbinary>();
852+
EXPECT_EQ(0, VLiteral(binary_type, binary).get_digest(12345));
853+
EXPECT_EQ(0, VLiteral(make_nullable(binary_type), binary).get_digest(12345));
854+
const DataTypePtr binary_array_type =
855+
std::make_shared<DataTypeArray>(make_nullable(binary_type));
856+
EXPECT_EQ(0, VLiteral(binary_array_type, Field::create_field<TYPE_ARRAY>(Array {binary}))
857+
.get_digest(12345));
858+
859+
// The zero reaches the conjunct through the cast above the literal, so the scan skips the cache.
860+
TExprNode cast_node;
861+
cast_node.__set_node_type(TExprNodeType::CAST_EXPR);
862+
cast_node.__set_type(create_type_desc(PrimitiveType::TYPE_STRING));
863+
cast_node.__set_num_children(1);
864+
auto cast_expr = VCastExpr::create_shared(cast_node);
865+
cast_expr->add_child(VLiteral::create_shared(binary_type, binary));
866+
const auto conjunct = VExprContext::create_shared(cast_expr);
867+
EXPECT_EQ(0, conjunct->get_digest(12345));
868+
EXPECT_EQ(0, Scanner::TEST_build_condition_cache_digest(12345, {conjunct}));
869+
870+
// Other literals still digest their value.
871+
const auto text = Field::create_field<TYPE_STRING>(std::string("abc"));
872+
const DataTypePtr string_type = std::make_shared<DataTypeString>();
873+
const uint64_t text_digest = VLiteral(string_type, text).get_digest(12345);
874+
EXPECT_NE(0, text_digest);
875+
EXPECT_EQ(text_digest, VLiteral(string_type, text).get_digest(12345));
876+
EXPECT_NE(text_digest,
877+
VLiteral(string_type, Field::create_field<TYPE_STRING>(std::string("abd")))
878+
.get_digest(12345));
879+
}
880+
836881
namespace doris {
837882

838883
// A concrete VExpr that returns a column provided at construction time.

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/CheckAfterRewrite.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.doris.nereids.rules.Rule;
2424
import org.apache.doris.nereids.rules.RuleType;
2525
import org.apache.doris.nereids.trees.expressions.Alias;
26+
import org.apache.doris.nereids.trees.expressions.ComparisonPredicate;
2627
import org.apache.doris.nereids.trees.expressions.Expression;
2728
import org.apache.doris.nereids.trees.expressions.Match;
2829
import org.apache.doris.nereids.trees.expressions.Slot;
@@ -192,20 +193,27 @@ private void checkMetricTypeIsUsedCorrectly(Plan plan) {
192193
} else if (plan instanceof LogicalJoin) {
193194
LogicalJoin<?, ?> join = (LogicalJoin<?, ?>) plan;
194195
for (Expression conjunct : join.getHashJoinConjuncts()) {
195-
if (conjunct.anyMatch(e -> ((Expression) e).getDataType().isVarBinaryType())) {
196+
if (comparesVarBinary(conjunct)) {
196197
throw new AnalysisException(
197198
"varbinary type could not in join equal conditions: " + conjunct.toSql());
198199
}
199200
}
200201
for (Expression conjunct : join.getMarkJoinConjuncts()) {
201-
if (conjunct.anyMatch(e -> ((Expression) e).getDataType().isVarBinaryType())) {
202+
if (comparesVarBinary(conjunct)) {
202203
throw new AnalysisException(
203204
"varbinary type could not in join equal conditions: " + conjunct.toSql());
204205
}
205206
}
206207
}
207208
}
208209

210+
// BE cannot hash or compare VARBINARY values, but a join condition may compare strings cast from VARBINARY,
211+
// such as CAST(X'FF' AS STRING) = s, so only the compared operands are checked.
212+
private static boolean comparesVarBinary(Expression conjunct) {
213+
return conjunct.anyMatch(e -> e instanceof ComparisonPredicate && ((ComparisonPredicate) e).children()
214+
.stream().anyMatch(operand -> operand.getDataType().isVarBinaryType()));
215+
}
216+
209217
private void checkMatchIsUsedCorrectly(Plan plan) {
210218
for (Expression expression : plan.getExpressions()) {
211219
if (expression instanceof Match) {

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnBE.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
import org.apache.doris.nereids.types.StructField;
8080
import org.apache.doris.nereids.types.StructType;
8181
import org.apache.doris.nereids.types.TimeStampNsType;
82+
import org.apache.doris.nereids.util.TypeCoercionUtils;
8283
import org.apache.doris.proto.InternalService;
8384
import org.apache.doris.proto.InternalService.PConstantExprResult;
8485
import org.apache.doris.proto.Types.PScalarType;
@@ -298,6 +299,13 @@ private static boolean shouldSkipFold(Expression expr) {
298299
cast.isStrict() || SessionVariable.enableStrictCast()) || cast.child().isNullLiteral()) {
299300
return true;
300301
}
302+
// FoldConstantRuleOnFE leaves a cast of VARBINARY bytes for BE when no FE string literal can carry
303+
// its result (see VarBinaryLiteral.castValueToString). Folding the cast here would build that literal
304+
// from the BE result anyway: getResultExpression decodes invalid UTF-8 as U+FFFD, and FE string
305+
// code treats a NUL byte or a supplementary character unlike BE.
306+
if (TypeCoercionUtils.hasVarBinaryType(cast.child().getDataType())) {
307+
return true;
308+
}
301309
}
302310

303311
// This kind of function is often used to change the attributes of columns.

‎fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ public static boolean hasTimeStampNsType(DataType dataType) {
387387
return hasSpecifiedType(dataType, TimeStampNsType.class);
388388
}
389389

390+
public static boolean hasVarBinaryType(DataType dataType) {
391+
return hasSpecifiedType(dataType, VarBinaryType.class);
392+
}
393+
390394
private static boolean hasTimeStampNsCompatibleDateTimeType(DataType dataType) {
391395
return hasSpecifiedType(dataType, type -> type instanceof DateTimeType
392396
|| type instanceof DateTimeV2Type || type instanceof TimeStampTzType);
@@ -2923,27 +2927,12 @@ private static void checkNoVarbinaryComparison(String operation, List<Expression
29232927
// Common binary types support value selection, but BE comparison kernels do not.
29242928
// Check before equal-type shortcuts or casts can hide an unsupported binary argument.
29252929
for (Expression argument : arguments) {
2926-
if (containsVarbinary(argument.getDataType())) {
2930+
if (hasVarBinaryType(argument.getDataType())) {
29272931
throw new AnalysisException(operation + " does not support VARBINARY arguments");
29282932
}
29292933
}
29302934
}
29312935

2932-
private static boolean containsVarbinary(DataType type) {
2933-
if (type instanceof ArrayType) {
2934-
return containsVarbinary(((ArrayType) type).getItemType());
2935-
}
2936-
if (type instanceof StructType) {
2937-
return ((StructType) type).getFields().stream()
2938-
.anyMatch(field -> containsVarbinary(field.getDataType()));
2939-
}
2940-
if (type instanceof MapType) {
2941-
return containsVarbinary(((MapType) type).getKeyType())
2942-
|| containsVarbinary(((MapType) type).getValueType());
2943-
}
2944-
return type.isVarBinaryType();
2945-
}
2946-
29472936
private static Optional<DataType> findCommonBinaryType(DataType left, DataType right) {
29482937
if (left.isVarBinaryType() && right.isVarBinaryType()) {
29492938
return Optional.of(VarBinaryType.createVarBinaryType(

‎fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantRuleOnBETest.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.doris.nereids.rules.expression.rules.FoldConstantRuleOnFE;
2424
import org.apache.doris.nereids.trees.expressions.Add;
2525
import org.apache.doris.nereids.trees.expressions.And;
26+
import org.apache.doris.nereids.trees.expressions.Cast;
2627
import org.apache.doris.nereids.trees.expressions.EqualTo;
2728
import org.apache.doris.nereids.trees.expressions.Expression;
2829
import org.apache.doris.nereids.trees.expressions.IsNull;
@@ -32,8 +33,12 @@
3233
import org.apache.doris.nereids.trees.expressions.literal.Literal;
3334
import org.apache.doris.nereids.trees.expressions.literal.MapLiteral;
3435
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
36+
import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
3537
import org.apache.doris.nereids.trees.expressions.literal.StructLiteral;
3638
import org.apache.doris.nereids.trees.expressions.literal.UuidLiteral;
39+
import org.apache.doris.nereids.trees.expressions.literal.VarBinaryLiteral;
40+
import org.apache.doris.nereids.types.ArrayType;
41+
import org.apache.doris.nereids.types.StringType;
3742
import org.apache.doris.nereids.types.UuidType;
3843
import org.apache.doris.thrift.TExpr;
3944

@@ -88,6 +93,23 @@ void testKeepSupportedSibling() {
8893
collectConstants(new And(new IsNull(uuidResult), supported)));
8994
}
9095

96+
@Test
97+
void testSkipCastFromVarBinary() {
98+
// FE leaves these casts for BE because no FE string literal carries 0xFF. BE folding would build such a
99+
// literal from its result anyway, decoding 0xFF as U+FFFD, so they wait for BE to evaluate them.
100+
VarBinaryLiteral notUtf8 = new VarBinaryLiteral(new byte[] {(byte) 0xFF});
101+
List<Expression> casts = ImmutableList.of(new Cast(notUtf8, StringType.INSTANCE),
102+
new Cast(new ArrayLiteral(ImmutableList.of(notUtf8)), ArrayType.of(StringType.INSTANCE)));
103+
for (Expression cast : casts) {
104+
Assertions.assertEquals(cast, FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(cast, context));
105+
Assertions.assertTrue(collectConstants(cast).isEmpty(), cast.toSql());
106+
}
107+
Expression filter = new EqualTo(casts.get(0), new StringLiteral(String.valueOf((char) 0xFFFD)));
108+
Expression supported = new EqualTo(new Add(new IntegerLiteral(1), new IntegerLiteral(2)),
109+
new IntegerLiteral(3));
110+
Assertions.assertEquals(ImmutableList.of(supported), collectConstants(new And(filter, supported)));
111+
}
112+
91113
private List<Expression> collectConstants(Expression expression) {
92114
Map<String, Expression> constants = new HashMap<>();
93115
Map<String, TExpr> serialized = new HashMap<>();

‎fe/fe-core/src/test/java/org/apache/doris/nereids/types/VarBinarySqlSupportTest.java‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,34 @@
1818
package org.apache.doris.nereids.types;
1919

2020
import org.apache.doris.catalog.Env;
21+
import org.apache.doris.nereids.exceptions.AnalysisException;
22+
import org.apache.doris.nereids.hint.DistributeHint;
23+
import org.apache.doris.nereids.rules.analysis.CheckAfterRewrite;
24+
import org.apache.doris.nereids.trees.expressions.Alias;
25+
import org.apache.doris.nereids.trees.expressions.Cast;
26+
import org.apache.doris.nereids.trees.expressions.EqualTo;
27+
import org.apache.doris.nereids.trees.expressions.Expression;
28+
import org.apache.doris.nereids.trees.expressions.MarkJoinSlotReference;
29+
import org.apache.doris.nereids.trees.expressions.Slot;
2130
import org.apache.doris.nereids.trees.expressions.literal.VarBinaryLiteral;
31+
import org.apache.doris.nereids.trees.plans.DistributeType;
32+
import org.apache.doris.nereids.trees.plans.JoinType;
33+
import org.apache.doris.nereids.trees.plans.Plan;
34+
import org.apache.doris.nereids.trees.plans.RelationId;
35+
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
36+
import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation;
2237
import org.apache.doris.nereids.util.PlanChecker;
2338
import org.apache.doris.qe.GlobalVariable;
2439
import org.apache.doris.thrift.TUniqueId;
2540
import org.apache.doris.utframe.TestWithFeService;
2641

42+
import com.google.common.collect.ImmutableList;
2743
import org.junit.jupiter.api.Assertions;
2844
import org.junit.jupiter.api.Test;
2945

46+
import java.util.List;
47+
import java.util.Optional;
48+
3049
public class VarBinarySqlSupportTest extends TestWithFeService {
3150
@Test
3251
public void testNativeVarbinaryRemainsUnsupported() {
@@ -142,6 +161,41 @@ private void assertBinaryExpressionsRejected(String[] expressions) {
142161
}
143162
}
144163

164+
@Test
165+
public void testJoinConditionComparingStringsCastFromBinary() {
166+
// FE leaves CAST(X'FF' AS STRING) for BE because no FE string literal carries 0xFF, so a nullable IN
167+
// subquery keeps the cast in a mark join condition, which compares two strings.
168+
PlanChecker.from(connectContext)
169+
.analyze("select cast(X'FF' as string) in (select encoded from source_bytes)").rewrite();
170+
PlanChecker.from(connectContext)
171+
.analyze("select cast(X'610062' as string) not in (select encoded from source_bytes)").rewrite();
172+
173+
// CheckAfterRewrite rejects a join condition only when it compares VARBINARY values.
174+
Plan left = new LogicalOneRowRelation(new RelationId(1),
175+
ImmutableList.of(new Alias(new VarBinaryLiteral(new byte[] {(byte) 0xFF}), "l")));
176+
Plan right = new LogicalOneRowRelation(new RelationId(2),
177+
ImmutableList.of(new Alias(new VarBinaryLiteral(new byte[] {(byte) 0xFF}), "r")));
178+
Slot l = left.getOutput().get(0);
179+
Slot r = right.getOutput().get(0);
180+
Expression strings = new EqualTo(new Cast(l, StringType.INSTANCE), new Cast(r, StringType.INSTANCE));
181+
for (boolean mark : new boolean[] {false, true}) {
182+
PlanChecker.from(connectContext, join(left, right, strings, mark)).applyBottomUp(new CheckAfterRewrite());
183+
AnalysisException error = Assertions.assertThrows(AnalysisException.class,
184+
() -> PlanChecker.from(connectContext, join(left, right, new EqualTo(l, r), mark))
185+
.applyBottomUp(new CheckAfterRewrite()));
186+
Assertions.assertTrue(error.getMessage().contains("varbinary type could not in join equal conditions"),
187+
error.getMessage());
188+
}
189+
}
190+
191+
private static Plan join(Plan left, Plan right, Expression condition, boolean mark) {
192+
List<Expression> conditions = ImmutableList.of(condition);
193+
return new LogicalJoin<>(mark ? JoinType.LEFT_SEMI_JOIN : JoinType.INNER_JOIN,
194+
mark ? ImmutableList.of() : conditions, ImmutableList.of(), mark ? conditions : ImmutableList.of(),
195+
new DistributeHint(DistributeType.NONE),
196+
mark ? Optional.of(new MarkJoinSlotReference("mark")) : Optional.empty(), left, right, null);
197+
}
198+
145199
@Test
146200
public void testView() throws Exception {
147201
Assertions.assertEquals("X'0080FF'", new VarBinaryLiteral("0080FF").toSql());

‎regression-test/data/query_p0/sql_functions/binary_functions/test_binary_hex_function.out‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
9 aaabbbcccdddeeefff aaabbbcccdddeeefff
105105

106106
-- !sql_25 --
107-
61 5A 616263647879
107+
a Z abcdxy
108108

109109
-- !sql_26 --
110110
0x6162 0x48656C6C6F2C20215E25 0x446F726973

0 commit comments

Comments
 (0)