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 2 commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ Thank you to all who have contributed!
- Change `StaticType.AnyOfType`'s `.toString` to not perform `.flatten()`

### Deprecated
- The current SqlBlock, SqlDialect, and SqlLayout are marked as deprecated and will be slightly changed in the next release.

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

### Removed

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

## [0.14.4]

Expand Down
30 changes: 28 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,41 @@
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 {
RCHowell marked this conversation as resolved.
Show resolved Hide resolved
val head = InternalSqlDialect.PARTIQL.apply(this)
return InternalSqlLayout.format(head)
}

/**
* Pretty-print this [AstNode] as SQL text with the given [SqlLayout]
*/
@JvmOverloads
@Deprecated("To be removed in the next minor version")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these @Deprecated messages, should they say next major version?

Suggested change
@Deprecated("To be removed in the next minor version")
@Deprecated("To be removed in the next major version")

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 [SqlDialect]
*/
@Deprecated("To be removed in the next minor version")
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] and [SqlDialect]
*/
@Deprecated("To be removed in the next minor version")
public fun AstNode.sql(
layout: SqlLayout,
dialect: SqlDialect,
): String = dialect.apply(this).sql(layout)

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

Expand Down
4 changes: 4 additions & 0 deletions partiql-ast/src/main/kotlin/org/partiql/ast/sql/SqlBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ package org.partiql.ast.sql
* @param layout SQL formatting ruleset
* @return SQL text
*/
@Deprecated("To be removed in the next minor version")
public fun SqlBlock.sql(layout: SqlLayout = SqlLayout.DEFAULT): String = layout.format(this)

/**
* Representation of some textual corpus; akin to Wadler's "A prettier printer" Document type.
*/
@Deprecated("This will be changed in the next minor version")
sealed interface SqlBlock {

public override fun toString(): String
Expand Down Expand Up @@ -54,6 +56,7 @@ sealed interface SqlBlock {
}
}

@Deprecated("This will be changed in the next minor version")
public interface BlockVisitor<R, C> {

public fun visit(block: SqlBlock, ctx: C): R
Expand All @@ -69,6 +72,7 @@ public interface BlockVisitor<R, C> {
public fun visitLink(block: SqlBlock.Link, ctx: C): R
}

@Deprecated("This will be changed in the next minor version")
public abstract class BlockBaseVisitor<R, C> : BlockVisitor<R, C> {

public abstract fun defaultReturn(block: SqlBlock, ctx: C): R
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import java.io.PrintStream
/**
* SqlDialect represents the base behavior for transforming an [AstNode] tree into a [SqlBlock] tree.
*/
@Deprecated("This will be changed in the next minor version")
@Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE")
public abstract class SqlDialect : AstBaseVisitor<SqlBlock, SqlBlock>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.partiql.ast.sql
/**
* [SqlLayout] determines how an [SqlBlock] tree is transformed in SQL text.
*/
@Deprecated("This will be changed in the next minor version")
public abstract class SqlLayout {

abstract val indent: Indent
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