[ https://issues.apache.org/jira/browse/CALCITE-4926?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Vladimir Steshin updated CALCITE-4926: -------------------------------------- Description: "SELECT * FROM emp ORDER BY (WITH t AS (SELECT 1) SELECT * FROM t)" fails with: "Column 'T' not found in any table". Working similar one: "WITH t AS (SELECT 1) SELECT * FROM emp ORDER BY (SELECT * FROM t)" As I understand, SqlValidatorImpl#OrderExpressionExpander (extends SqlScopedShuttle) hurries to find the alias in the upper, parent query, whole "select * from ...". But this query and the related namespaces has no such alias. Probably, it's better to search order-by's namespace first. {code:java} newValidationError:5266, SqlValidatorImpl (org.apache.calcite.sql.validate) fullyQualify:273, DelegatingScope (org.apache.calcite.sql.validate) fullyQualify:95, OrderByScope (org.apache.calcite.sql.validate) visit:6548, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) visit:6461, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) accept:324, SqlIdentifier (org.apache.calcite.sql) visitChild:134, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) visitChild:101, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) acceptCall:954, SqlOperator (org.apache.calcite.sql) visit:68, SqlShuttle (org.apache.calcite.sql.util) visitScoped:64, SqlScopedShuttle (org.apache.calcite.sql.validate) visitScoped:6557, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) visit:54, SqlScopedShuttle (org.apache.calcite.sql.validate) visit:37, SqlScopedShuttle (org.apache.calcite.sql.validate) accept:161, SqlCall (org.apache.calcite.sql) visit:80, SqlShuttle (org.apache.calcite.sql.util) visit:41, SqlShuttle (org.apache.calcite.sql.util) accept:266, SqlNodeList (org.apache.calcite.sql) visitChild:134, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) visitChild:101, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) acceptCall:954, SqlOperator (org.apache.calcite.sql) visit:68, SqlShuttle (org.apache.calcite.sql.util) visitScoped:64, SqlScopedShuttle (org.apache.calcite.sql.validate) visitScoped:6557, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) visit:54, SqlScopedShuttle (org.apache.calcite.sql.validate) visit:37, SqlScopedShuttle (org.apache.calcite.sql.validate) accept:161, SqlCall (org.apache.calcite.sql) visitChild:134, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) visitChild:101, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) acceptCall:954, SqlOperator (org.apache.calcite.sql) visit:68, SqlShuttle (org.apache.calcite.sql.util) visitScoped:64, SqlScopedShuttle (org.apache.calcite.sql.validate) visitScoped:6557, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) visit:54, SqlScopedShuttle (org.apache.calcite.sql.validate) visit:37, SqlScopedShuttle (org.apache.calcite.sql.validate) accept:161, SqlCall (org.apache.calcite.sql) go:6474, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) expandOrderExpr:4222, SqlValidatorImpl (org.apache.calcite.sql.validate) validateExpr:125, OrderByScope (org.apache.calcite.sql.validate) validateExpr:4467, SqlValidatorImpl (org.apache.calcite.sql.validate) validateOrderItem:4217, SqlValidatorImpl (org.apache.calcite.sql.validate) validateOrderList:4166, SqlValidatorImpl (org.apache.calcite.sql.validate) validateSelect:3658, SqlValidatorImpl (org.apache.calcite.sql.validate) validateImpl:64, SelectNamespace (org.apache.calcite.sql.validate) validate:89, AbstractNamespace (org.apache.calcite.sql.validate) validateNamespace:1100, SqlValidatorImpl (org.apache.calcite.sql.validate) validateQuery:1071, SqlValidatorImpl (org.apache.calcite.sql.validate) validate:247, SqlSelect (org.apache.calcite.sql) validateScopedExpression:1046, SqlValidatorImpl (org.apache.calcite.sql.validate) validate:752, SqlValidatorImpl (org.apache.calcite.sql.validate) {code} was: "SELECT * FROM emp ORDER BY (WITH t AS (SELECT 1) SELECT * FROM t)" fails with: "Column 'T' not found in any table". Working similar one: "WITH t AS (SELECT 1) SELECT * FROM emp ORDER BY (SELECT * FROM t)" As I understand, SqlValidatorImpl#OrderExpressionExpander (extends SqlScopedShuttle) hurries to find the alias in the upper, parent query, whole "select * from ...". But this query and the related namespaces has no such alias. Probably, it's better to search order-by's namespace first. {code:java} <init>:57, SqlValidatorException (org.apache.calcite.sql.validate) newInstance0:-1, NativeConstructorAccessorImpl (sun.reflect) newInstance:62, NativeConstructorAccessorImpl (sun.reflect) newInstance:45, DelegatingConstructorAccessorImpl (sun.reflect) newInstance:423, Constructor (java.lang.reflect) ex:505, Resources$ExInstWithCause (org.apache.calcite.runtime) ex:599, Resources$ExInst (org.apache.calcite.runtime) newContextException:932, SqlUtil (org.apache.calcite.sql) newContextException:917, SqlUtil (org.apache.calcite.sql) newValidationError:5266, SqlValidatorImpl (org.apache.calcite.sql.validate) fullyQualify:273, DelegatingScope (org.apache.calcite.sql.validate) fullyQualify:95, OrderByScope (org.apache.calcite.sql.validate) visit:6548, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) visit:6461, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) accept:324, SqlIdentifier (org.apache.calcite.sql) visitChild:134, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) visitChild:101, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) acceptCall:954, SqlOperator (org.apache.calcite.sql) visit:68, SqlShuttle (org.apache.calcite.sql.util) visitScoped:64, SqlScopedShuttle (org.apache.calcite.sql.validate) visitScoped:6557, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) visit:54, SqlScopedShuttle (org.apache.calcite.sql.validate) visit:37, SqlScopedShuttle (org.apache.calcite.sql.validate) accept:161, SqlCall (org.apache.calcite.sql) visit:80, SqlShuttle (org.apache.calcite.sql.util) visit:41, SqlShuttle (org.apache.calcite.sql.util) accept:266, SqlNodeList (org.apache.calcite.sql) visitChild:134, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) visitChild:101, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) acceptCall:954, SqlOperator (org.apache.calcite.sql) visit:68, SqlShuttle (org.apache.calcite.sql.util) visitScoped:64, SqlScopedShuttle (org.apache.calcite.sql.validate) visitScoped:6557, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) visit:54, SqlScopedShuttle (org.apache.calcite.sql.validate) visit:37, SqlScopedShuttle (org.apache.calcite.sql.validate) accept:161, SqlCall (org.apache.calcite.sql) visitChild:134, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) visitChild:101, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) acceptCall:954, SqlOperator (org.apache.calcite.sql) visit:68, SqlShuttle (org.apache.calcite.sql.util) visitScoped:64, SqlScopedShuttle (org.apache.calcite.sql.validate) visitScoped:6557, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) visit:54, SqlScopedShuttle (org.apache.calcite.sql.validate) visit:37, SqlScopedShuttle (org.apache.calcite.sql.validate) accept:161, SqlCall (org.apache.calcite.sql) go:6474, SqlValidatorImpl$OrderExpressionExpander (org.apache.calcite.sql.validate) expandOrderExpr:4222, SqlValidatorImpl (org.apache.calcite.sql.validate) validateExpr:125, OrderByScope (org.apache.calcite.sql.validate) validateExpr:4467, SqlValidatorImpl (org.apache.calcite.sql.validate) validateOrderItem:4217, SqlValidatorImpl (org.apache.calcite.sql.validate) validateOrderList:4166, SqlValidatorImpl (org.apache.calcite.sql.validate) validateSelect:3658, SqlValidatorImpl (org.apache.calcite.sql.validate) validateImpl:64, SelectNamespace (org.apache.calcite.sql.validate) validate:89, AbstractNamespace (org.apache.calcite.sql.validate) validateNamespace:1100, SqlValidatorImpl (org.apache.calcite.sql.validate) validateQuery:1071, SqlValidatorImpl (org.apache.calcite.sql.validate) validate:247, SqlSelect (org.apache.calcite.sql) validateScopedExpression:1046, SqlValidatorImpl (org.apache.calcite.sql.validate) validate:752, SqlValidatorImpl (org.apache.calcite.sql.validate) assertExceptionIsThrown:129, AbstractSqlTester (org.apache.calcite.sql.test) ok:315, SqlValidatorTestCase$Sql (org.apache.calcite.test) testOrder1:6551, SqlValidatorTest (org.apache.calcite.test) invoke0:-1, NativeMethodAccessorImpl (sun.reflect) invoke:62, NativeMethodAccessorImpl (sun.reflect) invoke:43, DelegatingMethodAccessorImpl (sun.reflect) invoke:498, Method (java.lang.reflect) invokeMethod:725, ReflectionUtils (org.junit.platform.commons.util) proceed:60, MethodInvocation (org.junit.jupiter.engine.execution) proceed:131, InvocationInterceptorChain$ValidatingInvocation (org.junit.jupiter.engine.execution) proceed:46, TimeoutInvocation (org.junit.jupiter.engine.extension) intercept:149, TimeoutExtension (org.junit.jupiter.engine.extension) interceptTestableMethod:140, TimeoutExtension (org.junit.jupiter.engine.extension) interceptTestMethod:84, TimeoutExtension (org.junit.jupiter.engine.extension) apply:-1, 442199874 (org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor$$Lambda$148) lambda$ofVoidMethod$0:115, ExecutableInvoker$ReflectiveInterceptorCall (org.junit.jupiter.engine.execution) apply:-1, 1807015220 (org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall$$Lambda$149) lambda$invoke$0:105, ExecutableInvoker (org.junit.jupiter.engine.execution) apply:-1, 963578981 (org.junit.jupiter.engine.execution.ExecutableInvoker$$Lambda$334) proceed:106, InvocationInterceptorChain$InterceptedInvocation (org.junit.jupiter.engine.execution) proceed:64, InvocationInterceptorChain (org.junit.jupiter.engine.execution) chainAndInvoke:45, InvocationInterceptorChain (org.junit.jupiter.engine.execution) invoke:37, InvocationInterceptorChain (org.junit.jupiter.engine.execution) invoke:104, ExecutableInvoker (org.junit.jupiter.engine.execution) invoke:98, ExecutableInvoker (org.junit.jupiter.engine.execution) lambda$invokeTestMethod$7:214, TestMethodTestDescriptor (org.junit.jupiter.engine.descriptor) execute:-1, 578203516 (org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor$$Lambda$437) execute:73, ThrowableCollector (org.junit.platform.engine.support.hierarchical) invokeTestMethod:210, TestMethodTestDescriptor (org.junit.jupiter.engine.descriptor) execute:135, TestMethodTestDescriptor (org.junit.jupiter.engine.descriptor) execute:66, TestMethodTestDescriptor (org.junit.jupiter.engine.descriptor) lambda$executeRecursively$6:151, NodeTestTask (org.junit.platform.engine.support.hierarchical) execute:-1, 1294322265 (org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$265) execute:73, ThrowableCollector (org.junit.platform.engine.support.hierarchical) lambda$executeRecursively$8:141, NodeTestTask (org.junit.platform.engine.support.hierarchical) invoke:-1, 1901999970 (org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$264) around:137, Node (org.junit.platform.engine.support.hierarchical) lambda$executeRecursively$9:139, NodeTestTask (org.junit.platform.engine.support.hierarchical) execute:-1, 1737295428 (org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$263) execute:73, ThrowableCollector (org.junit.platform.engine.support.hierarchical) executeRecursively:138, NodeTestTask (org.junit.platform.engine.support.hierarchical) execute:95, NodeTestTask (org.junit.platform.engine.support.hierarchical) compute:185, ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask (org.junit.platform.engine.support.hierarchical) invokeAll:129, ForkJoinPoolHierarchicalTestExecutorService (org.junit.platform.engine.support.hierarchical) lambda$executeRecursively$6:155, NodeTestTask (org.junit.platform.engine.support.hierarchical) execute:-1, 1294322265 (org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$265) execute:73, ThrowableCollector (org.junit.platform.engine.support.hierarchical) lambda$executeRecursively$8:141, NodeTestTask (org.junit.platform.engine.support.hierarchical) invoke:-1, 1901999970 (org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$264) around:137, Node (org.junit.platform.engine.support.hierarchical) lambda$executeRecursively$9:139, NodeTestTask (org.junit.platform.engine.support.hierarchical) execute:-1, 1737295428 (org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$263) execute:73, ThrowableCollector (org.junit.platform.engine.support.hierarchical) executeRecursively:138, NodeTestTask (org.junit.platform.engine.support.hierarchical) execute:95, NodeTestTask (org.junit.platform.engine.support.hierarchical) compute:185, ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask (org.junit.platform.engine.support.hierarchical) invokeAll:129, ForkJoinPoolHierarchicalTestExecutorService (org.junit.platform.engine.support.hierarchical) lambda$executeRecursively$6:155, NodeTestTask (org.junit.platform.engine.support.hierarchical) execute:-1, 1294322265 (org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$265) execute:73, ThrowableCollector (org.junit.platform.engine.support.hierarchical) lambda$executeRecursively$8:141, NodeTestTask (org.junit.platform.engine.support.hierarchical) invoke:-1, 1901999970 (org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$264) around:137, Node (org.junit.platform.engine.support.hierarchical) lambda$executeRecursively$9:139, NodeTestTask (org.junit.platform.engine.support.hierarchical) execute:-1, 1737295428 (org.junit.platform.engine.support.hierarchical.NodeTestTask$$Lambda$263) execute:73, ThrowableCollector (org.junit.platform.engine.support.hierarchical) executeRecursively:138, NodeTestTask (org.junit.platform.engine.support.hierarchical) execute:95, NodeTestTask (org.junit.platform.engine.support.hierarchical) compute:185, ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask (org.junit.platform.engine.support.hierarchical) exec:189, RecursiveAction (java.util.concurrent) doExec:289, ForkJoinTask (java.util.concurrent) runTask:1056, ForkJoinPool$WorkQueue (java.util.concurrent) runWorker:1692, ForkJoinPool (java.util.concurrent) run:157, ForkJoinWorkerThread (java.util.concurrent) {code} > 'ORDER BY' misses alias/table of own 'WITH' > ------------------------------------------- > > Key: CALCITE-4926 > URL: https://issues.apache.org/jira/browse/CALCITE-4926 > Project: Calcite > Issue Type: Bug > Reporter: Vladimir Steshin > Priority: Minor > > "SELECT * FROM emp ORDER BY (WITH t AS (SELECT 1) SELECT * FROM t)" > fails with: > "Column 'T' not found in any table". > Working similar one: > "WITH t AS (SELECT 1) SELECT * FROM emp ORDER BY (SELECT * FROM t)" > As I understand, SqlValidatorImpl#OrderExpressionExpander (extends > SqlScopedShuttle) hurries to find the alias in the upper, parent query, whole > "select * from ...". But this query and the related namespaces has no such > alias. Probably, it's better to search order-by's namespace first. > > {code:java} > newValidationError:5266, SqlValidatorImpl (org.apache.calcite.sql.validate) > fullyQualify:273, DelegatingScope (org.apache.calcite.sql.validate) > fullyQualify:95, OrderByScope (org.apache.calcite.sql.validate) > visit:6548, SqlValidatorImpl$OrderExpressionExpander > (org.apache.calcite.sql.validate) > visit:6461, SqlValidatorImpl$OrderExpressionExpander > (org.apache.calcite.sql.validate) > accept:324, SqlIdentifier (org.apache.calcite.sql) > visitChild:134, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) > visitChild:101, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) > acceptCall:954, SqlOperator (org.apache.calcite.sql) > visit:68, SqlShuttle (org.apache.calcite.sql.util) > visitScoped:64, SqlScopedShuttle (org.apache.calcite.sql.validate) > visitScoped:6557, SqlValidatorImpl$OrderExpressionExpander > (org.apache.calcite.sql.validate) > visit:54, SqlScopedShuttle (org.apache.calcite.sql.validate) > visit:37, SqlScopedShuttle (org.apache.calcite.sql.validate) > accept:161, SqlCall (org.apache.calcite.sql) > visit:80, SqlShuttle (org.apache.calcite.sql.util) > visit:41, SqlShuttle (org.apache.calcite.sql.util) > accept:266, SqlNodeList (org.apache.calcite.sql) > visitChild:134, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) > visitChild:101, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) > acceptCall:954, SqlOperator (org.apache.calcite.sql) > visit:68, SqlShuttle (org.apache.calcite.sql.util) > visitScoped:64, SqlScopedShuttle (org.apache.calcite.sql.validate) > visitScoped:6557, SqlValidatorImpl$OrderExpressionExpander > (org.apache.calcite.sql.validate) > visit:54, SqlScopedShuttle (org.apache.calcite.sql.validate) > visit:37, SqlScopedShuttle (org.apache.calcite.sql.validate) > accept:161, SqlCall (org.apache.calcite.sql) > visitChild:134, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) > visitChild:101, SqlShuttle$CallCopyingArgHandler (org.apache.calcite.sql.util) > acceptCall:954, SqlOperator (org.apache.calcite.sql) > visit:68, SqlShuttle (org.apache.calcite.sql.util) > visitScoped:64, SqlScopedShuttle (org.apache.calcite.sql.validate) > visitScoped:6557, SqlValidatorImpl$OrderExpressionExpander > (org.apache.calcite.sql.validate) > visit:54, SqlScopedShuttle (org.apache.calcite.sql.validate) > visit:37, SqlScopedShuttle (org.apache.calcite.sql.validate) > accept:161, SqlCall (org.apache.calcite.sql) > go:6474, SqlValidatorImpl$OrderExpressionExpander > (org.apache.calcite.sql.validate) > expandOrderExpr:4222, SqlValidatorImpl (org.apache.calcite.sql.validate) > validateExpr:125, OrderByScope (org.apache.calcite.sql.validate) > validateExpr:4467, SqlValidatorImpl (org.apache.calcite.sql.validate) > validateOrderItem:4217, SqlValidatorImpl (org.apache.calcite.sql.validate) > validateOrderList:4166, SqlValidatorImpl (org.apache.calcite.sql.validate) > validateSelect:3658, SqlValidatorImpl (org.apache.calcite.sql.validate) > validateImpl:64, SelectNamespace (org.apache.calcite.sql.validate) > validate:89, AbstractNamespace (org.apache.calcite.sql.validate) > validateNamespace:1100, SqlValidatorImpl (org.apache.calcite.sql.validate) > validateQuery:1071, SqlValidatorImpl (org.apache.calcite.sql.validate) > validate:247, SqlSelect (org.apache.calcite.sql) > validateScopedExpression:1046, SqlValidatorImpl > (org.apache.calcite.sql.validate) > validate:752, SqlValidatorImpl (org.apache.calcite.sql.validate) > {code} -- This message was sent by Atlassian Jira (v8.20.1#820001)