|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import bigframes_vendored.sqlglot.expressions as sge |
| 16 | +from google.cloud import bigquery |
| 17 | +import pytest |
| 18 | + |
| 19 | +from bigframes.core.compile.sqlglot.sql import base, dml |
| 20 | + |
| 21 | +pytest.importorskip("pytest_snapshot") |
| 22 | + |
| 23 | + |
| 24 | +def test_insert_from_select(snapshot): |
| 25 | + query = sge.select("*").from_( |
| 26 | + sge.Table(this=sge.Identifier(this="source_table", quoted=True)) |
| 27 | + ) |
| 28 | + destination = bigquery.TableReference.from_string( |
| 29 | + "bigframes-dev.sqlglot_test.dest_table" |
| 30 | + ) |
| 31 | + |
| 32 | + expr = dml.insert(query, destination) |
| 33 | + sql = base.to_sql(expr) |
| 34 | + |
| 35 | + snapshot.assert_match(sql, "out.sql") |
| 36 | + |
| 37 | + |
| 38 | +def test_insert_from_table(snapshot): |
| 39 | + query = sge.Table(this=sge.Identifier(this="source_table", quoted=True)) |
| 40 | + destination = bigquery.TableReference.from_string( |
| 41 | + "bigframes-dev.sqlglot_test.dest_table" |
| 42 | + ) |
| 43 | + |
| 44 | + expr = dml.insert(query, destination) |
| 45 | + sql = base.to_sql(expr) |
| 46 | + |
| 47 | + snapshot.assert_match(sql, "out.sql") |
| 48 | + |
| 49 | + |
| 50 | +def test_replace_from_select(snapshot): |
| 51 | + query = sge.select("*").from_( |
| 52 | + sge.Table(this=sge.Identifier(this="source_table", quoted=True)) |
| 53 | + ) |
| 54 | + destination = bigquery.TableReference.from_string( |
| 55 | + "bigframes-dev.sqlglot_test.dest_table" |
| 56 | + ) |
| 57 | + |
| 58 | + expr = dml.replace(query, destination) |
| 59 | + sql = base.to_sql(expr) |
| 60 | + |
| 61 | + snapshot.assert_match(sql, "out.sql") |
| 62 | + |
| 63 | + |
| 64 | +def test_replace_from_table(snapshot): |
| 65 | + query = sge.Table(this=sge.Identifier(this="source_table", quoted=True)) |
| 66 | + destination = bigquery.TableReference.from_string( |
| 67 | + "bigframes-dev.sqlglot_test.dest_table" |
| 68 | + ) |
| 69 | + |
| 70 | + expr = dml.replace(query, destination) |
| 71 | + sql = base.to_sql(expr) |
| 72 | + |
| 73 | + snapshot.assert_match(sql, "out.sql") |
0 commit comments