mimo-openenv-software / tasks /format-code-task-002860.json
akseljoonas's picture
Publish validated twelve-task MiMo curriculum and immutable image request
2a3480c verified
Raw History Blame Contribute Delete
4.02 kB
{"cwd": "/testbed", "dataset_type": "opensource-code", "docker_image": "format-code-task-002860:latest", "instance_id": "format-code-task-002860", "problem_statement": "BigQuery does not parse COUNTIF\nHello :wave:! I noticed `COUNTIF` is not correctly transpiled from BigQuery to DuckDB, and while digging for the issue, it seems to be because it doesn't parse correctly.\r\n\r\nIn BigQuery it's `COUNTIF` [1] without underscore, but the default parser for this function seems to be `COUNT_IF`.\r\n\r\n**Reproduction:**\r\n```pyshell\r\n\u03bb python\r\nPython 3.11.3 | packaged by conda-forge | (main, Apr 6 2023, 08:57:19) [GCC 11.3.0] on linux\r\n[...]\r\n>>> import sqlglot\r\n>>> sqlglot.parse_one('select countif(x)', 'bigquery')\r\nSelect(\r\n expressions=[\r\n Anonymous(\r\n this=countif,\r\n expressions=[\r\n Column(\r\n this=Identifier(this=x, quoted=False))])])\r\n```\r\n\r\nI expected `countif` to be parsed as `exp.CountIf`, but it gets parsed as an anonymous expression.\r\n\r\nI have a patch with a failing test case and a corresponding fix that I am happy to submit for review if that's of interest.\r\n\r\nThanks in advance.\r\n\r\nRefs\r\n\r\n[1] https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators", "test_command": "bash /testbed/mimo_test_command.sh", "test_patch": "diff --git a/test_commands.json b/test_commands.json\nnew file mode 100644\nindex 000000000..e69e1c52d\n--- /dev/null\n+++ b/test_commands.json\n@@ -0,0 +1,3 @@\n+{\n+ \"test_commands\": [\"python -m unittest tests.dialects.test_bigquery.TestBigQuery.test_countif -v\"]\n+}\ndiff --git a/tests/dialects/test_bigquery.py b/tests/dialects/test_bigquery.py\nindex a01ff292a..438ff93d8 100644\n--- a/tests/dialects/test_bigquery.py\n+++ b/tests/dialects/test_bigquery.py\n@@ -5,7 +5,9 @@ from sqlglot import (\n ParseError,\n TokenError,\n UnsupportedError,\n+ exp,\n parse,\n+ parse_one,\n transpile,\n )\n from sqlglot.helper import logger as helper_logger\n@@ -1088,3 +1090,38 @@ OPTIONS (\n \n with self.assertRaises(ParseError):\n transpile(\"SELECT JSON_OBJECT('a', 1, 'b') AS json_data\", read=\"bigquery\")\n+\n+ def test_countif(self):\n+\n+ ast_lower = parse_one(\"SELECT countif(x) FROM t\", read=\"bigquery\")\n+ self.assertIsInstance(ast_lower.expressions[0], exp.CountIf)\n+ self.assertNotIsInstance(ast_lower.expressions[0], exp.Anonymous)\n+\n+ ast_upper = parse_one(\"SELECT COUNTIF(x) FROM t\", read=\"bigquery\")\n+ self.assertIsInstance(ast_upper.expressions[0], exp.CountIf)\n+\n+\n+ ast_underscore = parse_one(\"SELECT COUNT_IF(x) FROM t\")\n+ self.assertIsInstance(ast_underscore.expressions[0], exp.CountIf)\n+\n+\n+ ast_from_default = parse_one(\"SELECT COUNT_IF(x) FROM t\")\n+ self.assertEqual(ast_from_default.sql(dialect=\"bigquery\"), \"SELECT COUNTIF(x) FROM t\")\n+\n+\n+ self.assertEqual(\n+ transpile(\"SELECT COUNTIF(x) FROM t\", read=\"bigquery\", write=\"bigquery\")[0],\n+ \"SELECT COUNTIF(x) FROM t\",\n+ )\n+ self.assertEqual(\n+ transpile(\"SELECT COUNTIF(x) FROM t\", read=\"bigquery\", write=\"duckdb\")[0],\n+ \"SELECT COUNT_IF(x) FROM t\",\n+ )\n+ self.assertEqual(\n+ transpile(\"SELECT COUNT_IF(x) FROM t\", read=\"spark\", write=\"bigquery\")[0],\n+ \"SELECT COUNTIF(x) FROM t\",\n+ )\n+ self.assertEqual(\n+ transpile(\"SELECT COUNT_IF(x) FROM t\", write=\"bigquery\")[0],\n+ \"SELECT COUNTIF(x) FROM t\",\n+ )\ndiff --git a/mimo_test_command.sh b/mimo_test_command.sh\nnew file mode 100755\n--- /dev/null\n+++ b/mimo_test_command.sh\n@@ -0,0 +1,8 @@\n+#!/usr/bin/env bash\n+\n+set -uo pipefail\n+cd /testbed\n+rc=0\n+python -m unittest tests.dialects.test_bigquery.TestBigQuery.test_countif -v\n+rc=$(( rc | $? ))\n+exit $rc\n", "verifier_timeout_sec": 1800}