@@ -580,3 +580,288 @@ def test_stdout_output_json(self, example_worker: str) -> None:
580580 assert result.exit_code == 0
581581 # Should have JSON output
582582 assert "n" in result.output
583+
584+
585+ class TestCLIScalarFunction:
586+ """Tests for CLI scalar function invocation with --type scalar."""
587+
588+ @pytest.fixture
589+ def scalar_input_parquet(self, tmp_path: Path) -> Path:
590+ """Create a parquet file suitable for scalar function tests."""
591+ batch = pa.RecordBatch.from_pydict({"x": [1, 2, 3, 4, 5]})
592+ input_file = tmp_path / "scalar_input.parquet"
593+ pq.write_table(pa.Table.from_batches([batch]), str(input_file))
594+ return input_file
595+
596+ def test_scalar_function_invocation(
597+ self, example_worker: str, scalar_input_parquet: Path
598+ ) -> None:
599+ """Invoke a scalar function with --type scalar."""
600+ runner = CliRunner()
601+ result = runner.invoke(
602+ cli,
603+ [
604+ "--input",
605+ str(scalar_input_parquet),
606+ "--function",
607+ "double_column",
608+ "--args",
609+ '["x"]',
610+ "--type",
611+ "scalar",
612+ "--server",
613+ example_worker,
614+ ],
615+ )
616+ assert result.exit_code == 0
617+
618+ def test_scalar_function_with_output_file(
619+ self, example_worker: str, scalar_input_parquet: Path, tmp_path: Path
620+ ) -> None:
621+ """Scalar function with output to file."""
622+ output_file = tmp_path / "output.jsonl"
623+ runner = CliRunner()
624+ result = runner.invoke(
625+ cli,
626+ [
627+ "--input",
628+ str(scalar_input_parquet),
629+ "--output",
630+ str(output_file),
631+ "--format",
632+ "json",
633+ "--function",
634+ "double_column",
635+ "--args",
636+ '["x"]',
637+ "--type",
638+ "scalar",
639+ "--server",
640+ example_worker,
641+ ],
642+ )
643+ assert result.exit_code == 0
644+ assert output_file.exists()
645+ lines = output_file.read_text().strip().split("\n")
646+ # Should have 5 rows
647+ assert len(lines) == 5
648+ # Verify first row is doubled
649+ first_row = json.loads(lines[0])
650+ assert first_row["result"] == 2
651+
652+ def test_scalar_function_parquet_output(
653+ self, example_worker: str, scalar_input_parquet: Path, tmp_path: Path
654+ ) -> None:
655+ """Scalar function with parquet output."""
656+ output_file = tmp_path / "output.parquet"
657+ runner = CliRunner()
658+ result = runner.invoke(
659+ cli,
660+ [
661+ "--input",
662+ str(scalar_input_parquet),
663+ "--output",
664+ str(output_file),
665+ "--format",
666+ "parquet",
667+ "--function",
668+ "double_column",
669+ "--args",
670+ '["x"]',
671+ "--type",
672+ "scalar",
673+ "--server",
674+ example_worker,
675+ ],
676+ )
677+ assert result.exit_code == 0
678+ # Verify parquet output
679+ table = pq.read_table(str(output_file))
680+ assert table.num_rows == 5
681+ assert table.column_names == ["result"]
682+ assert table.column("result").to_pylist() == [2, 4, 6, 8, 10]
683+
684+ def test_scalar_type_requires_input(self, example_worker: str) -> None:
685+ """--type scalar requires --input."""
686+ runner = CliRunner()
687+ result = runner.invoke(
688+ cli,
689+ [
690+ "--function",
691+ "double_column",
692+ "--args",
693+ '["x"]',
694+ "--type",
695+ "scalar",
696+ "--server",
697+ example_worker,
698+ ],
699+ )
700+ assert result.exit_code != 0
701+ assert "requires --input" in result.output
702+
703+ def test_table_in_out_type_requires_input(self, example_worker: str) -> None:
704+ """--type table-in-out requires --input."""
705+ runner = CliRunner()
706+ result = runner.invoke(
707+ cli,
708+ [
709+ "--function",
710+ "echo",
711+ "--type",
712+ "table-in-out",
713+ "--server",
714+ example_worker,
715+ ],
716+ )
717+ assert result.exit_code != 0
718+ assert "requires --input" in result.output
719+
720+ def test_table_type_rejects_input(
721+ self, example_worker: str, scalar_input_parquet: Path
722+ ) -> None:
723+ """--type table does not accept --input."""
724+ runner = CliRunner()
725+ result = runner.invoke(
726+ cli,
727+ [
728+ "--input",
729+ str(scalar_input_parquet),
730+ "--function",
731+ "sequence",
732+ "--args",
733+ "[5]",
734+ "--type",
735+ "table",
736+ "--server",
737+ example_worker,
738+ ],
739+ )
740+ assert result.exit_code != 0
741+ assert "does not accept --input" in result.output
742+
743+ def test_auto_type_with_input_uses_table_in_out(
744+ self, example_worker: str, scalar_input_parquet: Path, tmp_path: Path
745+ ) -> None:
746+ """--type auto with --input uses table-in-out (echo function)."""
747+ output_file = tmp_path / "output.jsonl"
748+ runner = CliRunner()
749+ result = runner.invoke(
750+ cli,
751+ [
752+ "--input",
753+ str(scalar_input_parquet),
754+ "--output",
755+ str(output_file),
756+ "--format",
757+ "json",
758+ "--function",
759+ "echo",
760+ "--type",
761+ "auto",
762+ "--server",
763+ example_worker,
764+ ],
765+ )
766+ assert result.exit_code == 0
767+ # Echo should preserve the original column name "x"
768+ content = output_file.read_text()
769+ assert '"x"' in content
770+
771+ def test_auto_type_without_input_uses_table(
772+ self, example_worker: str, tmp_path: Path
773+ ) -> None:
774+ """--type auto without --input uses table function."""
775+ output_file = tmp_path / "output.jsonl"
776+ runner = CliRunner()
777+ result = runner.invoke(
778+ cli,
779+ [
780+ "--output",
781+ str(output_file),
782+ "--format",
783+ "json",
784+ "--function",
785+ "sequence",
786+ "--args",
787+ "[3]",
788+ "--type",
789+ "auto",
790+ "--server",
791+ example_worker,
792+ ],
793+ )
794+ assert result.exit_code == 0
795+ lines = output_file.read_text().strip().split("\n")
796+ assert len(lines) == 3
797+
798+ def test_scalar_with_add_columns(
799+ self, example_worker: str, tmp_path: Path
800+ ) -> None:
801+ """Test add_columns scalar function via CLI."""
802+ # Create input with two columns
803+ batch = pa.RecordBatch.from_pydict({"a": [1, 2, 3], "b": [10, 20, 30]})
804+ input_file = tmp_path / "input.parquet"
805+ pq.write_table(pa.Table.from_batches([batch]), str(input_file))
806+
807+ output_file = tmp_path / "output.jsonl"
808+ runner = CliRunner()
809+ result = runner.invoke(
810+ cli,
811+ [
812+ "--input",
813+ str(input_file),
814+ "--output",
815+ str(output_file),
816+ "--format",
817+ "json",
818+ "--function",
819+ "add_columns",
820+ "--args",
821+ '["a", "b"]',
822+ "--type",
823+ "scalar",
824+ "--server",
825+ example_worker,
826+ ],
827+ )
828+ assert result.exit_code == 0
829+ lines = output_file.read_text().strip().split("\n")
830+ assert len(lines) == 3
831+ # Verify sums
832+ results = [json.loads(line)["result"] for line in lines]
833+ assert results == [11, 22, 33]
834+
835+ def test_scalar_with_upper_case(
836+ self, example_worker: str, tmp_path: Path
837+ ) -> None:
838+ """Test upper_case scalar function via CLI."""
839+ batch = pa.RecordBatch.from_pydict({"name": ["alice", "bob"]})
840+ input_file = tmp_path / "input.parquet"
841+ pq.write_table(pa.Table.from_batches([batch]), str(input_file))
842+
843+ output_file = tmp_path / "output.jsonl"
844+ runner = CliRunner()
845+ result = runner.invoke(
846+ cli,
847+ [
848+ "--input",
849+ str(input_file),
850+ "--output",
851+ str(output_file),
852+ "--format",
853+ "json",
854+ "--function",
855+ "upper_case",
856+ "--args",
857+ '["name"]',
858+ "--type",
859+ "scalar",
860+ "--server",
861+ example_worker,
862+ ],
863+ )
864+ assert result.exit_code == 0
865+ lines = output_file.read_text().strip().split("\n")
866+ results = [json.loads(line)["result"] for line in lines]
867+ assert results == ["ALICE", "BOB"]
0 commit comments