Skip to content

chore(java-engine): fix counting view issue #1144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
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 @@ -403,22 +403,22 @@ private void analyzeSelectSingleColumn(SingleColumn singleColumn, Scope scope, I
ExpressionAnalysis expressionAnalysis = analyzeExpression(singleColumn.getExpression(), scope);

if (expressionAnalysis.isRequireRelation()) {
Node source = scope.getRelationId().getSourceNode()
.orElseThrow(() -> new IllegalArgumentException("count(*) should have a followed source"));

// collect only the source node that is a table for generating the required column for models
DefaultTraversalVisitor<Scope> visitor = new DefaultTraversalVisitor<>()
{
@Override
protected Void visitTable(Table node, Scope scope)
Optional<Node> source = scope.getRelationId().getSourceNode();
if (source.isPresent()) {
// collect only the source node that is a table for generating the required column for models
DefaultTraversalVisitor<Scope> visitor = new DefaultTraversalVisitor<>()
{
if (scope.getNamedQuery(node.getName().getSuffix()).isEmpty()) {
analysis.addRequiredSourceNode(node);
@Override
protected Void visitTable(Table node, Scope scope)
{
if (scope.getNamedQuery(node.getName().getSuffix()).isEmpty()) {
analysis.addRequiredSourceNode(node);
}
return null;
}
return null;
}
};
visitor.process(source, scope);
};
visitor.process(source.get(), scope);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.airlift.json.JsonCodec;
import io.wren.base.dto.JoinType;
import io.wren.base.dto.Manifest;
import io.wren.base.dto.View;
import io.wren.main.web.dto.DryPlanDtoV2;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -309,6 +310,58 @@ orders_custkey AS (
""");
}

@Test
public void testCountView()
{
Manifest manifest = Manifest.builder()
.setCatalog("wrenai")
.setSchema("tpch")
.setModels(List.of(
model("Customer", "SELECT * FROM tpch.customer",
List.of(column("custkey", "integer", null, false, "c_custkey"),
column("name", "varchar", null, false, "c_name")))))
.setViews(List.of(new View("view1", "select * from Customer", ImmutableMap.of())))
.build();
String manifestStr = base64Encode(toJson(manifest));
DryPlanDtoV2 dryPlanDto = new DryPlanDtoV2(manifestStr, "select count(*) from view1");
String dryPlan = dryPlanV2(dryPlanDto);
assertThat(dryPlan).isEqualTo("""
"WITH
"Customer" AS (
SELECT
"Customer"."custkey" "custkey"
, "Customer"."name" "name"
FROM
(
SELECT
"Customer"."custkey" "custkey"
, "Customer"."name" "name"
FROM
(
SELECT
c_custkey "custkey"
, c_name "name"
FROM
(
SELECT *
FROM
tpch.customer
) "Customer"
) "Customer"
) "Customer"
)\s
, "view1" AS (
SELECT *
FROM
Customer
)\s
SELECT count(*)
FROM
view1
"
""");
}

private String toJson(Manifest manifest)
{
return MANIFEST_JSON_CODEC.toJson(manifest);
Expand Down