diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java index 0650a7cbf86f05..2ee92281c76cf5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java @@ -99,7 +99,7 @@ public Slot toSlot() throws UnboundException { slotReference != null ? ((SlotReference) child()).getOriginalTable().orElse(null) : null, slotReference != null ? slotReference.getOriginalColumn().orElse(null) : null, slotReference != null ? ((SlotReference) child()).getOneLevelTable().orElse(null) : null, - slotReference != null ? slotReference.getOriginalColumn().orElse(null) : null, + slotReference != null ? slotReference.getOneLevelColumn().orElse(null) : null, slotReference != null ? slotReference.getSubPath() : ImmutableList.of(), Optional.empty() ); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java index 1c77dc669acb72..8fa2b135d3bd6d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/SlotReference.java @@ -340,7 +340,7 @@ public SlotReference withColumn(Column column) { @Override public Slot withOneLevelTableAndColumnAndQualifier(TableIf oneLevelTable, Column column, List qualifier) { return new SlotReference(exprId, name, dataType, nullable, qualifier, - originalTable, column, oneLevelTable, column, + originalTable, originalColumn, oneLevelTable, column, subPath, indexInSqlString); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/ExpressionTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/ExpressionTest.java index cb43efb0b31748..43ba8a637a190c 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/ExpressionTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/ExpressionTest.java @@ -17,6 +17,8 @@ package org.apache.doris.nereids.trees.expressions; +import org.apache.doris.catalog.Column; +import org.apache.doris.catalog.PrimitiveType; import org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentDate; import org.apache.doris.nereids.trees.expressions.functions.scalar.DaysAdd; import org.apache.doris.nereids.trees.expressions.functions.scalar.UnixTimestamp; @@ -26,6 +28,7 @@ import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral; import org.apache.doris.nereids.types.IntegerType; +import com.google.common.collect.ImmutableList; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -66,4 +69,18 @@ public void testContainsNondeterministic() { new UnixTimestamp(), new IntegerLiteral(2)).containsNondeterministic()); } + + @Test + public void testAliasToSlotKeepsOneLevelColumn() { + SlotReference slotReference = new SlotReference(new ExprId(1), "k1", IntegerType.INSTANCE, true, + ImmutableList.of("test", "view_metadata"), null, new Column("k1", PrimitiveType.INT), + null, new Column("vk1", PrimitiveType.INT)); + + SlotReference aliasSlot = (SlotReference) new Alias(slotReference, "c2").toSlot(); + + Assertions.assertTrue(aliasSlot.getOriginalColumn().isPresent()); + Assertions.assertTrue(aliasSlot.getOneLevelColumn().isPresent()); + Assertions.assertEquals("k1", aliasSlot.getOriginalColumn().get().getName()); + Assertions.assertEquals("vk1", aliasSlot.getOneLevelColumn().get().getName()); + } } diff --git a/regression-test/data/query_p0/topn_lazy/lazy_materialize_view.out b/regression-test/data/query_p0/topn_lazy/lazy_materialize_view.out new file mode 100644 index 00000000000000..90b9d05460fc44 --- /dev/null +++ b/regression-test/data/query_p0/topn_lazy/lazy_materialize_view.out @@ -0,0 +1,13 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !view_lazy -- +aaa +bbb + +-- !chain_view_lazy -- +aaa +bbb + +-- !view_lazy_filter -- +bbb 200 +ccc 300 + diff --git a/regression-test/suites/mysql_compatibility_p0/metadata.groovy b/regression-test/suites/mysql_compatibility_p0/metadata.groovy index f6b303dbfd6980..284347a6b974f6 100644 --- a/regression-test/suites/mysql_compatibility_p0/metadata.groovy +++ b/regression-test/suites/mysql_compatibility_p0/metadata.groovy @@ -63,8 +63,8 @@ suite ("metadata") { println viewMeta assertEquals("c1", viewMeta.getColumnName(1)) - assertEquals("vk1", viewMeta.getColumnName(2)) - assertEquals("vk1", viewMeta.getColumnName(3)) + assertEquals("vk1", viewMeta.getColumnName(2),"viewMeta.getColumnName(2)") + assertEquals("vk1", viewMeta.getColumnName(3), "viewMeta.getColumnName(3)") assertEquals("k2", viewMeta.getColumnName(4)) assertEquals("k2", viewMeta.getColumnName(5)) assertEquals("c4", viewMeta.getColumnName(6)) diff --git a/regression-test/suites/query_p0/topn_lazy/lazy_materialize_view.groovy b/regression-test/suites/query_p0/topn_lazy/lazy_materialize_view.groovy new file mode 100644 index 00000000000000..8e58ddaa7cbe68 --- /dev/null +++ b/regression-test/suites/query_p0/topn_lazy/lazy_materialize_view.groovy @@ -0,0 +1,84 @@ +// 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("lazy_materialize_view") { + sql """ + set enable_two_phase_read_opt = true; + set topn_opt_limit_threshold = 1000; + set topn_lazy_materialization_threshold = -1; + """ + + sql "drop table if exists lazy_mat_view_t" + sql """ + CREATE TABLE lazy_mat_view_t ( + k1 INT NOT NULL, + k2 INT NOT NULL, + v1 VARCHAR(100) NULL, + v2 INT NULL + ) + DUPLICATE KEY(k1) + DISTRIBUTED BY HASH(k1) BUCKETS 1 + PROPERTIES ( + "replication_num" = "1" + ); + """ + + sql """ + insert into lazy_mat_view_t values + (1, 10, 'aaa', 100), + (2, 20, 'bbb', 200), + (3, 30, 'ccc', 300); + """ + + sql "sync" + + sql "drop view if exists lazy_mat_view_v1" + sql "CREATE VIEW lazy_mat_view_v1 AS SELECT * FROM lazy_mat_view_t" + + sql "drop view if exists lazy_mat_view_v2" + sql "CREATE VIEW lazy_mat_view_v2 AS SELECT * FROM lazy_mat_view_v1" + + // lazy materialization through a single-level view + explain { + sql "select v1 from lazy_mat_view_v1 order by k1 limit 2" + contains "OPT TWO PHASE" + } + + order_qt_view_lazy """ + select v1 from lazy_mat_view_v1 order by k1 limit 2 + """ + + // lazy materialization through a chained view (v2 -> v1 -> t) + explain { + sql "select v1 from lazy_mat_view_v2 order by k1 limit 2" + contains "OPT TWO PHASE" + } + + order_qt_chain_view_lazy """ + select v1 from lazy_mat_view_v2 order by k1 limit 2 + """ + + // lazy materialization through view with filter + explain { + sql "select v1, v2 from lazy_mat_view_v1 where k2 > 10 order by k1 limit 2" + contains "OPT TWO PHASE" + } + + order_qt_view_lazy_filter """ + select v1, v2 from lazy_mat_view_v1 where k2 > 10 order by k1 limit 2 + """ +}