@@ -486,6 +486,58 @@ private void assertInsertColumns(String sql, String... expectedColumns) {
486486 Assert .assertEquals (actualColumns , expectedColumns , "Insert column names mismatch for: " + sql );
487487 }
488488
489+ @ Test (dataProvider = "insertTargetFunctionDP" )
490+ public void testInsertTargetTableFunctionIsReportedAsFunction (String sql , boolean expectedUseFunction ,
491+ int expectedValuesGroups ) {
492+ ParsedPreparedStatement stmt = parser .parsePreparedStatement (sql );
493+ Assert .assertFalse (stmt .isHasErrors (), "Query should parse without errors: " + sql );
494+ Assert .assertTrue (stmt .isInsert (), "Should be an INSERT: " + sql );
495+ Assert .assertEquals (stmt .isUseFunction (), expectedUseFunction , "useFunction mismatch for: " + sql );
496+ Assert .assertEquals (stmt .getAssignValuesGroups (), expectedValuesGroups ,
497+ "Values group count mismatch for: " + sql );
498+ }
499+
500+ @ DataProvider
501+ public static Object [][] insertTargetFunctionDP () {
502+ return new Object [][] {
503+ // An INSERT whose target is a table function has no plain table behind it
504+ {"INSERT INTO FUNCTION null('id UInt32') VALUES (?)" , true , 1 },
505+ {"INSERT INTO TABLE FUNCTION null('id UInt32') VALUES (?)" , true , 1 },
506+ {"INSERT INTO FUNCTION remoteSecure('h', 'db', 't', 'u', 'p') (id, name) VALUES (?, ?)" , true , 1 },
507+ {"INSERT INTO FUNCTION s3('url', 'key', 'secret', 'CSV') SELECT * FROM t" , true , 0 },
508+ {"insert into function null('id UInt32') values (?)" , true , 1 },
509+ {"INSERT INTO\n TABLE FUNCTION null('id UInt32')\n VALUES (?)" , true , 1 },
510+ {"INSERT INTO /* target */ FUNCTION null('id UInt32') VALUES (?)" , true , 1 },
511+ // Contrast: plain table targets, including a table literally named "function"
512+ {"INSERT INTO t VALUES (?)" , false , 1 },
513+ {"INSERT INTO TABLE t VALUES (?)" , false , 1 },
514+ {"INSERT INTO db.t VALUES (?)" , false , 1 },
515+ {"INSERT INTO function VALUES (?)" , false , 1 },
516+ {"INSERT INTO TABLE function VALUES (?)" , false , 1 },
517+ {"INSERT INTO function (id) VALUES (?)" , false , 1 },
518+ // Contrast: a function inside the values list is already reported as a function
519+ {"INSERT INTO t VALUES (now(), ?)" , true , 1 },
520+ };
521+ }
522+
523+ @ Test (dataProvider = "insertTargetTableNameDP" )
524+ public void testInsertPlainTableTargetKeepsTableName (String sql , String expectedTable ) {
525+ ParsedPreparedStatement stmt = parser .parsePreparedStatement (sql );
526+ Assert .assertFalse (stmt .isHasErrors (), "Query should parse without errors: " + sql );
527+ Assert .assertEquals (stmt .getTable (), expectedTable , "Table name mismatch for: " + sql );
528+ }
529+
530+ @ DataProvider
531+ public static Object [][] insertTargetTableNameDP () {
532+ return new Object [][] {
533+ {"INSERT INTO t VALUES (?)" , "t" },
534+ {"INSERT INTO TABLE t VALUES (?)" , "t" },
535+ {"INSERT INTO db.t VALUES (?)" , "db.t" },
536+ {"INSERT INTO function VALUES (?)" , "function" },
537+ {"INSERT INTO TABLE function VALUES (?)" , "function" },
538+ };
539+ }
540+
489541 @ Test (dataProvider = "testCreateStmtDP" )
490542 public void testCreateStatement (String sql ) {
491543 ParsedPreparedStatement stmt = parser .parsePreparedStatement (sql );
0 commit comments