LoginSignup
8
4

More than 1 year has passed since last update.

Github Copilot Chat にて生成される SQL 文を Few-shot learning により改善する方法

Last updated at Posted at 2023-06-07

概要

Github Copilot Chat にて SQL 文(CREATE TABLE 文)を Few-shot learning により改善できたため、実施内容を共有します。データストアに最適化した SQL を生成するプロンプトに関する記事があまり見つけられなかったため、参考にしていただければ幸いです。

想定実行対象のデータストアは、Databricks(Spark) であり、Spark に関する情報を学習していないようで、想定通りの CREATE TABLE 文を生成できませんでした。

Create a new table with the following conditions: 

SQL Dialect: Spark SQL
schema name: default
table name: city
comlumns and data types: /*
    - ID bigint
    - NAME char(100)
    - cur_date date
options:
- IDENTITY columns:
    - ID

CREATE TABLE default.city (
  ID BIGINT,
  NAME CHAR(100),
  cur_date DATE
)
USING spark
OPTIONS (
  "identity" "ID"
)

image.png

上記の実行をすると、spark というデータソースがないとうエラーが表示されます。。Zero-shot では、想定通りの結果とならないようです。

org.apache.spark.SparkClassNotFoundException: [DATA_SOURCE_NOT_FOUND] Failed to find data source: spark. Please find packages at `https://spark.apache.org/third-party-projects.html`.

image.png

Few-shot learning による改善

基本方針

Spark における CREATE TABLE 文と自動採番(IDENTITY 列として設定)する文を例示してから質問することとします。

When you create new table with following conditions, you will get the following code.

conditions : /*
SQL Dialect: Spark SQL
schema name: default
table name: talbe_name
data source: delta 
comlumns and data types:
    - col_1 int
options: /**/

code : /*
CREATE TABLE default.talbe_name (
  col_1 int
)
USING delta;
*/
When you create new table with following conditions, you will get the following code.

conditions : /*
SQL Dialect: Spark SQL
schema name: default
table name: talbe_name
data source: delta 
comlumns and data types:
    - col_1 bigint
options:
- IDENTITY columns:
    - col_1

code : /*
CREATE TABLE default.talbe_name (
  col_1 bigint GENERATED ALWAYS AS IDENTITY
)
USING delta
;
*/

Github Copilot chat にて実行

下記のプロンプトを実行します。

/*
When you create new table with following conditions, you will get the following code.

conditions : /*
SQL Dialect: Spark SQL
schema name: default
table name: talbe_name
data source: delta 
comlumns and data types:
    - col_1 int
options: /**/

code : /*
CREATE TABLE default.talbe_name (
  col_1 int
)
USING delta;
*/


/*
When you create new table with following conditions, you will get the following code.

conditions : /*
SQL Dialect: Spark SQL
schema name: default
table name: talbe_name
data source: delta 
comlumns and data types:
    - col_1 bigint
options:
- IDENTITY columns:
    - col_1

code : /*
CREATE TABLE default.talbe_name (
  col_1 bigint GENERATED ALWAYS AS IDENTITY
)
USING delta
;
*/

Create a new table with the following conditions: 

SQL Dialect: Spark SQL
schema name: default
table name: city
comlumns and data types: /*
    - ID bigint
    - NAME char(100)
    - cur_date date
options:
- IDENTITY columns:
    - ID

image.png

上記プロンプトの実行後、次の SQL が生成され、Databricks にて想定通りにマネージドテーブルを作成することができました。

CREATE TABLE default.city (
  ID bigint GENERATED ALWAYS AS IDENTITY,
  NAME char(100),
  cur_date date
)
USING delta;

image.png

Bing Chat(GPT-4)での実行結果

Bing Chat(GPT-4)にて同じプロンプトを実行したところ、同等の結果となりました。

CREATE TABLE default.city (
  ID BIGINT GENERATED ALWAYS AS IDENTITY,
  NAME CHAR(100),
  cur_date DATE
)
USING delta;

image.png

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4