Skip to content
Merged
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 @@ -121,9 +121,14 @@ private List<Slot> computeOutputInternal(boolean asteriskOutput) {
newQualifier.addAll(qualifier);
}

// Subquery alias outputs are synthetic slots. They should keep the aliased
// qualifier/name, but must not reuse the child slot's SQL index. Otherwise
// CREATE VIEW rewrite can map an outer alias-star expansion back onto inner
// aggregate SQL text and persist invalid self-references.
Slot qualified = originSlot
.withQualifier(newQualifier)
.withName(columnAlias);
.withName(columnAlias)
.withIndexInSql(null);
currentOutput.add(qualified);
}
return currentOutput.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,30 @@ public void testAlterView() throws Exception {
alter1.getInlineViewDef());
}

@Test
public void testCreateViewWithAliasStarOverAggregateSubquery() throws Exception {
String createViewSql = "create view test.alias_star_agg_view as "
+ "with sale2 as (select 1 as pack_factory, 2 as area, 3 as purchase_month, "
+ "4 as label_model, 50000 as mile_range), "
+ "sale3 as ("
+ "select c.*, s.id, s.id * 10000 as mile_range "
+ "from (select pack_factory, area, purchase_month, label_model, "
+ "max(mile_range) / 10000 as max_range "
+ "from sale2 group by pack_factory, area, purchase_month, label_model) c "
+ "join (select 1 as id) s where s.id <= max_range) "
+ "select * from sale3";
ExceptionChecker.expectThrowsNoException(() -> createView(createViewSql));

Database db = Env.getCurrentInternalCatalog().getDbOrDdlException("test");
View view = (View) db.getTableOrDdlException("alias_star_agg_view");
Assert.assertFalse(view.getInlineViewDef().contains("group by c."));
Assert.assertFalse(view.getInlineViewDef().contains("select c.pack_factory"));

String explainString = UtFrameUtils.getSQLPlanOrErrorMsg(connectContext,
"EXPLAIN select * from test.alias_star_agg_view");
Assert.assertFalse(explainString.contains("Unknown column"));
}

@Test
public void testViewRejectVarbinary() throws Exception {
ExceptionChecker.expectThrowsWithMsg(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !select_view --
1 2 3 4 5 1 10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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("bind_view_alias_star_agg") {
sql "SET enable_nereids_planner=true"
sql "set runtime_filter_mode=OFF"
sql "SET enable_fallback_to_original_planner=false"

sql "DROP VIEW IF EXISTS v_bind_view_alias_star_agg"

sql """
create view v_bind_view_alias_star_agg as
with sale2 as (
select 1 as pack_factory, 2 as area, 3 as purchase_month, 4 as label_model, 50000 as mile_range
),
sale3 as (
select c.*, s.id, s.id * 10000 as mile_range
from (
select pack_factory, area, purchase_month, label_model, max(mile_range) / 10000 as max_range
from sale2
group by pack_factory, area, purchase_month, label_model
) c
join (select 1 as id) s
where s.id <= max_range
)
select * from sale3
"""

order_qt_select_view """
select * from v_bind_view_alias_star_agg
"""
}
Loading