|  | @@ -28,7 +28,7 @@ public class JdbcCsvSpecIT extends CsvSpecTestCase {
 | 
	
		
			
				|  |  |      public static final String EXTRACT_FN_NAME = "EXTRACT";
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private static final Pattern DESCRIBE_OR_SHOW = Pattern.compile("(?i)\\s*(DESCRIBE|SHOW).*");
 | 
	
		
			
				|  |  | -    private static final Pattern FROM_QUALIFIED = Pattern.compile("(?i)FROM\\w+" + REMOTE_CLUSTER_NAME + ":");
 | 
	
		
			
				|  |  | +    private static final Pattern FROM_QUALIFIED = Pattern.compile(".*(?i)FROM\\s+[^\\s:]+:[^\\s:]+\\s.*");
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @ParametersFactory(argumentFormatting = PARAM_FORMATTING)
 | 
	
		
			
				|  |  |      public static List<Object[]> readScriptSpec() throws Exception {
 | 
	
	
		
			
				|  | @@ -39,7 +39,13 @@ public class JdbcCsvSpecIT extends CsvSpecTestCase {
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      public JdbcCsvSpecIT(String fileName, String groupName, String testName, Integer lineNumber, CsvTestCase testCase) {
 | 
	
		
			
				|  |  | -        super(fileName, groupName, testName, lineNumber, randomBoolean() ? qualifyFromClause(testCase) : testCase);
 | 
	
		
			
				|  |  | +        super(
 | 
	
		
			
				|  |  | +            fileName,
 | 
	
		
			
				|  |  | +            groupName,
 | 
	
		
			
				|  |  | +            testName,
 | 
	
		
			
				|  |  | +            lineNumber,
 | 
	
		
			
				|  |  | +            randomBoolean() && isFromQualified(testCase.query) == false ? qualifyFromClause(testCase) : testCase
 | 
	
		
			
				|  |  | +        );
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      // qualify the query FROM clause with the cluster name, but (crudely) skip `EXTRACT(a FROM b)` calls.
 | 
	
	
		
			
				|  | @@ -94,7 +100,9 @@ public class JdbcCsvSpecIT extends CsvSpecTestCase {
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public Connection esJdbc() throws SQLException {
 | 
	
		
			
				|  |  |          Connection connection = esJdbc(connectionProperties());
 | 
	
		
			
				|  |  | -        if (FROM_QUALIFIED.matcher(csvTestCase().query).matches() == false) {
 | 
	
		
			
				|  |  | +        // Only set the default catalog if the query index isn't yet qualified with the catalog, which can happen if query has been written
 | 
	
		
			
				|  |  | +        // qualified from the start (for the documentation) or edited in qualifyFromClause() above.
 | 
	
		
			
				|  |  | +        if (isFromQualified(csvTestCase().query) == false) {
 | 
	
		
			
				|  |  |              connection.setCatalog(REMOTE_CLUSTER_NAME);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          return connection;
 | 
	
	
		
			
				|  | @@ -113,4 +121,10 @@ public class JdbcCsvSpecIT extends CsvSpecTestCase {
 | 
	
		
			
				|  |  |          // similar to https://github.com/elastic/elasticsearch/issues/35176 quicker
 | 
	
		
			
				|  |  |          return fileName.startsWith("nested") && randomBoolean() ? randomIntBetween(1, 5) : super.fetchSize();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    // Simple check if the FROM clause of a query contains a cluster-qualified index (pattern).
 | 
	
		
			
				|  |  | +    // Note: it won't work reliably with multiple clauses (subselects) or queries embedded in strings.
 | 
	
		
			
				|  |  | +    private static boolean isFromQualified(String query) {
 | 
	
		
			
				|  |  | +        return FROM_QUALIFIED.matcher(query).matches();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  }
 |