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

Formatting update for subqueries/applied functions #3215

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ function meta::external::store::relational::sqlDialectTranslation::defaults::inL
nodeProcessor(
InListExpression,
{sqlDialect, l, state, config |
let sep0 = $state.separator(0, $config);
let sep1 = $state.separator(1, $config);
let whenClauses = $l.values->map(v | $sqlDialect->executeNodeProcessor($v, [], $state->increaseLevel(), $config))->joinStrings(',' + $sep1);
let sep0 = $state.separatorIfPretty(0, $config);
let sep1 = $state.separatorIfPretty(1, $config);
let whenClauses = $l.values->map(v | $sqlDialect->executeNodeProcessor($v, [], $state->increaseLevel(), $config))->joinStrings(',' + $state.separator(1, $config));
'(' + $sep1 + $whenClauses + $sep0 + ')';
},
{n | true}
Expand Down Expand Up @@ -551,11 +551,11 @@ function meta::external::store::relational::sqlDialectTranslation::defaults::fun
assert($f.group->isEmpty(), | 'Grouped function call processing not implemented yet');
assert($f.orderBy->isEmpty(), | 'Sorted function call processing not implemented yet');

let sep0 = $state.separator(0, $config);
let sep1 = $state.separator(1, $config);
let sep0 = $state.separatorIfPretty(0, $config);
let sep1 = $state.separatorIfPretty(1, $config);

$sqlDialect->qualifiedName($f.name, $state, $config) + '(' + if($f.arguments->isEmpty(), | '', | $sep1) +
$f.arguments->map(a | $sqlDialect->executeNodeProcessor($a, [], $state->increaseLevel(), $config))->joinStrings(',' + $sep1) +
$f.arguments->map(a | $sqlDialect->executeNodeProcessor($a, [], $state->increaseLevel(), $config))->joinStrings(',' + $state.separator(1, $config)) +
+ if($f.arguments->isEmpty(), | '', | $sep0) + ')' +
if ($f.window->isNotEmpty(),
| ' ' + $sqlDialect->keyword('over', $state, $config) + ' (' + $sep1 + $sqlDialect->executeNodeProcessor($f.window->toOne(), [], $state->increaseLevel(), $config) + $sep0 + ')',
Expand Down Expand Up @@ -720,8 +720,8 @@ function meta::external::store::relational::sqlDialectTranslation::defaults::sub
nodeProcessor(
SubqueryExpression,
{sqlDialect, s, state, config |
let sep0 = $state.separator(0, $config);
let sep1 = $state.separator(1, $config);
let sep0 = $state.separatorIfPretty(0, $config);
let sep1 = $state.separatorIfPretty(1, $config);
'(' + $sep1 + $sqlDialect->executeNodeProcessor($s.query, $state->increaseLevel(), $config) + $sep0 + ')';
},
{n | true}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ function <<test.Test>> meta::external::store::relational::sqlDialectTranslation:
let config = testSqlDialectTranslationConfig(false);

let e = ^InListExpression(values = [literal(101), literal(102), literal(103)]);
assertEquals('( 101, 102, 103 )', $dialect->executeNodeProcessor($e, $state, $config));
assertEquals('(101, 102, 103)', $dialect->executeNodeProcessor($e, $state, $config));
}

function <<test.Test>> meta::external::store::relational::sqlDialectTranslation::defaults::tests::testInListExpressionPrettyFormatProcessing(): Boolean[1]
Expand All @@ -372,7 +372,7 @@ function <<test.Test>> meta::external::store::relational::sqlDialectTranslation:
let valueList = ^InListExpression(values = [literal(101), literal(102), literal(103)]);
let e1 = ^InPredicate(valueList = $valueList, value = literal(101));
let e2 = ^InPredicate(valueList = $valueList, value = literal(101));
assertEquals('101 IN ( 101, 102, 103 )', $dialect->executeNodeProcessor($e1, $state, $config));
assertEquals('101 IN (101, 102, 103)', $dialect->executeNodeProcessor($e1, $state, $config));
}

function <<test.Test>> meta::external::store::relational::sqlDialectTranslation::defaults::tests::testInPredicatePrettyFormatProcessing(): Boolean[1]
Expand Down Expand Up @@ -476,9 +476,9 @@ function <<test.Test>> meta::external::store::relational::sqlDialectTranslation:
let c4 = ^QualifiedNameReference(name = ^QualifiedName(parts = ['db', 'schema', 'table', 'c4']));

assertEquals('db.func()', $dialect->executeNodeProcessor(^FunctionCall(name = $name), $state, $config));
assertEquals('db.func( 101 )', $dialect->executeNodeProcessor(^FunctionCall(name = $name, arguments = literal(101)), $state, $config));
assertEquals('db.func(101)', $dialect->executeNodeProcessor(^FunctionCall(name = $name, arguments = literal(101)), $state, $config));
assertEquals(
'db.func( 101, FALSE ) OVER ( PARTITION BY db.schema.table.c1, db.schema.table.c2 ORDER BY db.schema.table.c3 DESC NULLS FIRST, db.schema.table.c4 ASC NULLS LAST )',
'db.func(101, FALSE) OVER (PARTITION BY db.schema.table.c1, db.schema.table.c2 ORDER BY db.schema.table.c3 DESC NULLS FIRST, db.schema.table.c4 ASC NULLS LAST)',
$dialect->executeNodeProcessor(
^FunctionCall(
name = $name,
Expand Down Expand Up @@ -1016,7 +1016,7 @@ function <<test.Test>> meta::external::store::relational::sqlDialectTranslation:
from = ^Table(name = ^QualifiedName(parts = ['t'])),
where = ^ComparisonExpression(left = literal(103), right = literal(104), operator = ComparisonOperator.LESS_THAN)
);
assertEquals('( SELECT 101 + 102 FROM t WHERE 103 < 104 )', $dialect->executeNodeProcessor(^SubqueryExpression(query = ^Query(queryBody = $q1)), $state, $config));
assertEquals('(SELECT 101 + 102 FROM t WHERE 103 < 104)', $dialect->executeNodeProcessor(^SubqueryExpression(query = ^Query(queryBody = $q1)), $state, $config));
}

function <<test.Test>> meta::external::store::relational::sqlDialectTranslation::defaults::tests::testSubqueryExpressionPrettyFormatProcessing(): Boolean[1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,21 @@ Class meta::external::store::relational::sqlDialectTranslation::SqlDialectTransl
{
level: Integer[1] = 0;

{doc.doc = 'Returns right level indentation for next line if pretty format configured, else space'}
separator(offsetIndex: Integer[1], config: SqlDialectTranslationConfig[1]) {
if ($config.formatConfig.pretty,
| $config.formatConfig.lineSeparator + repeat($config.formatConfig.indent, $this.level + $offsetIndex)->joinStrings(),
| ' '
)
}: String[1];

{doc.doc = 'Returns right level indentation for next line if pretty format configured, else empty string'}
separatorIfPretty(offsetIndex: Integer[1], config: SqlDialectTranslationConfig[1]) {
if ($config.formatConfig.pretty,
| $config.formatConfig.lineSeparator + repeat($config.formatConfig.indent, $this.level + $offsetIndex)->joinStrings(),
| ''
)
}: String[1];
}

function meta::external::store::relational::sqlDialectTranslation::increaseLevel(state: SqlDialectTranslationState[1]): SqlDialectTranslationState[1]
Expand Down
Loading