|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from datafusion import SessionContext |
| 19 | +from datafusion.expr import ( |
| 20 | + Projection, |
| 21 | + Filter, |
| 22 | + Aggregate, |
| 23 | + Limit, |
| 24 | + Sort, |
| 25 | + TableScan, |
| 26 | +) |
| 27 | +import pytest |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def test_ctx(): |
| 32 | + ctx = SessionContext() |
| 33 | + ctx.register_csv("test", "testing/data/csv/aggregate_test_100.csv") |
| 34 | + return ctx |
| 35 | + |
| 36 | + |
| 37 | +def test_projection(test_ctx): |
| 38 | + df = test_ctx.sql("select c1, 123, c1 < 123 from test") |
| 39 | + plan = df.logical_plan() |
| 40 | + |
| 41 | + plan = plan.to_variant() |
| 42 | + assert isinstance(plan, Projection) |
| 43 | + |
| 44 | + plan = plan.input().to_variant() |
| 45 | + assert isinstance(plan, TableScan) |
| 46 | + |
| 47 | + |
| 48 | +def test_filter(test_ctx): |
| 49 | + df = test_ctx.sql("select c1 from test WHERE c1 > 5") |
| 50 | + plan = df.logical_plan() |
| 51 | + |
| 52 | + plan = plan.to_variant() |
| 53 | + assert isinstance(plan, Projection) |
| 54 | + |
| 55 | + plan = plan.input().to_variant() |
| 56 | + assert isinstance(plan, Filter) |
| 57 | + |
| 58 | + |
| 59 | +def test_limit(test_ctx): |
| 60 | + df = test_ctx.sql("select c1 from test LIMIT 10") |
| 61 | + plan = df.logical_plan() |
| 62 | + |
| 63 | + plan = plan.to_variant() |
| 64 | + assert isinstance(plan, Limit) |
| 65 | + |
| 66 | + |
| 67 | +def test_aggregate(test_ctx): |
| 68 | + df = test_ctx.sql("select c1, COUNT(*) from test GROUP BY c1") |
| 69 | + plan = df.logical_plan() |
| 70 | + |
| 71 | + plan = plan.to_variant() |
| 72 | + assert isinstance(plan, Projection) |
| 73 | + |
| 74 | + plan = plan.input().to_variant() |
| 75 | + assert isinstance(plan, Aggregate) |
| 76 | + |
| 77 | + |
| 78 | +def test_sort(test_ctx): |
| 79 | + df = test_ctx.sql("select c1 from test order by c1") |
| 80 | + plan = df.logical_plan() |
| 81 | + |
| 82 | + plan = plan.to_variant() |
| 83 | + assert isinstance(plan, Sort) |
0 commit comments