or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

cli.mdcore-parsing.mdexceptions.mdindex.mdinput-dialects.mdoutput-formatting.md

output-formatting.mddocs/

0

# Output Formatting

1

2

Convert parsed DDL metadata back into formatted SQL DDL statements optimized for specific database platforms, with dialect-specific syntax and conventions. The output system transforms the structured metadata into properly formatted DDL that matches each database's requirements.

3

4

## Capabilities

5

6

### Supported Output Dialects

7

8

Dictionary mapping dialect names to output formatter classes for generating platform-specific DDL output.

9

10

```python { .api }

11

supported_dialects: Dict[str, Any]

12

```

13

14

Available output modes include:

15

- `"sql"` - Generic SQL (default)

16

- `"mysql"` - MySQL dialect formatting

17

- `"postgres"` - PostgreSQL dialect formatting

18

- `"mssql"` - Microsoft SQL Server dialect formatting

19

- `"oracle"` - Oracle dialect formatting

20

- `"hql"` - Hive Query Language dialect formatting

21

- `"bigquery"` - Google BigQuery dialect formatting

22

- `"redshift"` - AWS Redshift dialect formatting

23

- `"snowflake"` - Snowflake dialect formatting

24

- `"spark_sql"` - Apache Spark SQL dialect formatting

25

- `"ibm_db2"` - IBM DB2 dialect formatting

26

- `"athena"` - AWS Athena dialect formatting

27

- `"databricks"` - Databricks dialect formatting

28

- `"sqlite"` - SQLite dialect formatting

29

- `"vertica"` - Vertica dialect formatting

30

31

### Output Dialect Classes

32

33

Platform-specific output formatting classes that generate properly formatted DDL statements.

34

35

```python { .api }

36

class Redshift:

37

"""AWS Redshift output formatter with Redshift-specific syntax."""

38

39

class SparkSQL:

40

"""Apache Spark SQL output formatter with Spark syntax."""

41

42

class MySQL:

43

"""MySQL output formatter with MySQL-specific syntax."""

44

45

class BigQuery:

46

"""Google BigQuery output formatter with BigQuery syntax."""

47

48

class MSSQL:

49

"""Microsoft SQL Server output formatter with T-SQL syntax."""

50

51

class Databricks:

52

"""Databricks output formatter with Databricks-specific syntax."""

53

54

class Sqlite:

55

"""SQLite output formatter with SQLite syntax limitations."""

56

57

class Vertica:

58

"""Vertica output formatter with Vertica-specific syntax."""

59

60

class IbmDB2:

61

"""IBM DB2 output formatter with DB2 syntax."""

62

63

class PostgreSQL:

64

"""PostgreSQL output formatter with PostgreSQL syntax."""

65

66

class Oracle:

67

"""Oracle output formatter with Oracle SQL syntax."""

68

69

class HQL:

70

"""Hive Query Language output formatter with HQL syntax."""

71

72

class Snowflake:

73

"""Snowflake output formatter with Snowflake syntax."""

74

75

class Athena:

76

"""AWS Athena output formatter with Athena/Presto syntax."""

77

```

78

79

### Output Cleanup Function

80

81

Clean up and format output data according to specified dialect requirements.

82

83

```python { .api }

84

def dialects_clean_up(output_mode: str, table_data) -> Dict:

85

"""

86

Clean up output data based on specified dialect.

87

88

Parameters:

89

- output_mode (str): Target dialect for output formatting

90

- table_data: Parsed table metadata to format

91

92

Returns:

93

Dict: Cleaned and formatted table data for specified dialect

94

"""

95

```

96

97

### Output Core Functions

98

99

Core output functionality for data serialization and file operations.

100

101

```python { .api }

102

def dump_data_to_file(table_name: str, dump_path: str, data: List[Dict]) -> None:

103

"""

104

Dump parsed data to JSON file.

105

106

Parameters:

107

- table_name (str): Name of table for filename

108

- dump_path (str): Directory path for output file

109

- data (List[Dict]): Parsed data to write

110

"""

111

112

class Output:

113

"""Output formatting class for data serialization."""

114

```

115

116

## Usage Examples

117

118

### Basic Output Formatting

119

120

```python

121

from simple_ddl_parser import DDLParser

122

123

ddl = """

124

CREATE TABLE users (

125

id SERIAL PRIMARY KEY,

126

name VARCHAR(50) NOT NULL

127

);

128

"""

129

130

parser = DDLParser(ddl)

131

132

# Default SQL output

133

result = parser.run(output_mode="sql")

134

135

# PostgreSQL-specific output

136

pg_result = parser.run(output_mode="postgres")

137

138

# MySQL-specific output

139

mysql_result = parser.run(output_mode="mysql")

140

```

141

142

### Dump Output to Files

143

144

```python

145

from simple_ddl_parser import DDLParser

146

147

parser = DDLParser(complex_ddl_content)

148

149

# Save formatted output to files

150

result = parser.run(

151

dump=True,

152

dump_path="./output_schemas",

153

output_mode="postgres"

154

)

155

# Creates JSON files in ./output_schemas/ directory

156

```

157

158

### JSON String Output

159

160

```python

161

from simple_ddl_parser import DDLParser

162

163

parser = DDLParser(ddl_content)

164

165

# Get JSON string instead of Python dict

166

json_output = parser.run(

167

output_mode="bigquery",

168

json_dump=True

169

)

170

print(json_output) # JSON formatted string

171

```

172

173

### Group by Entity Type

174

175

```python

176

from simple_ddl_parser import DDLParser

177

178

parser = DDLParser(schema_with_tables_and_sequences)

179

180

# Group results by database object type

181

grouped_result = parser.run(

182

group_by_type=True,

183

output_mode="oracle"

184

)

185

# Returns: {"tables": [...], "sequences": [...], "indexes": [...]}

186

```

187

188

### Dialect-Specific Cleanup

189

190

```python

191

from simple_ddl_parser.output.dialects import dialects_clean_up

192

193

# Apply dialect-specific formatting to parsed data

194

cleaned_data = dialects_clean_up("snowflake", raw_table_data)

195

```

196

197

## Output Format Examples

198

199

### MySQL Output

200

201

```python

202

# MySQL-specific formatting includes:

203

{

204

"table_name": "users",

205

"columns": [

206

{

207

"name": "id",

208

"type": "INT",

209

"auto_increment": True,

210

"primary_key": True

211

}

212

],

213

"engine": "InnoDB",

214

"charset": "utf8mb4"

215

}

216

```

217

218

### PostgreSQL Output

219

220

```python

221

# PostgreSQL-specific formatting includes:

222

{

223

"table_name": "users",

224

"columns": [

225

{

226

"name": "id",

227

"type": "SERIAL",

228

"primary_key": True

229

},

230

{

231

"name": "tags",

232

"type": "VARCHAR[]",

233

"size": 50

234

}

235

]

236

}

237

```

238

239

### BigQuery Output

240

241

```python

242

# BigQuery-specific formatting includes:

243

{

244

"table_name": "events",

245

"dataset": "analytics",

246

"project": "my-project",

247

"columns": [

248

{

249

"name": "user_data",

250

"type": "STRUCT",

251

"fields": [

252

{"name": "name", "type": "STRING"},

253

{"name": "age", "type": "INT64"}

254

]

255

}

256

],

257

"clustering": ["user_id"],

258

"partitioning": {"field": "date", "type": "DAY"}

259

}

260

```

261

262

## Platform-Specific Features

263

264

Each output dialect handles:

265

266

- **Data Type Mapping**: Convert generic types to platform-specific equivalents

267

- **Syntax Formatting**: Apply platform-specific DDL syntax rules

268

- **Feature Support**: Include/exclude features based on platform capabilities

269

- **Naming Conventions**: Apply platform-specific identifier rules

270

- **Constraints**: Format constraints using platform-specific syntax

271

- **Storage Options**: Include platform-specific storage and performance options