Skip to content

Commit ae16f6c

Browse files
[KYUUBI #7379][PR 2a/4] Data Agent Engine: tool system, data source, and prompt templates
1 parent 3c028ae commit ae16f6c

25 files changed

Lines changed: 2445 additions & 0 deletions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.kyuubi.engine.dataagent.datasource;
19+
20+
import com.zaxxer.hikari.HikariConfig;
21+
import com.zaxxer.hikari.HikariDataSource;
22+
import javax.sql.DataSource;
23+
24+
/** Factory for creating pooled DataSource instances from JDBC URLs. */
25+
public final class DataSourceFactory {
26+
27+
private static final int DEFAULT_MAX_POOL_SIZE = 5;
28+
29+
private DataSourceFactory() {}
30+
31+
/**
32+
* Create a pooled DataSource from a JDBC URL. Supports any JDBC driver available on the
33+
* classpath. For authenticated databases, credentials can be embedded in the URL.
34+
*
35+
* @param jdbcUrl the JDBC connection URL
36+
* @return a HikariCP-backed DataSource
37+
*/
38+
public static DataSource create(String jdbcUrl) {
39+
return create(jdbcUrl, null);
40+
}
41+
42+
/**
43+
* Create a pooled DataSource from a JDBC URL with an explicit username. When the data-agent
44+
* connects back to Kyuubi Server, the username determines the proxy user for the downstream
45+
* engine (e.g. Spark). Without it, Kyuubi defaults to "anonymous" which typically fails Hadoop
46+
* impersonation checks.
47+
*
48+
* @param jdbcUrl the JDBC connection URL
49+
* @param user the username for the JDBC connection, may be null
50+
* @return a HikariCP-backed DataSource
51+
*/
52+
public static DataSource create(String jdbcUrl, String user) {
53+
HikariConfig config = new HikariConfig();
54+
config.setJdbcUrl(jdbcUrl);
55+
if (user != null && !user.isEmpty()) {
56+
config.setUsername(user);
57+
}
58+
config.setMaximumPoolSize(DEFAULT_MAX_POOL_SIZE);
59+
config.setMinimumIdle(1);
60+
config.setInitializationFailTimeout(-1);
61+
config.setPoolName("kyuubi-data-agent");
62+
return new HikariDataSource(config);
63+
}
64+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.kyuubi.engine.dataagent.datasource;
19+
20+
/**
21+
* SQL dialect abstraction for datasource-specific SQL generation.
22+
*
23+
* <p>Each dialect maps to a datasource name used for prompt resource lookup ({@code
24+
* prompts/datasource-{name}.md}).
25+
*/
26+
public interface JdbcDialect {
27+
28+
/** Datasource name for prompt resource lookup (e.g. "spark", "trino"). */
29+
String datasourceName();
30+
31+
/**
32+
* Quote an identifier (table/column/database name) using the dialect-appropriate quote character.
33+
* Escapes any embedded quote characters by doubling them.
34+
*/
35+
String quoteIdentifier(String identifier);
36+
37+
/**
38+
* Infer the dialect from a JDBC URL.
39+
*
40+
* @param jdbcUrl the JDBC connection URL
41+
* @return the matching dialect, or {@code null} if unrecognized
42+
*/
43+
static JdbcDialect fromUrl(String jdbcUrl) {
44+
if (jdbcUrl == null) {
45+
return null;
46+
}
47+
String lower = jdbcUrl.toLowerCase();
48+
if (lower.startsWith("jdbc:hive2:") || lower.startsWith("jdbc:spark:")) {
49+
return SparkDialect.INSTANCE;
50+
}
51+
if (lower.startsWith("jdbc:trino:")) {
52+
return TrinoDialect.INSTANCE;
53+
}
54+
if (lower.startsWith("jdbc:mysql:")) {
55+
return MysqlDialect.INSTANCE;
56+
}
57+
if (lower.startsWith("jdbc:sqlite:")) {
58+
return SqliteDialect.INSTANCE;
59+
}
60+
// MySQL as fallback for unrecognized JDBC URLs
61+
return MysqlDialect.INSTANCE;
62+
}
63+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.kyuubi.engine.dataagent.datasource;
19+
20+
/** MySQL dialect. Uses backtick quoting for identifiers. */
21+
public final class MysqlDialect implements JdbcDialect {
22+
23+
static final MysqlDialect INSTANCE = new MysqlDialect();
24+
25+
private MysqlDialect() {}
26+
27+
@Override
28+
public String datasourceName() {
29+
return "mysql";
30+
}
31+
32+
@Override
33+
public String quoteIdentifier(String identifier) {
34+
String escaped = identifier.replace("`", "``");
35+
return "`" + escaped + "`";
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.kyuubi.engine.dataagent.datasource;
19+
20+
/** Spark SQL dialect. Uses backtick quoting for identifiers. */
21+
public final class SparkDialect implements JdbcDialect {
22+
23+
static final SparkDialect INSTANCE = new SparkDialect();
24+
25+
private SparkDialect() {}
26+
27+
@Override
28+
public String datasourceName() {
29+
return "spark";
30+
}
31+
32+
@Override
33+
public String quoteIdentifier(String identifier) {
34+
String escaped = identifier.replace("`", "``");
35+
return "`" + escaped + "`";
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.kyuubi.engine.dataagent.datasource;
19+
20+
/** SQLite dialect. Uses double-quote quoting for identifiers. */
21+
public final class SqliteDialect implements JdbcDialect {
22+
23+
static final SqliteDialect INSTANCE = new SqliteDialect();
24+
25+
private SqliteDialect() {}
26+
27+
@Override
28+
public String datasourceName() {
29+
return "sqlite";
30+
}
31+
32+
@Override
33+
public String quoteIdentifier(String identifier) {
34+
String escaped = identifier.replace("\"", "\"\"");
35+
return "\"" + escaped + "\"";
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.kyuubi.engine.dataagent.datasource;
19+
20+
/** Trino SQL dialect. Uses double-quote quoting for identifiers. */
21+
public final class TrinoDialect implements JdbcDialect {
22+
23+
static final TrinoDialect INSTANCE = new TrinoDialect();
24+
25+
private TrinoDialect() {}
26+
27+
@Override
28+
public String datasourceName() {
29+
return "trino";
30+
}
31+
32+
@Override
33+
public String quoteIdentifier(String identifier) {
34+
String escaped = identifier.replace("\"", "\"\"");
35+
return "\"" + escaped + "\"";
36+
}
37+
}

0 commit comments

Comments
 (0)