diff --git a/ibis-server/tests/routers/v2/connector/test_postgres.py b/ibis-server/tests/routers/v2/connector/test_postgres.py index 0c3092fa2..a1d72d936 100644 --- a/ibis-server/tests/routers/v2/connector/test_postgres.py +++ b/ibis-server/tests/routers/v2/connector/test_postgres.py @@ -67,6 +67,36 @@ ], "primaryKey": "orderkey", }, + { + "name": "Customer", + "refSql": "SELECT * FROM public.customer", + "columns": [ + { + "name": "custkey", + "type": "integer", + "expression": "c_custkey", + }, + { + "name": "orders", + "type": "Orders", + "relationship": "CustomerOrders", + }, + { + "name": "orders_key", + "type": "varchar", + "isCalculated": True, + "expression": "orders.orderkey", + }, + ], + }, + ], + "relationships": [ + { + "name": "CustomerOrders", + "models": ["Customer", "Orders"], + "joinType": "ONE_TO_MANY", + "condition": "Customer.custkey = Orders.custkey", + } ], } @@ -83,6 +113,9 @@ def postgres(request) -> PostgresContainer: pd.read_parquet(file_path("resource/tpch/data/orders.parquet")).to_sql( "orders", engine, index=False ) + pd.read_parquet(file_path("resource/tpch/data/customer.parquet")).to_sql( + "customer", engine, index=False + ) with engine.begin() as conn: conn.execute(text("COMMENT ON TABLE orders IS 'This is a table comment'")) conn.execute(text("COMMENT ON COLUMN orders.o_comment IS 'This is a comment'")) @@ -148,6 +181,29 @@ def test_query_with_connection_url(manifest_str, postgres: PostgresContainer): assert result["data"][0][0] == 1 assert result["dtypes"] is not None + def test_query_with_dot_all(manifest_str, postgres: PostgresContainer): + connection_info = _to_connection_info(postgres) + test_sqls = [ + 'SELECT "Customer".* FROM "Customer"', + 'SELECT c.* FROM "Customer" AS c', + 'SELECT c.* FROM "Customer" AS c JOIN "Orders" AS o ON c.custkey = o.custkey', + ] + for sql in test_sqls: + response = client.post( + url=f"{base_url}/query", + params={"limit": 1}, + json={ + "connectionInfo": connection_info, + "manifestStr": manifest_str, + "sql": sql, + }, + ) + assert response.status_code == 200 + result = response.json() + assert len(result["columns"]) == 1 # Not include calculated column + assert len(result["data"]) == 1 + assert result["dtypes"] is not None + def test_dry_run_with_connection_url_and_password_with_bracket_should_not_raise_value_error( manifest_str, postgres: PostgresContainer ): diff --git a/wren-base/src/main/java/io/wren/base/dto/Column.java b/wren-base/src/main/java/io/wren/base/dto/Column.java index f65533342..02a8f3779 100644 --- a/wren-base/src/main/java/io/wren/base/dto/Column.java +++ b/wren-base/src/main/java/io/wren/base/dto/Column.java @@ -51,7 +51,7 @@ public static Column relationshipColumn(String name, String type, String relatio return new Column(name, type, relationship, false, false, null, null); } - public static Column caluclatedColumn(String name, String type, String expression) + public static Column calculatedColumn(String name, String type, String expression) { return new Column(name, type, null, true, false, expression, null); } diff --git a/wren-base/src/main/java/io/wren/base/sqlrewrite/analyzer/StatementAnalyzer.java b/wren-base/src/main/java/io/wren/base/sqlrewrite/analyzer/StatementAnalyzer.java index f1cd51ec3..7575de23e 100644 --- a/wren-base/src/main/java/io/wren/base/sqlrewrite/analyzer/StatementAnalyzer.java +++ b/wren-base/src/main/java/io/wren/base/sqlrewrite/analyzer/StatementAnalyzer.java @@ -62,6 +62,7 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Stream; import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.ImmutableList.toImmutableList; @@ -374,21 +375,26 @@ else if (item instanceof SingleColumn) { private void analyzeSelectAllColumns(AllColumns allColumns, Scope scope, ImmutableList.Builder outputExpressions) { + List fields = scope.getRelationType().getFields(); + Stream collectedColumns = fields.stream().filter(f -> f.getSourceColumn().map(c -> !c.isCalculated()).orElse(true)); + Stream outputExpressionStream = fields.stream(); + if (allColumns.getTarget().isPresent()) { - // TODO handle target.* - } - else { - List fields = scope.getRelationType() - .getFields() - .stream() - .filter(f -> f.getSourceColumn().map(c -> !c.isCalculated()).orElse(true)) - .collect(toImmutableList()); - analysis.addCollectedColumns(fields); - scope.getRelationType().getFields().stream().map(field -> - field.getRelationAlias().map(DereferenceExpression::from) - .orElse(DereferenceExpression.from(QualifiedName.of(field.getTableName().getSchemaTableName().getTableName(), field.getColumnName())))) - .forEach(outputExpressions::add); + QualifiedName target = QualifiedName.of(((Identifier) allColumns.getTarget().get()).getValue()); + collectedColumns = collectedColumns + .filter(field -> + field.getRelationAlias().map(ra -> ra.equals(target)).orElse(false) + || field.getTableName().getSchemaTableName().getTableName().equals(target.getParts().getFirst())); + outputExpressionStream = outputExpressionStream + .filter(field -> field.getRelationAlias().map(ra -> ra.equals(target)).orElse(false)); } + + analysis.addCollectedColumns(collectedColumns.collect(toImmutableList())); + outputExpressionStream + .map(field -> + field.getRelationAlias().map(DereferenceExpression::from) + .orElse(DereferenceExpression.from(QualifiedName.of(field.getTableName().getSchemaTableName().getTableName(), field.getColumnName())))) + .forEach(outputExpressions::add); } private void analyzeSelectSingleColumn(SingleColumn singleColumn, Scope scope, ImmutableList.Builder outputExpressions) diff --git a/wren-base/src/test/java/io/wren/base/sqlrewrite/AbstractTestModel.java b/wren-base/src/test/java/io/wren/base/sqlrewrite/AbstractTestModel.java index abb064c06..7856dabeb 100644 --- a/wren-base/src/test/java/io/wren/base/sqlrewrite/AbstractTestModel.java +++ b/wren-base/src/test/java/io/wren/base/sqlrewrite/AbstractTestModel.java @@ -113,10 +113,10 @@ public void testToManyCalculated() Model newCustomer = addColumnsToModel( customer, Column.column("orders", "Orders", "OrdersCustomer", true), - Column.caluclatedColumn("totalprice", WrenTypes.BIGINT, "sum(orders.totalprice)"), - Column.caluclatedColumn("buy_item_count", WrenTypes.BIGINT, "count(distinct orders.lineitem.orderkey_linenumber)"), - Column.caluclatedColumn("lineitem_totalprice", WrenTypes.BIGINT, "sum(orders.lineitem.discount * orders.lineitem.extendedprice)"), - Column.caluclatedColumn("test_col", WrenTypes.BIGINT, "sum(orders.lineitem.discount * nationkey)")); + Column.calculatedColumn("totalprice", WrenTypes.BIGINT, "sum(orders.totalprice)"), + Column.calculatedColumn("buy_item_count", WrenTypes.BIGINT, "count(distinct orders.lineitem.orderkey_linenumber)"), + Column.calculatedColumn("lineitem_totalprice", WrenTypes.BIGINT, "sum(orders.lineitem.discount * orders.lineitem.extendedprice)"), + Column.calculatedColumn("test_col", WrenTypes.BIGINT, "sum(orders.lineitem.discount * nationkey)")); Manifest manifest = withDefaultCatalogSchema() .setModels(List.of(newCustomer, orders, lineitem)) .setRelationships(List.of(ordersCustomer, ordersLineitem)) @@ -156,8 +156,8 @@ public void testToOneCalculated() Model newLineitem = addColumnsToModel( lineitem, Column.column("orders", "Orders", "OrdersLineitem", true), - Column.caluclatedColumn("col_1", WrenTypes.BIGINT, "orders.totalprice + orders.totalprice"), - Column.caluclatedColumn("col_2", WrenTypes.BIGINT, "concat(orders.orderkey, '#', orders.customer.custkey)")); + Column.calculatedColumn("col_1", WrenTypes.BIGINT, "orders.totalprice + orders.totalprice"), + Column.calculatedColumn("col_2", WrenTypes.BIGINT, "concat(orders.orderkey, '#', orders.customer.custkey)")); Model newOrders = addColumnsToModel( orders, Column.column("customer", "Customer", "OrdersCustomer", true)); @@ -221,11 +221,11 @@ public void testModelWithCycle() Model newCustomer = addColumnsToModel( customer, Column.column("orders", "Orders", "OrdersCustomer", true), - Column.caluclatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)")); + Column.calculatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)")); Model newOrders = addColumnsToModel( orders, Column.column("customer", "Customer", "OrdersCustomer", true), - Column.caluclatedColumn("customer_name", WrenTypes.BIGINT, "customer.name")); + Column.calculatedColumn("customer_name", WrenTypes.BIGINT, "customer.name")); Manifest manifest = withDefaultCatalogSchema() .setModels(List.of(newCustomer, newOrders)) .setRelationships(List.of(ordersCustomer, ordersLineitem)) @@ -252,7 +252,7 @@ public void testModelOnModel() Model newCustomer = addColumnsToModel( customer, Column.column("orders", "Orders", "OrdersCustomer", true), - Column.caluclatedColumn("totalprice", WrenTypes.BIGINT, "sum(orders.totalprice)")); + Column.calculatedColumn("totalprice", WrenTypes.BIGINT, "sum(orders.totalprice)")); Model onCustomer = Model.onBaseObject( "OnCustomer", "Customer", @@ -282,17 +282,17 @@ public void testCalculatedUseAnotherCalculated() Model newCustomer = addColumnsToModel( customer, Column.column("orders", "Orders", "OrdersCustomer", true), - Column.caluclatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)")); + Column.calculatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)")); Model newOrders = addColumnsToModel( orders, Column.column("customer", "Customer", "OrdersCustomer", true), Column.column("lineitem", "Lineitem", "OrdersLineitem", true), - Column.caluclatedColumn("customer_name", WrenTypes.BIGINT, "customer.name"), - Column.caluclatedColumn("extended_price", WrenTypes.BIGINT, "sum(lineitem.extendedprice)")); + Column.calculatedColumn("customer_name", WrenTypes.BIGINT, "customer.name"), + Column.calculatedColumn("extended_price", WrenTypes.BIGINT, "sum(lineitem.extendedprice)")); Model newLineitem = addColumnsToModel( lineitem, Column.column("orders", "Orders", "OrdersLineitem", true), - Column.caluclatedColumn("test_column", WrenTypes.BIGINT, "orders.customer.total_price + extendedprice")); + Column.calculatedColumn("test_column", WrenTypes.BIGINT, "orders.customer.total_price + extendedprice")); Manifest manifest = withDefaultCatalogSchema() .setModels(List.of(newCustomer, newOrders, newLineitem)) .setRelationships(List.of(ordersCustomer, ordersLineitem)) @@ -309,17 +309,17 @@ public void testSelectEmpty() Model newCustomer = addColumnsToModel( customer, Column.column("orders", "Orders", "OrdersCustomer", true), - Column.caluclatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)")); + Column.calculatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)")); Model newOrders = addColumnsToModel( orders, Column.column("customer", "Customer", "OrdersCustomer", true), Column.column("lineitem", "Lineitem", "OrdersLineitem", true), - Column.caluclatedColumn("customer_name", WrenTypes.BIGINT, "customer.name"), - Column.caluclatedColumn("extended_price", WrenTypes.BIGINT, "sum(lineitem.extendedprice)")); + Column.calculatedColumn("customer_name", WrenTypes.BIGINT, "customer.name"), + Column.calculatedColumn("extended_price", WrenTypes.BIGINT, "sum(lineitem.extendedprice)")); Model newLineitem = addColumnsToModel( lineitem, Column.column("orders", "Orders", "OrdersLineitem", true), - Column.caluclatedColumn("test_column", WrenTypes.BIGINT, "orders.customer.total_price + extendedprice")); + Column.calculatedColumn("test_column", WrenTypes.BIGINT, "orders.customer.total_price + extendedprice")); Manifest manifest = withDefaultCatalogSchema() .setModels(List.of(newCustomer, newOrders, newLineitem)) .setRelationships(List.of(ordersCustomer, ordersLineitem)) diff --git a/wren-base/src/test/java/io/wren/base/sqlrewrite/TestAllRulesRewrite.java b/wren-base/src/test/java/io/wren/base/sqlrewrite/TestAllRulesRewrite.java index a80344a73..f9d44b350 100644 --- a/wren-base/src/test/java/io/wren/base/sqlrewrite/TestAllRulesRewrite.java +++ b/wren-base/src/test/java/io/wren/base/sqlrewrite/TestAllRulesRewrite.java @@ -56,7 +56,7 @@ public TestAllRulesRewrite() Column.relationshipColumn("band", "Band", "AlbumBand"), Column.column("price", WrenTypes.INTEGER, null, true), Column.column("bandId", WrenTypes.INTEGER, null, true), - Column.caluclatedColumn("bandName", WrenTypes.VARCHAR, "band.name"), + Column.calculatedColumn("bandName", WrenTypes.VARCHAR, "band.name"), Column.column("status", "Inventory", null, true), Column.column("statusA", "InventoryA", null, true), Column.relationshipColumn("orders", "Order", "AlbumOrder")), diff --git a/wren-base/src/test/java/io/wren/base/sqlrewrite/TestMetric.java b/wren-base/src/test/java/io/wren/base/sqlrewrite/TestMetric.java index d92977c02..b8304f8e1 100644 --- a/wren-base/src/test/java/io/wren/base/sqlrewrite/TestMetric.java +++ b/wren-base/src/test/java/io/wren/base/sqlrewrite/TestMetric.java @@ -60,8 +60,8 @@ public TestMetric() Column.column("clerk", WrenTypes.VARCHAR, null, true), Column.column("shippriority", WrenTypes.INTEGER, null, true), Column.column("comment", WrenTypes.VARCHAR, null, true), - Column.caluclatedColumn("customer_name", WrenTypes.VARCHAR, "customer.name"), - Column.caluclatedColumn("cumstomer_address", WrenTypes.VARCHAR, "customer.address"), + Column.calculatedColumn("customer_name", WrenTypes.VARCHAR, "customer.name"), + Column.calculatedColumn("cumstomer_address", WrenTypes.VARCHAR, "customer.address"), Column.column("customer", "Customer", "OrdersCustomer", true), Column.column("lineitem", "Lineitem", "OrdersLineitem", true)), "orderkey"), diff --git a/wren-base/src/test/java/io/wren/base/sqlrewrite/TestWrenDataLineage.java b/wren-base/src/test/java/io/wren/base/sqlrewrite/TestWrenDataLineage.java index c7f194357..67a8d7d68 100644 --- a/wren-base/src/test/java/io/wren/base/sqlrewrite/TestWrenDataLineage.java +++ b/wren-base/src/test/java/io/wren/base/sqlrewrite/TestWrenDataLineage.java @@ -108,20 +108,20 @@ public void testAnalyze() Model newCustomer = addColumnsToModel( customer, Column.column("orders", "Orders", "OrdersCustomer", true), - Column.caluclatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)"), - Column.caluclatedColumn("discount_extended_price", WrenTypes.BIGINT, "sum(orders.lineitem.discount + orders.extended_price)"), - Column.caluclatedColumn("lineitem_price", WrenTypes.BIGINT, "sum(orders.lineitem.discount * orders.lineitem.extendedprice)")); + Column.calculatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)"), + Column.calculatedColumn("discount_extended_price", WrenTypes.BIGINT, "sum(orders.lineitem.discount + orders.extended_price)"), + Column.calculatedColumn("lineitem_price", WrenTypes.BIGINT, "sum(orders.lineitem.discount * orders.lineitem.extendedprice)")); Model newOrders = addColumnsToModel( orders, Column.column("customer", "Customer", "OrdersCustomer", true), Column.column("lineitem", "Lineitem", "OrdersLineitem", true), - Column.caluclatedColumn("customer_name", WrenTypes.BIGINT, "customer.name"), - Column.caluclatedColumn("extended_price", WrenTypes.BIGINT, "sum(lineitem.extendedprice)"), - Column.caluclatedColumn("extended_price_2", WrenTypes.BIGINT, "sum(lineitem.extendedprice + totalprice)")); + Column.calculatedColumn("customer_name", WrenTypes.BIGINT, "customer.name"), + Column.calculatedColumn("extended_price", WrenTypes.BIGINT, "sum(lineitem.extendedprice)"), + Column.calculatedColumn("extended_price_2", WrenTypes.BIGINT, "sum(lineitem.extendedprice + totalprice)")); Model newLineitem = addColumnsToModel( lineitem, Column.column("orders", "Orders", "OrdersLineitem", true), - Column.caluclatedColumn("test_column", WrenTypes.BIGINT, "orders.customer.total_price + extendedprice")); + Column.calculatedColumn("test_column", WrenTypes.BIGINT, "orders.customer.total_price + extendedprice")); Manifest manifest = withDefaultCatalogSchema() .setModels(List.of(newCustomer, newOrders, newLineitem)) .setRelationships(List.of(ordersCustomer, ordersLineitem)) @@ -203,7 +203,7 @@ public void testAnalyzeModelOnModel() Model newCustomer = addColumnsToModel( customer, Column.column("orders", "Orders", "OrdersCustomer", true), - Column.caluclatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)")); + Column.calculatedColumn("total_price", WrenTypes.BIGINT, "sum(orders.totalprice)")); Model onCustomer = Model.onBaseObject( "OnCustomer", "Customer", @@ -215,7 +215,7 @@ public void testAnalyzeModelOnModel() Model newOrders = addColumnsToModel( orders, Column.column("on_customer", "OnCustomer", "OrdersOnCustomer", true), - Column.caluclatedColumn("customer_name", WrenTypes.BIGINT, "on_customer.mom_name")); + Column.calculatedColumn("customer_name", WrenTypes.BIGINT, "on_customer.mom_name")); Relationship ordersOnCustomer = Relationship.relationship("OrdersOnCustomer", List.of("Orders", "OnCustomer"), JoinType.MANY_TO_ONE, "Orders.custkey = OnCustomer.mom_custkey"); Manifest manifest = withDefaultCatalogSchema() .setModels(List.of(newOrders, newCustomer, onCustomer)) @@ -516,7 +516,7 @@ public void testGetSourceColumns() Model newCustomer = addColumnsToModel( customer, Column.column("orders", "Orders", "OrdersCustomer", true), - Column.caluclatedColumn("discount_extended_price", WrenTypes.BIGINT, "sum(orders.lineitem.discount + orders.lineitem.extendedprice)")); + Column.calculatedColumn("discount_extended_price", WrenTypes.BIGINT, "sum(orders.lineitem.discount + orders.lineitem.extendedprice)")); Manifest manifest = withDefaultCatalogSchema() .setModels(List.of(newCustomer, orders, lineitem)) .setRelationships(List.of(ordersCustomer, ordersLineitem)) diff --git a/wren-base/src/test/java/io/wren/base/sqlrewrite/analyzer/TestDecisionPointAnalyzer.java b/wren-base/src/test/java/io/wren/base/sqlrewrite/analyzer/TestDecisionPointAnalyzer.java index 5d90c92a4..8aabd66e6 100644 --- a/wren-base/src/test/java/io/wren/base/sqlrewrite/analyzer/TestDecisionPointAnalyzer.java +++ b/wren-base/src/test/java/io/wren/base/sqlrewrite/analyzer/TestDecisionPointAnalyzer.java @@ -76,7 +76,7 @@ public TestDecisionPointAnalyzer() Column.column("shippriority", WrenTypes.INTEGER, null, true), Column.column("comment", WrenTypes.VARCHAR, null, true), Column.column("customer", "customer", "CustomerOrders", false), - Column.caluclatedColumn("customer_name", WrenTypes.VARCHAR, "customer.name")); + Column.calculatedColumn("customer_name", WrenTypes.VARCHAR, "customer.name")); List lineitemColumns = List.of( Column.column("orderkey", WrenTypes.INTEGER, null, true), Column.column("partkey", WrenTypes.INTEGER, null, true), diff --git a/wren-base/src/test/java/io/wren/base/sqlrewrite/analyzer/TestStatementAnalyzer.java b/wren-base/src/test/java/io/wren/base/sqlrewrite/analyzer/TestStatementAnalyzer.java index baba5046e..c71b323f8 100644 --- a/wren-base/src/test/java/io/wren/base/sqlrewrite/analyzer/TestStatementAnalyzer.java +++ b/wren-base/src/test/java/io/wren/base/sqlrewrite/analyzer/TestStatementAnalyzer.java @@ -37,7 +37,10 @@ import static io.wren.base.CatalogSchemaTableName.catalogSchemaTableName; import static io.wren.base.WrenMDL.EMPTY; import static io.wren.base.WrenMDL.fromManifest; +import static io.wren.base.dto.Column.calculatedColumn; +import static io.wren.base.dto.Column.column; import static io.wren.base.dto.Column.relationshipColumn; +import static io.wren.base.dto.Model.model; import static io.wren.base.dto.Relationship.relationship; import static io.wren.base.sqlrewrite.Utils.parseSql; import static io.wren.base.sqlrewrite.analyzer.StatementAnalyzer.analyze; @@ -235,6 +238,36 @@ public void testScopeWithRelationship() assertThat(analysis.getCollectedColumns().get(catalogSchemaTableName("test", "test", "table_2"))).containsExactly("c1", "c2"); } + @Test + public void testTargetDotAll() + { + Manifest manifest = Manifest.builder() + .setCatalog("test") + .setSchema("test") + .setModels(List.of( + model("Orders", "SELECT * FROM tpch.orders", + List.of(column("orderkey", "integer", null, false, "o_orderkey"), + column("custkey", "integer", null, false, "o_custkey"), + column("customer", "Customer", "CustomerOrders", false), + calculatedColumn("customer_name", "varchar", "customer.name")), + "orderkey"), + model("Customer", "SELECT * FROM tpch.customer", + List.of(column("custkey", "integer", null, false, "c_custkey"), + column("name", "varchar", null, false, "c_name"))))) + .setRelationships(List.of(relationship("CustomerOrders", List.of("Customer", "Orders"), JoinType.ONE_TO_MANY, "Customer.custkey = Orders.custkey"))) + .build(); + List testSqls = ImmutableList.of( + "SELECT Orders.* FROM Orders", + "SELECT o.* FROM Orders AS o", + "SELECT o.* FROM Orders AS o JOIN Customer AS c ON o.custkey = c.custkey"); + for (String sql : testSqls) { + Statement statement = parseSql(sql); + Analysis analysis = new Analysis(statement); + analyze(analysis, statement, DEFAULT_SESSION_CONTEXT, fromManifest(manifest)); + assertThat(analysis.getCollectedColumns().get(catalogSchemaTableName("test", "test", "Orders"))).containsExactly("custkey", "orderkey", "customer"); + } + } + private static Column varcharColumn(String name) { return Column.column(name, "VARCHAR", null, false, null); diff --git a/wren-tests/src/test/java/io/wren/testing/TestMDLResource.java b/wren-tests/src/test/java/io/wren/testing/TestMDLResource.java index fc1562cde..71c02fb7d 100644 --- a/wren-tests/src/test/java/io/wren/testing/TestMDLResource.java +++ b/wren-tests/src/test/java/io/wren/testing/TestMDLResource.java @@ -31,7 +31,7 @@ import static io.wren.base.config.WrenConfig.DataSourceType.DUCKDB; import static io.wren.base.config.WrenConfig.WREN_DATASOURCE_TYPE; import static io.wren.base.config.WrenConfig.WREN_ENABLE_DYNAMIC_FIELDS; -import static io.wren.base.dto.Column.caluclatedColumn; +import static io.wren.base.dto.Column.calculatedColumn; import static io.wren.base.dto.Column.column; import static io.wren.base.dto.Model.model; import static io.wren.base.dto.Relationship.relationship; @@ -114,7 +114,7 @@ public void testDryRunAndDryPlan() List.of(column("orderkey", "integer", null, false, "o_orderkey"), column("custkey", "integer", null, false, "o_custkey"), column("customer", "Customer", "CustomerOrders", false), - caluclatedColumn("customer_name", "varchar", "customer.name")), + calculatedColumn("customer_name", "varchar", "customer.name")), "orderkey"))) .setRelationships(List.of(relationship("CustomerOrders", List.of("Customer", "Orders"), JoinType.ONE_TO_MANY, "Customer.custkey = Orders.custkey"))) .build(); diff --git a/wren-tests/src/test/java/io/wren/testing/TestMDLResourceV2.java b/wren-tests/src/test/java/io/wren/testing/TestMDLResourceV2.java index d10cb9a6e..1c160da67 100644 --- a/wren-tests/src/test/java/io/wren/testing/TestMDLResourceV2.java +++ b/wren-tests/src/test/java/io/wren/testing/TestMDLResourceV2.java @@ -30,7 +30,7 @@ import static io.wren.base.config.WrenConfig.WREN_DATASOURCE_TYPE; import static io.wren.base.config.WrenConfig.WREN_DIRECTORY; import static io.wren.base.config.WrenConfig.WREN_ENABLE_DYNAMIC_FIELDS; -import static io.wren.base.dto.Column.caluclatedColumn; +import static io.wren.base.dto.Column.calculatedColumn; import static io.wren.base.dto.Column.column; import static io.wren.base.dto.Model.model; import static io.wren.base.dto.Relationship.relationship; @@ -76,7 +76,7 @@ public void testDryPlan() List.of(column("orderkey", "integer", null, false, "o_orderkey"), column("custkey", "integer", null, false, "o_custkey"), column("customer", "Customer", "CustomerOrders", false), - caluclatedColumn("customer_name", "varchar", "customer.name")), + calculatedColumn("customer_name", "varchar", "customer.name")), "orderkey"))) .setRelationships(List.of(relationship("CustomerOrders", List.of("Customer", "Orders"), JoinType.ONE_TO_MANY, "Customer.custkey = Orders.custkey"))) .build(); @@ -206,7 +206,7 @@ public void testSetManyToMany() List.of(column("orderkey", "integer", null, false, "o_orderkey"), column("custkey", "integer", null, false, "o_custkey"), column("customer", "Customer", "CustomerOrders", false), - caluclatedColumn("customer_name", "varchar", "customer.name")), + calculatedColumn("customer_name", "varchar", "customer.name")), "orderkey"))) .setRelationships(List.of(relationship("CustomerOrders", List.of("Customer", "Orders"), JoinType.MANY_TO_MANY, "Customer.custkey = Orders.custkey"))) .build(); diff --git a/wren-tests/src/test/java/io/wren/testing/duckdb/TestDynamicFields.java b/wren-tests/src/test/java/io/wren/testing/duckdb/TestDynamicFields.java index 628515f39..448c820ff 100644 --- a/wren-tests/src/test/java/io/wren/testing/duckdb/TestDynamicFields.java +++ b/wren-tests/src/test/java/io/wren/testing/duckdb/TestDynamicFields.java @@ -14,7 +14,9 @@ package io.wren.testing.duckdb; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import io.wren.base.Column; import io.wren.base.dto.Manifest; import io.wren.main.web.dto.QueryResultDto; import io.wren.testing.AbstractTestFramework; @@ -23,8 +25,9 @@ import java.nio.file.Files; import java.nio.file.Path; -import java.sql.SQLException; +import java.util.List; +import static io.wren.base.Column.column; import static io.wren.base.config.WrenConfig.DataSourceType.DUCKDB; import static io.wren.base.config.WrenConfig.WREN_DATASOURCE_TYPE; import static io.wren.base.config.WrenConfig.WREN_ENABLE_DYNAMIC_FIELDS; @@ -56,7 +59,6 @@ protected TestingWrenServer createWrenServer() @Test public void testDynamicMetric() - throws SQLException { // select one dimension and measure QueryResultDto actual = query(manifest, "SELECT customer, totalprice FROM CustomerDailyRevenue WHERE customer = 'Customer#000000048'"); @@ -68,7 +70,31 @@ public void testDynamicMetric() // select two dimensions and measure actual = query(manifest, "SELECT customer, date, totalprice FROM CustomerDailyRevenue WHERE customer = 'Customer#000000048' ORDER BY 1, 2"); expected = query(manifest, "SELECT c.name as customer, o.orderdate as date, SUM(o.totalprice) as totalprice FROM Orders o LEFT JOIN Customer c ON o.custkey = c.custkey\n" + - "WHERE c.name = 'Customer#000000048' GROUP BY 1, 2 ORDER BY 1, 2"); + "WHERE c.name = 'Customer#000000048' GROUP BY 1, 2 ORDER BY 1, 2"); assertThat(actual).isEqualTo(expected); } + + @Test + public void testTargetDotAllWillNotIncludeCalculatedField() + { + // Show that there is a calculated field in Orders. + QueryResultDto calculated = query(manifest, "SELECT nation_name FROM \"Orders\" LIMIT 1"); + assertThat(calculated.getColumns()).containsExactly(column("nation_name", "VARCHAR")); + + List expectedColumns = ImmutableList.of( + column("orderkey", "INTEGER"), + column("custkey", "INTEGER"), + column("orderstatus", "VARCHAR"), + column("totalprice", "DECIMAL(15,2)"), + column("orderdate", "DATE")); + + QueryResultDto case1 = query(manifest, "SELECT \"Orders\".* FROM \"Orders\" LIMIT 1"); + assertThat(case1.getColumns()).isEqualTo(expectedColumns); + + QueryResultDto case2 = query(manifest, "SELECT o.* FROM \"Orders\" AS o LIMIT 1"); + assertThat(case2.getColumns()).isEqualTo(expectedColumns); + + QueryResultDto case3 = query(manifest, "SELECT o.* FROM \"Orders\" AS o JOIN \"Customer\" AS c ON o.custkey = c.custkey LIMIT 1"); + assertThat(case3.getColumns()).isEqualTo(expectedColumns); + } }