Skip to content

Commit ed32fc5

Browse files
committed
support formatted desc
1 parent 960b7d7 commit ed32fc5

File tree

11 files changed

+316
-104
lines changed

11 files changed

+316
-104
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ SQLRec has the following features:
3030
- Built on existing big data ecosystem, easy to integrate
3131
- Easy to extend, supports custom UDFs, Table types, and Model types
3232

33-
For detailed information, refer to the [SQLRec User Manual](https://sqlrec.github.io/sqlrec).
33+
For detailed information, refer to the [SQLRec User Manual](https://sqlrec.github.io/sqlrec/en/).
3434

3535
## Quick Start
3636

docs/docs/sql_reference.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,14 @@ SHOW MODELS;
170170
**语法:**
171171

172172
```sql
173-
{DESCRIBE | DESC} MODEL model_name [CHECKPOINT = 'checkpoint_name']
173+
{DESCRIBE | DESC} [FORMATTED] MODEL model_name [CHECKPOINT = 'checkpoint_name']
174174
```
175175

176176
**参数:**
177177

178178
| 参数 | 描述 |
179179
|------|------|
180+
| `FORMATTED` | 可选。以格式化表格形式显示详细信息,包括模型信息、输入字段、输出字段和模型参数 |
180181
| `model_name` | 模型名称 |
181182
| `checkpoint_name` | 可选。检查点名称,如果指定则显示该检查点的详细信息 |
182183

@@ -186,6 +187,10 @@ SHOW MODELS;
186187
DESCRIBE MODEL my_model;
187188

188189
DESC MODEL my_model CHECKPOINT = 'v1.0';
190+
191+
DESCRIBE FORMATTED MODEL my_model;
192+
193+
DESCRIBE FORMATTED MODEL my_model CHECKPOINT = 'v1.0';
189194
```
190195

191196

@@ -329,13 +334,14 @@ SHOW SERVICES;
329334
**语法:**
330335

331336
```sql
332-
{DESCRIBE | DESC} SERVICE service_name
337+
{DESCRIBE | DESC} [FORMATTED] SERVICE service_name
333338
```
334339

335340
**参数:**
336341

337342
| 参数 | 描述 |
338343
|------|------|
344+
| `FORMATTED` | 可选。以格式化表格形式显示详细信息,包括服务信息、关联模型信息和模型字段 |
339345
| `service_name` | 服务名称 |
340346

341347
**示例:**
@@ -344,6 +350,8 @@ SHOW SERVICES;
344350
DESCRIBE SERVICE my_service;
345351

346352
DESC SERVICE my_service;
353+
354+
DESCRIBE FORMATTED SERVICE my_service;
347355
```
348356

349357

docs/en/docs/sql_reference.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,14 @@ Show model creation statement or checkpoint information.
170170
**Syntax:**
171171

172172
```sql
173-
{DESCRIBE | DESC} MODEL model_name [CHECKPOINT = 'checkpoint_name']
173+
{DESCRIBE | DESC} [FORMATTED] MODEL model_name [CHECKPOINT = 'checkpoint_name']
174174
```
175175

176176
**Parameters:**
177177

178178
| Parameter | Description |
179179
|-----------|-------------|
180+
| `FORMATTED` | Optional. Display detailed information in a formatted table, including model information, input fields, output fields, and model parameters |
180181
| `model_name` | Model name |
181182
| `checkpoint_name` | Optional. Checkpoint name, if specified shows detailed information for that checkpoint |
182183

@@ -186,6 +187,10 @@ Show model creation statement or checkpoint information.
186187
DESCRIBE MODEL my_model;
187188

188189
DESC MODEL my_model CHECKPOINT = 'v1.0';
190+
191+
DESCRIBE FORMATTED MODEL my_model;
192+
193+
DESCRIBE FORMATTED MODEL my_model CHECKPOINT = 'v1.0';
189194
```
190195

191196

@@ -329,13 +334,14 @@ Show service creation statement.
329334
**Syntax:**
330335

331336
```sql
332-
{DESCRIBE | DESC} SERVICE service_name
337+
{DESCRIBE | DESC} [FORMATTED] SERVICE service_name
333338
```
334339

335340
**Parameters:**
336341

337342
| Parameter | Description |
338343
|-----------|-------------|
344+
| `FORMATTED` | Optional. Display detailed information in a formatted table, including service information, associated model information, and model fields |
339345
| `service_name` | Service name |
340346

341347
**Examples:**
@@ -344,6 +350,8 @@ Show service creation statement.
344350
DESCRIBE SERVICE my_service;
345351

346352
DESC SERVICE my_service;
353+
354+
DESCRIBE FORMATTED SERVICE my_service;
347355
```
348356

349357

sqlrec-core/src/test/java/com/sqlrec/TestSqlParse.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public void testCalciteSql() throws Exception {
5656
"show checkpoints test_model",
5757
"describe model test_model",
5858
"describe model test_model checkpoint='checkpoint1'",
59+
"describe formatted model test_model",
60+
"describe formatted model test_model checkpoint='checkpoint1'",
5961
"export model test_model checkpoint='checkpoint_path'",
6062
"export model test_model checkpoint='checkpoint_path' on data_db.test_table where dt>='2023-01-01'",
6163
"export model test_model checkpoint='checkpoint_path' WITH ( 'param1' = 'value1', 'param2' = 'value2' )",
@@ -67,6 +69,7 @@ public void testCalciteSql() throws Exception {
6769
"create service if not exists my_service on model test_model checkpoint='checkpoint_path'",
6870
"show services",
6971
"describe service my_service",
72+
"describe formatted service my_service",
7073
"drop service my_service",
7174
"drop service if exists my_service",
7275
"drop sql function fun1",
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.sqlrec.frontend.common;
2+
3+
import com.sqlrec.common.model.ModelConfig;
4+
import com.sqlrec.common.model.ModelController;
5+
import com.sqlrec.common.schema.FieldSchema;
6+
import com.sqlrec.entity.Checkpoint;
7+
import com.sqlrec.entity.Model;
8+
import com.sqlrec.entity.Service;
9+
import com.sqlrec.model.ModelControllerFactory;
10+
import com.sqlrec.model.ModelEntityConverter;
11+
12+
import java.text.SimpleDateFormat;
13+
import java.util.Date;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
public class CommonUtils {
18+
19+
public static void addModelInfo(List<List<String>> rows, Model model) throws Exception {
20+
ModelConfig modelConfig = ModelEntityConverter.convertToModel(model.getDdl());
21+
addModelInfo(rows, modelConfig, model);
22+
}
23+
24+
public static void addModelInfo(List<List<String>> rows, String modelDdl, Model model) throws Exception {
25+
ModelConfig modelConfig = ModelEntityConverter.convertToModel(modelDdl);
26+
addModelInfo(rows, modelConfig, model);
27+
}
28+
29+
public static void addModelInfo(List<List<String>> rows, ModelConfig modelConfig, Model model) throws Exception {
30+
ModelController modelController = ModelControllerFactory.getModelController(modelConfig);
31+
if (modelController == null) {
32+
throw new IllegalArgumentException("model controller not found for model: " + model.getName());
33+
}
34+
35+
List<FieldSchema> inputFields = modelConfig.getInputFields();
36+
List<FieldSchema> outputFields = modelController.getOutputFields(modelConfig);
37+
Map<String, String> params = modelConfig.getParams();
38+
39+
rows.add(java.util.Arrays.asList("# Model Information", ""));
40+
rows.add(java.util.Arrays.asList("Model Name:", model.getName()));
41+
rows.add(java.util.Arrays.asList("Created At:", formatTimestamp(model.getCreatedAt())));
42+
rows.add(java.util.Arrays.asList("Updated At:", formatTimestamp(model.getUpdatedAt())));
43+
rows.add(java.util.Arrays.asList("", ""));
44+
45+
addFieldSection(rows, "# Input Fields", inputFields);
46+
addFieldSection(rows, "# Output Fields", outputFields);
47+
addParametersSection(rows, "# Model Parameters", params);
48+
}
49+
50+
public static void addCheckpointInfo(List<List<String>> rows, Checkpoint checkpoint) {
51+
rows.add(java.util.Arrays.asList("", ""));
52+
rows.add(java.util.Arrays.asList("# Checkpoint Information", ""));
53+
rows.add(java.util.Arrays.asList("Checkpoint Name:", checkpoint.getCheckpointName()));
54+
rows.add(java.util.Arrays.asList("Checkpoint Type:",
55+
checkpoint.getCheckpointType() != null ? checkpoint.getCheckpointType() : "N/A"));
56+
rows.add(java.util.Arrays.asList("Status:",
57+
checkpoint.getStatus() != null ? checkpoint.getStatus() : "N/A"));
58+
rows.add(java.util.Arrays.asList("Created At:", formatTimestamp(checkpoint.getCreatedAt())));
59+
rows.add(java.util.Arrays.asList("Updated At:", formatTimestamp(checkpoint.getUpdatedAt())));
60+
}
61+
62+
public static void addServiceInfo(List<List<String>> rows, Service service) throws Exception {
63+
rows.add(java.util.Arrays.asList("# Service Information", ""));
64+
rows.add(java.util.Arrays.asList("Service Name:", service.getName()));
65+
rows.add(java.util.Arrays.asList("Model Name:", service.getModelName()));
66+
if (service.getCheckpointName() != null) {
67+
rows.add(java.util.Arrays.asList("Checkpoint Name:", service.getCheckpointName()));
68+
}
69+
if (service.getUrl() != null) {
70+
rows.add(java.util.Arrays.asList("URL:", service.getUrl()));
71+
}
72+
rows.add(java.util.Arrays.asList("Created At:", formatTimestamp(service.getCreatedAt())));
73+
rows.add(java.util.Arrays.asList("Updated At:", formatTimestamp(service.getUpdatedAt())));
74+
rows.add(java.util.Arrays.asList("", ""));
75+
76+
if (service.getModelDdl() != null) {
77+
ModelConfig modelConfig = ModelEntityConverter.convertToModel(service.getModelDdl());
78+
ModelController modelController = ModelControllerFactory.getModelController(modelConfig);
79+
if (modelController == null) {
80+
throw new IllegalArgumentException("model controller not found for model: " + service.getModelName());
81+
}
82+
83+
List<FieldSchema> inputFields = modelConfig.getInputFields();
84+
List<FieldSchema> outputFields = modelController.getOutputFields(modelConfig);
85+
Map<String, String> params = modelConfig.getParams();
86+
87+
addFieldSection(rows, "# Input Fields", inputFields);
88+
addFieldSection(rows, "# Output Fields", outputFields);
89+
addParametersSection(rows, "# Model Parameters", params);
90+
}
91+
}
92+
93+
private static void addFieldSection(List<List<String>> rows, String sectionTitle, List<FieldSchema> fields) {
94+
rows.add(java.util.Arrays.asList(sectionTitle, ""));
95+
rows.add(java.util.Arrays.asList("Name", "Type"));
96+
if (fields != null && !fields.isEmpty()) {
97+
for (FieldSchema field : fields) {
98+
rows.add(java.util.Arrays.asList(field.getName(), field.getType()));
99+
}
100+
} else {
101+
rows.add(java.util.Arrays.asList("(none)", ""));
102+
}
103+
rows.add(java.util.Arrays.asList("", ""));
104+
}
105+
106+
private static void addParametersSection(List<List<String>> rows, String sectionTitle, Map<String, String> params) {
107+
rows.add(java.util.Arrays.asList(sectionTitle, ""));
108+
rows.add(java.util.Arrays.asList("Key", "Value"));
109+
if (params != null && !params.isEmpty()) {
110+
for (Map.Entry<String, String> entry : params.entrySet()) {
111+
rows.add(java.util.Arrays.asList(entry.getKey(), entry.getValue()));
112+
}
113+
} else {
114+
rows.add(java.util.Arrays.asList("(none)", ""));
115+
}
116+
}
117+
118+
private static String formatTimestamp(long timestamp) {
119+
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(timestamp));
120+
}
121+
}

sqlrec-frontend/src/main/java/com/sqlrec/frontend/common/SqlProcessor.java

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -355,30 +355,7 @@ private SqlProcessResult processResourceQuery(SqlNode sqlNode) throws Exception
355355
}
356356

357357
if (sqlNode instanceof SqlShowCreateModel) {
358-
SqlShowCreateModel showCreateModel = (SqlShowCreateModel) sqlNode;
359-
if (showCreateModel.hasCheckpoint()) {
360-
String checkpointName = SchemaUtils.removeQuotes(showCreateModel.getCheckpoint().toString());
361-
Checkpoint checkpoint = DbUtils.getCheckpoint(
362-
showCreateModel.getModelName().getSimple(),
363-
checkpointName
364-
);
365-
if (checkpoint == null) {
366-
return Utils.convertMsgToResult(
367-
"checkpoint not exists: " + checkpointName + " for model " + showCreateModel.getModelName().getSimple(),
368-
"error"
369-
);
370-
}
371-
return Utils.convertMsgToResult(checkpoint.getDdl(), "create sql");
372-
} else {
373-
Model model = DbUtils.getModel(showCreateModel.getModelName().getSimple());
374-
if (model == null) {
375-
return Utils.convertMsgToResult(
376-
"model not exists: " + showCreateModel.getModelName().getSimple(),
377-
"error"
378-
);
379-
}
380-
return Utils.convertMsgToResult(model.getDdl(), "create sql");
381-
}
358+
return processShowCreateModel((SqlShowCreateModel) sqlNode);
382359
}
383360

384361
if (sqlNode instanceof SqlShowCheckpoint) {
@@ -399,15 +376,7 @@ private SqlProcessResult processResourceQuery(SqlNode sqlNode) throws Exception
399376
}
400377

401378
if (sqlNode instanceof SqlShowCreateService) {
402-
SqlShowCreateService showCreateService = (SqlShowCreateService) sqlNode;
403-
Service service = DbUtils.getService(showCreateService.getServiceName().getSimple());
404-
if (service == null) {
405-
return Utils.convertMsgToResult(
406-
"service not exists: " + showCreateService.getServiceName().getSimple(),
407-
"error"
408-
);
409-
}
410-
return Utils.convertMsgToResult(service.getDdl(), "create sql");
379+
return processShowCreateService((SqlShowCreateService) sqlNode);
411380
}
412381

413382
return null;
@@ -438,4 +407,74 @@ public static void saveSqlApi(SqlCreateApi api) {
438407
DbUtils.insertSqlApi(sqlApi);
439408
}
440409
}
410+
411+
private SqlProcessResult processShowCreateModel(SqlShowCreateModel showCreateModel) throws Exception {
412+
String modelName = showCreateModel.getModelName().getSimple();
413+
Model model = DbUtils.getModel(modelName);
414+
if (model == null) {
415+
return Utils.convertMsgToResult(
416+
"model not exists: " + modelName,
417+
"error"
418+
);
419+
}
420+
421+
Checkpoint checkpoint = null;
422+
if (showCreateModel.hasCheckpoint()) {
423+
String checkpointName = SchemaUtils.removeQuotes(showCreateModel.getCheckpoint().toString());
424+
checkpoint = DbUtils.getCheckpoint(modelName, checkpointName);
425+
if (checkpoint == null) {
426+
return Utils.convertMsgToResult(
427+
"checkpoint not exists: " + checkpointName + " for model " + modelName,
428+
"error"
429+
);
430+
}
431+
}
432+
433+
if (showCreateModel.isFormatted()) {
434+
java.util.List<java.util.List<String>> rows = new java.util.ArrayList<>();
435+
436+
if (checkpoint != null) {
437+
CommonUtils.addModelInfo(rows, checkpoint.getModelDdl(), model);
438+
CommonUtils.addCheckpointInfo(rows, checkpoint);
439+
} else {
440+
CommonUtils.addModelInfo(rows, model);
441+
}
442+
443+
Enumerable<Object[]> enumerable = com.sqlrec.common.utils.DataTransformUtils.convertListToArrayToEnumerable(rows);
444+
java.util.List<RelDataTypeField> fields = com.sqlrec.common.utils.DataTypeUtils.getStringTypeFieldList(
445+
java.util.Arrays.asList("col_name", "data_type")
446+
);
447+
return Utils.convertEnumerableToTRowSet(enumerable, fields);
448+
} else {
449+
if (checkpoint != null) {
450+
return Utils.convertMsgToResult(checkpoint.getDdl(), "create sql");
451+
} else {
452+
return Utils.convertMsgToResult(model.getDdl(), "create sql");
453+
}
454+
}
455+
}
456+
457+
private SqlProcessResult processShowCreateService(SqlShowCreateService showCreateService) throws Exception {
458+
String serviceName = showCreateService.getServiceName().getSimple();
459+
Service service = DbUtils.getService(serviceName);
460+
if (service == null) {
461+
return Utils.convertMsgToResult(
462+
"service not exists: " + serviceName,
463+
"error"
464+
);
465+
}
466+
467+
if (showCreateService.isFormatted()) {
468+
java.util.List<java.util.List<String>> rows = new java.util.ArrayList<>();
469+
CommonUtils.addServiceInfo(rows, service);
470+
471+
Enumerable<Object[]> enumerable = com.sqlrec.common.utils.DataTransformUtils.convertListToArrayToEnumerable(rows);
472+
java.util.List<RelDataTypeField> fields = com.sqlrec.common.utils.DataTypeUtils.getStringTypeFieldList(
473+
java.util.Arrays.asList("col_name", "data_type")
474+
);
475+
return Utils.convertEnumerableToTRowSet(enumerable, fields);
476+
} else {
477+
return Utils.convertMsgToResult(service.getDdl(), "create sql");
478+
}
479+
}
441480
}

0 commit comments

Comments
 (0)