11import { describe , expect , test } from "vitest" ;
22import {
33 convertToQueryType ,
4- type DatabricksStatementExecutionResponse ,
54 extractParameters ,
5+ extractParameterTypes ,
66 SERVER_INJECTED_PARAMS ,
77} from "../query-registry" ;
8+ import type { DatabricksStatementExecutionResponse } from "../types" ;
89
910describe ( "extractParameters" , ( ) => {
1011 test ( "extracts parameters from SQL query" , ( ) => {
@@ -47,6 +48,69 @@ describe("SERVER_INJECTED_PARAMS", () => {
4748 } ) ;
4849} ) ;
4950
51+ describe ( "extractParameterTypes" , ( ) => {
52+ test ( "extracts parameter types from SQL comments" , ( ) => {
53+ const sql = `-- @param startDate DATE
54+ -- @param endDate DATE
55+ -- @param groupBy STRING
56+ SELECT * FROM users WHERE date BETWEEN :startDate AND :endDate` ;
57+ const types = extractParameterTypes ( sql ) ;
58+
59+ expect ( types . startDate ) . toBe ( "DATE" ) ;
60+ expect ( types . endDate ) . toBe ( "DATE" ) ;
61+ expect ( types . groupBy ) . toBe ( "STRING" ) ;
62+ } ) ;
63+
64+ test ( "returns empty object for SQL without @param comments" , ( ) => {
65+ const sql = "SELECT * FROM users WHERE date = :startDate" ;
66+ const types = extractParameterTypes ( sql ) ;
67+
68+ expect ( Object . keys ( types ) . length ) . toBe ( 0 ) ;
69+ } ) ;
70+
71+ test ( "handles all supported types" , ( ) => {
72+ const sql = `-- @param str STRING
73+ -- @param num NUMERIC
74+ -- @param bool BOOLEAN
75+ -- @param dt DATE
76+ -- @param ts TIMESTAMP
77+ -- @param bin BINARY
78+ SELECT 1` ;
79+ const types = extractParameterTypes ( sql ) ;
80+
81+ expect ( types . str ) . toBe ( "STRING" ) ;
82+ expect ( types . num ) . toBe ( "NUMERIC" ) ;
83+ expect ( types . bool ) . toBe ( "BOOLEAN" ) ;
84+ expect ( types . dt ) . toBe ( "DATE" ) ;
85+ expect ( types . ts ) . toBe ( "TIMESTAMP" ) ;
86+ expect ( types . bin ) . toBe ( "BINARY" ) ;
87+ } ) ;
88+
89+ test ( "ignores malformed @param comments" , ( ) => {
90+ const sql = `-- @param startDate
91+ -- @param INVALID
92+ -- @param noType
93+ -- this is not a param comment
94+ SELECT 1` ;
95+ const types = extractParameterTypes ( sql ) ;
96+
97+ expect ( Object . keys ( types ) . length ) . toBe ( 0 ) ;
98+ } ) ;
99+
100+ test ( "handles mixed valid and invalid annotations" , ( ) => {
101+ const sql = `-- @param validDate DATE
102+ -- @param invalidParam
103+ -- @param validString STRING
104+ SELECT 1` ;
105+ const types = extractParameterTypes ( sql ) ;
106+
107+ expect ( types . validDate ) . toBe ( "DATE" ) ;
108+ expect ( types . validString ) . toBe ( "STRING" ) ;
109+ expect ( types . invalidParam ) . toBeUndefined ( ) ;
110+ expect ( Object . keys ( types ) . length ) . toBe ( 2 ) ;
111+ } ) ;
112+ } ) ;
113+
50114describe ( "convertToQueryType" , ( ) => {
51115 const mockResponse : DatabricksStatementExecutionResponse = {
52116 statement_id : "test-123" ,
@@ -74,7 +138,7 @@ describe("convertToQueryType", () => {
74138
75139 expect ( result ) . toContain ( 'name: "users"' ) ;
76140 expect ( result ) . toContain ( "parameters:" ) ;
77- expect ( result ) . toContain ( "startDate: string " ) ;
141+ expect ( result ) . toContain ( "startDate: SQLTypeMarker " ) ;
78142 expect ( result ) . toContain ( "result: Array<{" ) ;
79143 } ) ;
80144
@@ -83,8 +147,20 @@ describe("convertToQueryType", () => {
83147 "SELECT * FROM users WHERE workspace_id = :workspaceId AND date = :startDate" ;
84148 const result = convertToQueryType ( mockResponse , sql , "users" ) ;
85149
86- expect ( result ) . toContain ( "startDate: string" ) ;
87- expect ( result ) . not . toContain ( "workspaceId: string" ) ;
150+ expect ( result ) . toContain ( "startDate: SQLTypeMarker" ) ;
151+ expect ( result ) . not . toContain ( "workspaceId:" ) ;
152+ } ) ;
153+
154+ test ( "uses specific marker types when @param annotation is provided" , ( ) => {
155+ const sql = `-- @param startDate DATE
156+ -- @param count NUMERIC
157+ -- @param name STRING
158+ SELECT * FROM users WHERE date = :startDate AND count = :count AND name = :name` ;
159+ const result = convertToQueryType ( mockResponse , sql , "users" ) ;
160+
161+ expect ( result ) . toContain ( "startDate: SQLDateMarker" ) ;
162+ expect ( result ) . toContain ( "count: SQLNumberMarker" ) ;
163+ expect ( result ) . toContain ( "name: SQLStringMarker" ) ;
88164 } ) ;
89165
90166 test ( "generates Record<string, never> for queries without params" , ( ) => {
0 commit comments