Skip to content

Commit 3715946

Browse files
authored
chore(java-engine): fix counting view issue (#1144)
Thanks
1 parent c1d19e8 commit 3715946

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

wren-core-legacy/wren-base/src/main/java/io/wren/base/sqlrewrite/analyzer/StatementAnalyzer.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -403,22 +403,22 @@ private void analyzeSelectSingleColumn(SingleColumn singleColumn, Scope scope, I
403403
ExpressionAnalysis expressionAnalysis = analyzeExpression(singleColumn.getExpression(), scope);
404404

405405
if (expressionAnalysis.isRequireRelation()) {
406-
Node source = scope.getRelationId().getSourceNode()
407-
.orElseThrow(() -> new IllegalArgumentException("count(*) should have a followed source"));
408-
409-
// collect only the source node that is a table for generating the required column for models
410-
DefaultTraversalVisitor<Scope> visitor = new DefaultTraversalVisitor<>()
411-
{
412-
@Override
413-
protected Void visitTable(Table node, Scope scope)
406+
Optional<Node> source = scope.getRelationId().getSourceNode();
407+
if (source.isPresent()) {
408+
// collect only the source node that is a table for generating the required column for models
409+
DefaultTraversalVisitor<Scope> visitor = new DefaultTraversalVisitor<>()
414410
{
415-
if (scope.getNamedQuery(node.getName().getSuffix()).isEmpty()) {
416-
analysis.addRequiredSourceNode(node);
411+
@Override
412+
protected Void visitTable(Table node, Scope scope)
413+
{
414+
if (scope.getNamedQuery(node.getName().getSuffix()).isEmpty()) {
415+
analysis.addRequiredSourceNode(node);
416+
}
417+
return null;
417418
}
418-
return null;
419-
}
420-
};
421-
visitor.process(source, scope);
419+
};
420+
visitor.process(source.get(), scope);
421+
}
422422
}
423423
}
424424

wren-core-legacy/wren-tests/src/test/java/io/wren/testing/TestMDLResourceV2.java

+53
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.airlift.json.JsonCodec;
1919
import io.wren.base.dto.JoinType;
2020
import io.wren.base.dto.Manifest;
21+
import io.wren.base.dto.View;
2122
import io.wren.main.web.dto.DryPlanDtoV2;
2223
import org.testng.annotations.Test;
2324

@@ -309,6 +310,58 @@ orders_custkey AS (
309310
""");
310311
}
311312

313+
@Test
314+
public void testCountView()
315+
{
316+
Manifest manifest = Manifest.builder()
317+
.setCatalog("wrenai")
318+
.setSchema("tpch")
319+
.setModels(List.of(
320+
model("Customer", "SELECT * FROM tpch.customer",
321+
List.of(column("custkey", "integer", null, false, "c_custkey"),
322+
column("name", "varchar", null, false, "c_name")))))
323+
.setViews(List.of(new View("view1", "select * from Customer", ImmutableMap.of())))
324+
.build();
325+
String manifestStr = base64Encode(toJson(manifest));
326+
DryPlanDtoV2 dryPlanDto = new DryPlanDtoV2(manifestStr, "select count(*) from view1");
327+
String dryPlan = dryPlanV2(dryPlanDto);
328+
assertThat(dryPlan).isEqualTo("""
329+
"WITH
330+
"Customer" AS (
331+
SELECT
332+
"Customer"."custkey" "custkey"
333+
, "Customer"."name" "name"
334+
FROM
335+
(
336+
SELECT
337+
"Customer"."custkey" "custkey"
338+
, "Customer"."name" "name"
339+
FROM
340+
(
341+
SELECT
342+
c_custkey "custkey"
343+
, c_name "name"
344+
FROM
345+
(
346+
SELECT *
347+
FROM
348+
tpch.customer
349+
) "Customer"
350+
) "Customer"
351+
) "Customer"
352+
)\s
353+
, "view1" AS (
354+
SELECT *
355+
FROM
356+
Customer
357+
)\s
358+
SELECT count(*)
359+
FROM
360+
view1
361+
"
362+
""");
363+
}
364+
312365
private String toJson(Manifest manifest)
313366
{
314367
return MANIFEST_JSON_CODEC.toJson(manifest);

0 commit comments

Comments
 (0)