Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds more efficient internal pretty-printing #1403

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Thank you to all who have contributed!
### Deprecated

### Fixed
- Updates the default `.sql()` method to use a more efficient (internal) printer implementation.

### Removed

Expand All @@ -43,6 +44,7 @@ Thank you to all who have contributed!
### Contributors
Thank you to all who have contributed!
- @<your-username>
- @rchowell

## [0.14.4]

Expand Down
27 changes: 25 additions & 2 deletions partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
package org.partiql.ast.sql

import org.partiql.ast.AstNode
import org.partiql.ast.sql.internal.InternalSqlDialect
import org.partiql.ast.sql.internal.InternalSqlLayout

/**
* No argument uses optimized internal. Leaving older ones for backwards-compatibility.
*/
public fun AstNode.sql(): String {
val head = InternalSqlDialect.PARTIQL.apply(this)
return InternalSqlLayout.format(head)
}

/**
* Pretty-print this [AstNode] as SQL text with the given [SqlLayout]
*/
@JvmOverloads
public fun AstNode.sql(
layout: SqlLayout = SqlLayout.DEFAULT,
): String = SqlDialect.PARTIQL.apply(this).sql(layout)

/**
* Pretty-print this [AstNode] as SQL text with the given [SqlLayout]
*/
public fun AstNode.sql(
dialect: SqlDialect = SqlDialect.PARTIQL,
): String = accept(dialect, SqlBlock.Nil).sql(layout)
): String = dialect.apply(this).sql(SqlLayout.DEFAULT)

/**
* Pretty-print this [AstNode] as SQL text with the given [SqlLayout]
*/
public fun AstNode.sql(
layout: SqlLayout,
dialect: SqlDialect,
): String = dialect.apply(this).sql(layout)

// a <> b <-> a concat b

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at:
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/

package org.partiql.ast.sql.internal

/**
* Representation of some textual elements as a token (singly-linked) list.
*/
internal sealed class InternalSqlBlock {

/**
* Next token (if any) in the list.
*/
internal var next: InternalSqlBlock? = null

/**
* A newline / link break token.
*/
internal class NL : InternalSqlBlock()

/**
* A raw text token. Cannot be broken.
*/
internal class Text(val text: String) : InternalSqlBlock()

/**
* A nest token representing a (possible indented) token sublist.
*
* @property prefix A prefix character such as '{', '(', or '['.
* @property postfix A postfix character such as '}', ')', or ']].
* @property child
*/
internal class Nest(
val prefix: String?,
val postfix: String?,
val child: InternalSqlBlock,
) : InternalSqlBlock()

companion object {

/**
* Helper function to create root node (empty).
*/
@JvmStatic
internal fun root(): InternalSqlBlock = Text("")
}
}
Loading
Loading