Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
import org.apache.doris.nereids.trees.expressions.literal.Literal;
import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionVisitor;
import org.apache.doris.nereids.types.DataType;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.thrift.TDictFunction;
import org.apache.doris.thrift.TFunctionBinaryType;

Expand Down Expand Up @@ -219,20 +220,33 @@ public Expr visitMatch(Match match, PlanTranslatorContext context) {
.orElseThrow(() -> new AnalysisException(
"No SlotReference found in Match, SQL is " + match.toSql()));

Column column = slot.getOriginalColumn()
.orElseThrow(() -> new AnalysisException(
"SlotReference in Match failed to get Column, SQL is " + match.toSql()));

OlapTable olapTbl = getOlapTableDirectly(slot);
if (olapTbl == null) {
throw new AnalysisException("SlotReference in Match failed to get OlapTable, SQL is " + match.toSql());
}

String analyzer = match.getAnalyzer().orElse(null);
Index invertedIndex = olapTbl.getInvertedIndex(column, slot.getSubPath(), analyzer);
if (analyzer != null && invertedIndex == null) {
throw new AnalysisException("No inverted index found for analyzer '" + analyzer
+ "' on column " + column.getName());
Index invertedIndex = null;
Column column = slot.getOriginalColumn().orElse(null);
OlapTable olapTbl = getOlapTableDirectly(slot);
if (column != null && olapTbl != null) {
invertedIndex = olapTbl.getInvertedIndex(column, slot.getSubPath(), analyzer);
if (analyzer != null && invertedIndex == null) {
throw new AnalysisException("No inverted index found for analyzer '" + analyzer
+ "' on column " + column.getName());
}
} else {
// The slot lacks column metadata. This happens when MATCH is on an alias output
// slot whose child is not a direct SlotReference (e.g., variant subcolumn access
// Cast(ElementAt(...)) in a CTE/subquery), and an OR predicate prevents the
// optimizer from pushing MATCH down to the scan level.
// When enable_match_without_index_check is true, fall back to function-based
// matching; otherwise throw an error.
boolean allowNoIndex = ConnectContext.get() != null
&& ConnectContext.get().getSessionVariable().enableMatchWithoutIndexCheck;
if (!allowNoIndex) {
throw new AnalysisException(
"SlotReference in Match failed to get Column, SQL is " + match.toSql()
+ ". If the MATCH is on an alias column from a CTE/subquery with variant"
+ " subcolumns in an OR predicate, try setting"
+ " 'set enable_match_without_index_check = true' to allow function-based"
+ " matching fallback.");
}
}

MatchPredicate.Operator op = match.op();
Expand Down
11 changes: 11 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/qe/SessionVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ public class SessionVariable implements Serializable, Writable {
"cloud_table_version_cache_ttl_ms";
// CLOUD_VARIABLES_BEGIN

public static final String ENABLE_MATCH_WITHOUT_INDEX_CHECK = "enable_match_without_index_check";
public static final String ENABLE_MATCH_WITHOUT_INVERTED_INDEX = "enable_match_without_inverted_index";
public static final String ENABLE_FALLBACK_ON_MISSING_INVERTED_INDEX = "enable_fallback_on_missing_inverted_index";
public static final String ENABLE_INVERTED_INDEX_SEARCHER_CACHE = "enable_inverted_index_searcher_cache";
Expand Down Expand Up @@ -3167,6 +3168,16 @@ public void setDetailShapePlanNodes(String detailShapePlanNodes) {
})
public boolean enableESParallelScroll = true;

@VariableMgr.VarAttr(name = ENABLE_MATCH_WITHOUT_INDEX_CHECK, description = {
"开启后,MATCH 谓词在无法获取倒排索引元数据时(例如 CTE/子查询中 variant 子列的 alias slot),"
+ "会回退到函数匹配而非报错。默认关闭。",
"When enabled, MATCH predicates on slots without inverted index metadata"
+ " (e.g., alias output slots from CTE/subquery with variant subcolumns)"
+ " will fall back to function-based matching instead of throwing an error."
+ " Default is false."
})
public boolean enableMatchWithoutIndexCheck = false;

@VariableMgr.VarAttr(name = ENABLE_MATCH_WITHOUT_INVERTED_INDEX, description = {
"开启无索引 match 查询功能,建议正式环境保持开启",
"Enable no-index match query functionality."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,32 @@
import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.VarcharType;
import org.apache.doris.qe.ConnectContext;

import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ExpressionTranslatorTest {

private ConnectContext savedContext;

@BeforeEach
public void setUp() {
savedContext = ConnectContext.get();
}

@AfterEach
public void tearDown() {
ConnectContext.remove();
if (savedContext != null) {
savedContext.setThreadLocalInfo();
}
}

@Test
public void testUnaryArithmetic() throws Exception {
BitNot bitNot = new BitNot(new IntegerLiteral(1));
Expand All @@ -51,12 +70,45 @@ public void testUnaryArithmetic() throws Exception {
}

@Test
public void testMatch() {
public void testMatchNoSlotReference() {
// MATCH with no SlotReference in left operand should throw
MatchAny matchAny = new MatchAny(new VarcharLiteral("collections"), new NullLiteral());
ExpressionTranslator translator = ExpressionTranslator.INSTANCE;
Assertions.assertThrows(AnalysisException.class, () -> translator.visitMatch(matchAny, null));
}

@Test
public void testMatchOnSlotWithoutColumnMetadataDefaultThrows() {
// By default (enable_match_without_index_check = false), MATCH on an alias slot
// without column metadata should throw.
SlotReference aliasSlot = new SlotReference("firstname", VarcharType.SYSTEM_DEFAULT, true, ImmutableList.of());
MatchAny matchAny = new MatchAny(aliasSlot, new VarcharLiteral("hello"));
ExpressionTranslator translator = ExpressionTranslator.INSTANCE;
PlanTranslatorContext context = new PlanTranslatorContext();
context.addExprIdSlotRefPair(aliasSlot.getExprId(), new SlotRef(Type.VARCHAR, true));

// No ConnectContext → defaults to strict mode → should throw
ConnectContext.remove();
Assertions.assertThrows(AnalysisException.class, () -> translator.visitMatch(matchAny, context));
}

@Test
public void testMatchOnSlotWithoutColumnMetadataWithVariableEnabled() {
// When enable_match_without_index_check = true, MATCH on an alias slot
// without column metadata should NOT throw (falls back to function match).
SlotReference aliasSlot = new SlotReference("firstname", VarcharType.SYSTEM_DEFAULT, true, ImmutableList.of());
MatchAny matchAny = new MatchAny(aliasSlot, new VarcharLiteral("hello"));
ExpressionTranslator translator = ExpressionTranslator.INSTANCE;
PlanTranslatorContext context = new PlanTranslatorContext();
context.addExprIdSlotRefPair(aliasSlot.getExprId(), new SlotRef(Type.VARCHAR, true));

ConnectContext ctx = new ConnectContext();
ctx.setThreadLocalInfo();
ctx.getSessionVariable().enableMatchWithoutIndexCheck = true;

Assertions.assertDoesNotThrow(() -> translator.visitMatch(matchAny, context));
}

@Test void testFlattenAndOrNullable() {
SlotReference a = new SlotReference("a", IntegerType.INSTANCE, true);
SlotReference b = new SlotReference("b", IntegerType.INSTANCE, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !variant_cte_or --
1 100 hello world
3 300 hello foo

-- !variant_subquery_or --
1 100 hello world
3 300 hello foo

-- !variant_cte_match_any_or --
1 100 hello world
3 300 hello foo

-- !variant_cte_and_only --
1 100 hello world
3 300 hello foo

-- !normal_cast_cte_or --
1 hello world 1
2 foo bar 2
3 hello foo 1

-- !normal_no_cast_cte_or --
1 hello world 1
2 foo bar 2
3 hello foo 1

-- !variant_exists_or --
1 100 hello world
3 300 hello foo

Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// 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.

suite("test_match_with_or_on_alias", "p0") {
// Test for: MATCH expressions on alias columns (from CTE/subquery) combined with OR
// Bug: When MATCH is inside an OR predicate with a LEFT JOIN, the optimizer cannot fully
// push MATCH down to the scan level. The remaining post-join filter references an
// alias output slot that lacks column metadata (because the alias child is
// Cast(ElementAt(...)) for variant subcolumns, not a direct SlotReference).
// ExpressionTranslator.visitMatch() then crashes with:
// "SlotReference in Match failed to get Column"
//
// Fix: session variable enable_match_without_index_check allows MATCH to fall back
// to function-based matching when inverted index metadata is unavailable.

def tblVariant = "test_match_or_alias_variant"
def tblNormal = "test_match_or_alias_normal"
def tblAssoc = "test_match_or_alias_assoc"

sql "DROP TABLE IF EXISTS ${tblVariant}"
sql "DROP TABLE IF EXISTS ${tblNormal}"
sql "DROP TABLE IF EXISTS ${tblAssoc}"

// Table with variant column + inverted index
sql """
CREATE TABLE ${tblVariant} (
id INT,
OBJECTID INT,
OVERFLOWPROPERTIES VARIANT,
INDEX idx_var (OVERFLOWPROPERTIES) USING INVERTED PROPERTIES("parser"="standard")
) ENGINE=OLAP
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES('replication_num'='1')
"""

// Table with normal string column + inverted index
sql """
CREATE TABLE ${tblNormal} (
id INT,
name VARCHAR(100),
category INT,
INDEX idx_name (name) USING INVERTED PROPERTIES("parser"="standard")
) ENGINE=OLAP
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES('replication_num'='1')
"""

// Association table for joins
sql """
CREATE TABLE ${tblAssoc} (
from_id INT,
to_id INT
) ENGINE=OLAP
DUPLICATE KEY(from_id)
DISTRIBUTED BY HASH(from_id) BUCKETS 1
PROPERTIES('replication_num'='1')
"""

sql """INSERT INTO ${tblVariant} VALUES
(1, 100, '{"firstname": "hello world"}'),
(2, 200, '{"firstname": "foo bar"}'),
(3, 300, '{"firstname": "hello foo"}')"""

sql """INSERT INTO ${tblNormal} VALUES
(1, 'hello world', 1),
(2, 'foo bar', 2),
(3, 'hello foo', 1)"""

sql """INSERT INTO ${tblAssoc} VALUES (1, 1), (2, 3)"""

sql "sync"

// Enable the session variable to allow function-based match fallback
sql "set enable_match_without_index_check = true"

// =============================
// Variant subcolumn + CTE + OR
// =============================

// Case 1: variant subcolumn + CTE + MATCH_ALL + OR (was crashing)
qt_variant_cte_or """
WITH cte AS (
SELECT id, OBJECTID,
cast(OVERFLOWPROPERTIES['firstname'] as VARCHAR) as firstname
FROM ${tblVariant}
)
SELECT cte.id, cte.OBJECTID, cte.firstname FROM cte
LEFT JOIN ${tblAssoc} a ON cte.id = a.to_id
WHERE (cte.firstname MATCH_ALL 'hello' AND a.to_id IS NOT NULL)
OR cte.OBJECTID > 200
ORDER BY cte.id
"""

// Case 2: variant subcolumn + subquery + MATCH_ALL + OR (same bug, no CTE)
qt_variant_subquery_or """
SELECT cte.id, cte.OBJECTID, cte.firstname FROM (
SELECT id, OBJECTID,
cast(OVERFLOWPROPERTIES['firstname'] as VARCHAR) as firstname
FROM ${tblVariant}
) cte
LEFT JOIN ${tblAssoc} a ON cte.id = a.to_id
WHERE (cte.firstname MATCH_ALL 'hello' AND a.to_id IS NOT NULL)
OR cte.OBJECTID > 200
ORDER BY cte.id
"""

// Case 3: variant subcolumn + CTE + MATCH_ANY + OR
qt_variant_cte_match_any_or """
WITH cte AS (
SELECT id, OBJECTID,
cast(OVERFLOWPROPERTIES['firstname'] as VARCHAR) as firstname
FROM ${tblVariant}
)
SELECT cte.id, cte.OBJECTID, cte.firstname FROM cte
LEFT JOIN ${tblAssoc} a ON cte.id = a.to_id
WHERE (cte.firstname MATCH_ANY 'hello foo' AND a.to_id IS NOT NULL)
OR cte.OBJECTID > 200
ORDER BY cte.id
"""

// Case 4: variant subcolumn + CTE + MATCH_ALL + AND only (baseline, was always working)
qt_variant_cte_and_only """
WITH cte AS (
SELECT id, OBJECTID,
cast(OVERFLOWPROPERTIES['firstname'] as VARCHAR) as firstname
FROM ${tblVariant}
)
SELECT cte.id, cte.OBJECTID, cte.firstname FROM cte
LEFT JOIN ${tblAssoc} a ON cte.id = a.to_id
WHERE cte.firstname MATCH_ALL 'hello'
AND a.to_id IS NOT NULL
ORDER BY cte.id
"""

// =============================================
// Normal string + explicit Cast + CTE + OR
// =============================================

// Case 5: normal string + explicit CAST + CTE + OR (was crashing)
qt_normal_cast_cte_or """
WITH cte AS (
SELECT id, CAST(name AS VARCHAR(200)) as name_casted, category
FROM ${tblNormal}
)
SELECT cte.id, cte.name_casted, cte.category FROM cte
LEFT JOIN ${tblAssoc} a ON cte.id = a.to_id
WHERE (cte.name_casted MATCH_ALL 'hello' AND a.to_id IS NOT NULL)
OR cte.category > 1
ORDER BY cte.id
"""

// Case 6: normal string + no Cast + CTE + OR (was always working)
qt_normal_no_cast_cte_or """
WITH cte AS (
SELECT id, name, category FROM ${tblNormal}
)
SELECT cte.id, cte.name, cte.category FROM cte
LEFT JOIN ${tblAssoc} a ON cte.id = a.to_id
WHERE (cte.name MATCH_ALL 'hello' AND a.to_id IS NOT NULL)
OR cte.category > 1
ORDER BY cte.id
"""

// =============================================
// EXISTS + OR pattern (original bug report)
// =============================================

// Case 7: variant subcolumn + CTE + EXISTS + OR (original bug pattern)
qt_variant_exists_or """
WITH cte AS (
SELECT id, OBJECTID,
cast(OVERFLOWPROPERTIES['firstname'] as VARCHAR) as firstname
FROM ${tblVariant}
)
SELECT cte.id, cte.OBJECTID, cte.firstname FROM cte
WHERE (cte.firstname MATCH_ALL 'hello'
AND EXISTS (SELECT 1 FROM ${tblAssoc} a WHERE a.to_id = cte.id))
OR cte.OBJECTID > 200
ORDER BY cte.id
"""

sql "DROP TABLE IF EXISTS ${tblVariant}"
sql "DROP TABLE IF EXISTS ${tblNormal}"
sql "DROP TABLE IF EXISTS ${tblAssoc}"
}