Skip to content

Commit

Permalink
Subquery, function, join dialect translation improvements (#3250)
Browse files Browse the repository at this point in the history
  • Loading branch information
gs-ssh16 authored Nov 17, 2024
1 parent dac201d commit c0ac835
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,12 @@ function meta::external::store::relational::sqlDialectTranslation::defaults::fun
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(',' + $state.separator(1, $config)) +
+ if($f.arguments->isEmpty(), | '', | $sep0) + ')' +
let args = $f.arguments->map(a | $sqlDialect->executeNodeProcessor($a, [], $state->increaseLevel(), $config));
let noArgIsMultiLine = !($args->isNotEmpty() && $config.formatConfig.pretty && $args->map(a | $a->indexOf($config.formatConfig.lineSeparator) >= 0)->or());

$sqlDialect->qualifiedName($f.name, $state, $config) + '(' + if($noArgIsMultiLine, | '', | $sep1) +
$args->joinStrings(',' + if($noArgIsMultiLine, | ' ', | $state.separator(1, $config))) +
+ if($noArgIsMultiLine, | '', | $sep0) + ')' +
if ($f.window->isNotEmpty(),
| ' ' + $sqlDialect->keyword('over', $state, $config) + ' (' + $sep1 + $sqlDialect->executeNodeProcessor($f.window->toOne(), [], $state->increaseLevel(), $config) + $sep0 + ')',
| ''
Expand Down Expand Up @@ -601,6 +604,7 @@ function meta::external::store::relational::sqlDialectTranslation::defaults::joi
pair(|$j.type == JoinType.CROSS, | $sqlDialect->keyword('cross join', $state, $config)),
pair(|$j.type == JoinType.LEFT, | $sqlDialect->keyword('left outer join', $state, $config)),
pair(|$j.type == JoinType.RIGHT, | $sqlDialect->keyword('right outer join', $state, $config)),
pair(|$j.type == JoinType.FULL, | $sqlDialect->keyword('full outer join', $state, $config)),
pair(|$j.type == JoinType.INNER, | $sqlDialect->keyword('inner join', $state, $config))
],
| failWithMessage('Unhandled join type: ' + $j.type->toString())
Expand Down Expand Up @@ -727,3 +731,15 @@ function meta::external::store::relational::sqlDialectTranslation::defaults::sub
{n | true}
)
}

function meta::external::store::relational::sqlDialectTranslation::defaults::tableSubqueryProcessor_default(): NodeProcessor<TableSubquery>[1]
{
nodeProcessor(
TableSubquery,
{sqlDialect, s, state, config |
let sep0 = $state.separatorIfPretty(0, $config);
let sep1 = $state.separatorIfPretty(1, $config);
'(' + $sep1 + $sqlDialect->executeNodeProcessor($s.query, $state->increaseLevel(), $config) + $sep0 + ')';
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,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(\n' +
' 101\n' +
')', $dialect->executeNodeProcessor(^FunctionCall(name = $name, arguments = literal(101)), $state, $config));
assertEquals(
'db.func(\n' +
' 101,\n' +
' FALSE\n' +
') OVER (\n' +
'db.func(101, FALSE) OVER (\n' +
' PARTITION BY\n' +
' db.schema.table.c1,\n' +
' db.schema.table.c2\n' +
Expand All @@ -540,6 +534,24 @@ function <<test.Test>> meta::external::store::relational::sqlDialectTranslation:
$state, $config
)
);

let caseExpression = ^SearchedCaseExpression(whenClauses = [^WhenClause(result = literal(101), operand = literal(false)), ^WhenClause(result = literal(102), operand = literal(true))]);
assertEquals(
'db.func(\n' +
' 101,\n' +
' CASE\n' +
' WHEN\n' +
' FALSE\n' +
' THEN\n' +
' 101\n' +
' WHEN\n' +
' TRUE\n' +
' THEN\n' +
' 102\n' +
' END\n' +
')',
$dialect->executeNodeProcessor(^FunctionCall(name = $name, arguments = [literal(101), $caseExpression]), $state, $config)
);
}

function <<test.Test>> meta::external::store::relational::sqlDialectTranslation::defaults::tests::testTableProcessing(): Boolean[1]
Expand Down Expand Up @@ -574,6 +586,7 @@ function <<test.Test>> meta::external::store::relational::sqlDialectTranslation:
assertEquals('db.schema.table_a CROSS JOIN db.schema.table_b', $dialect->executeNodeProcessor(^Join(left = $tblA, right = $tblB, type = JoinType.CROSS), $state, $config));
assertEquals('db.schema.table_a LEFT OUTER JOIN db.schema.table_b', $dialect->executeNodeProcessor(^Join(left = $tblA, right = $tblB, type = JoinType.LEFT), $state, $config));
assertEquals('db.schema.table_a RIGHT OUTER JOIN db.schema.table_b', $dialect->executeNodeProcessor(^Join(left = $tblA, right = $tblB, type = JoinType.RIGHT), $state, $config));
assertEquals('db.schema.table_a FULL OUTER JOIN db.schema.table_b', $dialect->executeNodeProcessor(^Join(left = $tblA, right = $tblB, type = JoinType.FULL), $state, $config));
assertEquals('db.schema.table_a INNER JOIN db.schema.table_b', $dialect->executeNodeProcessor(^Join(left = $tblA, right = $tblB, type = JoinType.INNER), $state, $config));

assertEquals(
Expand Down Expand Up @@ -1040,3 +1053,40 @@ function <<test.Test>> meta::external::store::relational::sqlDialectTranslation:
' 103 < 104\n' +
')', $dialect->executeNodeProcessor(^SubqueryExpression(query = ^Query(queryBody = $q1)), $state, $config));
}


function <<test.Test>> meta::external::store::relational::sqlDialectTranslation::defaults::tests::testTableSubqueryProcessing(): Boolean[1]
{
let dialect = testDialect();
let state = testSqlDialectTranslationState();
let config = testSqlDialectTranslationConfig(false);

let q1 = ^QuerySpecification(
select = ^Select(selectItems = [^SingleColumn(expression = ^ArithmeticExpression(left = literal(101), right = literal(102), type = ArithmeticType.ADD))]),
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(^TableSubquery(query = ^Query(queryBody = $q1)), $state, $config));
}

function <<test.Test>> meta::external::store::relational::sqlDialectTranslation::defaults::tests::testTableSubqueryPrettyFormatProcessing(): Boolean[1]
{
let dialect = testDialect();
let state = testSqlDialectTranslationState();
let config = testSqlDialectTranslationConfig(true);

let q1 = ^QuerySpecification(
select = ^Select(selectItems = [^SingleColumn(expression = ^ArithmeticExpression(left = literal(101), right = literal(102), type = ArithmeticType.ADD))]),
from = ^Table(name = ^QualifiedName(parts = ['t'])),
where = ^ComparisonExpression(left = literal(103), right = literal(104), operator = ComparisonOperator.LESS_THAN)
);
assertEquals(
'(\n' +
' SELECT\n' +
' 101 + 102\n' +
' FROM\n' +
' t\n' +
' WHERE\n' +
' 103 < 104\n' +
')', $dialect->executeNodeProcessor(^TableSubquery(query = ^Query(queryBody = $q1)), $state, $config));
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ function <<access.private>> meta::external::store::relational::sqlDialectTransla
aliasedRelationProcessor_default(),
joinProcessor_default(),
querySpecificationProcessor_default(),
unionProcessor_default()
unionProcessor_default(),
tableSubqueryProcessor_default()
]
}

Expand Down

0 comments on commit c0ac835

Please sign in to comment.