Skip to content

Commit

Permalink
identifier normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
yliuuuu committed Jul 18, 2024
1 parent 7ed91d2 commit 98696cb
Show file tree
Hide file tree
Showing 24 changed files with 772 additions and 297 deletions.
381 changes: 204 additions & 177 deletions partiql-ast/api/partiql-ast.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.partiql.ast.sql.internal

import org.partiql.ast.AstNode
import org.partiql.ast.Binder
import org.partiql.ast.Exclude
import org.partiql.ast.Expr
import org.partiql.ast.From
Expand All @@ -29,6 +30,7 @@ import org.partiql.ast.SetQuantifier
import org.partiql.ast.Sort
import org.partiql.ast.Statement
import org.partiql.ast.Type
import org.partiql.ast.sql.sql
import org.partiql.ast.visitor.AstBaseVisitor
import org.partiql.value.MissingValue
import org.partiql.value.NullValue
Expand Down Expand Up @@ -92,6 +94,8 @@ internal abstract class InternalSqlDialect : AstBaseVisitor<InternalSqlBlock, In
return tail concat path
}

override fun visitBinder(node: Binder, tail: InternalSqlBlock): InternalSqlBlock = tail concat node.sql()

override fun visitPath(node: Path, tail: InternalSqlBlock): InternalSqlBlock {
val path = node.steps.fold(node.root.sql()) { p, step ->
when (step) {
Expand Down Expand Up @@ -845,4 +849,9 @@ internal abstract class InternalSqlDialect : AstBaseVisitor<InternalSqlBlock, In
Identifier.CaseSensitivity.SENSITIVE -> "\"$symbol\""
Identifier.CaseSensitivity.INSENSITIVE -> symbol // verbatim ..
}

private fun Binder.sql() = when (isRegular) {
true -> symbol
false -> "\"$symbol\""
}
}
38 changes: 23 additions & 15 deletions partiql-ast/src/main/resources/partiql_ast.ion
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ statement::[
insert::{
target: identifier,
values: expr,
as_alias: optional::'.identifier.symbol',
as_alias: optional::binder,
on_conflict: optional::on_conflict,
},

Expand All @@ -34,14 +34,14 @@ statement::[
upsert::{
target: identifier,
values: expr,
as_alias: optional::'.identifier.symbol',
as_alias: optional::binder,
},

// REPLACE INTO <target> [AS <alias>] <values>
replace::{
target: identifier,
values: expr,
as_alias: optional::'.identifier.symbol',
as_alias: optional::binder,
},

// UPDATE <target> SET <set clause list> WHERE <expr>
Expand All @@ -65,9 +65,9 @@ statement::[
delete::{
target: {
path: path,
as_alias: optional::'.identifier.symbol',
at_alias: optional::'.identifier.symbol',
by_alias: optional::'.identifier.symbol',
as_alias: optional::binder,
at_alias: optional::binder,
by_alias: optional::binder,
},
where: optional::expr,
returning: optional::returning,
Expand All @@ -84,7 +84,7 @@ statement::[
insert::{
target: identifier,
values: expr,
as_alias: optional::'.identifier.symbol',
as_alias: optional::binder,
on_conflict: optional::on_conflict,
},
insert_legacy::{
Expand Down Expand Up @@ -233,7 +233,7 @@ type::[
fields: list::[field],
_ : [
field :: {
name: '.identifier.symbol',
name: binder,
type: '.type',
// This could be a boolean flag since we only support NOT NULL constraint
// for struct subfield. But modeling this to be a list of constraints
Expand Down Expand Up @@ -274,6 +274,14 @@ identifier::[
],
]

// identifier for lvalue
// binder can not be qualified.
// it exists for eaiser visitor implementation and maintainence
binder::{
symbol: string,
is_regular: bool
}

// Path Literals
// - Much like qualified identifier but allowing bracket notation '[' <int> | <string> ']'
// - Not a variant of `identifier`, as path literals are not explicit in the specification.
Expand Down Expand Up @@ -585,7 +593,7 @@ select::[
_: [
item::[
all::{ expr: expr }, // <expr>.*
expression::{ expr: expr, as_alias: optional::'.identifier.symbol' } // <expr> [as <identifier>]
expression::{ expr: expr, as_alias: optional::binder } // <expr> [as <identifier>]
],
],
},
Expand Down Expand Up @@ -626,9 +634,9 @@ from::[
value::{
expr: expr,
type: [ SCAN, UNPIVOT ],
as_alias: optional::'.identifier.symbol',
at_alias: optional::'.identifier.symbol',
by_alias: optional::'.identifier.symbol',
as_alias: optional::binder,
at_alias: optional::binder,
by_alias: optional::binder,
},

// TODO https://github.com/partiql/partiql-spec/issues/41
Expand Down Expand Up @@ -656,7 +664,7 @@ let::{
_: [
binding::{
expr: expr,
as_alias: '.identifier.symbol',
as_alias: binder,
},
],
}
Expand All @@ -665,11 +673,11 @@ let::{
group_by::{
strategy: [ FULL, PARTIAL ],
keys: list::[key],
as_alias: optional::'.identifier.symbol',
as_alias: optional::binder,
_: [
key::{
expr: expr,
as_alias: optional::'.identifier.symbol',
as_alias: optional::binder,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.partiql.ast.GroupBy
import org.partiql.ast.Identifier
import org.partiql.ast.SetQuantifier
import org.partiql.ast.Sort
import org.partiql.ast.binder
import org.partiql.ast.builder.AstBuilder
import org.partiql.ast.builder.ast
import org.partiql.ast.exprLit
Expand Down Expand Up @@ -128,6 +129,8 @@ class ToLegacyAstTest {
// Shortcut to construct a "legacy-compatible" simple identifier
private fun id(name: String) = identifierSymbol(name, Identifier.CaseSensitivity.INSENSITIVE)

private fun bindName(name: String) = binder(name, true)

@JvmStatic
fun literals() = listOf(
expect("(lit null)") {
Expand Down Expand Up @@ -422,7 +425,7 @@ class ToLegacyAstTest {
fail("The legacy AST does not support field declaration in struct type") {
typeStruct {
fields += org.partiql.ast.typeStructField(
org.partiql.ast.identifierSymbol("a", Identifier.CaseSensitivity.INSENSITIVE),
org.partiql.ast.binder("a", true),
typeInt2(),
emptyList(),
false,
Expand Down Expand Up @@ -549,7 +552,7 @@ class ToLegacyAstTest {
}
items += selectProjectItemExpression {
expr = exprLit(int32Value(1))
asAlias = id("x")
asAlias = bindName("x")
}
}
},
Expand Down Expand Up @@ -579,9 +582,9 @@ class ToLegacyAstTest {
fromValue {
expr = NULL
type = From.Value.Type.SCAN
asAlias = id("a")
atAlias = id("b")
byAlias = id("c")
asAlias = bindName("a")
atAlias = bindName("b")
byAlias = bindName("c")
}
},
expect("(unpivot (lit null) null null null)") {
Expand All @@ -594,9 +597,9 @@ class ToLegacyAstTest {
fromValue {
expr = NULL
type = From.Value.Type.UNPIVOT
asAlias = id("a")
atAlias = id("b")
byAlias = id("c")
asAlias = bindName("a")
atAlias = bindName("b")
byAlias = bindName("c")
}
},
expect(
Expand Down Expand Up @@ -647,7 +650,7 @@ class ToLegacyAstTest {
let {
bindings += letBinding {
expr = NULL
asAlias = id("x")
asAlias = bindName("x")
}
}
},
Expand All @@ -665,7 +668,7 @@ class ToLegacyAstTest {
groupBy {
strategy = GroupBy.Strategy.FULL
keys += groupByKey(exprLit(stringValue("a")), null)
keys += groupByKey(exprLit(stringValue("b")), id("x"))
keys += groupByKey(exprLit(stringValue("b")), bindName("x"))
}
},
expect(
Expand All @@ -682,8 +685,8 @@ class ToLegacyAstTest {
groupBy {
strategy = GroupBy.Strategy.PARTIAL
keys += groupByKey(exprLit(stringValue("a")), null)
keys += groupByKey(exprLit(stringValue("b")), id("x"))
asAlias = id("as")
keys += groupByKey(exprLit(stringValue("b")), bindName("x"))
asAlias = bindName("as")
}
},
expect(
Expand Down
Loading

0 comments on commit 98696cb

Please sign in to comment.